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

const int INF = 1111111111 ;
const int maxn = 3333 ;
int tot ,  cnt  ,cc  ;
int head[maxn] ;
struct{
	int u , v , next  ,ty ;
}edge[maxn<<2] ;
void init()
{
	tot = 0 ;
	memset( head , - 1 , sizeof(head)) ;
}
void add( int u , int v , int ty )
{
	edge[tot].v = v , edge[tot].ty = ty , edge[tot].next = head[u] , head[u] = tot ++ ;
}
void dfs( int u , int fa , int x , int y )
{
	int i , v ;
	cc = min(cc , x - y ) ;
	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 , min( x , y ) , y + 1 ) ;
			cnt++ ;
		}
		else
			dfs( v , u , x + 1 ,  y ) ;
	}
}
int main()
{
	int i , j , n , m ;
	int u , v ;
	scanf("%d" ,&n) ;
	init() ;
	for( i = 1 ; i < n ; i++ )
	{
		scanf("%d%d" ,&u,&v) ;
		add( u , v , 1 ) ;
		add( v , u , -1 ) ;
	}
	int ans = INF ;
	for( i = 1 ; i <= n ; i++ )
	{
	//	printf("i==%d\n" , i ) ;
		cnt =  0 ;
		cc = 0 ;
		dfs( i , i , 0 , 0  ) ;
		ans  = min ( ans , cnt + cc ) ;

	}
	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 , 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 ;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dp[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;
 rep(i,m)
 {   int u=edge[i].ff,v=edge[i].ss;
     dfs(u,v);
     dfs(v,u);
     res=min(dp[u][v]+dp[v][u],res);
     //cout<<u<<" "<<v<<" "<<res<<endl;
 }

 rep(i,n)
 {
     int ret=ddfs(i,n);
     res=min(res,ret);
     //cout<<i<<" "<<res<<endl;
 }
 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dp[g[i][j]][i]+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];
           ret1=c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i);
           res=min(res,ret1);
           //cout<<i<<" "<<res<<endl;
       }
 }
 cout<<res<<endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dp[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;
 rep(i,m)
 {   int u=edge[i].ff,v=edge[i].ss;
     dfs(u,v);
     dfs(v,u);
     res=min(dp[u][v]+dp[v][u],res);
     //cout<<u<<" "<<v<<" "<<res<<endl;
 }

 rep(i,n)
 {
     int ret=ddfs(i,n);
     res=min(res,ret);
     //cout<<i<<" "<<res<<endl;
 }
 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dp[g[i][j]][i]+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];
           res=min(res,c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i));
           res=min(res,c[v][i]+tot-c[i][v]-dp[v][i]+ddfs(v,i));
           res=min(res,c[w][i]+tot-c[i][w]-dp[w][i]+ddfs(w,i));
           res=min(res,tot);
           //cout<<i<<" "<<res<<endl;
       }
 }
 cout<<res<<endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dd[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;
 rep(i,m)
 {   int u=edge[i].ff,v=edge[i].ss;
     dfs(u,v);
     dfs(v,u);
     res=min(dp[u][v]+dp[v][u],res);
     //cout<<u<<" "<<v<<" "<<res<<endl;
 }

 rep(i,n)
 {
     int ret=ddfs(i,n);
     res=min(res,ret);
     //cout<<i<<" "<<res<<endl;
 }
 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dp[g[i][j]][i]+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];//cout<<v<<"v "<<w<<endl;
           res=min(res,c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i));
           res=min(res,c[v][i]+tot-c[i][v]-dp[v][i]+ddfs(v,i));
           res=min(res,c[w][i]+tot-c[i][w]-dp[w][i]+ddfs(w,i));
           res=min(res,tot);
           //cout<<i<<" "<<c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i)<<endl;
        // cout<<"here"<<ddfs(w,i)<<" "<<ddfs(v,i)<<endl;
       }
 }
 cout<<res<<endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dp[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;
 rep(i,m)
 {   int u=edge[i].ff,v=edge[i].ss;
     dfs(u,v);
     dfs(v,u);
     res=min(dp[u][v]+dp[v][u],res);
     //cout<<u<<" "<<v<<" "<<res<<endl;
 }

 rep(i,n)
 {
     int ret=ddfs(i,n);
     res=min(res,ret);
     //cout<<i<<" "<<res<<endl;
 }
 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dp[g[i][j]][i]+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];
           ret1=c[w][i]+c[v][i]+tot-c[i][w]-c[i][v];
           res=min(res,ret1);
           //cout<<i<<" "<<res<<endl;
       }
 }
 cout<<res<<endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dd[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;

 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dfs(g[i][j],i)+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];//cout<<v<<"v "<<w<<endl;
           res=min(res,c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i));
           res=min(res,c[v][i]+tot-c[i][v]-dp[v][i]+ddfs(v,i));
           res=min(res,c[w][i]+tot-c[i][w]-dp[w][i]+ddfs(w,i));
           res=min(res,tot);
           //cout<<i<<" "<<c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i)<<endl;
        // cout<<"here"<<ddfs(w,i)<<" "<<ddfs(v,i)<<endl;
       }
 }
 cout<<res<<endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(j,i,n) for(int j=i;j<n;j++)
#define  f first
#define  s second
#define mp make_pair
#define  pb   push_back
#define  fil(a,v) memset(a,v,sizeof(a))
typedef long long ll;
int dp[3005][3005],dd[3005][3005];
vector<vector<int> >g;
int c[3005][3005];
//vector<pair<int,int> >edge;
struct xx{
int ff,ss;}edge[3005];
int dfs(int u,int par)
{
    int &ret=dp[u][par];
    if(ret!=-1)return ret;
    ret=0;
    rep(i,g[u].size())
    {
        int v=g[u][i];
        if(v==par)continue;
        ret+=dfs(v,u)+c[u][v];
    }
    return ret;
}
int ddfs(int u,int par)
{   if(dd[u][par]!=-1)return dd[u][par];
    int ret=0;
    rep(i,g[u].size())if(g[u][i]!=par)
    ret+=dfs(g[u][i],u)+c[u][g[u][i]];
    int ret1=ret;
    rep(i,g[u].size())
    {
        int v=g[u][i];if(v==par)continue;
        ret1=min(ret1,ret-dfs(v,u)-c[u][v]+ddfs(v,u)+c[v][u]);
    }
    return dd[u][par]=ret1;

}
int main()
{
 fil(dp,-1);fil(dd,-1);
 int n;
 cin>>n;
 g.resize(n);
 int m;
 m=n-1;//edge.resize(m);
 rep(i,m)
 {
     int x,y;
     cin>>x>>y;
     x--;y--;
     g[x].pb(y);edge[i].ff=x;edge[i].ss=y;
     g[y].pb(x);
     c[x][y]=0;
     c[y][x]=1;
 }
 int res=(int)1e9;

 rep(i,n)
 {
     int sz=g[i].size();int ret1;int tot=0;
     rep(j,sz)tot+=dfs(g[i][j],i)+c[i][g[i][j]];
     rep(j,sz)
      for(int k=j+1;k<sz;k++)
       {   int v=g[i][j],w=g[i][k];//cout<<v<<"v "<<w<<endl;
           res=min(res,c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i));
           res=min(res,c[v][i]+tot-c[i][v]-dp[v][i]+ddfs(v,i));
           res=min(res,c[w][i]+tot-c[i][w]-dp[w][i]+ddfs(w,i));
           res=min(res,tot);
           //cout<<i<<" "<<c[w][i]+c[v][i]+tot-c[i][w]-c[i][v]-dp[w][i]-dp[v][i]+ddfs(w,i)+ddfs(v,i)<<endl;
        // cout<<"here"<<ddfs(w,i)<<" "<<ddfs(v,i)<<endl;
       }
 }
 if(n<=2)res=0;
 cout<<res<<endl;

}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
int chk[3000];
char dir[3000][3000];
vector<int> adj[3000];

