#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; using ll = long long; using ld = long double; template < class T > class tree_diameter { public: T diameter; vector path, parent; vector< T > dist; tree_diameter(const vector>> &tree) { int n = tree.size(); parent.assign(n, -1); dist.resize(n); dist[0] = T(0); dfs(tree, 0, -1); int u = max_element(dist.begin(), dist.end()) - dist.begin(); dist[u] = T(0); dfs(tree, u, -1); int v = max_element(dist.begin(), dist.end()) - dist.begin(); diameter = dist[v]; while(v != -1) path.push_back(v), v = parent[v]; } void dfs(const vector>> &tree, int v, int p) { parent[v] = p; for(auto [to, cost] : tree[v]) if(to != p) dist[to] = dist[v] + cost, dfs(tree, to, v); } }; int main(){ cin.tie(0); ios::sync_with_stdio(0); int N; cin >> N; vector>> G(N); rep(i,N-1) { int u,v; cin >> u >> v; u--; v--; G[u].push_back({v, 1}); G[v].push_back({u, 1}); } cout << 2 * (N - 1) - tree_diameter(G).diameter << endl; }