#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int inf = int(1e9)+7;
int n;
vector<int> g[maxN];
int dp[maxN];
bool bad;
void dfs(int u , int par , int k){
dp[u] = 0;
for (int v : g[u]){
if (v != par){
if (bad) return;
dfs(v , u , k);
}
}
multiset<int> st;
for (int v : g[u]){
if (v != par){
st.insert(dp[v] + 1);
}
}
if (st.empty()) return;
while (sz(st) > 2){
int x = *st.begin(); st.erase(st.begin());
if (st.lower_bound(k - x) == st.end()){
dp[u] = x;
while (st.empty() == 0){
int y = *st.begin();
if (y >= k) continue;
auto it = st.lower_bound(k - y);
if (it == st.end()){
bad = true;
return;
}
st.erase(it);
}
return;
}
st.erase(st.lower_bound(k - x));
}
if (sz(st) == 1){
dp[u] = *st.begin();
}
else{
int x = *st.begin(); st.erase(st.begin());
int y = *st.begin(); st.erase(st.begin());
if (x + y < k){
bad = true;
return;
}
if (y >= k){
dp[u] = x;
}
else{
dp[u] = 0;
}
}
}
bool check(int k){
bad = false;
multiset<int> st;
for (int v : g[1]){
dfs(v , 1 , k);
st.insert(dp[v] + 1);
}
if (bad){
return false;
}
else{
while (st.empty() == 0){
int x = *st.begin(); st.erase(st.begin());
if (x >= k) continue;
auto it = st.lower_bound(k - x);
if (it == st.end()) return false;
st.erase(it);
}
return true;
}
}
void solve(){
cin >> n;
for (int i = 1 ; i < n ; i++){
int u , v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int lef = 0 , rig = n - 1 , ans = -1;
while (lef <= rig){
int mid = (lef + rig) / 2;
if (check(mid)){
ans = mid;
lef = mid + 1;
}
else{
rig = mid - 1;
}
}
cout << ans << "\n";
}
#define name "P"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDJlNSkrNzsKY29uc3QgaW50IGluZiA9IGludCgxZTkpKzc7CgppbnQgbjsKdmVjdG9yPGludD4gZ1ttYXhOXTsKaW50IGRwW21heE5dOwpib29sIGJhZDsKCnZvaWQgZGZzKGludCB1ICwgaW50IHBhciAsIGludCBrKXsKICAgIGRwW3VdID0gMDsKICAgIGZvciAoaW50IHYgOiBnW3VdKXsKICAgICAgICBpZiAodiAhPSBwYXIpewogICAgICAgICAgICBpZiAoYmFkKSByZXR1cm47CiAgICAgICAgICAgIGRmcyh2ICwgdSAsIGspOwogICAgICAgIH0KICAgIH0KICAgIG11bHRpc2V0PGludD4gc3Q7CiAgICBmb3IgKGludCB2IDogZ1t1XSl7CiAgICAgICAgaWYgKHYgIT0gcGFyKXsKICAgICAgICAgICAgc3QuaW5zZXJ0KGRwW3ZdICsgMSk7CiAgICAgICAgfQogICAgfQogICAgaWYgKHN0LmVtcHR5KCkpIHJldHVybjsKICAgIHdoaWxlIChzeihzdCkgPiAyKXsKICAgICAgICBpbnQgeCA9ICpzdC5iZWdpbigpOyBzdC5lcmFzZShzdC5iZWdpbigpKTsKICAgICAgICBpZiAoc3QubG93ZXJfYm91bmQoayAtIHgpID09IHN0LmVuZCgpKXsKICAgICAgICAgICAgZHBbdV0gPSB4OwogICAgICAgICAgICB3aGlsZSAoc3QuZW1wdHkoKSA9PSAwKXsKICAgICAgICAgICAgICAgIGludCB5ID0gKnN0LmJlZ2luKCk7CiAgICAgICAgICAgICAgICBpZiAoeSA+PSBrKSBjb250aW51ZTsKICAgICAgICAgICAgICAgIGF1dG8gaXQgPSBzdC5sb3dlcl9ib3VuZChrIC0geSk7CiAgICAgICAgICAgICAgICBpZiAoaXQgPT0gc3QuZW5kKCkpewogICAgICAgICAgICAgICAgICAgIGJhZCA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3QuZXJhc2UoaXQpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgc3QuZXJhc2Uoc3QubG93ZXJfYm91bmQoayAtIHgpKTsKICAgIH0KICAgIGlmIChzeihzdCkgPT0gMSl7CiAgICAgICAgZHBbdV0gPSAqc3QuYmVnaW4oKTsKICAgIH0KICAgIGVsc2V7CiAgICAgICAgaW50IHggPSAqc3QuYmVnaW4oKTsgc3QuZXJhc2Uoc3QuYmVnaW4oKSk7CiAgICAgICAgaW50IHkgPSAqc3QuYmVnaW4oKTsgc3QuZXJhc2Uoc3QuYmVnaW4oKSk7CiAgICAgICAgaWYgKHggKyB5IDwgayl7CiAgICAgICAgICAgIGJhZCA9IHRydWU7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgaWYgKHkgPj0gayl7CiAgICAgICAgICAgIGRwW3VdID0geDsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgZHBbdV0gPSAwOwogICAgICAgIH0KICAgIH0KfQoKYm9vbCBjaGVjayhpbnQgayl7CiAgICBiYWQgPSBmYWxzZTsKICAgIG11bHRpc2V0PGludD4gc3Q7CiAgICBmb3IgKGludCB2IDogZ1sxXSl7CiAgICAgICAgZGZzKHYgLCAxICwgayk7CiAgICAgICAgc3QuaW5zZXJ0KGRwW3ZdICsgMSk7CiAgICB9CiAgICBpZiAoYmFkKXsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgICBlbHNlewogICAgICAgIHdoaWxlIChzdC5lbXB0eSgpID09IDApewogICAgICAgICAgICBpbnQgeCA9ICpzdC5iZWdpbigpOyBzdC5lcmFzZShzdC5iZWdpbigpKTsKICAgICAgICAgICAgaWYgKHggPj0gaykgY29udGludWU7CiAgICAgICAgICAgIGF1dG8gaXQgPSBzdC5sb3dlcl9ib3VuZChrIC0geCk7CiAgICAgICAgICAgIGlmIChpdCA9PSBzdC5lbmQoKSkgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICBzdC5lcmFzZShpdCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgfQp9Cgp2b2lkIHNvbHZlKCl7CiAgICBjaW4gPj4gbjsKICAgIGZvciAoaW50IGkgPSAxIDsgaSA8IG4gOyBpKyspewogICAgICAgIGludCB1ICwgdjsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIGdbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKHUpOwogICAgfQogICAgaW50IGxlZiA9IDAgLCByaWcgPSBuIC0gMSAsIGFucyA9IC0xOwogICAgd2hpbGUgKGxlZiA8PSByaWcpewogICAgICAgIGludCBtaWQgPSAobGVmICsgcmlnKSAvIDI7CiAgICAgICAgaWYgKGNoZWNrKG1pZCkpewogICAgICAgICAgICBhbnMgPSBtaWQ7CiAgICAgICAgICAgIGxlZiA9IG1pZCArIDE7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIHJpZyA9IG1pZCAtIDE7CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKfQoKI2RlZmluZSBuYW1lICJQIgoKaW50IG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaWYgKGZvcGVuKG5hbWUiLklOUCIgLCAiciIpKXsKICAgICAgICBmcmVvcGVuKG5hbWUiLklOUCIgLCAiciIgLCBzdGRpbik7CiAgICAgICAgZnJlb3BlbihuYW1lIi5PVVQiICwgInciICwgc3Rkb3V0KTsKICAgIH0KICAgIGludCB0ID0gMTsgLy9jaW4gPj4gdDsKICAgIHdoaWxlICh0LS0pIHNvbHZlKCk7CiAgICByZXR1cm4gMDsKfQoK