import sys sys.setrecursionlimit(10 ** 9) # import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') n = int(input()) edges = [[] for _ in range(n)] for _ in range(n - 1): u, v = map(int, input().split()) u -= 1 v -= 1 edges[u].append(v) edges[v].append(u) max_R = [0] * n max_dist = [0] * n def dfs(pos, bpos): ma1 = 0 ma2 = 0 for npos in edges[pos]: if npos == bpos: continue dfs(npos, pos) if max_R[pos] < max_R[npos]: max_R[pos] = max_R[npos] if ma2 < max_dist[npos] + 1: ma2 = max_dist[npos] + 1 if ma2 > ma1: ma1, ma2 = ma2, ma1 max_dist[pos] = ma1 max_R[pos] = max(max_R[pos], ma1 + ma2) dfs(0, -1) ans = n def dfs2(pos, bpos): if bpos != -1: global ans r1 = max_R[bpos] r2 = max_R[pos] tmp = max((r1 + 1) // 2 + (r2 + 1) // 2 + 1, r1, r2) ans = min(ans, tmp) ma1 = 0 ma2 = 0 ma3 = 0 le = len(edges[pos]) L_r = [0] * (le + 1) R_r = [0] * (le + 1) for i, npos in enumerate(edges[pos], 1): L_r[i] = max(L_r[i - 1], max_R[npos]) if ma3 < max_dist[npos] + 1: ma3 = max_dist[npos] + 1 if ma3 > ma2: ma2, ma3 = ma3, ma2 if ma2 > ma1: ma1, ma2 = ma2, ma1 for i in range(le - 1, -1, -1): npos = edges[pos][i] R_r[i] = max(R_r[i + 1], max_R[npos]) for i, npos in enumerate(edges[pos]): if npos == bpos: continue max_R[pos] = max(L_r[i], R_r[i + 1]) if max_dist[npos] + 1 == ma1: max_dist[pos] = ma2 max_R[pos] = max(max_R[pos], ma2 + ma3) elif max_dist[npos] + 1 == ma2: max_dist[pos] = ma1 max_R[pos] = max(max_R[pos], ma1 + ma3) else: max_dist[pos] = ma1 max_R[pos] = max(max_R[pos], ma1 + ma2) dfs2(npos, pos) dfs2(0 ,-1) print(ans)