using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
//3
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
WillReturn.Add("3 5");
//4
}
else if (InputPattern == "Input3") {
WillReturn.Add("10");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("2 4");
WillReturn.Add("4 5");
WillReturn.Add("4 6");
WillReturn.Add("3 7");
WillReturn.Add("7 8");
WillReturn.Add("8 9");
WillReturn.Add("8 10");
//8
}
else if (InputPattern == "Input4") {
WillReturn.Add("31");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("2 4");
WillReturn.Add("2 5");
WillReturn.Add("3 6");
WillReturn.Add("3 7");
WillReturn.Add("4 8");
WillReturn.Add("4 9");
WillReturn.Add("5 10");
WillReturn.Add("5 11");
WillReturn.Add("6 12");
WillReturn.Add("6 13");
WillReturn.Add("7 14");
WillReturn.Add("7 15");
WillReturn.Add("8 16");
WillReturn.Add("8 17");
WillReturn.Add("9 18");
WillReturn.Add("9 19");
WillReturn.Add("10 20");
WillReturn.Add("10 21");
WillReturn.Add("11 22");
WillReturn.Add("11 23");
WillReturn.Add("12 24");
WillReturn.Add("12 25");
WillReturn.Add("13 26");
WillReturn.Add("13 27");
WillReturn.Add("14 28");
WillReturn.Add("14 29");
WillReturn.Add("15 30");
WillReturn.Add("15 31");
//9
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
// 隣接リスト
static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
foreach (string EachStr in InputList.Skip(1).Take(mN - 1)) {
SplitAct(EachStr);
int FromNode = wkArr[0];
int ToNode = wkArr[1];
if (mToNodeListDict.ContainsKey(FromNode) == false) {
mToNodeListDict[FromNode] = new List<int>();
}
if (mToNodeListDict.ContainsKey(ToNode) == false) {
mToNodeListDict[ToNode] = new List<int>();
}
mToNodeListDict[FromNode].Add(ToNode);
mToNodeListDict[ToNode].Add(FromNode);
}
int LeafNode1, Distance1;
ExecBFS(1, out LeafNode1, out Distance1);
int LeafNode2, Distance2;
ExecBFS(LeafNode1, out LeafNode2, out Distance2);
Console.WriteLine(Distance2 + 1);
}
struct JyoutaiDef
{
internal int CurrNode;
internal int Distance;
}
// 開始ノードを引数として、一番遠い葉ノードと距離を設定
static void ExecBFS(int pStaNode, out int pLeafNode, out int pDistance)
{
pLeafNode = pDistance = -1;
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrNode = pStaNode;
WillEnqueue.Distance = 0;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pStaNode);
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
pLeafNode = Dequeued.CurrNode;
pDistance = Dequeued.Distance;
if (mToNodeListDict.ContainsKey(Dequeued.CurrNode)) {
foreach (int EachToNode in mToNodeListDict[Dequeued.CurrNode]) {
if (VisitedSet.Add(EachToNode)) {
WillEnqueue.CurrNode = EachToNode;
WillEnqueue.Distance = Dequeued.Distance + 1;
Que.Enqueue(WillEnqueue);
}
}
}
}
}
}