#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

const int N = 3100;

int n, m, c[N][N], smin = 10000, p[N], s[N], cmin[N], rad;
vector<int> v[N], v1[N];
bool ver[N];

void dfs(int nod) {
	ver[nod] = true;

	for(int i = 0; i != v[nod].size(); ++i) if(!ver[v[nod][i]]) {
		p[v[nod][i]] = nod;
		v1[nod].push_back(v[nod][i]);

		dfs(v[nod][i]);

		s[nod] += s[v[nod][i]] + c[v[nod][i]][nod];
	}
}

void din(int nod) {

	cmin[nod] = s[nod];

	for(int i = 0; i < v1[nod].size(); ++i) {
		int el = v1[nod][i];
		din(el);

		cmin[nod] = min(cmin[nod], cmin[el] + c[nod][el] + s[nod] - s[el] - c[el][nod]);
	}

	smin = min(smin, cmin[nod] + s[rad] - s[nod] - c[nod][p[nod]]);
}

int main() {
	int i, a, b, j;

	//freopen("ttt", "r", stdin);

	cin >> n;

	for(i = 1; i!=n; ++i) {
		cin >> a >> b;

		v[a].push_back(b);
		v[b].push_back(a);
		c[a][b] = 1;
	}

	for(i = 1; i<=n; ++i) {

		for(j = 1; j<=n; ++j)
			v1[j].clear(), ver[j] = false, s[j] = 0, cmin[j] = 10000;
		rad = i;
		dfs(i);

		din(i);
	}

	cout << smin;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

const int N = 3100;

int n, m, c[N][N], smin = 10000, p[N], s[N], cmin[N], rad;
vector<int> v[N], v1[N];
bool ver[N];

void dfs(int nod) {
	ver[nod] = true;

	for(int i = 0; i != v[nod].size(); ++i) if(!ver[v[nod][i]]) {
		p[v[nod][i]] = nod;
		v1[nod].push_back(v[nod][i]);

		dfs(v[nod][i]);

		s[nod] += s[v[nod][i]] + c[v[nod][i]][nod];
	}
}

void din(int nod) {

	if(!v1[nod].size())
		cmin[nod] = 0;

	for(vector<int>::iterator it = v1[nod].begin(); it != v1[nod].end(); ++it) {

		din(*it);

		cmin[nod] = min(min(cmin[nod], s[nod]), cmin[*it] + c[nod][*it] + s[nod] - s[*it] - c[*it][nod]);
	}

	smin = min(smin, cmin[nod] + s[rad] - s[nod] - c[nod][p[nod]]);
}

int main() {
	int i, a, b, j;

	freopen("ttt", "r", stdin);

	cin >> n;

	for(i = 1; i!=n; ++i) {
		cin >> a >> b;

		v[a].push_back(b);
		v[b].push_back(a);
		c[a][b] = 1;
	}

	for(i = 1; i<=n; ++i) {

		for(j = 1; j<=n; ++j)
			v1[j].clear(), ver[j] = false, s[j] = 0, cmin[j] = 10000;
		rad = i;
		dfs(i);

		din(i);
	}

	cout << smin;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

const int N = 3100;

int n, m, c[N][N], smin = 10000, p[N], s[N], cmin[N], rad;
vector<int> v[N], v1[N];
bool ver[N];

void dfs(int nod) {
	ver[nod] = true;

	for(int i = 0; i != v[nod].size(); ++i) if(!ver[v[nod][i]]) {
		p[v[nod][i]] = nod;
		v1[nod].push_back(v[nod][i]);

		dfs(v[nod][i]);

		s[nod] += s[v[nod][i]] + c[v[nod][i]][nod];
	}
}

void din(int nod) {

	if(!v1[nod].size())
		cmin[nod] = 0;

	for(vector<int>::iterator it = v1[nod].begin(); it != v1[nod].end(); ++it) {

		din(*it);

		cmin[nod] = min(min(cmin[nod], s[nod]), cmin[*it] + c[nod][*it] + s[nod] - s[*it]);
	}

	smin = min(smin, cmin[nod] + s[rad] - s[nod] - c[nod][p[nod]]);
}

int main() {
	int i, a, b, j;

	//freopen("ttt", "r", stdin);

	cin >> n;

	for(i = 1; i!=n; ++i) {
		cin >> a >> b;

		v[a].push_back(b);
		v[b].push_back(a);
		c[a][b] = 1;
	}

	for(i = 1; i<=n; ++i) {

		for(j = 1; j<=n; ++j)
			v1[j].clear(), ver[j] = false, s[j] = 0, cmin[j] = 10000;
		rad = i;
		dfs(i);

		din(i);
	}

	cout << smin;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define inf 10000000000000007
#define eps 1e-8
#define PI 3.14159265359
#define sp printf(" ")
#define vi vector<int>
#define vl vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pcc pair<char, char>
#define mp make_pair
#define FF first
#define SS second
#define pb(x) push_back(x)
#define sd(x) scanf("%d", &(x))
#define pd(x) printf("%d", (x))
#define pdn(x) printf("%d\n", (x))
#define sld(x) scanf("%ld", &(x))
#define pld(x) printf("%ld", (x))
#define pldn(x) printf("%ld\n", (x))
#define slf(x) scanf("%lf", &(x))
#define plf(x) printf("%.9lf", (x))
#define plfn(x) printf("%.12lf\n", (x))
#define sch(x) scanf("%c", &(x))
#define pch(x) printf("%c", (x))
#define pchn(x) printf("%c\n", (x))
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0)
#define endl '\n'
using namespace std;
int par[3005][30],dp[3004][3004],dp1[3004][3004];
vector<pii>a[3004];vector<int>v;int z[005],pj[3005],h[3005];
void dfs(int i,int p,int y)
{
    for(int j=0;j<v.size();j++)
    {
        dp1[i][v[j]]=dp1[p][v[j]]+1-y;
        dp[i][v[j]]=min(dp1[i][v[j]],y+dp[p][v[j]]);
       // cout<<i<<" RRR "<<v[j]<<" "<<dp[i][v[j]]<<" "<<dp1[i][v[j]]<<endl;
    }
    v.pb(i);
    par[i][0]=p;
    for(int k=1;k<=20;k++)
    {
        if(par[i][k-1]!=-1)
        {
            par[i][k]=par[par[i][k-1]][k-1];
        }
    }
    for(int j=0;j<a[i].size();j++)
    {
        if(a[i][j].FF!=p)
        {
            h[a[i][j].FF]=h[i]+1;
            dfs(a[i][j].FF,i,1-a[i][j].SS);
            z[i]+=z[a[i][j].FF]+a[i][j].SS;
        }
    }
   // cout<<i<<" "<<z[i]<<endl;
    v.pop_back();
}
int lca(int u,int v)
{
    if(h[v]<h[u])swap(u,v);
   // cout<<u<<" TTT "<<v<<endl;
    for(int i=13;i>=0;i--)
    {
       // cout<<par[v][i]<<" vvv "<<h[par[v][i]]<<" "<<h[u]<<endl;
        if(par[v][i]!=-1&&h[par[v][i]]>=h[u])v=par[v][i];
    }
    //cout<<v<<endl;

    if(u==v)return v;
    for(int i=13;i>=0;i--)
    {
        if(par[v][i]!=par[u][i]){v=par[v][i];u=par[u][i];}
    }
    return par[v][0];

}
void dfs2(int i,int p)
{

   // cout<<i<<" "<<p<<" "<<pj[i]<<endl;
    for(int j=0;j<a[i].size();j++)
    {
        int r=a[i][j].FF;
        if(r!=p)
        {
            pj[r]=pj[i]+z[i]-(z[r]+a[i][j].SS)+1-a[i][j].SS;
            dfs2(r,i);
        }
    }
}
int main()
{
    ios;
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        a[x].pb(mp(y,0));
        a[y].pb(mp(x,1));
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=13;j++)par[i][j]=-1;
    }
    h[1]=1;
    dfs(1,-1,0);
    dfs2(1,-1);
    int ans=n-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int r=lca(i,j);
            int w1=(h[i]-h[r])-dp1[i][r];
            int w2=(h[j]-h[r])-dp1[j][r];
            int w=min(w1+dp[j][r],w2+dp[i][r]);
            w+=z[r]-dp1[i][r]-dp1[j][r]+pj[r];
          //  cout<<i<<" "<<j<<" "<<w<<endl;
            ans=min(ans,w);
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define inf 10000000000000007
#define eps 1e-8
#define PI 3.14159265359
#define sp printf(" ")
#define vi vector<int>
#define vl vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pcc pair<char, char>
#define mp make_pair
#define FF first
#define SS second
#define pb(x) push_back(x)
#define sd(x) scanf("%d", &(x))
#define pd(x) printf("%d", (x))
#define pdn(x) printf("%d\n", (x))
#define sld(x) scanf("%ld", &(x))
#define pld(x) printf("%ld", (x))
#define pldn(x) printf("%ld\n", (x))
#define slf(x) scanf("%lf", &(x))
#define plf(x) printf("%.9lf", (x))
#define plfn(x) printf("%.12lf\n", (x))
#define sch(x) scanf("%c", &(x))
#define pch(x) printf("%c", (x))
#define pchn(x) printf("%c\n", (x))
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0)
#define endl '\n'
using namespace std;
int par[3005][30],dp[3004][3004],dp1[3004][3004];
vector<pii>a[3004];vector<int>v;int z[3005],pj[3005],h[3005];
void dfs(int i,int p,int y)
{
    for(int j=0;j<v.size();j++)
    {
        dp1[i][v[j]]=dp1[p][v[j]]+1-y;
        dp[i][v[j]]=min(dp1[i][v[j]],y+dp[p][v[j]]);
       // cout<<i<<" RRR "<<v[j]<<" "<<dp[i][v[j]]<<" "<<dp1[i][v[j]]<<endl;
    }
    v.pb(i);
    par[i][0]=p;
    for(int k=1;k<=20;k++)
    {
        if(par[i][k-1]!=-1)
        {
            par[i][k]=par[par[i][k-1]][k-1];
        }
    }
    for(int j=0;j<a[i].size();j++)
    {
        if(a[i][j].FF!=p)
        {
            h[a[i][j].FF]=h[i]+1;
            dfs(a[i][j].FF,i,1-a[i][j].SS);
            z[i]+=z[a[i][j].FF]+a[i][j].SS;
        }
    }
   // cout<<i<<" "<<z[i]<<endl;
    v.pop_back();
}
int lca(int u,int v)
{
    if(h[v]<h[u])swap(u,v);
   // cout<<u<<" TTT "<<v<<endl;
    for(int i=13;i>=0;i--)
    {
       // cout<<par[v][i]<<" vvv "<<h[par[v][i]]<<" "<<h[u]<<endl;
        if(par[v][i]!=-1&&h[par[v][i]]>=h[u])v=par[v][i];
    }
    //cout<<v<<endl;

    if(u==v)return v;
    for(int i=13;i>=0;i--)
    {
        if(par[v][i]!=par[u][i]){v=par[v][i];u=par[u][i];}
    }
    return par[v][0];

}
void dfs2(int i,int p)
{

   // cout<<i<<" "<<p<<" "<<pj[i]<<endl;
    for(int j=0;j<a[i].size();j++)
    {
        int r=a[i][j].FF;
        if(r!=p)
        {
            pj[r]=pj[i]+z[i]-(z[r]+a[i][j].SS)+1-a[i][j].SS;
            dfs2(r,i);
        }
    }
}
int main()
{
    ios;
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        a[x].pb(mp(y,0));
        a[y].pb(mp(x,1));
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=13;j++)par[i][j]=-1;
    }
    dfs(1,-1,0);
    dfs2(1,-1);
    int ans=n-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int r=lca(i,j);
            int w1=(h[i]-h[r])-dp1[i][r];
            int w2=(h[j]-h[r])-dp1[j][r];
            int w=min(w1+dp[j][r],w2+dp[i][r]);
            w+=z[r]-dp1[i][r]-dp1[j][r]+pj[r];
          //  cout<<i<<" "<<j<<" "<<w<<endl;
            ans=min(ans,w);
        }
    }
    cout<<ans<<endl;

}