int dfs(int u, int r) {
	int w = 0;
	int r2;
	chk[u] = 1;
	for (int i = 0; i < (int)adj[u].size(); ++i) {
		int v = adj[u][i];
		if (chk[v]) continue;
		if (dir[v][u]) {
			r2 = r + 1;
			w++;
		} else {
			r2 = 0;
		}
		w += dfs(v, r2);
	}
	m = max(m, r);
	return w;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
		dir[a][b] = 1;
	}
	int res = n - 1;
	for (int i = 0; i < n; ++i) {
		fill(chk, chk + n, 0);
		m = 0;
		int j = dfs(i, 0);
		j -= m;
		res = min(res, j);
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
int chk[3000];
char dir[3000][3000];
vector<int> adj[3000];

int dfs(int u, int r) {
	int w = 0;
	int r2;
	chk[u] = 1;
	for (int i = 0; i < (int)adj[u].size(); ++i) {
		int v = adj[u][i];
		if (chk[v]) continue;
		r2 = r + dir[v][u];
		w += dir[v][u];
		w += dfs(v, r2);
	}
	m = max(m, r);
	return w;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
		dir[a][b] = 1;
	}
	int res = n - 1;
	for (int i = 0; i < n; ++i) {
		fill(chk, chk + n, 0);
		m = 0;
		int j = dfs(i, 0);
		j -= m;
		res = min(res, j);
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
int chk[3000];
char dir[3000][3000];
vector<int> adj[3000];

int dfs(int u, int r) {
	int w = 0;
	int r2;
	chk[u] = 1;
	for (int i = 0; i < (int)adj[u].size(); ++i) {
		int v = adj[u][i];
		if (chk[v]) continue;
		if (dir[v][u]) {
			r2 = r + 1;
			w++;
		} else {
			r2 = r - 1;
		}
		r2 = max(r2, 0);
		w += dfs(v, r2);
	}
	m = max(m, r);
	return w;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
		dir[a][b] = 1;
	}
	int res = n - 1;
	for (int i = 0; i < n; ++i) {
		fill(chk, chk + n, 0);
		m = 0;
		int j = dfs(i, 0);
		j -= m;
		res = min(res, j);
	}
	printf("%d\n", res);
	return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m;
int chk[3000];
char dir[3000][3000];
vector<int> adj[3000];

int dfs(int u, int r) {
	int w = 0;
	int r2;
	chk[u] = 1;
	for (int i = 0; i < (int)adj[u].size(); ++i) {
		int v = adj[u][i];
		if (chk[v]) continue;
		if (dir[u][v]) {
			r2 = r + 1;
			w++;
		} else {
			r2 = 0;
		}
		w += dfs(v, r2);
	}
	m = max(m, r);
	return w;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
		dir[b][a] = 1;
	}
	int res = n - 1;
	for (int i = 0; i < n; ++i) {
		fill(chk, chk + n, 0);
		m = 0;
		int j = dfs(i, 0);
		j -= m;
		res = min(res, j);
	}
	printf("%d\n", res);
	return 0;
}


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

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-12;

const int dr[] = {-1, -1, -1, 0, +1, +1, +1, 0};
const int dc[] = {-1, 0, +1, +1, +1, 0, -1, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 9;

#define maxn 3005

int n, total, best;
vector<II> adj[maxn];

void dfs1(int u, int dad) {
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) ++total;
			dfs1(v, u);
		}
	}
}

void dfs2(int u, int dad, int rev) {
	best = min(best, total - rev);
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) dfs2(v, u, rev + 1);
			else dfs2(v, u, 0);
		}
	}
}

int calc(int root) {
	total = 0;
	dfs1(root, -1);

	best = total;
	dfs2(root, -1, 0);

	return best;
}

int main() {
//	freopen("input.txt", "r", stdin);

	scanf("%d", &n);

	int u, v;
	For(i, 1, n - 1) {
		scanf("%d %d", &u, &v);
		adj[u].pb(II(v, 1));
		adj[v].pb(II(u, 0));
	}

	int res = inf;
	For(root, 1, n) res = min(res, calc(root));

	printf("%d\n", res);

	return 0;
}


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

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-12;

const int dr[] = {-1, -1, -1, 0, +1, +1, +1, 0};
const int dc[] = {-1, 0, +1, +1, +1, 0, -1, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 9;

#define maxn 3005

int n;
vector<II> adj[maxn];

int cntrev(int u, int dad) {
	int ret = 0;

	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		if (v == dad) continue;
		ret += adj[u][i].se;
		ret += cntrev(v, u);
	}

	return ret;
}

int cntdiff(int u, int dad, int cnt) {
	if (cnt < 0) cnt = 0;
	int ret = cnt;

	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		if (v == dad) continue;
		ret = max(ret, cntdiff(v, u, cnt + (adj[u][i].se ? 1 : -1)));
	}

	return ret;
}

int calc(int root) {
	return cntrev(root, 0) - cntdiff(root, 0, 0);
}

int main() {
//	freopen("input.txt", "r", stdin);

	scanf("%d", &n);

	int u, v;
	For(i, 1, n - 1) {
		scanf("%d %d", &u, &v);
		adj[u].pb(II(v, 0));
		adj[v].pb(II(u, 1));
	}

	int res = inf;
	For(root, 1, n) res = min(res, calc(root));

	printf("%d\n", res);

	return 0;
}


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

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-12;

const int dr[] = {-1, -1, -1, 0, +1, +1, +1, 0};
const int dc[] = {-1, 0, +1, +1, +1, 0, -1, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 9;

#define maxn 3005

int n, best, total;
vector<II> adj[maxn];

void dfs1(int u, int dad) {
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) ++total;
			dfs1(v, u);
		}
	}
}

void dfs(int u, int dad, int rev) {
	best = min(best, rev);
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) dfs(v, u, rev + 1);
			else dfs(v, u, rev - 1);
		}
	}
}

int calc(int root) {
	total = 0;
	dfs1(root, -1);

	best = inf;
	dfs(root, -1, total);

	if (best < 0) best = 0;
	return best;
}

int main() {
//	freopen("input.txt", "r", stdin);

	scanf("%d", &n);

	int u, v;
	For(i, 1, n - 1) {
		scanf("%d %d", &u, &v);
		adj[u].pb(II(v, 1));
		adj[v].pb(II(u, 0));
	}

	int res = inf;
	For(root, 1, n) res = min(res, calc(root));

	printf("%d\n", res);

	return 0;
}


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

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; }
template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> T sqr(T x) { return x * x; }
template<class T> T cube(T x) { return x * x * x; }
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); }

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-12;

const int dr[] = {-1, -1, -1, 0, +1, +1, +1, 0};
const int dc[] = {-1, 0, +1, +1, +1, 0, -1, -1};

const int inf = (int)1e9 + 5;
const ll linf = (ll)1e16 + 5;
const ll mod = (ll)1e9 + 9;

#define maxn 3005

int n, best, total;
vector<II> adj[maxn];

void dfs1(int u, int dad) {
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) ++total;
			dfs1(v, u);
		}
	}
}

void dfs(int u, int dad, int rev) {
	best = min(best, rev);
	Rep(i, sz(adj[u])) {
		int v = adj[u][i].fi;
		int k = adj[u][i].se;
		if (v != dad) {
			if (k == 0) dfs(v, u, rev + 1);
			else dfs(v, u, rev - 1);
		}
	}
}

int calc(int root) {
	total = 0;
	dfs1(root, -1);

	best = inf;
	dfs(root, -1, total);
	return best;
}

int main() {
//	freopen("input.txt", "r", stdin);

	scanf("%d", &n);

	int u, v;
	For(i, 1, n - 1) {
		scanf("%d %d", &u, &v);
		adj[u].pb(II(v, 1));
		adj[v].pb(II(u, 0));
	}

	int res = inf;
	For(root, 1, n) res = min(res, calc(root));

	printf("%d\n", res);

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include<string>
#define ALL(X) X.begin(),X.end()
#define SIZE(X) ((int)X.size())
#define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++)
#define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(a,b) for(typeof(b.begin()) a = b.begin(); a != b.end(); ++a)
#define FOREACHR(a,b) for(typeof(b.rbegin()) a = b.rbegin(); a != b.rend(); ++a)
#define LBOUND(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})

using namespace std;
typedef long double ldouble;
typedef signed long long slong;

const slong Infinity = 1000000001;
const ldouble Pi = 2.0L*asinl(1.0L);
const ldouble Epsilon = 0.000000001;

template<class T, class U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.first << "," << p.second << ")"; }
template<class T> ostream& operator << (ostream &os, const vector<T>& V) {os << "["; FOREACH(i, V) os << *i << (i-V.begin()+1==SIZE(V)?"":","); os << "]\n"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; FOREACH(i, S) os << *i << (*i==*S.rbegin()?"":","); os << ")\n"; return os; }
template<class T, class U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; FOREACH(i, M) os << *i << (i->first==M.rbegin()->first?"":","); os << "}\n"; return os; }

const int MAXN = 4040;
vector<int> G[MAXN];
int D[MAXN][MAXN];
int N;

void readData()
{
    scanf("%d", &N);
    FOR(i,2,N)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
        D[a][b] = 1;
        D[b][a] = -1;
    }
}

