#include<bits/stdc++.h>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define NL(x) " \n"[(x)]
#define lld long long
#define pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 100010
int N, distA[MAXN], distB[MAXN], A, B, a, b;
vector<int> adj[MAXN];
void dfs(int node, int prev, int dist[]) {
    dist[node] = dist[prev] + 1;
    for(auto x : adj[node]) {
        if(x != prev) dfs(x, node, dist);
    }
}
int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
    cin >> N;
    for(int i = 0; i < N-1; i++) {
        int x, y; cin >> x >> y;
        adj[x].pb(y); adj[y].pb(x);
    }
    distB[1] = -1; dfs(1, 1, distB);
    for(int i = 1; i <= N; i++) if(distB[i] > distB[A]) A = i;
    distA[A] = -1; dfs(A, A, distA);
    for(int i = 1; i <= N; i++) if(distA[i] > distA[B]) B = i;
    distB[B] = -1; dfs(B, B, distB);

    for(int i = 1; i <= N; i++) if(distA[i] == distA[B]) b++;
    for(int i = 1; i <= N; i++) if(distB[i] == distB[A]) a++;

    if(distA[B]%2 == 0) a = (b+1)/2, b = b+1-a;

    if(N%2 == 1) {
        if(a > 1 || b > 1) cout << distA[B] << endl;
        else cout << distA[B]-1 << endl;
    } else {
        if(a > 1 && b > 1) cout << distA[B] << endl;
        else if(a > 1 || b > 1) cout << distA[B]-1 << endl;
        else cout << distA[B]-2 << endl;
    }
    return 0;
}