#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define inf 10000000000000007
#define eps 1e-8
#define PI 3.14159265359
#define sp printf(" ")
#define vi vector<int>
#define vl vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pcc pair<char, char>
#define mp make_pair
#define FF first
#define SS second
#define pb(x) push_back(x)
#define sd(x) scanf("%d", &(x))
#define pd(x) printf("%d", (x))
#define pdn(x) printf("%d\n", (x))
#define sld(x) scanf("%ld", &(x))
#define pld(x) printf("%ld", (x))
#define pldn(x) printf("%ld\n", (x))
#define slf(x) scanf("%lf", &(x))
#define plf(x) printf("%.9lf", (x))
#define plfn(x) printf("%.12lf\n", (x))
#define sch(x) scanf("%c", &(x))
#define pch(x) printf("%c", (x))
#define pchn(x) printf("%c\n", (x))
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define ios ios::sync_with_stdio(false); cin.tie(0) ; cout.tie(0)
#define endl '\n'
using namespace std;
int par[3005][30],dp[3004][3004],dp1[3004][3004];
vector<pii>a[3004];vector<int>v;int z[005],pj[3005],h[3005];
void dfs(int i,int p,int y)
{
    for(int j=0;j<v.size();j++)
    {
        dp1[i][v[j]]=dp1[p][v[j]]+1-y;
        dp[i][v[j]]=min(dp1[i][v[j]],y+dp[p][v[j]]);
       // cout<<i<<" RRR "<<v[j]<<" "<<dp[i][v[j]]<<" "<<dp1[i][v[j]]<<endl;
    }
    v.pb(i);
    par[i][0]=p;
    for(int k=1;k<=20;k++)
    {
        if(par[i][k-1]!=-1)
        {
            par[i][k]=par[par[i][k-1]][k-1];
        }
    }
    for(int j=0;j<a[i].size();j++)
    {
        if(a[i][j].FF!=p)
        {
            h[a[i][j].FF]=h[i]+1;
            dfs(a[i][j].FF,i,1-a[i][j].SS);
            z[i]+=z[a[i][j].FF]+a[i][j].SS;
        }
    }
   // cout<<i<<" "<<z[i]<<endl;
    v.pop_back();
}
int lca(int u,int v)
{
    if(h[v]<h[u])swap(u,v);
   // cout<<u<<" TTT "<<v<<endl;
    for(int i=20;i>=0;i--)
    {
       // cout<<par[v][i]<<" vvv "<<h[par[v][i]]<<" "<<h[u]<<endl;
        if(par[v][i]!=-1&&h[par[v][i]]>=h[u])v=par[v][i];
    }
    //cout<<v<<endl;

    if(u==v)return v;
    for(int i=20;i>=0;i--)
    {
        if(par[v][i]!=par[u][i]){v=par[v][i];u=par[u][i];}
    }
    return par[v][0];

}
void dfs2(int i,int p)
{

   // cout<<i<<" "<<p<<" "<<pj[i]<<endl;
    for(int j=0;j<a[i].size();j++)
    {
        int r=a[i][j].FF;
        if(r!=p)
        {
            pj[r]=pj[i]+z[i]-(z[r]+a[i][j].SS)+1-a[i][j].SS;
            dfs2(r,i);
        }
    }
}
int main()
{
    ios;
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        a[x].pb(mp(y,0));
        a[y].pb(mp(x,1));
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=20;j++)par[i][j]=-1;
    }
    dfs(1,-1,0);
    dfs2(1,-1);
    int ans=n;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int r=lca(i,j);
            int w1=(h[i]-h[r])-dp1[i][r];
            int w2=(h[j]-h[r])-dp1[j][r];
            int w=min(w1+dp[j][r],w2+dp[i][r]);
            w+=z[r]-dp1[i][r]-dp1[j][r]+pj[r];
          //  cout<<i<<" "<<j<<" "<<w<<endl;
            ans=min(ans,w);
        }
    }
    cout<<ans<<endl;

}


#include <cstdio>

const int INF = 1 << 30;

int ss[11111], ff[11111], he[11111], dp[11111], dwn[11111], bad[11111], was[11111];
int ne[11111];

int n, got;

void dfs(int v, int p) {
  was[v] = true;
  dwn[v] = 0;
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e]) continue;
    if (ff[e] == p) continue;
    dfs(ff[e], v);
    dwn[v] += dwn[ff[e]];
    if (e >= n - 1) dwn[v]++;
  }
}

void dfs2(int v, int p, int dd) {
  dp[v] = dd + dwn[v];
  if (got > dp[v]) got = dp[v];
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e] || ff[e] == p) continue;
    int cur = e >= n - 1 ? 1 : 0;
    dfs2(ff[e], v, dd + dwn[v] - dwn[ff[e]] - cur);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i + 1 < n; i++) {
    scanf("%d%d", ss + i, ff + i);
    --ss[i];
    --ff[i];
    ss[i + n - 1] = ff[i];
    ff[i + n - 1] = ss[i];
  }
  for (int i = 0; i < n; i++) he[i] = -1;
  for (int i = 0; i < n + n - 2; i++) {
    ne[i] = he[ss[i]];
    he[ss[i]] = i;
  }
  int ans = INF;
  for (int i = 0; i + 1 < n; i++) {
    bad[i] = bad[i + n - 1] = true;
    for (int j = 0; j < n; j++) was[j] = false;
    int cur = 0;
    for (int j = 0; j < n; j++) {
      if (was[j]) continue;
      got = INF;
      dfs(j, -1);
      dfs2(j, -1, 0);
      cur += got;
    }
    if (ans > cur) ans = cur;
    bad[i] = bad[i + n - 1] = false;
  }
  printf("%d\n", ans);
}


#include <cstdio>

const int INF = 1 << 30;

int ss[11111], ff[11111], he[11111], dp[11111], dwn[11111], bad[11111], was[11111];
int ne[11111];

int n, got;

void dfs(int v, int p) {
  was[v] = true;
  dwn[v] = 0;
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e]) continue;
    if (ff[e] == p) continue;
    dfs(ff[e], v);
    dwn[v] += dwn[ff[e]];
    if (e >= n - 1) dwn[v]++;
  }
}