int O[MAXN];
int R[MAXN];

void dfs(int v, int p)
{
    int o = 0;
    int r = 0;
    FOREACH(u,G[v]) if(*u != p)
    {
        dfs(*u,v);
        o = max(o, O[*u] + D[*u][v]);
        r += R[*u] + max(0,D[*u][v]);
    }
    R[v] = r;
    O[v] = o;
}

void solve()
{
    int result = Infinity;
    if(N == 1) result = 0;
    if(N == 2) result = 0;
    FOR(i,1,N) if(SIZE(G[i]) >= 2)
    {
        dfs(i,-1);

        int a = 0;
        int b = 0;
        FOREACH(j,G[i])
        {
            int t = O[*j] + D[*j][i];
            if(t > a)
            {
                b = a;
                a = t;
            }
            else if(t > b)
            {
                b = t;
            }
        }
        //cout << i << " " << R[i] << " " << a << " " << b << endl;

        result = min(result, R[i] - a - b);
    }
    printf("%d ", result);
}

int main()
{

    readData();
    solve();
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include<string>
#define ALL(X) X.begin(),X.end()
#define SIZE(X) ((int)X.size())
#define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++)
#define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(a,b) for(typeof(b.begin()) a = b.begin(); a != b.end(); ++a)
#define FOREACHR(a,b) for(typeof(b.rbegin()) a = b.rbegin(); a != b.rend(); ++a)
#define LBOUND(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})

using namespace std;
typedef long double ldouble;
typedef signed long long slong;

const slong Infinity = 1000000001;
const ldouble Pi = 2.0L*asinl(1.0L);
const ldouble Epsilon = 0.000000001;

template<class T, class U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.first << "," << p.second << ")"; }
template<class T> ostream& operator << (ostream &os, const vector<T>& V) {os << "["; FOREACH(i, V) os << *i << (i-V.begin()+1==SIZE(V)?"":","); os << "]\n"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; FOREACH(i, S) os << *i << (*i==*S.rbegin()?"":","); os << ")\n"; return os; }
template<class T, class U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; FOREACH(i, M) os << *i << (i->first==M.rbegin()->first?"":","); os << "}\n"; return os; }

const int MAXN = 4040;
vector<int> G[MAXN];
int D[MAXN][MAXN];
int N;

void readData()
{
    scanf("%d", &N);
    FOR(i,2,N)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
        D[a][b] = 1;
    }
}

int O[MAXN];
int R[MAXN];

void dfs(int v, int p)
{
    int o = 0;
    int r = 0;
    FOREACH(u,G[v]) if(*u != p)
    {
        dfs(*u,v);
        o = min(o, O[*u] + (1 - D[*u][v]));
        r += R[*u] + D[*u][v];
    }
    R[v] = r;
    O[v] = o;
}

void solve()
{
    int result = Infinity;
    if(N == 1) result = 0;
    if(N == 2) result = 0;
    FOR(i,1,N) if(SIZE(G[i]) >= 2)
    {
        dfs(i,-1);

        int a = 0;
        int b = 0;
        FOREACH(j,G[i])
        {
            int t = O[*j] + D[*j][i];
            if(t > a)
            {
                b = a;
                a = t;
            }
            else if(t > b)
            {
                b = t;
            }
        }
        //cout << i << " " << R[i] << " " << a << " " << b << endl;

        result = min(result, R[i] - a - b);
    }
    printf("%d ", result);
}

int main()
{

    readData();
    solve();
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include<string>
#define ALL(X) X.begin(),X.end()
#define SIZE(X) ((int)X.size())
#define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++)
#define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(a,b) for(typeof(b.begin()) a = b.begin(); a != b.end(); ++a)
#define FOREACHR(a,b) for(typeof(b.rbegin()) a = b.rbegin(); a != b.rend(); ++a)
#define LBOUND(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})

using namespace std;
typedef long double ldouble;
typedef signed long long slong;

const slong Infinity = 1000000001;
const ldouble Pi = 2.0L*asinl(1.0L);
const ldouble Epsilon = 0.000000001;

template<class T, class U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.first << "," << p.second << ")"; }
template<class T> ostream& operator << (ostream &os, const vector<T>& V) {os << "["; FOREACH(i, V) os << *i << (i-V.begin()+1==SIZE(V)?"":","); os << "]\n"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; FOREACH(i, S) os << *i << (*i==*S.rbegin()?"":","); os << ")\n"; return os; }
template<class T, class U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; FOREACH(i, M) os << *i << (i->first==M.rbegin()->first?"":","); os << "}\n"; return os; }

const int MAXN = 4040;
vector<int> G[MAXN];
int D[MAXN][MAXN];
int N;

void readData()
{
    scanf("%d", &N);
    FOR(i,2,N)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
        D[a][b] = 1;
    }
}

int O[MAXN];
int R[MAXN];

void dfs1(int v, int p)
{
    int o = 0;
    int r = 0;
    FOREACH(u,G[v]) if(*u != p)
    {
        dfs1(*u,v);
        o = max(o, O[*u] + D[*u][v]);
        r += R[*u] + D[*u][v];
    }
    R[v] = r;
    O[v] = o;
}

void solve()
{
    int result = Infinity;
    FOR(i,1,N) if(SIZE(G[i]) >= 2)
    {
        dfs1(i,-1);

        int a = 0;
        int b = 0;
        FOREACH(j,G[i])
        {
            int t = O[*j] + D[*j][i];
            if(t > a)
            {
                b = a;
                a = t;
            }
            else if(t > b)
            {
                b = t;
            }
        }

        result = min(result, R[i] - a - b);
    }
    printf("%d ", result);
}

int main()
{

    readData();
    solve();
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include<string>
#define ALL(X) X.begin(),X.end()
#define SIZE(X) ((int)X.size())
#define FOR(I,A,B) for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++)
#define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--)
#define FOREACH(a,b) for(typeof(b.begin()) a = b.begin(); a != b.end(); ++a)
#define FOREACHR(a,b) for(typeof(b.rbegin()) a = b.rbegin(); a != b.rend(); ++a)
#define LBOUND(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})

using namespace std;
typedef long double ldouble;
typedef signed long long slong;

const slong Infinity = 1000000001;
const ldouble Pi = 2.0L*asinl(1.0L);
const ldouble Epsilon = 0.000000001;

template<class T, class U> ostream& operator << (ostream& os, const pair<T,U>&p) {return os << "(" << p.first << "," << p.second << ")"; }
template<class T> ostream& operator << (ostream &os, const vector<T>& V) {os << "["; FOREACH(i, V) os << *i << (i-V.begin()+1==SIZE(V)?"":","); os << "]\n"; return os; }
template<class T> ostream& operator << (ostream &os, const set<T>& S) {os << "("; FOREACH(i, S) os << *i << (*i==*S.rbegin()?"":","); os << ")\n"; return os; }
template<class T, class U> ostream& operator << (ostream &os, const map<T, U>& M){os << "{"; FOREACH(i, M) os << *i << (i->first==M.rbegin()->first?"":","); os << "}\n"; return os; }

const int MAXN = 4040;
vector<int> G[MAXN];
int D[MAXN][MAXN];
int N;

void readData()
{
    scanf("%d", &N);
    FOR(i,2,N)
    {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
        D[a][b] = 1;
    }
}

int O[MAXN];
int R[MAXN];

void dfs1(int v, int p)
{
    int o = 0;
    int r = 0;
    FOREACH(u,G[v]) if(*u != p)
    {
        dfs1(*u,v);
        o = max(o, O[*u] + D[*u][v]);
        r += R[*u] + D[*u][v];
    }
    R[v] = r;
    O[v] = o;
}

void solve()
{
    int result = Infinity;
    if(N == 1) result = 0;
    if(N == 2) result = 0;
    FOR(i,1,N) if(SIZE(G[i]) >= 2)
    {
        dfs1(i,-1);

        int a = 0;
        int b = 0;
        FOREACH(j,G[i])
        {
            int t = O[*j] + D[*j][i];
            if(t > a)
            {
                b = a;
                a = t;
            }
            else if(t > b)
            {
                b = t;
            }
        }

        result = min(result, R[i] - a - b);
    }
    printf("%d ", result);
}

int main()
{

    readData();
    solve();
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define mem(x,val) memset (x, val, sizeof (x))
#define MAXN 3005
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 1e9;
vector<int>e[MAXN],v[MAXN];
int a[MAXN], b[MAXN], d[MAXN], down[MAXN], up[MAXN];
int an[10];

void dfs (int x, int fa, int k) {
    int i, y, size = e[x].size();
    down[x] = 0;  up[x] = 0;
    d[x] = k;
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        dfs (y, x, k);
        down[x] += down[y] + v[x][i];
    }
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        up[y] = up[x] + down[x] - down[y] + 1 - 2 * v[x][i];
    }
}

