#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, root, src;
int par[100005], dp[100005][4];
int sforcecolor = -1;
int rblue = -1;
bool visited[100005];
vector<int> adj[100005];
int add(int a, int b)
{
if (a >= INF || b >= INF) return INF;
return a + b;
}
void cc(int u, int p)
{
if (root) return;
visited[u] = true;
par[u] = p;
for (int v : adj[u])
{
if (v == p) continue;
if (visited[v])
{
if (root == 0)
{
root = v;
src = u;
return;
}
}
else
{
cc(v, u);
if (root) return;
}
}
}
void dfsdp(int u, int p)
{
int sum0 = 0, cntw0 = 0;
int sum1 = 0, cntw1 = 0;
vector<int> child;
for (int v : adj[u])
if (v != p)
{
child.push_back(v);
dfsdp(v, u);
}
for (int v : child)
{
if (dp[v][0] >= INF) cntw0++;
else sum0 += dp[v][0];
if (dp[v][1] >= INF) cntw1++;
else sum1 += dp[v][1];
}
int res0 = INF;
for (int v : child)
{
int val = dp[v][2];
int remain = (dp[v][0] >= INF) ? (cntw0 == 1 ? sum0 : INF) : (cntw0 == 0 ? sum0 - dp[v][0] : INF);
res0 = min(res0, add(val, remain));
}
dp[u][0] = res0;
dp[u][1] = (cntw0 == 0) ? sum0 : INF;
int res2 = INF;
for (int v : child)
{
int val = dp[v][3];
int remain = (dp[v][1] >= INF) ? (cntw1 == 1 ? sum1 : INF) : (cntw1 == 0 ? sum1 - dp[v][1] : INF);
res2 = min(res2, add(1, add(val, remain)));
}
dp[u][2] = res2;
dp[u][3] = (cntw1 == 0) ? add(1, sum1) : INF;
if (u == src)
{
int childwhite = dp[u][0];
int nowhite = dp[u][1];
int childblue = dp[u][2];
int noblue = dp[u][3];
dp[u][0] = dp[u][1] = dp[u][2] = dp[u][3] = INF;
if (rblue)
{
dp[u][0] = nowhite;
dp[u][1] = INF;
dp[u][2] = noblue;
dp[u][3] = INF;
}
else
{
dp[u][0] = childwhite;
dp[u][1] = nowhite;
dp[u][2] = childblue;
dp[u][3] = noblue;
}
if (sforcecolor == 0)
dp[u][2] = dp[u][3] = INF;
else
dp[u][0] = dp[u][1] = INF;
}
}
int caseoh(int rcolor, int scolor)
{
sforcecolor = scolor;
rblue = rcolor;
int dpstate = -1;
if (rcolor == 0 && scolor == 0) dpstate = 0;
else if (rcolor == 0 && scolor == 1) dpstate = 1;
else if (rcolor == 1 && scolor == 0) dpstate = 2;
else dpstate = 3;
dfsdp(root, 0);
return dp[root][dpstate];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("PAINT.inp", "r"))
{
freopen("PAINT.inp", "r", stdin);
freopen("PAINT.out", "w", stdout);
}
cin >> n;
for (int i = 1, u, v; i <= n; i++)
{
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cc(1, 0);
for (int &v : adj[root])
if (v == src)
{
v = adj[root].back();
adj[root].pop_back();
break;
}
for (int &v : adj[src])
if (v == root)
{
v = adj[src].back();
adj[src].pop_back();
break;
}
int ans = INF;
ans = min(ans, caseoh(0, 0));
ans = min(ans, caseoh(0, 1));
ans = min(ans, caseoh(1, 0));
ans = min(ans, caseoh(1, 1));
if (ans >= INF) cout << -1;
else cout << ans;
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IElORiA9IDFlOTsKaW50IG4sIHJvb3QsIHNyYzsKaW50IHBhclsxMDAwMDVdLCBkcFsxMDAwMDVdWzRdOwppbnQgc2ZvcmNlY29sb3IgPSAtMTsKaW50IHJibHVlID0gLTE7CmJvb2wgdmlzaXRlZFsxMDAwMDVdOwp2ZWN0b3I8aW50PiBhZGpbMTAwMDA1XTsKaW50IGFkZChpbnQgYSwgaW50IGIpCnsKICAgIGlmIChhID49IElORiB8fCBiID49IElORikgcmV0dXJuIElORjsKICAgIHJldHVybiBhICsgYjsKfQp2b2lkIGNjKGludCB1LCBpbnQgcCkKewogICAgaWYgKHJvb3QpIHJldHVybjsKICAgIHZpc2l0ZWRbdV0gPSB0cnVlOwogICAgcGFyW3VdID0gcDsKICAgIGZvciAoaW50IHYgOiBhZGpbdV0pCiAgICB7CiAgICAgICAgaWYgKHYgPT0gcCkgY29udGludWU7CiAgICAgICAgaWYgKHZpc2l0ZWRbdl0pCiAgICAgICAgewogICAgICAgICAgICBpZiAocm9vdCA9PSAwKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByb290ID0gdjsKICAgICAgICAgICAgICAgIHNyYyA9IHU7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgY2ModiwgdSk7CiAgICAgICAgICAgIGlmIChyb290KSByZXR1cm47CiAgICAgICAgfQogICAgfQp9CnZvaWQgZGZzZHAoaW50IHUsIGludCBwKQp7CiAgICBpbnQgc3VtMCA9IDAsIGNudHcwID0gMDsKICAgIGludCBzdW0xID0gMCwgY250dzEgPSAwOwogICAgdmVjdG9yPGludD4gY2hpbGQ7CiAgICBmb3IgKGludCB2IDogYWRqW3VdKQogICAgICAgIGlmICh2ICE9IHApCiAgICAgICAgewogICAgICAgICAgICBjaGlsZC5wdXNoX2JhY2sodik7CiAgICAgICAgICAgIGRmc2RwKHYsIHUpOwogICAgICAgIH0KICAgIGZvciAoaW50IHYgOiBjaGlsZCkKICAgIHsKICAgICAgICBpZiAoZHBbdl1bMF0gPj0gSU5GKSBjbnR3MCsrOwogICAgICAgIGVsc2Ugc3VtMCArPSBkcFt2XVswXTsKICAgICAgICBpZiAoZHBbdl1bMV0gPj0gSU5GKSBjbnR3MSsrOwogICAgICAgIGVsc2Ugc3VtMSArPSBkcFt2XVsxXTsKICAgIH0KICAgIGludCByZXMwID0gSU5GOwogICAgZm9yIChpbnQgdiA6IGNoaWxkKQogICAgewogICAgICAgIGludCB2YWwgPSBkcFt2XVsyXTsKICAgICAgICBpbnQgcmVtYWluID0gKGRwW3ZdWzBdID49IElORikgPyAoY250dzAgPT0gMSA/IHN1bTAgOiBJTkYpIDogKGNudHcwID09IDAgPyBzdW0wIC0gZHBbdl1bMF0gOiBJTkYpOwogICAgICAgIHJlczAgPSBtaW4ocmVzMCwgYWRkKHZhbCwgcmVtYWluKSk7CiAgICB9CiAgICBkcFt1XVswXSA9IHJlczA7CiAgICBkcFt1XVsxXSA9IChjbnR3MCA9PSAwKSA/IHN1bTAgOiBJTkY7CiAgICBpbnQgcmVzMiA9IElORjsKICAgIGZvciAoaW50IHYgOiBjaGlsZCkKICAgIHsKICAgICAgICBpbnQgdmFsID0gZHBbdl1bM107CiAgICAgICAgaW50IHJlbWFpbiA9IChkcFt2XVsxXSA+PSBJTkYpID8gKGNudHcxID09IDEgPyBzdW0xIDogSU5GKSA6IChjbnR3MSA9PSAwID8gc3VtMSAtIGRwW3ZdWzFdIDogSU5GKTsKICAgICAgICByZXMyID0gbWluKHJlczIsIGFkZCgxLCBhZGQodmFsLCByZW1haW4pKSk7CiAgICB9CiAgICBkcFt1XVsyXSA9IHJlczI7CiAgICBkcFt1XVszXSA9IChjbnR3MSA9PSAwKSA/IGFkZCgxLCBzdW0xKSA6IElORjsKICAgIGlmICh1ID09IHNyYykKICAgIHsKICAgICAgICBpbnQgY2hpbGR3aGl0ZSA9IGRwW3VdWzBdOwogICAgICAgIGludCBub3doaXRlICAgID0gZHBbdV1bMV07CiAgICAgICAgaW50IGNoaWxkYmx1ZSAgPSBkcFt1XVsyXTsKICAgICAgICBpbnQgbm9ibHVlICAgICA9IGRwW3VdWzNdOwogICAgICAgIGRwW3VdWzBdID0gZHBbdV1bMV0gPSBkcFt1XVsyXSA9IGRwW3VdWzNdID0gSU5GOwogICAgICAgIGlmIChyYmx1ZSkKICAgICAgICB7CiAgICAgICAgICAgIGRwW3VdWzBdID0gbm93aGl0ZTsKICAgICAgICAgICAgZHBbdV1bMV0gPSBJTkY7CiAgICAgICAgICAgIGRwW3VdWzJdID0gbm9ibHVlOwogICAgICAgICAgICBkcFt1XVszXSA9IElORjsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgZHBbdV1bMF0gPSBjaGlsZHdoaXRlOwogICAgICAgICAgICBkcFt1XVsxXSA9IG5vd2hpdGU7CiAgICAgICAgICAgIGRwW3VdWzJdID0gY2hpbGRibHVlOwogICAgICAgICAgICBkcFt1XVszXSA9IG5vYmx1ZTsKICAgICAgICB9CiAgICAgICAgaWYgKHNmb3JjZWNvbG9yID09IDApCiAgICAgICAgICAgIGRwW3VdWzJdID0gZHBbdV1bM10gPSBJTkY7CiAgICAgICAgZWxzZQogICAgICAgICAgICBkcFt1XVswXSA9IGRwW3VdWzFdID0gSU5GOwogICAgfQp9CmludCBjYXNlb2goaW50IHJjb2xvciwgaW50IHNjb2xvcikKewogICAgc2ZvcmNlY29sb3IgPSBzY29sb3I7CiAgICByYmx1ZSA9IHJjb2xvcjsKICAgIGludCBkcHN0YXRlID0gLTE7CiAgICBpZiAocmNvbG9yID09IDAgJiYgc2NvbG9yID09IDApIGRwc3RhdGUgPSAwOwogICAgZWxzZSBpZiAocmNvbG9yID09IDAgJiYgc2NvbG9yID09IDEpIGRwc3RhdGUgPSAxOwogICAgZWxzZSBpZiAocmNvbG9yID09IDEgJiYgc2NvbG9yID09IDApIGRwc3RhdGUgPSAyOwogICAgZWxzZSBkcHN0YXRlID0gMzsKICAgIGRmc2RwKHJvb3QsIDApOwogICAgcmV0dXJuIGRwW3Jvb3RdW2Rwc3RhdGVdOwp9CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKICAgIGlmIChmb3BlbigiUEFJTlQuaW5wIiwgInIiKSkKICAgIHsKICAgICAgICBmcmVvcGVuKCJQQUlOVC5pbnAiLCAiciIsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKCJQQUlOVC5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICB9CiAgICBjaW4gPj4gbjsKICAgIGZvciAoaW50IGkgPSAxLCB1LCB2OyBpIDw9IG47IGkrKykKICAgIHsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIGFkalt1XS5wdXNoX2JhY2sodik7CiAgICAgICAgYWRqW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIGNjKDEsIDApOwogICAgZm9yIChpbnQgJnYgOiBhZGpbcm9vdF0pCiAgICAgICAgaWYgKHYgPT0gc3JjKQogICAgICAgIHsKICAgICAgICAgICAgdiA9IGFkaltyb290XS5iYWNrKCk7CiAgICAgICAgICAgIGFkaltyb290XS5wb3BfYmFjaygpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICBmb3IgKGludCAmdiA6IGFkaltzcmNdKQogICAgICAgIGlmICh2ID09IHJvb3QpCiAgICAgICAgewogICAgICAgICAgICB2ID0gYWRqW3NyY10uYmFjaygpOwogICAgICAgICAgICBhZGpbc3JjXS5wb3BfYmFjaygpOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICBpbnQgYW5zID0gSU5GOwogICAgYW5zID0gbWluKGFucywgY2FzZW9oKDAsIDApKTsKICAgIGFucyA9IG1pbihhbnMsIGNhc2VvaCgwLCAxKSk7CiAgICBhbnMgPSBtaW4oYW5zLCBjYXNlb2goMSwgMCkpOwogICAgYW5zID0gbWluKGFucywgY2FzZW9oKDEsIDEpKTsKICAgIGlmIChhbnMgPj0gSU5GKSBjb3V0IDw8IC0xOwogICAgZWxzZSBjb3V0IDw8IGFuczsKICAgIHJldHVybiAwOwp9Cg==