void dfs2(int v, int p, int dd) {
  dp[v] = dd + dwn[v];
  if (got > dp[v]) got = dp[v];
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e] || ff[e] == p) continue;
    int cur = e >= n - 1 ? 1 : -1;
    dfs2(ff[e], v, dd + dwn[v] - dwn[ff[e]] - cur);
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i + 1 < n; i++) {
    scanf("%d%d", ss + i, ff + i);
    --ss[i];
    --ff[i];
    ss[i + n - 1] = ff[i];
    ff[i + n - 1] = ss[i];
  }
  for (int i = 0; i < n; i++) he[i] = -1;
  for (int i = 0; i < n + n - 2; i++) {
    ne[i] = he[ss[i]];
    he[ss[i]] = i;
  }
  int ans = INF;
  for (int i = 0; i + 1 < n; i++) {
    bad[i] = bad[i + n - 1] = true;
    for (int j = 0; j < n; j++) was[j] = false;
    int cur = 0;
    for (int j = 0; j < n; j++) {
      if (was[j]) continue;
      got = INF;
      dfs(j, -1);
      dfs2(j, -1, 0);
      cur += got;
    }
    if (ans > cur) ans = cur;
    bad[i] = bad[i + n - 1] = false;
  }
  printf("%d\n", ans);
}


#include <cstdio>

const int INF = 1 << 30;

int ss[11111], ff[11111], he[11111], dp[11111], dwn[11111], bad[11111], was[11111];
int ne[11111];

int n, got;

void dfs(int v, int p) {
  was[v] = true;
  dwn[v] = 0;
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e]) continue;
    if (ff[e] == p) continue;
    dfs(ff[e], v);
    dwn[v] += dwn[ff[e]];
    if (e >= n - 1) dwn[v]++;
  }
}

void dfs2(int v, int p, int dd) {
  dp[v] = dd + dwn[v];
  if (got > dp[v]) got = dp[v];
  for (int e = he[v]; e >= 0; e = ne[e]) {
    if (bad[e] || ff[e] == p) continue;
    int cur = e >= n - 1 ? 1 : -1;
    dfs2(ff[e], v, dd + dwn[v] - dwn[ff[e]] - cur);
  }
}

int main() {
  scanf("%d", &n);
  if (n == 1) {
    puts("0");
    return 0;
  }
  for (int i = 0; i + 1 < n; i++) {
    scanf("%d%d", ss + i, ff + i);
    --ss[i];
    --ff[i];
    ss[i + n - 1] = ff[i];
    ff[i + n - 1] = ss[i];
  }
  for (int i = 0; i < n; i++) he[i] = -1;
  for (int i = 0; i < n + n - 2; i++) {
    ne[i] = he[ss[i]];
    he[ss[i]] = i;
  }
  int ans = INF;
  for (int i = 0; i + 1 < n; i++) {
    bad[i] = bad[i + n - 1] = true;
    for (int j = 0; j < n; j++) was[j] = false;
    int cur = 0;
    for (int j = 0; j < n; j++) {
      if (was[j]) continue;
      got = INF;
      dfs(j, -1);
      dfs2(j, -1, 0);
      cur += got;
    }
    if (ans > cur) ans = cur;
    bad[i] = bad[i + n - 1] = false;
  }
  printf("%d\n", ans);
}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] ;

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }
    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( i!=j )
            {
                min1=min( ans[i]-lop[j] , min1 ) ;

            }

        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx];
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int h=0 , j1=j ;
                while( lll[j1][par[j1][0]] )
                {
                    j1=par[j1][0] ;
                    h++ ;
                }

                min1=min( ans[i]-h , min1 ) ;
            }
        }
    }
    cout << min1 ;

}
#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx];
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int h=0 , j1=j ;
                while( lll[j1][par[j1][0]] )
                {
                    j1=par[j1][0] ;
                    h++ ;
                }

                min1=min( ans[i]-h , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx] , dpop[mx] ;
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    if ( lll[x][p] )
        dpop[x]+=dpop[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                min1=min( ans[i]-dpop[j] , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx];
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int h=0 , j1=j ;
                while( lll[j1][par[j1][0]] )
                {
                    j1=par[j1][0] ;
                    h++ ;
                }

                min1=min( ans[i]-h , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx];

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }
    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
                min1=min( ans[i]-lop[j] , min1 ) ;
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx] , dpop[mx] ;
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    if ( lll[x][p] )
        dpop[x]=dpop[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int o=0 ;
                if ( dpop[j]>h[com_par( i , j )]-h[j] )
                    o=h[com_par( i , j )] ;
                    //if ( ans[i]-dpop[j]+o==-2 )
                        //cout << i << " " << j << endl ;
                min1=min( ans[i]-dpop[j]+o , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx];
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int h=0 , j1=j ;
                while( v[par[j1][0]].size()==2 && lll[j1][par[j1][0]] )
                {
                    j1=par[j1][0] ;
                    h++ ;
                }

                min1=min( ans[i]-h , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=2e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] ;

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }
    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( i!=j )
            {
                min1=min( ans[i]-lop[j] , min1 ) ;

            }

        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=2e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] ;

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( i!=j )
            {
                min1=min( ans[i]-lop[j] , min1 ) ;

            }

        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] ,  lop[mx] , par[mx][20] , d[mx] , dpop[mx] ;
bool lll[mx][mx] ;

inline int dfs1(int x,int p,int h1)
{
	d[x]=h1;
	par[x][0]=p;
	for(int i=1;i<20;i++)
		par[x][i]=par[par[x][i-1]][i-1];
	for(int i=0;i<v[x].size();i++)
		if(v[x][i].F!=p)
			dfs1(v[x][i].F,x,h1+1);
}
inline int Par(int x,int k)
{
	for(int i=0;i<20;i++)
		if(k&(1<<i))
			x=par[x][i];
	return x;
}
inline int com_par(int x,int y)
{
	if(d[x]!=d[y])
	{
		if(d[x]<d[y])
			y=Par(y,d[y]-d[x]);
		else
			x=Par(x,d[x]-d[y]);
	}
	if(x==y)
		return x;
	for(int i=19;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			x=par[x][i];
			y=par[y][i];
		}
	return par[x][0];
}

void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    if ( lll[x][p] )
        dpop[x]=dpop[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            lop[l.F]=(!l.S) ;
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
        lll[x][y]=1 ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }

    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
        {
            if ( com_par ( i , j)!=j )
            {
                int o=0 ;
                if ( dpop[j]>h[j]-h[com_par( i , j )] )
                    o=dpop[com_par( i , j )] ;
                    //if ( ans[i]-dpop[j]+o==-2 )
                        //cout << i << " " << j << endl ;
                min1=min( ans[i]-dpop[j]+o , min1 ) ;
            }
        }
    }
    cout << min1 ;

}


#include<bits/stdc++.h>
using namespace std ;
const int mx=3e3+4 ;

