World Eater Brothers

You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world.

As you know there are n countries in the world. These countries are connected by n - 1 directed roads. If you don’t consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once.

Each of the brothers wants to establish his reign in some country, then it’s possible for him to control the countries that can be reached from his country using directed roads.

The brothers can rule the world if there exists at most two countries for brothers to choose (and establish their reign in these countries) so that any other country is under control of at least one of them. In order to make this possible they want to change the direction of minimum number of roads. Your task is to calculate this minimum number of roads.

Input

The first line of input contains an integer n (1 ≤ n ≤ 3000). Each of the next n - 1 lines contains two space-separated integers a i and b i (1 ≤ a i, b i ≤ na i ≠ b i) saying there is a road from country a i to country b i.

Consider that countries are numbered from 1 to n. It’s guaranteed that if you don’t consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once.

Output

In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world.

Examples

input

4
2 1
3 1
4 1

output

1

input

5
2 1
2 3
4 3
4 5

output

0

Solution:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

using namespace std;

const int N = 42424;

int ss[N], ff[N], pred[N], last[N], was[N], sz[N];
int n, m, ans, i, st;

void dfs0(int v) {
was[v] = 1;
sz[v] = 0;
int j = last[v];
while (j > 0) {
if (!was[ff[j]]) {
dfs0(ff[j]);
sz[v] += sz[ff[j]];
if (j > m) sz[v]++;
}
j = pred[j];
}
}

void dfs(int v, int w, int s, int mn, int p) {
was[v] = 1;
int cur = w;
int j = last[v];
while (j > 0) {
if (!was[ff[j]]) {
cur += sz[ff[j]];
if (j > m) cur++;
}
j = pred[j];
}
if (cur+s+mn < ans) ans = cur+s+mn;
  j = last[v];
  while (j > 0) {
if (!was[ff[j]]) {
int cc = cur-sz[ff[j]];
if (j > m) cc--;
int newp = p+1, news = s;
if (j <= m) news++;
      int newmn = mn;
      if (newp-2*news < newmn) newmn = newp-2*news;
      dfs(ff[j], cc, news, newmn, newp);
    }
    j = pred[j];
  }
}

int main() {
//  freopen("in", "r", stdin);
//  freopen("out", "w", stdout);
  scanf("%d", &n);
  m = n-1;
  for (i=1;i<=m;i++) {
    scanf("%d %d", ss+i, ff+i);
    ss[i+m] = ff[i];
    ff[i+m] = ss[i];
  }
  for (i=1;i<=n;i++) last[i] = 0;
  for (i=1;i<=m+m;i++) {
    pred[i] = last[ss[i]];
    last[ss[i]] = i;
  }
  ans = 424242;
  for (st=1;st<=n;st++) {
    for (i=1;i<=n;i++) was[i] = 0;
    dfs0(st);
    for (i=1;i<=n;i++) was[i] = 0;
    dfs(st, 0, 0, 0, 0);
  }
  printf("%d\n", ans);
  return 0;
}