mod = 998244353 def main(): import sys from collections import deque input = sys.stdin.readline def radius(v0, v1): que = deque() que.append(v0) seen = {} seen[v0] = 0 v_last = 0 while que: v = que.popleft() for u in adj[v]: if u == v1: continue if u not in seen: seen[u] = seen[v] + 1 que.append(u) v_last = v v0 = v_last que = deque() que.append(v0) seen = {} seen[v0] = 0 v_last = 0 while que: v = que.popleft() for u in adj[v]: if u == v1: continue if u not in seen: seen[u] = seen[v] + 1 que.append(u) v_last = v return seen[v_last] N = int(input()) adj = [[] for _ in range(N + 1)] for _ in range(N - 1): a, b = map(int, input().split()) adj[a].append(b) adj[b].append(a) que = deque() que.append(1) seen = [-1] * (N+1) seen[1] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() v0 = seq[0] que = deque() que.append(v0) seen = [-1] * (N+1) seen[v0] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() v1 = seq[0] D = seen[v1] if D & 1: v = v1 for _ in range(D // 2): v = par[v] vl = v vr = par[v] dl = radius(vl, vr) dr = radius(vr, vl) print((dl + 1) // 2 + (dr + 1) // 2 + 1) else: assert False ans = N * 2 v = v1 for _ in range(D // 2): v = par[v] v0 = v que = deque() que.append(v0) seen = [-1] * (N+1) seen[v0] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() subtree_len = [(0, 0)] for c in child[v0]: que = deque() seq = [] que.append(c) while que: v = que.popleft() for vv in child[v]: que.append(vv) seq.append(v) subtree_len.append((seen[seq[-1]], c)) subtree_len.sort(key=lambda x: x[0], reverse=True) for c in child[v0]: cc = c que = deque() que.append(cc) seen = {} seen[cc] = 0 seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if u == v0: continue if u not in seen: seen[u] = seen[v] + 1 que.append(u) seq.reverse() cc = seq[0] que = deque() que.append(cc) seen = {} seen[cc] = 0 seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if u == v0: continue if u not in seen: seen[u] = seen[v] + 1 que.append(u) seq.reverse() dc = seen[seq[0]] if c == subtree_len[0][1]: dc2 = subtree_len[1][0] + subtree_len[2][0] elif c == subtree_len[1][1]: dc2 = subtree_len[0][0] + subtree_len[2][0] else: dc2 = subtree_len[0][0] + subtree_len[1][0] ans = min(ans, (dc + 1) // 2 + (dc2 + 1) // 2 + 1) #print(cc, ans, dc, dc2) print(ans) #print(subtree_len) if __name__ == '__main__': main()