#define F first
#define S second
#define pb push_back
#define po pop_back
typedef pair < int , int > pii ;
vector < pii > v[mx] ;
int dp[mx] , h[mx] , ha[mx] ;
int ans[mx] , dpop[mx][mx] ;
void dfs  ( int x , int p )
{
    h[x]+=h[p]+1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        pii l=v[x][i] ;
        if ( l.F!=p )
        {
            ha[l.F]+=!(l.S)+ha[x] ;
            dfs ( l.F , x ) ;
            dp[x]+=dp[l.F] ;
            dp[x]+=!(l.S) ;
        }
    }
}
void dfs1 ( int y , int x  , int p )
{
    pii l ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        l=v[x][i] ;
        if ( l.F!=p )
        {
            if ( l.S==1 )
                dpop[y][l.F]=max( dpop[y][x]-1 , 0 ) ;
            else
                dpop[y][l.F]=dpop[y][x]+1 ;
            dfs1 ( y , l.F , x ) ;
        }
    }
}
int main()
{
    int n ;
    cin >> n ;
    for ( int i=1 ; i<n ; i++ )
    {
        int x , y ;
        cin >> x >> y ;
        v[x].pb ( pii ( y , 1 ) ) ;
        v[y]. pb ( pii ( x , 0 ) ) ;
    }
    h[0]=-1 ;
    dfs ( 1 , 0 ) ;
    ans[1]=dp[1] ;
    for ( int i=1 ; i<=n ; i++ )
        ans[i]=ans[1]+(h[i]-2*ha[i]) ;
    int min1=1e9 ;
    dfs1 ( 1 , 0 , 0 ) ;
    if ( n==1 )
    {
        cout << 0 ;
        return 0 ;
    }
    for ( int i=1 ; i<=n ; i++ )
        dfs1 ( i , i , 0 ) ;
    for ( int i=1 ; i<=n ; i++ )
    {
        for ( int j=1 ; j<=n ; j++ )
            min1=min ( min1 , ans[i]-dpop[i][j] ) ;
    }

    cout << min1 ;

}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include <cstdio>
#define N 200010
const int inf = 0x3fffffff;
using namespace std;
struct Edge{
    int next,val;
};
int edge[N][2];
vector<Edge>V[N];
int dp[N][2];
int ans;
void dp1(int u,int pre,int r){
    dp[u][0]=0;
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp1(son,u,r);
        dp[u][0]+=dp[son][0]+V[u][i].val;
    }
}
void dp2(int u,int pre,int i,int r){
    if(u==pre)dp[u][1]=dp[u][0];
    else{
        if(V[pre][i].val==1)dp[u][1]=dp[pre][1]-1;
        else dp[u][1]=dp[pre][1]+1;
    }
    ans=min(ans,dp[u][1]);
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp2(son,u,i,r);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        puts("0");
        return 0;
    }
    for(int i = 1; i <= n; i++)
        V[i].clear();
    for(int i = 1; i < n; i++){
        scanf("%d%d",&edge[i][0],&edge[i][1]);
        Edge E;
        E.next=edge[i][1];
        E.val=0;
        V[edge[i][0]].push_back(E);
        E.next=edge[i][0];
        E.val=1;
        V[edge[i][1]].push_back(E);
    }
    int rst=inf;
    for(int i = 1; i < n; i++){
        int u=edge[i][0],v=edge[i][1];
        int ans1,ans2;
        ans=inf;
        dp1(u,u,v);dp2(u,u,-1,v);
        ans1=ans;
        ans=inf;
        dp1(v,v,u);dp2(v,v,-1,u);
        ans2=ans;
        rst=min(ans1+ans2,rst);
    }
    printf("%d\n",rst);
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include <cstdio>
#define N 200010
const int inf = 0x3fffffff;
using namespace std;
struct Edge{
    int next,val;
};
int edge[N][2];
vector<Edge>V[N];
int dp[N][2];
int ans;
void dp1(int u,int pre,int r){
    dp[u][0]=0;
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp1(son,u,r);
        dp[u][0]+=dp[son][0]+V[u][i].val;
    }
}
void dp2(int u,int pre,int i,int r){
    if(u==pre) dp[u][1]=dp[u][0];
    else{
        dp[u][1]=dp[pre][1] + (V[pre][i].val==1)?(-1):( 1 );
    }
    ans=min(ans,dp[u][1]);
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp2(son,u,i,r);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        puts("0");
        return 0;
    }
    for(int i = 1; i <= n; i++)
        V[i].clear();
    for(int i = 1; i < n; i++){
        cin>>edge[i][0]>>edge[i][1];
        Edge E;
        E.next=edge[i][1];
        E.val=0;
        V[edge[i][0]].push_back(E);
        E.next=edge[i][0];
        E.val=1;
        V[edge[i][1]].push_back(E);
    }
    int rst=(1<<30);
    for(int i = 1; i < n; i++){
        int u=edge[i][0],v=edge[i][1];
        int ans1,ans2;
        ans=inf;
        dp1(u,u,v);dp2(u,u,-1,v);
        ans1=ans;
        ans=inf;
        dp1(v,v,u);dp2(v,v,-1,u);
        ans2=ans;
        rst=min(ans1+ans2,rst);
    }
    printf("%d\n",rst);
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include <cstdio>
#define N 200010
const int inf = 0x3fffffff;
using namespace std;
struct Edge{
    int next,val;
};
int edge[N][2];
vector<Edge>V[N];
int dp[N][2];
int ans;
void dp1(int u,int pre,int r){
    dp[u][0]=0;
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp1(son,u,r);
        dp[u][0]+=dp[son][0]+V[u][i].val;
    }
}
void dp2(int u,int pre,int i,int r){
    if(u==pre) dp[u][1]=dp[u][0];
    else{
        dp[u][1]=dp[pre][1] + (V[pre][i].val==1)?(-1):( 1 );
    }
    ans=min(ans,dp[u][1]);
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp2(son,u,i,r);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        puts("0");
        return 0;
    }
    for(int i = 1; i <= n; i++)
        V[i].clear();
    for(int i = 1; i < n; i++){
        cin>>edge[i][0]>>edge[i][1];
        Edge E;
        E.next=edge[i][1];
        E.val=0;
        V[edge[i][0]].push_back(E);
        E.next=edge[i][0];
        E.val=1;
        V[edge[i][1]].push_back(E);
    }
    int rst=(1<<30);
    for(int i = 1; i < n; i++){
        int u=edge[i][0],v=edge[i][1];
        int ans1,ans2;
        ans=inf;
        dp1(u,u,v);dp2(u,u,-1,v);
        ans1=ans;
        ans=inf;
        dp1(v,v,u);dp2(v,v,-1,u);
        ans2=ans;
        rst=min(ans1+ans2,rst);
    }
    printf("%d\n",rst)
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 200010
const int inf = 0x3fffffff;
using namespace std;
struct Edge{
    int next,val;
};
int edge[N][2];
vector<Edge>V[N];
int dp[N][2];
int ans;
void dp1(int u,int pre,int r){
    dp[u][0]=0;
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp1(son,u,r);
        dp[u][0]+=dp[son][0]+V[u][i].val;
    }
}
void dp2(int u,int pre,int i,int r){
    if(u==pre) dp[u][1]=dp[u][0];
    else{
        dp[u][1]=dp[pre][1] + (V[pre][i].val==1)?(-1):( 1 );
    }
    ans=min(ans,dp[u][1]);
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp2(son,u,i,r);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        puts("0");
        return 0;
    }
    for(int i = 1; i <= n; i++)
        V[i].clear();
    for(int i = 1; i < n; i++){
        cin>>edge[i][0]>>edge[i][1];
        Edge E;
        E.next=edge[i][1];
        E.val=0;
        V[edge[i][0]].push_back(E);
        E.next=edge[i][0];
        E.val=1;
        V[edge[i][1]].push_back(E);
    }
    int rst=(1<<30);
    for(int i = 1; i < n; i++){
        int u=edge[i][0],v=edge[i][1];
        int ans1,ans2;
        ans=inf;
        dp1(u,u,v);dp2(u,u,-1,v);
        ans1=ans;
        ans=inf;
        dp1(v,v,u);dp2(v,v,-1,u);
        ans2=ans;
        rst=min(ans1+ans2,rst);
    }
    printf("%d\n",rst)
    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include <cstdio>
#define N 200010
const int inf = 0x3fffffff;
using namespace std;
struct Edge{
    int next,val;
};
int edge[N][2];
vector<Edge>V[N];
int dp[N][2];
int ans;
void dp1(int u,int pre,int r){
    dp[u][0]=0;
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp1(son,u,r);
        dp[u][0]+=dp[son][0]+V[u][i].val;
    }
}
void dp2(int u,int pre,int i,int r){
    if(u==pre)dp[u][1]=dp[u][0];
    else{
        if(V[pre][i].val==1)dp[u][1]=dp[pre][1]-1;
        else dp[u][1]=dp[pre][1]+1;
    }
    ans=min(ans,dp[u][1]);
    for(int i=0;i<V[u].size();i++){
        int son=V[u][i].next;
        if(son==pre||son==r)continue;
        dp2(son,u,i,r);
    }
}
int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        puts("0");
        return 0;
    }
    for(int i = 1; i <= n; i++)
        V[i].clear();
    for(int i = 1; i < n; i++){
        scanf("%d%d",&edge[i][0],edge[i][1]);
        Edge E;
        E.next=edge[i][1];
        E.val=0;
        V[edge[i][0]].push_back(E);
        E.next=edge[i][0];
        E.val=1;
        V[edge[i][1]].push_back(E);
    }
    int rst=inf;
    for(int i = 1; i < n; i++){
        int u=edge[i][0],v=edge[i][1];
        int ans1,ans2;
        ans=inf;
        dp1(u,u,v);dp2(u,u,-1,v);
        ans1=ans;
        ans=inf;
        dp1(v,v,u);dp2(v,v,-1,u);
        ans2=ans;
        rst=min(ans1+ans2,rst);
    }
    printf("%d\n",rst);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>

const int N=3005;
const int mod=1e9+7;

int qwe,ans,mn,anss=INT_MAX,x,y,i,n;

vector<pii > V[N];

void dfs(int x,int ata){

	int i;

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata){
			ans+=V[x][i].st;
			dfs(V[x][i].nd,x);
		}
}

void dfs2(int x,int ata,int edge,int bef){

	mn=min(mn,edge-2*bef);

	int i;

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata){
			if(edge or V[x][i].st)
				dfs2(V[x][i].nd,x,edge+1,bef+V[x][i].st);
			else dfs2(V[x][i].nd,x,edge,bef);
		}

}

main(){

	scanf("%d",&n);

	for(i=1 ; i<=n-1 ; i++){
		scanf("%d %d",&x,&y);
		V[x].pb(mp(0,y));
		V[y].pb(mp(1,x));
	}

	for(i=1 ; i<=n ; i++){
		ans=0;
		dfs(i,-1);
//		cout<<ans<<' ';
		mn=0;
		dfs2(i,-1,0,0);
//		cout<<mn<<' '<<endl;
		anss=min(anss,ans+mn);
	}

	cout<<anss<<endl;

}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>

const int N=3005;
const int mod=1e9+7;

int qwe,ans=INT_MAX,mn,x,y,i,n,ans1,ans2;
int A[N];

vector<pii > V[N],v;

void dfs(int x,int ata){

	int i;

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata){
			dfs(V[x][i].nd,x);
			A[x]+=A[V[x][i].nd]+V[x][i].st;
		}

	return ;
}

void f(int x,int ata,int p){

	int i;

	mn=min(mn,p+A[x]);

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata)
			f(V[x][i].nd,x,p+A[x]+(V[x][i].st==1?-1:1)-A[V[x][i].nd]);
}

main(){

	scanf("%d",&n);

	for(i=1 ; i<=n-1 ; i++){
		scanf("%d %d",&x,&y);
		v.pb(mp(x,y));
		V[x].pb(mp(0,y));
		V[y].pb(mp(1,x));
	}

	for(i=0 ; i<=n-2 ; i++){
		memset(A,0,sizeof A);
		x=v[i].st;
		y=v[i].nd;
		dfs(x,y);
		dfs(y,x);

		mn=INT_MAX;
		f(x,y,0);
		ans1=mn;
		mn=INT_MAX;
		f(y,x,0);
		ans2=mn;

		ans=min(ans,ans1+ans2);
	}

	cout<<ans<<endl;

}