int main () {
    int n, i, j, x, y, ans;
    scanf ("%d", &n);
    for (i = 1; i < n; i ++) {
        scanf ("%d %d", &x, &y);
        a[i] = x;  b[i] = y;
        e[x].push_back(y);
        v[x].push_back(0);
        e[y].push_back(x);
        v[y].push_back(1);
    }
    ans = inf;
    for (i = 1; i <= n; i ++) {
        dfs (a[i], b[i], 0);
        dfs (b[i], a[i], 1);
        an[0] = an[1] = inf;
        for (j = 1; j <= n; j ++)
            an[d[j]] = min (an[d[j]], down[j] + up[j]);
        ans = min (ans, an[0] + an[1]);
    }
    if (ans == inf)  ans = 0;
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define mem(x,val) memset (x, val, sizeof (x))
#define MAXN 3005
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 1e9;
vector<int>e[MAXN],v[MAXN];
int a[MAXN], b[MAXN], d[MAXN], down[MAXN], up[MAXN];
int an[10];

void dfs (int x, int fa, int k) {
    int i, y, size = e[x].size();
    d[x] = k;
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        dfs (y, x, k);
        down[x] += down[y] + v[x][i];
    }
}

void dfs1 (int x, int fa, int k) {
    int i, y, size = e[x].size();
    d[x] = k;
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        up[y] = up[x] + down[x] - down[y] + 1 - 2 * v[x][i];
        dfs1 (y, x, k);
    }
}

int main () {
    int n, i, j, x, y, ans;
    scanf ("%d", &n);
    for (i = 1; i < n; i ++) {
        scanf ("%d %d", &x, &y);
        a[i] = x;  b[i] = y;
        e[x].push_back(y);
        v[x].push_back(0);
        e[y].push_back(x);
        v[y].push_back(1);
    }
    ans = inf;
    for (i = 1; i < n; i ++) {
        dfs (a[i], b[i], 0);
        dfs (b[i], a[i], 1);
        dfs1 (a[i], b[i], 0);
        dfs1 (b[i], a[i], 1);
        an[0] = an[1] = inf;
        for (j = 1; j <= n; j ++){
            an[d[j]] = min (an[d[j]], down[j] + up[j]);
            up[j] = down[j] = 0;
        }
        ans = min (ans, an[0] + an[1]);
    }
    if (ans == inf)  ans = 0;
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define mem(x,val) memset (x, val, sizeof (x))
#define MAXN 3005
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 1e9;
vector<int>e[MAXN],v[MAXN];
int a[MAXN], b[MAXN], d[MAXN], down[MAXN], up[MAXN];
int an[10];

void dfs (int x, int fa, int k) {
    int i, y, size = e[x].size();
    down[x] = 0;  up[x] = 0;
    d[x] = k;
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        dfs (y, x, k);
        down[x] += down[y] + v[x][i];
    }
    for (i = 0; i < size; i ++) {
        y = e[x][i];
        if (y == fa)  continue;
        up[y] = up[x] + down[x] - down[y] + 1 - 2 * v[x][i];
    }
}