#include<bits/stdc++.h>

using namespace std;

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (root+root)
#define sag (root+root+1)
#define orta ((bas+son)/2)
#define ll long long
#define pii pair<int,int>

const int N=3005;
const int mod=1e9+7;

int qwe,ans=INT_MAX,mn,x,y,i,n,ans1,ans2;
int A[N];

vector<pii > V[N],v;

void dfs(int x,int ata){

	int i;

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata){
			dfs(V[x][i].nd,x);
			A[x]+=A[V[x][i].nd]+V[x][i].st;
		}

	return ;
}

void f(int x,int ata,int p){

	int i;

	mn=min(mn,p+A[x]);

	for(i=0 ; i<V[x].size() ; i++)
		if(V[x][i].nd!=ata)
			f(V[x][i].nd,x,p+A[x]+(V[x][i].st==1?-1:1)-A[V[x][i].nd]);
}

main(){

	scanf("%d",&n);

	if(n==1)
		return cout<<0,0;

	for(i=1 ; i<=n-1 ; i++){
		scanf("%d %d",&x,&y);
		v.pb(mp(x,y));
		V[x].pb(mp(0,y));
		V[y].pb(mp(1,x));
	}

	for(i=0 ; i<=n-2 ; i++){
		memset(A,0,sizeof A);
		x=v[i].st;
		y=v[i].nd;
		dfs(x,y);
		dfs(y,x);

		mn=INT_MAX;
		f(x,y,0);
		ans1=mn;
		mn=INT_MAX;
		f(y,x,0);
		ans2=mn;

		ans=min(ans,ans1+ans2);
	}

	cout<<ans<<endl;

}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans,cnt;
int b[3005][3005],mark[3001];
vector<int> a[3005];
int dp[3005],pred[3005];

void dfs(int i,int pr)
{
	int j;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt++;
			dfs(a[i][j],i);
		}
}

int f(int i,int pr)
{
	if (dp[i] != -1) return dp[i];
	int j;
	dp[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr && b[i][a[i][j]] == 0)
		{
			dp[i] = max(dp[i],f(a[i][j],i)+1);
		}
	return dp[i];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		cnt = 0;
memset(pred,-1,sizeof(pred));
		dfs(i,i);

		memset(dp,-1,sizeof(dp));
		for (j = 1;j<=n;j++)
			ans = min(ans,cnt-f(j,pred[j]));

	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans,cnt;
int b[3005][3005],mark[3001];
vector<int> a[3005];
int dp[3005],pred[3005];

void dfs(int i,int pr)
{
	pred[i] = pr;
	int j;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt++;
			dfs(a[i][j],i);
		}
}

int f(int i,int pr)
{
	if (dp[i] != -1) return dp[i];
	int j;
	dp[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr && b[i][a[i][j]] == 0)
		{
			dp[i] = max(dp[i],f(a[i][j],i)+1);
		}
	return dp[i];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		cnt = 0;
		memset(pred,-1,sizeof(pred));
		dfs(i,i);

		memset(dp,-1,sizeof(dp));
		for (j = 1;j<=n;j++)
			ans = min(ans,cnt-f(j,pred[j]));

	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans,cnt;
bool b[3001][3001],mark[3001];
vector<int> a[3001];
int dp[3001],pred[3001];

void dfs(int i,int pr)
{
	int j;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt++;
			dfs(a[i][j],i);
		}
}

int f(int i,int pr)
{
	if (dp[i] != -1) return dp[i];
	int j;
	dp[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr && b[i][a[i][j]] == 0)
		{
			dp[i] = max(dp[i],f(a[i][j],i)+1);
		}
	return dp[i];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		cnt = 0;
		dfs(i,i);
		memset(dp,-1,sizeof(dp));
		for (j = 1;j<=n;j++)
			ans = min(ans,cnt-f(j,pred[j]));

	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans,cnt;
int b[3005][3005],mark[3001];
vector<int> a[3005];
int dp[3005],pred[3005];

void dfs(int i,int pr)
{
	int j;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt++;
			dfs(a[i][j],i);
		}
}

int f(int i,int pr)
{
	if (dp[i] != -1) return dp[i];
	int j;
	dp[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr && b[i][a[i][j]] == 0)
		{
			dp[i] = max(dp[i],f(a[i][j],i)+1);
		}
	return dp[i];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		cnt = 0;
		dfs(i,i);
		memset(pred,-1,sizeof(pred));
		memset(dp,-1,sizeof(dp));
		for (j = 1;j<=n;j++)
			ans = min(ans,cnt-f(j,pred[j]));

	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans;
int b[3005][3005],mark[3001];
vector<int> a[3005];
int dp[3005][3],pred[3005],cnt[3005];

void dfs(int i,int pr)
{
	pred[i] = pr;
	int j;
	cnt[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt[i]++;
			dfs(a[i][j],i);
			cnt[i] += cnt[a[i][j]];
		}
}

int rec(int i,int t,int pr)
{
	if (dp[i][t] != -1) return dp[i][t];
	int j;
	dp[i][t] = cnt[i];
	int sum = cnt[i];
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			int q = a[i][j];
			if (t == 0)
			{
				dp[i][t] = min(dp[i][t],sum-cnt[q]+min(rec(q,0,i),rec(q,1,i)));
			}
			else if (t == 1)
			{
				int d;
				if (b[i][q] == 0) d = -1;
				else d = 1;
				dp[i][t] = min(dp[i][t],sum-cnt[q]+d+rec(q,t,i));
			}
		}
	return dp[i][t];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		memset(cnt,0,sizeof(cnt));

		dfs(i,i);

		memset(dp,-1,sizeof(dp));
		ans = min(ans,rec(i,0,i));
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <memory.h>

using namespace std;

int n,i,j,x,y,ans,cnt;
bool b[3001][3001],mark[3001];
vector<int> a[3001];
int dp[3001],pred[3001];

void dfs(int i,int pr)
{
	int j;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			pred[a[i][j]] = i;
			if (b[i][a[i][j]] == 0) cnt++;
			dfs(a[i][j],i);
		}
}

int f(int i,int pr)
{
	if (dp[i] != -1) return dp[i];
	int j;
	dp[i] = 0;
	for (j = 0;j<a[i].size();j++)
		if (a[i][j] != pr)
		{
			int p = 0;
			if (b[i][a[i][j]] == 0) p++;
			dp[i] = max(dp[i],p+f(a[i][j],i));
		}
	return dp[i];
}

int main()
{
    #ifndef ONLINE_JUDGE
	freopen("a.in", "rt", stdin);
    freopen("a.out", "wt", stdout);
    #endif
    cout.setf(ios :: fixed | ios :: showpoint);
    cout.precision(9);
	cin >> n;
	for (i = 1;i<n;i++)
	{
		cin >> x >> y;
		b[x][y] = 1;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	ans = n-1;
	for (i = 1;i<=n;i++)
	{
		cnt = 0;
		dfs(i,i);
		memset(dp,-1,sizeof(dp));
		for (j = 1;j<=n;j++)
			ans = min(ans,cnt-f(j,pred[j]));
	}
	cout << ans;
	return 0;
}


// inplace_merge(T + beg, T + mid + 1, T + end + 1);
// unsigned Long hash[maxn],p = 999999999989; hash[0] = 1; fo(i,1,maxn)hash[i] = hash[i-1] * p;
// __builtin_popcount(n);
/*
ID: Tariqul
PROG:
LANG: C++
*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))
#define inf 1e9 + 10
#ifdef localhost
#define Long long long
#else
typedef __int64 Long;
#endif // localhost
#define pi (2*acos(0))
#define eps 1e-9

#define two(X) (1<<(X))
#define twoL(X) (((Long)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

char BUFFER[100000 + 5];
bool readn(int &n)	{ return scanf("%d",&n) == 1; }
#ifdef localhost
bool readl(Long &n)	{ return scanf("%lld",&n) == 1; }
#else
bool readl(Long &n)	{ return scanf("%I64d",&n) == 1; }
#endif // localhost
bool readd(double &n){ return scanf("%lf",&n) == 1; }
bool reads(string &s){ s = ""; int n = scanf("%s",BUFFER); if(n == 1)s = BUFFER; return n == 1; }
bool readln(string &s){ char *valid = gets(BUFFER); if(valid)s = BUFFER; return ((bool)valid); }
#ifdef localhost
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%lld\n",n) : printf("%lld",n);  }
#else
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%I64d\n",n) :  printf("%I64d",n); }
#endif // localhost

vector<vector<int> > adj,direction;
vector<int> cost,pcost[2];

int dfs(int prev, int cur)
{
    int next,i,su = 0, val = 0;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        val = dfs(cur,next);
        val += direction[cur][i];
        su += val;
        pcost[1][cur] += val;
    }
    return  pcost[1][cur];
}

void dfs1(int prev, int cur, int extra = -1, int add = -1)
{
    if(prev != -1)
    {
        pcost[0][cur] = cost[prev] - pcost[1][cur] + add - extra;
    }
    cost[cur] = pcost[0][cur] + pcost[1][cur];
    int next,i;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        dfs1(cur,next,direction[cur][i],1 - direction[cur][i]);
    }
}

int dfs2(int prev, int cur, int w)
{
    int next,i,mi = cost[cur] - w;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs2(cur,next,w));
    }
    return mi;
}

int dfs3(int prev, int cur)
{
    int next,i,mi = inf;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs3(cur,next));
        mi = min(mi,dfs2(next,cur,pcost[1][next] + direction[cur][i]) + dfs2(cur,next,pcost[0][next]));
    }
    return mi;
}

int main()
{
    #ifdef localhost
    freopen("/home/tariqul/APC/Contest/input.txt","r",stdin);
    //freopen("/home/tariqul/APC/Contest/output-3.txt","w",stdout);
    #endif

    adj.clear(); direction.clear(); cost.clear(); pcost[0].clear(); pcost[1].clear();
    int i,j,n,a,b,root = 0,cur,next,best = inf; cin >> n; adj.resize(n,vector<int>(0)); direction.resize(n,vector<int>(0));
    cost.resize(n,0); pcost[0].resize(n,0); pcost[1].resize(n,0);
    fo(i,1,n)
    {
        cin >> a >> b; a--; b--; cost[a]++; cost[b]++;
        adj[a].push_back(b); direction[a].push_back(0);
        adj[b].push_back(a); direction[b].push_back(1);
    }

    foo(i,0,cost)if(cost[i] == 1){ root = i; break; }
    dfs(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    dfs1(-1,root);
    foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    best = dfs3(-1,root);
    if(best == inf)best = 0;
    cout << best << endl;

    return 0;
}


// inplace_merge(T + beg, T + mid + 1, T + end + 1);
// unsigned Long hash[maxn],p = 999999999989; hash[0] = 1; fo(i,1,maxn)hash[i] = hash[i-1] * p;
// __builtin_popcount(n);
/*
ID: Tariqul
PROG:
LANG: C++
*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))
#define inf 1e9 + 10
#ifdef localhost
#define Long long long
#else
typedef __int64 Long;
#endif // localhost
#define pi (2*acos(0))
#define eps 1e-9

#define two(X) (1<<(X))
#define twoL(X) (((Long)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

char BUFFER[100000 + 5];
bool readn(int &n)	{ return scanf("%d",&n) == 1; }
#ifdef localhost
bool readl(Long &n)	{ return scanf("%lld",&n) == 1; }
#else
bool readl(Long &n)	{ return scanf("%I64d",&n) == 1; }
#endif // localhost
bool readd(double &n){ return scanf("%lf",&n) == 1; }
bool reads(string &s){ s = ""; int n = scanf("%s",BUFFER); if(n == 1)s = BUFFER; return n == 1; }
bool readln(string &s){ char *valid = gets(BUFFER); if(valid)s = BUFFER; return ((bool)valid); }
#ifdef localhost
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%lld\n",n) : printf("%lld",n);  }
#else
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%I64d\n",n) :  printf("%I64d",n); }
#endif // localhost

vector<vector<int> > adj,direction;
vector<int> cost,pcost[2];

int dfs(int prev, int cur)
{
    int next,i,su = 0, val = 0;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        val = dfs(cur,next);
        val += direction[cur][i];
        su += val;
        pcost[1][cur] += val;
    }
    return  pcost[1][cur];
}

void dfs1(int prev, int cur, int extra = -1, int add = -1)
{
    if(prev != -1)
    {
        pcost[0][cur] = cost[prev] - pcost[1][cur] + add - extra;
    }
    cost[cur] = pcost[0][cur] + pcost[1][cur];
    int next,i;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        dfs1(cur,next,direction[cur][i],1 - direction[cur][i]);
    }
}

int dfs2(int prev, int cur, int w)
{
    int next,i,mi = cost[cur] - w;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs2(cur,next,w));
    }
    return mi;
}

int dfs3(int prev, int cur)
{
    int next,i,mi = inf;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs3(cur,next));
        mi = min(mi,dfs2(next,cur,pcost[1][next] + direction[cur][i]) + dfs2(cur,next,pcost[0][cur] + 1 - direction[cur][i]));
    }
    return mi;
}

int main()
{
    #ifdef localhost
    freopen("/home/tariqul/APC/Contest/input.txt","r",stdin);
    //freopen("/home/tariqul/APC/Contest/output-3.txt","w",stdout);
    #endif

    adj.clear(); direction.clear(); cost.clear(); pcost[0].clear(); pcost[1].clear();
    int i,j,n,a,b,root = -1,cur,next,best = inf; cin >> n; adj.resize(n,vector<int>(0)); direction.resize(n,vector<int>(0));
    cost.resize(n,0); pcost[0].resize(n,0); pcost[1].resize(n,0);
    fo(i,1,n)
    {
        cin >> a >> b; a--; b--; cost[a]++; cost[b]++;
        adj[a].push_back(b); direction[a].push_back(0);
        adj[b].push_back(a); direction[b].push_back(1);
    }

    foo(i,0,cost)if(cost[i] == 1){ root = i; break; }
    if(root >= 0)dfs(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    dfs1(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    cout << dfs3(-1,root) << endl;

    return 0;
}


// inplace_merge(T + beg, T + mid + 1, T + end + 1);
// unsigned Long hash[maxn],p = 999999999989; hash[0] = 1; fo(i,1,maxn)hash[i] = hash[i-1] * p;
// __builtin_popcount(n);
/*
ID: Tariqul
PROG:
LANG: C++
*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))
#define inf 1e9 + 10
#ifdef localhost
#define Long long long
#else
typedef __int64 Long;
#endif // localhost
#define pi (2*acos(0))
#define eps 1e-9

#define two(X) (1<<(X))
#define twoL(X) (((Long)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

char BUFFER[100000 + 5];
bool readn(int &n)	{ return scanf("%d",&n) == 1; }
#ifdef localhost
bool readl(Long &n)	{ return scanf("%lld",&n) == 1; }
#else
bool readl(Long &n)	{ return scanf("%I64d",&n) == 1; }
#endif // localhost
bool readd(double &n){ return scanf("%lf",&n) == 1; }
bool reads(string &s){ s = ""; int n = scanf("%s",BUFFER); if(n == 1)s = BUFFER; return n == 1; }
bool readln(string &s){ char *valid = gets(BUFFER); if(valid)s = BUFFER; return ((bool)valid); }
#ifdef localhost
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%lld\n",n) : printf("%lld",n);  }
#else
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%I64d\n",n) :  printf("%I64d",n); }
#endif // localhost

vector<vector<int> > adj,direction;
vector<int> cost,pcost[2];

int dfs(int prev, int cur)
{
    int next,i,su = 0, val = 0;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        val = dfs(cur,next);
        val += direction[cur][i];
        su += val;
        pcost[1][cur] += val;
    }
    return  pcost[1][cur];
}

void dfs1(int prev, int cur, int extra = -1, int add = -1)
{
    if(prev != -1)
    {
        pcost[0][cur] = cost[prev] - pcost[1][cur] + add - extra;
    }
    cost[cur] = pcost[0][cur] + pcost[1][cur];
    int next,i;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        dfs1(cur,next,direction[cur][i],1 - direction[cur][i]);
    }
}

int dfs2(int prev, int cur, int w)
{
    int next,i,mi = cost[cur] - w;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs2(cur,next,w));
    }
    return mi;
}

int dfs3(int prev, int cur)
{
    int next,i,mi = inf;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs3(cur,next));
        mi = min(mi,dfs2(next,cur,pcost[1][next] + direction[cur][i]) + dfs2(cur,next,pcost[0][next]));
    }
    return mi;
}

int main()
{
    #ifdef localhost
    freopen("/home/tariqul/APC/Contest/input.txt","r",stdin);
    //freopen("/home/tariqul/APC/Contest/output-3.txt","w",stdout);
    #endif

    adj.clear(); direction.clear(); cost.clear(); pcost[0].clear(); pcost[1].clear();
    int i,j,n,a,b,root = 0,cur,next,best = inf; cin >> n; adj.resize(n,vector<int>(0)); direction.resize(n,vector<int>(0));
    cost.resize(n,0); pcost[0].resize(n,0); pcost[1].resize(n,0);
    fo(i,1,n)
    {
        cin >> a >> b; a--; b--; cost[a]++; cost[b]++;
        adj[a].push_back(b); direction[a].push_back(0);
        adj[b].push_back(a); direction[b].push_back(1);
    }

    foo(i,0,cost)if(cost[i] == 1){ root = i; break; }
    dfs(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    dfs1(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    best = dfs3(-1,root);
    if(best == inf)best = 0;
    cout << best << endl;

    return 0;
}


// inplace_merge(T + beg, T + mid + 1, T + end + 1);
// unsigned Long hash[maxn],p = 999999999989; hash[0] = 1; fo(i,1,maxn)hash[i] = hash[i-1] * p;
// __builtin_popcount(n);
/*
ID: Tariqul
PROG:
LANG: C++
*/

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))
#define inf 1e9 + 10
#ifdef localhost
#define Long long long
#else
typedef __int64 Long;
#endif // localhost
#define pi (2*acos(0))
#define eps 1e-9

#define two(X) (1<<(X))
#define twoL(X) (((Long)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)