int main () {
    int n, i, j, x, y, ans;
    scanf ("%d", &n);
    for (i = 1; i < n; i ++) {
        scanf ("%d %d", &x, &y);
        a[i] = x;  b[i] = y;
        e[x].push_back(y);
        v[x].push_back(0);
        e[y].push_back(x);
        v[y].push_back(1);
    }
    ans = inf;
    for (i = 1; i <= n; i ++) {
        dfs (a[i], b[i], 0);
        dfs (b[i], a[i], 1);
        an[0] = an[1] = inf;
        for (j = 1; j <= n; j ++)
            an[d[j]] = min (an[d[j]], down[j] + up[j]);
        ans = min (ans, an[0] + an[1]);
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <assert.h>
#include <time.h>

using namespace std;

#pragma comment(linker, "/STACK:100000000")

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define sz(x) (int)(x).size()

int bitcnt(ull a) {
	int cnt = -1;
	for(int i = 0; i < 64; i++) {
		if((a << i) & 1ULL) cnt = i;
	}
	return cnt + 1;
}

ull pow2(int b) {
	ull ans = 1ULL;
	while(b-- > 0) ans *= 2ULL;
	return ans;
}

vector<ull> ans;

int main() {
	ull n;
	cin >> n;
	n *= 2ULL;
	for(int k = 0; k < 62; k++) {
		ull p2 = pow2(k + 1);
		p2 -= 1ULL;
		int bc = bitcnt(p2);
		ull l = 0, r = 1100000000ULL;
		while(l < r) {
			ull av = (l + r) / 2ULL;
			ull val = av * 2 + 1;
			if(max(bitcnt(val*val), bitcnt(val) + bc) > 63) {
				r = av;
				continue;
			}
			ull res = val * val + p2 * val - 2 * val;
			if(res > n) r = av;
			else l = av + 1;
			if(res == n) ans.pb(val << k);
		}
	}
	if(sz(ans) == 0) printf("-1\n");
	else {
		for(int i = 0; i < sz(ans); i++) {
			cout << ans[i] << endl;
		}
	}
	return 0;
}


#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 <cstring>

using namespace std;

const int maxn=200010;

long long jud(long long a,long long b,long long n)
{
	if(n/b>=a)
	{
		n-=a*b;
		n*=2;
		if(n/b>=b-1)
			return b*(b-1)<=n;
		return 0;
	}
	return 0;
}
vector<long long >V;
void BS(long long a,long long n)
{
	long long l=1,r=1000000000000000000ll,mid;

	while(l<r)
	{
		mid=(l+r+1)/2;
		if(!jud(a,mid,n))
			r=mid-1;
		else
			l=mid;
	}
	if(jud(a,l,n)&&a*l+l*(l-1)/2==n&&l%2==1)
		V.push_back((a+1)*l);
}
int main()
{
	int i,j,k;
	long long n,a=1;
	cin>>n;
	for(i=0,a=0;i<=62;i++)
	{
		BS(a,n);
		a=a*2+1;
	}
	if(V.size()==0)
		puts("-1");
	for(i=0;i<V.size();i++)
		cout<<V[i]<<endl;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{
    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long long l = 0, r = 1000000007;
        while(r - l > 1)
        {
            long long cer = (l + r) / 2;
            long long sum = cer * (cer - 1) / 2 + cer * ((1ll << i) - 1);
            if(sum < 0 || sum > n)
                r = cer - 1;
            else
                l = cer;
        }

        for(; l <= r + 3; l++)
        {
            long long sum = l * (l - 1) / 2 + l * ((1ll << i) - 1);
            if(l % 2 == 1 && sum == n)
                v.push_back(l * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
    {
        if(i > 0 && v[i] == v[i-1])
            continue;
        cout << v[i] << endl;
    }
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long long l = 0, r = n / (1ll << i) + 5;
        while(r - l > 1)
        {
            long long cer = (l + r) / 2;
            long double sum = (long double) cer * (cer - 1) / 2 +  (long double) cer * (long double)((1ll << i) - 1);
            if(sum < 0 || sum > (long double) n)
                r = cer - 1;
            else
                l = cer;
        }

        for(; l <= r + 5; l++)
        {
            long long sum = l * (l - 1) / 2 + l * ((1ll << i) - 1);
            if(l % 2 == 1 && sum == n)
                v.push_back(l * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
    {
        if(i > 0 && v[i] == v[i-1])
            continue;
        cout << v[i] << endl;
    }
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{

    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long double d = (double)((1ll << (1 + i)) - 3) * (double)((1ll << (1 + i)) - 3) + 8.0 * n;
        long long sq = sqrt(d) + 0.5;
        if(fabs( (sq - 1) * (sq - 1) - d) < 1e-5)
            sq--;
        if(fabs( (sq + 1) * (sq + 1) - d) < 1e-5)
            sq++;
        if(fabs( sq * sq - d) < 1e-5)
        {

            long long x = (sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
            x = (-sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{
    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long long l = 0, r = n / (1ll << i) + 1;
        while(r - l > 1)
        {
            long long cer = (l + r) / 2;
            long long sum = cer * (cer - 1) / 2 + cer * ((1ll << i) - 1);
            if(sum < 0 || sum > n)
                r = cer - 1;
            else
                l = cer;
        }

        for(; l <= r + 3; l++)
        {
            long long sum = l * (l - 1) / 2 + l * ((1ll << i) - 1);
            if(l % 2 == 1 && sum == n)
                v.push_back(l * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
    {
        if(i > 0 && v[i] == v[i-1])
            continue;
        cout << v[i] << endl;
    }
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long long l = 0, r = 2000000007;
        while(r - l > 1)
        {
            long long cer = (l + r) / 2;
            long double sum = (long double) cer * (cer - 1) / 2 +  (long double) cer * (long double)((1ll << i) - 1);
            if(sum < 0 || sum > (long double) n)
                r = cer - 1;
            else
                l = cer;
        }

        for(; l <= r + 5; l++)
        {
            long long sum = l * (l - 1) / 2 + l * ((1ll << i) - 1);
            if(l % 2 == 1 && sum == n)
                v.push_back(l * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
    {
        if(i > 0 && v[i] == v[i-1])
            continue;
        cout << v[i] << endl;
    }
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{
    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long long l = 0, r = 1000000007;
        while(r - l > 1)
        {
            long long cer = (l + r) / 2;
            long double sum = (long double) cer * (cer - 1) / 2 +  (long double) cer * ((1ll << i) - 1);
            if(sum < 0 || sum + 1e-4 > (long double) n)
                r = cer - 1;
            else
                l = cer;
        }

        for(; l <= r + 3; l++)
        {
            long long sum = l * (l - 1) / 2 + l * ((1ll << i) - 1);
            if(l % 2 == 1 && sum == n)
                v.push_back(l * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
    {
        if(i > 0 && v[i] == v[i-1])
            continue;
        cout << v[i] << endl;
    }
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{

    long long n;
    cin >> n;
    vector<long long> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long double d = (double)((1ll << (1 + i)) - 3) * ((1ll << (1 + i)) - 3) + 8 * n;
        long long sq = sqrt(d) + 0.5;
        if(fabs( sq * sq - d) < 1e-8)
        {

            long long x = (sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
            x = (-sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <string>

using namespace std;

//vector<int> cnt[1000005];

int main()
{

    long long n;
    cin >> n;
    vector<int> v;
    for(int i = 0; (1ll << i) <= n + 1; i++)
    {
        long double d = (double)((1ll << (1 + i)) - 3) * ((1ll << (1 + i)) - 3) + 8 * n;
        long long sq = sqrt(d) + 0.5;
        if(fabs( sq * sq - d) < 1e-8)
        {

            long long x = (sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
            x = (-sq - (1ll << (1 + i)) + 3) / 2;
            if(x >= 0 && x % 2 == 1 && x * ((1ll << i) - 1) + x * (x - 1) / 2 == n)
                v.push_back(x * (1ll << i));
        }
    }
    sort(v.begin(), v.end());
    unique(v.begin(), v.end());

    for(int i = 0; i < v.size(); i++)
        printf("%d\n", v[i]);
    if(v.size() == 0)
        printf("-1\n");

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iostream>
#include <iterator>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <sstream>

using namespace std;

#define REP(i,n) for (int i=0,_n=(n); i < _n; i++)
#define REPD(i,n) for (int i=(n)-1; i >= 0; i--)
#define FOR(i,a,b) for (int _b=(b), i=(a); i <= _b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))

#define CLEAR(x) memset(x,0,sizeof x);
#define CLEARA(x) memset(&x,0,sizeof x);
#define FILL(x,v) memset(x,v,sizeof x);
#define FILLA(x,v) memset(&x,v,sizeof x);

#define VAR(a,b) __typeof(b) a=(b)
#define FOREACH(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)

#define REVERSE(c) reverse(ALL(c))
#define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
#define INF 0x7fffffff
#define X first
#define Y second
#define pb push_back
#define SZ(c) (int)(c).size()
#define MP make_pair
#define eps 1.0e-11
const double pi = acos(-1.0);

typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;

int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);

	LL n;
	cin >> n;
	vector<LL> res;
	for (int i = 0; i < 61; i++)
	{
		LL A = 2;
		LL B = 1 + 2*((1LL << i) - 1);
		LL C = (1LL << i) - 1;
		LL left = 0;
		LL right = sqrt((double)n)*2 + 100;
		while (left <= right)
		{
			LL k = (left+right) / 2;
			double dval = A*(double)k*(double)k + B*(double)k + C;
			if (dval > n*1.1)
			{
				right = k-1;
				continue;
			}
			LL val = A*k*k + B*k + C;
			if (val == n)
			{
				res.pb((2*k+1)<<i);
				break;
			}
			if (val > n)
				right = k - 1;
			else
				left = k + 1;
		}
	}
	UNIQUE(res);
	REP(i,SZ(res))
	{
		cout << res[i] << "\n";
	}
	if (res.empty()) cout << "-1\n";
	return 0;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

                        while(r-l>1)
                                   {
                                        t=(r+l)/2;

                                        if( (step + t -2)*( 2*t -1) >n ) r=t;
                                        else l=t;

                                   }

               if( (step + r -2) == n/( 2*r -1) && n%( 2*r -1)==0 ) ans.pb( step*(2*r-1) );
               else
               {

                      if( (step + l -2) == n/( 2*l -1) && n%( 2*l -1)==0 ) ans.pb( step*(2*l-1) );
               }

   while(step<=n+1)
   {
       step*=2;

                r=(n/step-1)+5;
               l=1;

                                 while(r-l>1)
                                   {
                                        t=(r+l)/2;

                                        if( (step + t -2)*( 2*t -1) >n ) r=t;
                                        else l=t;

                                   }

               if( (step + r -2) == n/( 2*r -1) && n%( 2*r -1)==0 ) ans.pb( step*(2*r-1) );
               else
               {

                      if( (step + l -2) == n/( 2*l -1) && n%( 2*l -1)==0 ) ans.pb( step*(2*l-1) );
               }

   }

        Sort(ans);

        if(ans.size())
        for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

        else cout<<-1<<endl;

        //pause;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

   while(r-l>1)
   {
		t=(r+l)/2;

		if( (step + t -2)*( 2*t -1) >n ) r=t;
		else
		{
			if( (step + t -2)*( 2*t -1) <n ) l=t;
			else
			{
				//ans.pb( step*(2*t-1) );
				l=t;
				r=t;
			}
		}
   }

   if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
   else
   {

		  if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
   }

   while(step<=n+1)
   {
	   step*=2;

				r=(n/step-1)+5;
			   l=1;

								 while(r-l>1)
								   {
										t=(r+l)/2;

										if( (step + t -2)*( 2*t -1) >n ) r=t;
										else
										{
											if( (step + t -2)*( 2*t -1) <n ) l=t;
											else
											{
												//ans.pb( step*(2*t-1) );
												l=t;
												r=t;
											}
										}
								   }

			   if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
			   else
			   {

					  if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
			   }

   }

		Sort(ans);

		if(ans.size())
		for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

		else cout<<-1<<endl;

		//pause;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

   while(r-l>1)
   {
        t=(r+l)/2;

        if( (step + t -2)*( 2*t -1) >n ) r=t;
        else l=t;
   }

   if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
   else
   {
        if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
   }

   for(int i=1; i<61; i++)
   {
       step*=2;

                r=(n/step)+1;
               l=1;

               while(r-l>1)
               {
                    t=(r+l)/2;

                    if( (step + t -2)*( 2*t -1) >n ) r=t;
                    else l=t;
               }

               if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
               else
               {
                    if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
               }

   }

        Sort(ans);

        if(ans.size())
        for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

        else cout<<-1<<endl;

        //pause;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

   while(r-l>1)
   {
        t=(r+l)/2;

        if( (step + t -2)*( 2*t -1) >n ) r=t;
        else l=t;
   }

   if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
   else
   {

          if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
   }

   while(step<=n+1)
   {
       step*=2;

                r=(n/step-1)+2;
               l=1;

               while(r-l>1)
               {
                    t=(r+l)/2;

                    if( (step + t -2)*( 2*t -1) >n ) r=t;
                    else l=t;
               }

               if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
               else
               {

                      if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
               }

   }

        Sort(ans);

        if(ans.size())
        for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

        else cout<<-1<<endl;

        //pause;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

   while(r-l>1)
   {
        t=(r+l)/2;

        if( (step + t -2)*( 2*t -1) >n ) r=t;
        else l=t;
   }

   if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
   else
   {
        if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
   }

   while(step<=n+1)
   {
       step*=2;

                r=(n/step-1)+2;
               l=1;

               while(r-l>1)
               {
                    t=(r+l)/2;

                    if( (step + t -2)*( 2*t -1) >n ) r=t;
                    else l=t;
               }

               if( (step + l -2)*( 2*l -1) == n ) ans.pb( step*(2*l-1) );
               else
               {
                    if( (step + r -2)*( 2*r -1) == n ) ans.pb( step*(2*r-1) );
               }

   }

        Sort(ans);

        if(ans.size())
        for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

        else cout<<-1<<endl;

        //pause;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define pb push_back
# define Graph vector < vector <bool> >

bool bm( ll a, ll b, ll c) // is a*b>c
{
    if(a<=c/b) return false;

    return true;
}

int main()
{
   ll n, step=1, t, l, r;
   cin>>n;

   vector <ll> ans(0);

   r=(n/2)+2;
   l=1;

                        while(r-l>1)
                                   {
                                        t=(r+l)/2;

                                        if( bm((step + t -2) ,( 2*t -1) ,n) ) r=t;
                                        else l=t;

                                   }

               if( (step + r -2) == n/( 2*r -1) && n%( 2*r -1)==0 ) ans.pb( step*(2*r-1) );
               else
               {

                      if( (step + l -2) == n/( 2*l -1) && n%( 2*l -1)==0 ) ans.pb( step*(2*l-1) );
               }

   while(step<=n+1)
   {
       step*=2;

                r=(n/step-1)+5;
               l=1;

                                 while(r-l>1)
                                   {
                                        t=(r+l)/2;

                                        if( bm((step + t -2) ,( 2*t -1) ,n) ) r=t;
                                        else l=t;

                                   }

               if( (step + r -2) == n/( 2*r -1) && n%( 2*r -1)==0 ) ans.pb( step*(2*r-1) );
               else
               {

                      if( (step + l -2) == n/( 2*l -1) && n%( 2*l -1)==0 ) ans.pb( step*(2*l-1) );
               }

   }

        Sort(ans);

        if(ans.size())
        for(int i=0; i<ans.size(); i++) cout<<ans[i]<<endl;

        else cout<<-1<<endl;

        //pause;
}


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <hash_set>
using namespace std;
const int MIN = 0;

unsigned long long Check(long long n, int k) {
  unsigned long long s = ((1ULL << k) - 1) * 2;
  unsigned long long a = 1, b = (1ULL << 31) - 1;
  while (a < b - 1) {
    unsigned long long c = (a + b) / 2;
    unsigned long long t = 2 * n + c;
    long long larger = false;

    if (c * c > t) {
      larger = true;
    } else {
      t -= c * c;
      larger = t / c < s;
    }
    if (larger) {
      b = c;
    } else {
      a = c;
    }
  }
  unsigned long long t = 2 * n + a;
  if (a * a > t) return 0;
  t -= a * a;
  if (t % a == 0 && t / a == s && a % 2 == 1) return a * (1ULL << k);
  return 0;
}

void main() {
  long long n;
  scanf("%I64d", &n);
  vector<unsigned long long> ans;
  for (int i = 0; ; ++i) {
    if ((1ULL << i) > n * 2 + 100) break;
    unsigned long long t = Check(n, i);
    if (t != 0) {
      ans.push_back(t);
    }
  }
  if (ans.empty()) printf("-1\n");
  else {
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); ++i) {
      printf("%I64d\n", ans[i]);
    }
  }
  return;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>

using namespace std;

long long Sqrt(unsigned long long n) {
    long long l=0,r=3000000000;
    while(r-l>1) {
        long long m=(l+r)>>1;
        if (1ull*m*m<=n) l=m;
        else r=m;
    }
    return l;
}

int get(int n) {
    int ret=0;
    while(n%2==0) ret+=n/2,n/=2;
    return ret+n*(n-1)/2;
}

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    long long n;
    cin >> n;
    set <long long> f;
    for(int d=0;d<30;++d) {
        long long a=1,b=(1LL<<(d+1))-3,c=-2*n;
        unsigned long long D=b*b-4*a*c;
        if (D>=0) {
            long long s=Sqrt(D);
            if (s*s==D) {
                long long x=(-b+s)/2/a;
                if ((x&1)&&(s-b)%(2*a)==0) {
                    x<<=d;
                    f.insert(x);
                }
            }
        }
    }
    for(int i=1;i<=1000000;i+=2) {
        long long x=i,ret=x*(x-1)/2;
        while(ret<n) ret+=x,x<<=1;
        if (ret==n) f.insert(x);
    }
    for(set <long long>::iterator it=f.begin();it!=f.end();++it) {
//      if (get(*it)!=n) cout << "fail\n";
        cout << *it << endl;
    }
    if (f.size()==0) puts("-1");
/*
    for(int i=1;i<=100;++i)
        cout << i << "  " << get(i) << endl;
*/  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>

using namespace std;

long long Sqrt(long long n) {
    long long l=0,r=3000000000;
    while(r-l>1) {
        long long m=(l+r)>>1;
        if (m*m<=n) l=m;
        else r=m;
    }
    return l;
}

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    long long n;
    cin >> n;
    set <long long> f;
    for(int d=0;d<64;++d) {
        long long a=1,b=d*d+d-1,c=-2*n;
        long long D=b*b-4*a*c;
        long long s=Sqrt(D);
        if (s*s==D) {
            long long x=(-b+s)/2/a;
            if ((x&1)&&(s-b)%(2*a)==0) {
                x<<=d;
                f.insert(x);
            }
        }
    }
    for(set <long long>::iterator it=f.begin();it!=f.end();++it)
        cout << *it << endl;
    if (f.size()==0) puts("-1");
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>

using namespace std;

long long Sqrt(long long n) {
    long long l=0,r=3000000000;
    while(r-l>1) {
        long long m=(l+r)>>1;
        if (m*m<=n) l=m;
        else r=m;
    }
    return l;
}

int main() {
//  freopen("input.txt","r",stdin);
//  freopen("output.txt","w",stdout);
    long long n;
    cin >> n;
    set <long long> f;
    for(int d=0;d<62;++d) {
        long long a=1,b=(1LL<<(d+1))-3,c=-2*n;
        long long D=b*b-4*a*c;
        long long s=Sqrt(D);
        if (s*s==D) {
            long long x=(-b+s)/2/a;
            if ((x&1)&&(s-b)%(2*a)==0) {
                x<<=d;
                f.insert(x);
            }
        }
    }
    for(set <long long>::iterator it=f.begin();it!=f.end();++it)
        cout << *it << endl;
    if (f.size()==0) puts("-1");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
typedef long long LL;
typedef double dou;
const int Mod = 100000007;
const double eps = 1e-8;
const LL inf = (1 << 63) - 1;
using namespace std;

LL bs(LL s, LL e, LL i, LL n)
{
        dou st = (dou) s, en = (dou) e, mid = (dou) ((s + e) / 2), nn = (dou) n, ii = (dou) i;
        if (e - s == 1)
        {
                if (st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn < eps && st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn > -eps)
                        return s;
                else
                        return 0;
        }
        else
        {
                if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn > eps)
                        return bs(s, (s + e) / 2, i, n);
                else if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn < -eps)
                        return bs((s + e) / 2, e, i, n);
                else
                        return (s + e) / 2;
        }

        return 0;
}

int main()
{
        LL n, i, j, k, ans[10000] = {0}, num = 0, re;

        cin >> n;
        for (i = 0, j = 0; i < 63; ++i)
        {
                re = bs(1, n + 1, i, n);
                if (re & 1)
                        ans[j++] = re * (1 << i);
        }
        if (j)
        {
                sort(ans, ans + j);
                for (i = 0; i < j; ++i)
                        cout << ans[i] << endl;
        }
        else
                cout << -1 << endl;

        return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#define dou double
#define mem(a) memset(a, 0, sizeof(a))
#define memm(a) memset(a, -1, sizeof(a))
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
#define FI first
#define SE second
#define RI(a) scanf("%d", &(a))
#define RII(a, b) scanf("%d%d", &(a), &(b))
#define RIII(a, b, c) scanf("%d%d%d", &(a), &(b), &(c))
#define RL(a) scanf("%I64d", &(a))
#define RLL(a, b) scanf("%I64d%I64d", &(a), &(b))
#define RLLL(a, b, c) scanf("%I64d%I64d%I64d", &(a), &(b), &(c))
#define PI(r) printf("%d\n", (r))
#define PL(r) printf("%I64d\n", (r))
#define RS(s) scanf("%s", (s))
#define SL(a) strlen(a)
#define REP(i, n) for (int i = 0; i < (int) (n); ++i)
#define REPP(i, a, b) for (int i = (int) (a); i <= (int) (b); ++i)
#define FOR(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
//Segment tree
#define MID ((l + r) >> 1)
#define L (x << 1)
#define R ((x << 1) | 1)
#define LC L, l, MID
#define RC R, MID + 1, r
#define LB(x) ((x) & (-(x)))
#pragma warning (disable : 4996)
//#pragma comment(linker, "/STACK:102400000,102400000")
#define EPS 1e-8
#define INF 2000000000
#define LIM (1ll << 60)
#define MOD 1000000007
#define N 111111

using namespace std;

LL n, m;
LL ans[111];

LL cal(LL x, int y) {
	dou re = 0;
	re = (pow(2.0, 1.0 * y) - 1.0) * x + 1.0 * x * (x - 1) / 2.0;
	if (re > 1e18) return (LL) (1e18 + 10);
	else return ((1ll << y) - 1) * x + x * (x - 1) / 2ll;
}

int main(){
	int x, y, z, ca = 1;
	//freopen("D:/Contest/in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	//ios :: sync_with_stdio(false);
	RL(n);
	int tot = 0;
	REP(i, 60) {
		LL l = 1, r = 1ll << (60 - i);
		while (l + 1ll < r) {
			LL mid = (l + r) >> 1ll;
			if (cal(mid, i) > n) r = mid;
			else l = mid;
		}
		LL tem = ((1ll << i) - 1) * l + l * (l - 1) / 2ll;
		if (tem == n && (l & 1ll)) ans[tot++] = (1ll << i) * l;
		//cout << i << ' ' << l << ' ' << tem << ' ' << tot << endl;
	}
	sort(ans, ans + tot);
	REP(i, tot) PL(ans[i]);
	if (tot == 0) PI(-1);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#pragma warning (disable : 4996)
typedef long long LL;
const int Mod = 100000007;
const double eps = 1e-8;
const LL inf = 1 << 63 - 1;
using namespace std;

LL solve(LL min, LL max, LL i, LL n)
{
        LL mid;
        if (min == max - 1)
        {
                if (min * (min - 1) / 2 + ((1 << i) - 1) * min == n)
                {
                        if (min & 1)
                                return min;
                        else
                                return 0;
                }
                else
                        return 0;
        }
        else
        {
                mid = (min + max) / 2;
                if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid > n)
                        return solve(min, mid, i, n);
                else if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid < n)
                        return solve(mid, max, i, n);
                else if (mid & 1)
                        return mid;
        }
}

int main()
{
        LL n, i, j, k, max = 0, re;
        priority_queue<LL> a;
        stack<LL> b;

        cin >> n;
        max = (int) sqrt(2.0 * n) + 1;
        for (i = 0; (1 << i) - 1 <= n && i <= 62; ++i)
        {
                re = solve(1, max + 1, i, n);
                if (re > 0)
                        a.push(re * (1 << i));
        }
        if (a.size())
        {
                while (!a.empty())
                {
                        b.push(a.top());
                        a.pop();
                }
                while (!b.empty())
                {
                        cout << b.top() << endl;
                        b.pop();
                }
        }
        else
                cout << -1 << endl;
        return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
typedef long long LL;
typedef double dou;
const int Mod = 100000007;
const double eps = 1e-2;
const LL inf = (1 << 63) - 1;
using namespace std;

LL bs(LL s, LL e, LL i, LL n)
{
        dou st = (dou) s, en = (dou) e, mid = (dou) ((s + e) / 2), nn = (dou) n, ii = (dou) i;
        if (e - s == 1)
        {
                if (st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn < eps && st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn > -eps)
                        return s;
                else
                        return 0;
        }
        else
        {
                if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn > eps)
                        return bs(s, (s + e) / 2, i, n);
                else if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn < -eps)
                        return bs((s + e) / 2, e, i, n);
                else
                        return (s + e) / 2;
        }

        return 0;
}

int tru(LL ans, LL n)
{
        LL k = 0;

        while ((ans & 1) == 0)
        {
                k++;
                ans >>= 1;
        }
        if ((ans * ans - ans) / 2 + ((1 << k) - 1) * ans == n)
                return 1;
        else
                return 0;
}

int main()
{
        LL n, i, j, k, ans[10000] = {0}, num = 0, re, s = 0;

        cin >> n;
        for (i = 0, j = 0; i < 63; ++i)
        {
                re = bs(1, n + 1, i, n);
                if (re & 1)
                        ans[j++] = re * (1 << i);
        }
        if (j)
        {
                sort(ans, ans + j);
                for (i = 0; i < j; ++i)
                {
                        if (tru(ans[i], n))
                        {
                                cout << ans[i] << endl;
                                s = 1;
                        }
                }
                if (!s)
                        cout << -1 << endl;
        }
        else
                cout << -1 << endl;

        return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
typedef long long LL;
const int Mod = 100000007;
const double eps = 1e-8;
const LL inf = 1 << 63 - 1;
using namespace std;

LL solve(LL min, LL max, LL i, LL n)
{
		LL mid;
		if (min == max - 1)
		{
				if (min * (min - 1) / 2 + ((1 << i) - 1) * min == n)
				{
						if (min & 1)
								return min;
						else
								return 0;
				}
				else
						return 0;
		}
		else
		{
				mid = (min + max) / 2;
				if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid > n)
						return solve(min, mid, i, n);
				else if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid < n)
						return solve(mid, max, i, n);
				else if (mid & 1)
						return mid;
		}
}

int main()
{
		LL n, i, j, k, max = 0, re;
		priority_queue<LL> a;
		stack<LL> b;

		cin >> n;
		for (i = 0; (1 << i) - 1 <= n && i <= 62; ++i)
		{
				max = (LL) sqrt(2.0 * (n - (1 << i) + 1)) + 1;
				re = solve(1, max + 1, i, n);
				if (re > 0)
						a.push(re * (1 << i));
		}
		if (a.size())
		{
				while (!a.empty())
				{
						b.push(a.top());
						a.pop();
				}
				while (!b.empty())
				{
						cout << b.top() << endl;
						b.pop();
				}
		}
		else
				cout << -1 << endl;
		return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#define dou double
#define mem(a) memset(a, 0, sizeof(a))
#define memm(a) memset(a, -1, sizeof(a))
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
#define FI first
#define SE second
#define RI(a) scanf("%d", &(a))
#define RII(a, b) scanf("%d%d", &(a), &(b))
#define RIII(a, b, c) scanf("%d%d%d", &(a), &(b), &(c))
#define RL(a) scanf("%I64d", &(a))
#define RLL(a, b) scanf("%I64d%I64d", &(a), &(b))
#define RLLL(a, b, c) scanf("%I64d%I64d%I64d", &(a), &(b), &(c))
#define PI(r) printf("%d\n", (r))
#define PL(r) printf("%I64d\n", (r))
#define RS(s) scanf("%s", (s))
#define SL(a) strlen(a)
#define REP(i, n) for (int i = 0; i < (int) (n); ++i)
#define REPP(i, a, b) for (int i = (int) (a); i <= (int) (b); ++i)
#define FOR(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
//Segment tree
#define MID ((l + r) >> 1)
#define L (x << 1)
#define R ((x << 1) | 1)
#define LC L, l, MID
#define RC R, MID + 1, r
#define LB(x) ((x) & (-(x)))
#pragma warning (disable : 4996)
//#pragma comment(linker, "/STACK:102400000,102400000")
#define EPS 1e-8
#define INF 2000000000
#define LIM (1ll << 60)
#define MOD 1000000007
#define N 111111

using namespace std;

LL n, m;
LL ans[111];

LL cal(int x, int y) {
	dou re = 0;
	re = (pow(2.0, y) - 1.0) * x + 1.0 * x * (x - 1) / 2.0;
	if (re > 1e18) return (LL) (1e18 + 10.0);
	else return (LL) re;
}

int main(){
	int x, y, z, ca = 1;
	//freopen("D:/Contest/in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	//ios :: sync_with_stdio(false);
	RL(n);
	int tot = 0;
	REP(i, 60) {
		LL l = 1, r = 1 << (60 - i);
		while (l + 1 < r) {
			LL mid = (l + r) >> 1;
			if (cal(mid, i) > n) r = mid;
			else l = mid;
		}
		LL tem = cal(l, i);
		if (1.0 * abs(tem - n) / (1.0 * n) < EPS && (l & 1)) ans[tot++] = (1ll << i) * l;
		//cout << i << ' ' << l << ' ' << tem << ' ' << LB(l) << ' ' << tot << endl;
	}
	sort(ans, ans + tot);
	REP(i, tot) PL(ans[i]);
	if (tot == 0) PI(-1);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
typedef long long LL;
const int Mod = 100000007;
const double eps = 1e-8;
const LL inf = 1 << 63 - 1;
using namespace std;

LL solve(LL min, LL max, LL i, LL n)
{
		LL mid;
		if (min == max - 1)
		{
				if (min * (min - 1) / 2 + ((1 << i) - 1) * min == n)
				{
						if (min & 1)
								return min;
						else
								return 0;
				}
				else
						return 0;
		}
		else
		{
				mid = (min + max) / 2;
				if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid > n)
						return solve(min, mid, i, n);
				else if (mid * (mid - 1) / 2 + ((1 << i) - 1) * mid < n)
						return solve(mid, max, i, n);
				else if (mid & 1)
						return mid;
		}
}

int main()
{
		LL n, i, j, k, max = 0, re;
		priority_queue<LL> a;
		stack<LL> b;

		cin >> n;
		for (i = 0; (1 << i) - 1 <= n && i <= 62; ++i)
		{
				max = (LL) sqrt(2 * (n - (1 << i) + 1)) + 1;
				re = solve(1, max + 1, i, n);
				if (re > 0)
						a.push(re * (1 << i));
		}
		if (a.size())
		{
				while (!a.empty())
				{
						b.push(a.top());
						a.pop();
				}
				while (!b.empty())
				{
						cout << b.top() << endl;
						b.pop();
				}
		}
		else
				cout << -1 << endl;
		return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#define dou double
#define mem(a) memset(a, 0, sizeof(a))
#define memm(a) memset(a, -1, sizeof(a))
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
#define FI first
#define SE second
#define RI(a) scanf("%d", &(a))
#define RII(a, b) scanf("%d%d", &(a), &(b))
#define RIII(a, b, c) scanf("%d%d%d", &(a), &(b), &(c))
#define RL(a) scanf("%I64d", &(a))
#define RLL(a, b) scanf("%I64d%I64d", &(a), &(b))
#define RLLL(a, b, c) scanf("%I64d%I64d%I64d", &(a), &(b), &(c))
#define PI(r) printf("%d\n", (r))
#define PL(r) printf("%I64d\n", (r))
#define RS(s) scanf("%s", (s))
#define SL(a) strlen(a)
#define REP(i, n) for (int i = 0; i < (int) (n); ++i)
#define REPP(i, a, b) for (int i = (int) (a); i <= (int) (b); ++i)
#define FOR(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
//Segment tree
#define MID ((l + r) >> 1)
#define L (x << 1)
#define R ((x << 1) | 1)
#define LC L, l, MID
#define RC R, MID + 1, r
#define LB(x) ((x) & (-(x)))
#pragma warning (disable : 4996)
//#pragma comment(linker, "/STACK:102400000,102400000")
#define EPS 1e-8
#define INF 2000000000
#define LIM (1ll << 60)
#define MOD 1000000007
#define N 111111

using namespace std;

LL n, m;
LL ans[111];

LL cal(int x, int y) {
	dou re = 0;
	re = (pow(2.0, y) - 1.0) * x + 1.0 * x * (x - 1) / 2.0;
	if (re > 1e18) return (LL) (1e18 + 10.0);
	else return (LL) re;
}

int main(){
	int x, y, z, ca = 1;
	//freopen("D:/Contest/in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	//ios :: sync_with_stdio(false);
	RL(n);
	int tot = 0;
	REP(i, 60) {
		LL l = 1, r = 1 << (60 - i);
		while (l + 1 < r) {
			LL mid = (l + r) >> 1;
			if (cal(mid, i) > n) r = mid;
			else l = mid;
		}
		LL tem = cal(l, i);
		if (1.0 * abs(tem - n) / (1.0 * n) < EPS && (l & 1)) ans[tot++] = (1ll << i) * l;
		//cout << i << ' ' << l << ' ' << tem << ' ' << LB(l) << ' ' << tot << endl;
	}
	sort(ans, ans + tot);
	REP(i, tot) PI(ans[i]);
	if (tot == 0) PI(-1);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#define LL long long
typedef double dou;
const int Mod = 100000007;
const double eps = 1e-8;
const LL inf = ((LL) 1 << 63) - 1;
using namespace std;

LL bs(LL s, LL e, LL i, LL n)
{
        dou st = (dou) s, en = (dou) e, mid = (dou) ((s + e) / 2), nn = (dou) n, ii = (dou) i;
        if (e - s == 1)
        {
                if (st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn < eps && st * (st - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * st - nn > -eps)
                        return s;
                else
                        return 0;
        }
        else
        {
                if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn > eps)
                        return bs(s, (s + e) / 2, i, n);
                else if (mid * (mid - 1.0) / 2.0 + (pow(2.0, ii) - 1.0) * mid - nn < -eps)
                        return bs((s + e) / 2, e, i, n);
                else
                        return (s + e) / 2;
        }

        return 0;
}

int tru(LL ans, LL n)
{
        LL k = 0;

        while ((ans & 1) == 0)
        {
                k++;
                ans >>= 1;
        }
        if (((ans * ans - ans) >> 1) + (((LL)1 << k) - 1) * ans == n)
                return 1;
        else
                return 0;
}

int main()
{
        LL n, i, j, k, ans[100] = {0}, num = 0, re, s = 0;

        cin >> n;
        for (i = 0, j = 0; i < 63; ++i)
        {
                re = bs(1, n + 1, i, n);
                if (re & 1)
                        ans[j++] = re * ((LL)1 << i);
        }
        if (j)
        {
                sort(ans, ans + j);
                for (i = 0; i < j; ++i)
                {
                        if (tru(ans[i], n))
                        {
                                cout << ans[i] << endl;
                                s = 1;
                        }
                }
                if (!s)
                        cout << -1 << endl;
        }
        else
                cout << -1 << endl;

        return 0;
}


#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "b";

int64 n;
vector<int64> maybe;

int calc(int64 m)
{
	int64 res = 0;
	while (m % 2 == 0)
	{
		res += m / 2;
		m /= 2;
	}
	res += m * (m - 1) / 2;
	return res == n;
}

void solve(int64 ept)
{
	long double a, b, c;
	a = 0.5;
	b = ept - 1.5;
	c = -n;
//	cerr << a << " "<< b << " " << c << endl;
	if (b * b < 4 * a * c - 1e-3) return;
	long double d = sqrtl(b * b - 4 * a * c);
	long double x1 = ept * (-b - d) / 2 / a;
	long double x2 = ept * (-b + d) / 2 / a;
	maybe.pb(x1 - 1);
	maybe.pb(x1);
	maybe.pb(x1+ 1);
	maybe.pb(x2 - 1);
	maybe.pb(x2);
	maybe.pb(x2+ 1);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen((name + ".in").data(), "r", stdin);
	freopen((name + ".out").data(), "w", stdout);
#endif

	cin >> n;
	maybe.clear();
	forn(i, 60)
		solve(1LL << i);
	sort(all(maybe));
	maybe.resize(unique(all(maybe)) - maybe.begin());
	int ans = 0;
	forn(i, maybe.size())
		if (maybe[i] > 0 && calc(maybe[i]))
		{
			cout << maybe[i] << endl;
			ans++;
		}
	if (ans == 0) cout << -1 << endl;

	return 0;
}


#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "a";

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	freopen((name + ".in").data(), "r", stdin);
	freopen((name + ".out").data(), "w", stdout);

	return 0;
}


#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "a";

const int NMAX = ;

int main()
{
#ifndef ONLINE_JUDGE
	freopen((name + ".in").data(), "r", stdin);
	freopen((name + ".out").data(), "w", stdout);
#endif

	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>       //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>      //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

int BS(LL n, LL l)
{
    int p = 1, k = min((LL)2*sqrt(1.0*n), (l ? n/l : (LL)2*sqrt(1.0*n))) + 1;
    while(p+1 < k)
    {
        int s = (p+(LL)k)/2;
        if(s*(LL)(s-1)/2 + s*l <= n) p = s;
        else k = s;
    }
    return p;
}
LL go(LL n, LL l)
{
    int x = BS(n, l);
    if(x%2 && x*(LL)(x-1)/2 + x*l == n) return x*(l+1);
    return -1;
}//go

int main()
{
    LL n;
    scanf("%lld", &n);
    vector < LL > v;
    REP(i,60)
    {
        LL l = (1LL<<i)-1;
        if(l > n) break;
        LL pom = go(n,l);
        if(pom != -1) v.PB(pom);
    }
    if(v.empty()) printf("-1\n");
    else
    {
        sort(v.begin(),v.end());
        REP(i,(int)v.size()) printf("%lld\n", v[i]);
    }
    return 0;
}