char BUFFER[100000 + 5];
bool readn(int &n)	{ return scanf("%d",&n) == 1; }
#ifdef localhost
bool readl(Long &n)	{ return scanf("%lld",&n) == 1; }
#else
bool readl(Long &n)	{ return scanf("%I64d",&n) == 1; }
#endif // localhost
bool readd(double &n){ return scanf("%lf",&n) == 1; }
bool reads(string &s){ s = ""; int n = scanf("%s",BUFFER); if(n == 1)s = BUFFER; return n == 1; }
bool readln(string &s){ char *valid = gets(BUFFER); if(valid)s = BUFFER; return ((bool)valid); }
#ifdef localhost
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%lld\n",n) : printf("%lld",n);  }
#else
void Print(Long &n, bool printNewLine = true)	{ printNewLine ? printf("%I64d\n",n) :  printf("%I64d",n); }
#endif // localhost

vector<vector<int> > adj,direction;
vector<int> cost,pcost[2];

int dfs(int prev, int cur)
{
    int next,i,su = 0, val = 0;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        val = dfs(cur,next);
        val += direction[cur][i];
        su += val;
        pcost[1][cur] += val;
    }
    return  pcost[1][cur];
}

void dfs1(int prev, int cur, int extra = -1, int add = -1)
{
    if(prev != -1)
    {
        pcost[0][cur] = cost[prev] - pcost[1][cur] + add - extra;
    }
    cost[cur] = pcost[0][cur] + pcost[1][cur];
    int next,i;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        dfs1(cur,next,direction[cur][i],1 - direction[cur][i]);
    }
}

int dfs2(int prev, int cur, int w)
{
    int next,i,mi = cost[cur] - w;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs2(cur,next,w));
    }
    return mi;
}

int dfs3(int prev, int cur)
{
    int next,i,mi = inf;
    foo(i,0,adj[cur])
    {
        next = adj[cur][i];
        if(next == prev)continue;
        mi = min(mi,dfs3(cur,next));
        mi = min(mi,dfs2(next,cur,pcost[1][next] + direction[cur][i]) + dfs2(cur,next,pcost[0][cur] + 1 - direction[cur][i]));
    }
    return mi;
}

int main()
{
    #ifdef localhost
    freopen("/home/tariqul/APC/Contest/input.txt","r",stdin);
    //freopen("/home/tariqul/APC/Contest/output-3.txt","w",stdout);
    #endif

    adj.clear(); direction.clear(); cost.clear(); pcost[0].clear(); pcost[1].clear();
    int i,j,n,a,b,root = 0,cur,next,best = inf; cin >> n; adj.resize(n,vector<int>(0)); direction.resize(n,vector<int>(0));
    cost.resize(n,0); pcost[0].resize(n,0); pcost[1].resize(n,0);
    fo(i,1,n)
    {
        cin >> a >> b; a--; b--; cost[a]++; cost[b]++;
        adj[a].push_back(b); direction[a].push_back(0);
        adj[b].push_back(a); direction[b].push_back(1);
    }

    foo(i,0,cost)if(cost[i] == 1){ root = i; break; }
    dfs(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    dfs1(-1,root);
    //foo(i,0,cost)cout << i + 1 << " ---> " << pcost[0][i] << " ---- " << pcost[1][i] << " ---- " << cost[i] << endl;
    best = dfs3(-1,root);
    if(best == inf)best = 0;
    cout << best << endl;

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		dfs(1, -1); ans = dp[1][2];
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
bool vis[N<<1];
int dp[N][2];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs1(int x, int f) {
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f || !is[i]) continue;
		dfs1(v, x); vis[i] = true;
	}
}

void dfs2(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs2(v, x);
		res += dp[v][0];
		if (!vis[i] && !is[i]) {
			res ++;
		}
	}
	dp[x][0] = dp[x][1] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int jia = 0;
		if (!vis[i]) {
			if (is[i]) {
				jia = 1;
			} else {
				jia = -1;
			}
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+jia);
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=1; i<=n; i++) {
			memset(vis, 0, sizeof(vis));
			dfs1(i, -1); dfs2(1, -1);
			ans = min(dp[1][1], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[x][1]);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			int ff = min(add, min(add2, add+add2));
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+ff);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
	//	for ( int i=1; i<=n; i++) {
			dfs(1, -1);
			ans = min(dp[1][2], ans);
	//	}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		dfs(1, -1); ans = dp[1][2];
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];
bool vis[N<<1];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs1(int x, int f) {
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f || !is[i]) continue;
		dfs1(v, x);
		vis[i] = vis[i^1] = true;
	}
}

void dfs2(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs2(v, x);
		res += dp[v][0];
		if (!vis[i] && !is[i]) {
			res ++;
		}
	}
	dp[x][0] = dp[x][1] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int jia = 0;
		if (!vis[i]) {
			if (is[i]) {
				jia = 1;
			} else {
				jia = -1;
			}
		}
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+jia);
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+jia);
	}
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		dfs(1, -1); ans = dp[1][2];
		for ( int i=1; i<=n; i++) {
			memset(vis, 0, sizeof(vis));
			dfs1(i, -1); dfs2(1, -1);
			ans = min(dp[1][1], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add);
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
	//	for ( int i=1; i<=n; i++) {
			dfs(1, -1); ans = min(dp[1][2], ans);
	//	}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
bool vis[N<<1];
int dp[N][2];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs1(int x, int f) {
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f || !is[i]) continue;
		dfs1(v, x);
		vis[i] = vis[i^1] = true;
	}
}

void dfs2(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs2(v, x);
		res += dp[v][0];
		if (!vis[i] && !is[i]) {
			res ++;
		}
	}
	dp[x][0] = dp[x][1] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int jia = 0;
		if (!vis[i]) {
			if (is[i]) {
				jia = 1;
			} else {
				jia = -1;
			}
		}
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+jia);
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+jia);
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=1; i<=n; i++) {
			memset(vis, 0, sizeof(vis));
			dfs1(i, -1); dfs2(1, -1);
			ans = min(dp[1][1], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		dfs(1, -1); ans = dp[1][2];
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=1; i<=n; i++) {
			dfs(i, -1); ans = min(dp[i][2], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[x][1]);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			int ff = min(add, min(add2, add+add2));
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+ff);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
	//	for ( int i=1; i<=n; i++) {
			dfs(1, -1); ans = min(dp[1][2], ans);
	//	}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
bool vis[N<<1];
int dp[N][2];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs1(int x, int f) {
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f || !is[i]) continue;
		dfs1(v, x);
		vis[i] = vis[i^1] = true;
	}
}

void dfs2(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs2(v, x);
		res += dp[v][0];
		if (!vis[i] && !is[i]) {
			res ++;
		}
	}
	dp[x][0] = dp[x][1] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int jia = 0;
		if (!vis[i]) {
			if (is[i]) {
				jia = 1;
			} else {
				jia = -1;
			}
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+jia);
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=1; i<=n; i++) {
			memset(vis, 0, sizeof(vis));
			dfs1(i, -1); dfs2(1, -1);
			ans = min(dp[1][1], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
map <pair<int, int>, int> mymap;
int dp[N][2];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs1(int x, int f) {
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f || !is[i]) continue;
		dfs1(v, x);
		mymap[pair<int, int>(min(v, x), max(v, x))] = x;
	}
}

void dfs2(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs2(v, x);
		res += dp[v][0];
		if (mymap.find(pair<int, int>(min(v, x), max(v, x))) == mymap.end() && !is[i]) {
			res ++;
		}
	}
	dp[x][0] = dp[x][1] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int jia = 0;
		if (mymap.find(pair<int, int>(min(v, x), max(v, x))) == mymap.end()) {
			if (is[i]) {
				jia = 1;
			} else {
				jia = -1;
			}
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+jia);
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=1; i<=n; i++) {
			mymap.clear();
			dfs1(i, -1); dfs2(1, -1);
			ans = min(dp[1][1], ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>

using namespace std;
#define N 3010

int head[N], next[N<<1], to[N<<1], is[N<<1], e, m;
int dp[N][3];

void addnote(int u, int v, int i) {
	to[e] = v; is[e] = i;
	next[e] = head[u];
	head[u] = e++;
}

void dfs(int x, int f) {
	int res = 0;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		dfs(v, x); res += dp[v][0];
		if (!is[i]) res ++;
	}
	dp[x][0] = dp[x][1] = dp[x][2] = res;
	for ( int i=head[x]; ~i; i=next[i]) {
		int v = to[i];
		if (v == f) continue;
		int add = 0;
		if (is[i]) {
			add = 1;
		} else {
			add = -1;
		}
		dp[x][1] = min(dp[x][1], dp[v][1]+(res-dp[v][0])+add);
		dp[x][1] = min(dp[x][1], dp[v][0]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][1], dp[x][2]);
		dp[x][2] = min(dp[x][2], dp[v][2]+(res-dp[v][0])+add);
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0]));
		dp[x][2] = min(dp[x][2], dp[v][1]+(res-dp[v][0])+add);
		for ( int j=next[i]; ~j; j=next[j]) {
			int v2 = to[j];
			if (v2 == f) continue;
			int add2 = 0;
			if (is[j]) {
				add2 = 1;
			} else {
				add2 = -1;
			}
			dp[x][2] = min(dp[x][2], dp[v][1]+dp[v2][1]+(res-dp[v][0]-dp[v2][0])+add+add2);
		}
	}
}

int main() {
	while (scanf("%d", &m) != EOF) {
		int n = m;
		memset(head, -1, sizeof(head)); e = 0;
		for ( int i=1; i<m; i++) {
			int u, v;
			scanf("%d%d", &u, &v);
			addnote(u, v, 1);
			addnote(v, u, 0);
		}
		int ans = 10000000;
		for ( int i=0; i<100; i++) {
			int pp = rand() % n + 1;
			dfs(pp, -1); ans = dp[pp][2];
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std ;

const int maxn = 3333 ;
const int INF = 111111111 ;
int n ;
int edge[maxn][maxn] ;
struct{
	int u , v  ;
} e[maxn] ;

int cnt[5];
void dfs( int u , int fa  , int ty )
{
	int v , i ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;
		if( edge[u][i] == - 1 )
		cnt[ty]++ ;
		dfs( i , u , ty ) ;
	}
}

void DFS( int u , int fa , int sum, int ty )
{
	int v , i ;
	cnt[ty]= min( sum , cnt[ty]) ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;
		if( edge[u][i] == -1 )
		DFS( i , u , sum - 1, ty ) ;
		else
		DFS( i , u , sum , ty ) ;
	}
}
int main()
{
	int i , j  ;
	int u , v ;
	scanf("%d" , &n) ;
	memset( edge , 0 , sizeof(edge)) ;
	for( i = 1 ; i < n ; i++ )
	{
		scanf("%d%d" ,&u,&v) ;
		e[i].u = u ;
		e[i].v = v ;
		edge[u][v] = 1 ;
		edge[v][u] = -1 ;
	}
	int ans = INF ;
	for( i = 1 ; i < n ; i ++ )
	{
	//	printf("i== %d\n " , i  ) ;
		cnt[1] =cnt[2] = 0  ;
		dfs(e[i].u , e[i].v , 1 ) ;
		DFS(e[i].u , e[i].v , cnt[1] , 1 ) ;

		dfs(e[i].v , e[i].u , 2 ) ;
		DFS(e[i].v , e[i].u , cnt[2] , 2 ) ;
//		printf("%d %d\n " , e[i].u , e[i].v ) ;
//		printf("cnt[1] = %d cnt[2] = %d \n" , cnt[1] , cnt[2] ) ;
		ans = min( ans , cnt[1] + cnt[2] ) ;
	}
	if( ans == INF ) ans = 0 ;
	printf("%d\n" ,ans ) ;
	return 0 ;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std ;

const int maxn = 3333 ;
const int INF = 111111111 ;
int n ;
int edge[maxn][maxn] ;
struct{
	int u , v  ;
} e[maxn] ;

int cnt[5];
void dfs( int u , int fa  , int ty )
{
	int v , i ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;

		if( edge[u][i] == - 1 )
		{
			cnt[ty]++ ;
			//printf("u==%d i== %d\n" , u , i ) ;
		}
		dfs( i , u , ty ) ;
	}
}

void DFS( int u , int fa , int sum, int ty )
{
	int v , i ;
//	printf("u=%d sum[] = %d\n" , u , sum ) ;
	cnt[ty]= min( sum , cnt[ty]) ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;
		if( edge[u][i] == -1 )
		DFS( i , u , sum - 1, ty ) ;
		else
		DFS( i , u , sum + 1, ty ) ;
	}
}
int main()
{
	int i , j  ;
	int u , v ;
	scanf("%d" , &n) ;
	memset( edge , 0 , sizeof(edge)) ;
	for( i = 1 ; i < n ; i++ )
	{
		scanf("%d%d" ,&u,&v) ;
		e[i].u = u ;
		e[i].v = v ;
		edge[u][v] = 1 ;
		edge[v][u] = -1 ;
	}
	int ans = INF ;
	for( i = 1 ; i < n ; i ++ )
	{
	//	printf("i== %d\n " , i  ) ;
	//	printf("%d %d\n " , e[i].u , e[i].v ) ;
		cnt[1] =cnt[2] = 0  ;
		dfs(e[i].u , e[i].v , 1 ) ;
	//	printf("cnt[1] = %d\n" , cnt[1] ) ;
		DFS(e[i].u , e[i].v , cnt[1] , 1 ) ;

		dfs(e[i].v , e[i].u , 2 ) ;
	//	printf("cnt[2] = %d\n" , cnt[2] ) ;
		DFS(e[i].v , e[i].u , cnt[2] , 2 ) ;

	//	printf("cnt[1] = %d cnt[2] = %d \n" , cnt[1] , cnt[2] ) ;
		ans = min( ans , cnt[1] + cnt[2] ) ;
	}
	if( ans == INF ) ans = 0 ;
	printf("%d\n" ,ans ) ;
	return 0 ;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std ;

const int maxn = 3333 ;
const int INF = 111111111 ;
int n ;
int edge[maxn][maxn] ;
struct{
	int u , v  ;
} e[maxn] ;

int cnt[5];
void dfs( int u , int fa  , int ty )
{
	int v , i ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;

		if( edge[u][i] == - 1 )
		{
			cnt[ty]++ ;
			//printf("u==%d i== %d\n" , u , i ) ;
		}
		dfs( i , u , ty ) ;
	}
}

void DFS( int u , int fa , int sum, int ty )
{
	int v , i ;
	printf("u=%d sum[] = %d\n" , u , sum ) ;
	cnt[ty]= min( sum , cnt[ty]) ;
	for( i = 1 ; i <= n ; i++ )
	{
		if( i == fa ) continue ;
		if( edge[u][i] == 0 ) continue ;
		if( edge[u][i] == -1 )
		DFS( i , u , sum - 1, ty ) ;
		else
		DFS( i , u , sum + 1, ty ) ;
	}
}
int main()
{
	int i , j  ;
	int u , v ;
	scanf("%d" , &n) ;
	memset( edge , 0 , sizeof(edge)) ;
	for( i = 1 ; i < n ; i++ )
	{
		scanf("%d%d" ,&u,&v) ;
		e[i].u = u ;
		e[i].v = v ;
		edge[u][v] = 1 ;
		edge[v][u] = -1 ;
	}
	int ans = INF ;
	for( i = 1 ; i < n ; i ++ )
	{
	//	printf("i== %d\n " , i  ) ;
	//	printf("%d %d\n " , e[i].u , e[i].v ) ;
		cnt[1] =cnt[2] = 0  ;
		dfs(e[i].u , e[i].v , 1 ) ;
	//	printf("cnt[1] = %d\n" , cnt[1] ) ;
		DFS(e[i].u , e[i].v , cnt[1] , 1 ) ;

		dfs(e[i].v , e[i].u , 2 ) ;
	//	printf("cnt[2] = %d\n" , cnt[2] ) ;
		DFS(e[i].v , e[i].u , cnt[2] , 2 ) ;

	//	printf("cnt[1] = %d cnt[2] = %d \n" , cnt[1] , cnt[2] ) ;
		ans = min( ans , cnt[1] + cnt[2] ) ;
	}
	if( ans == INF ) ans = 0 ;
	printf("%d\n" ,ans ) ;
	return 0 ;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std ;

const int maxn = 3333 ;
const int INF = 111111111 ;

int n , tot ;
int head[maxn] ;

struct{
	int u , v , ty , next ;
} edge[maxn<<3] , e[maxn] ;

int cnt[5];

void init()
{
	tot = 0 ;
	memset( head ,-1 , sizeof(head)) ;
}
void add( int u , int v , int ty )
{
	edge[tot].v = v , edge[tot].u = u , edge[tot].ty = ty , edge[tot].next = head[u] , head[u]  = tot ++ ;
}

void dfs( int u , int fa  , int ty )
{
	int v , i ;
	for( i = head[u] ; i!=-1 ; i = edge[i].next )
	{
		v = edge[i].v;
		if( v == fa ) continue ;
		if( edge[i].ty == - 1 )
		cnt[ty]++ ;
		dfs( v , u , ty ) ;
	}
}

void DFS( int u , int fa , int sum, int ty )
{
	int v , i ;
	cnt[ty]= min( sum , cnt[ty]) ;
	for( i = head[u] ; i!=-1; i = edge[i].next )
	{
		v =  edge[i].v ;
		if( v == fa ) continue ;
		if( edge[i].ty == -1 )
		DFS( v , u , sum - 1, ty ) ;
		else
		DFS( v  , u , sum + 1, ty ) ;
	}
}
int main()
{
	int i , j  ;
	int u , v ;
	scanf("%d" , &n) ;
	init() ;
	for( i = 1 ; i < n ; i++ )
	{
		scanf("%d%d" ,&u,&v) ;
		e[i].u = u ;
		e[i].v = v ;
		add( u , v , 1 ) ;
		add( v , u , -1 ) ;
	}
	int ans = INF ;
	for( i = 1 ; i < n ; i ++ )
	{
	//	printf("i== %d\n " , i  ) ;
		cnt[1] =cnt[2] = 0  ;
		dfs(e[i].u , e[i].v , 1 ) ;
		DFS(e[i].u , e[i].v , cnt[1] , 1 ) ;

		dfs(e[i].v , e[i].u , 2 ) ;
		DFS(e[i].v , e[i].u , cnt[2] , 2 ) ;
//		printf("%d %d\n " , e[i].u , e[i].v ) ;
//		printf("cnt[1] = %d cnt[2] = %d \n" , cnt[1] , cnt[2] ) ;
		ans = min( ans , cnt[1] + cnt[2] ) ;
	}
	if( ans == INF ) ans = 0 ;
	printf("%d\n" ,ans ) ;
	return 0 ;
}