#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
    int n,k=0,d=-1;
   /* long long int iniprime[2000000001],primes[2000000001];
    memset(iniprime,0,sizeof(iniprime));
    iniprime[0]=1;
    iniprime[1]=1;
    for(i=2;i<=10000000;i++)
    {
        if(iniprime[i]==0)
        {
            for(j=2;i*j<=10000000;j++)
                iniprime[i*j]=1;
            primes[m]=i;
            m++;
        }
    } */
    cin>>n;
    long long int a[n],b[n],ct=0,big=0,x,i,j;
    for(i=0;i<=n-1;i++)
    {
        cin>>a[i]>>b[i];
        x=(a[i]>b[i])?a[i]:b[i];
        if(x>big)
            big=x;
        if(a[i]%2==0||b[i]%2==0)
            ct++;
    }
    if(ct==n)
    {
        k++;
        cout<<"2\n";
    }
    else
    {
        ct=0;
        for(i=3;i<=big;i=i+2)
        {
            for(j=0;j<=n-1;j++)
            {
                if(a[j]%i==0||b[j]%i==0)
                    ct++;
                else
                    break;
            }
            if(ct==n)
            {
                k++;
                cout<<i<<endl;
                break;
            }
            ct=0;
        }
        if(k==0)
            cout<<d<<endl;;
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
    int n,i,j,m=0,k=0,d=-1;
    int iniprime[44722],primes[44722];
    memset(iniprime,0,sizeof(iniprime));
    iniprime[0]=1;
    iniprime[1]=1;
    for(i=2;i<=44721;i++)
    {
        if(iniprime[i]==0)
        {
            for(j=2;i*j<=44721;j++)
                iniprime[i*j]=1;
            primes[m]=i;
            m++;
        }
    }
    cin>>n;
    long long int a[n],b[n],ct=0;
    for(i=0;i<=n-1;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]%2==0||b[i]%2==0)
            ct++;
    }
    if(ct==n)
    {
        k++;
        cout<<"2\n";
    }
    else
    {
        ct=0;
        for(i=1;i<=m-1;i++)
        {
            for(j=0;j<=n-1;j++)
            {
                if(a[j]%primes[i]==0||b[j]%primes[i]==0)
                    ct++;
                else
                    break;
            }
            if(ct==n)
            {
                k++;
                cout<<primes[i]<<endl;
                break;
            }
            ct=0;
        }
        if(k==0)
            cout<<d<<endl;;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	int m = 0 ;
	if ( gc % 2 == 0)
	{
		v.pb(2) ;
		m = 1 ;
	}
	if (gc % 3 == 0 && m == 0 )
	{
		v.pb(3) ;
		m = 1 ;
	}
	if (m == 0)
	{
		for2(5 , gc , 5)
		{
			if ( gc % j == 0)
			{
				v.pb(j) ;
				break ;
			}
			if (gc % (j + 1) == 0)
			{
				v.pb(j + 1) ;
				break ;
			}
			if (gc % (j + 2) == 0)
			{
				v.pb(j + 2) ;
				break ;
			}
			if (gc % (j + 3) == 0)
			{
				v.pb(j + 3) ;
				break ;
			}
			if (gc % (j + 4) == 0)
			{
				v.pb(j + 4) ;
				break ;
			}
		}
	}

	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	for2(2 , gc , 1)
	{
		if ( gc % j == 0)
		{
			v.pb(j) ;
			return 0 ;
			wh(gc % j == 0)
			{
				gc = gc / j ;
			}

		}
	}
	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j  * 2 <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	for2(2 , gc , 1)
	{

		if ( gc % j == 0)
		{
			v.pb(j) ;
			break ;
		}
	}
	ll u = sqrt(gc) ;
	if (u * u == gc)
	{
		v.pb(gc) ;
	}
	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	if ( gc % 2 == 0)
	{
		v.pb(2) ;
	}
	else
	{
		for2(3 , gc , 2)
		{
			if ( gc % j == 0)
			{
				v.pb(j) ;
				break ;
			}
		}
	}

	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	for2(2 , gc , 1)
	{
		if ( gc % j == 0)
		{
			v.pb(j) ;
			break ;
			wh(gc % j == 0)
			{
				gc = gc / j ;
			}

		}
	}
	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long int ll;
typedef long double dou ;
typedef pair<ll , ll> pii;
typedef map<ll, bool> mib;
typedef pair<ll, bool> pib;
typedef map<ll , ll> mii;
#define wh while
#define str string
#define vec vector
#define for1(a1 , a2 , a3) for(ll i = a1 ; i  < a2 ; i = i + a3)
#define for_1(b1 , b2 , b3) for(ll i = b1 ; i >= b2 ; i = i - b3)
#define for2(c1 , c2 , c3) for(ll j = c1 ; j * j <= c2 ; j = j + c3)
#define for_2(d1 , d2 , d3) for(ll j = d1 ; j >= d2 ; j = j - d3)
#define for3(e1 , e2 , e3) for(ll e = e1 ; e < e2 ; e = e + e3)
#define for_3(f1 , f2 , f3) for(ll e = f1 ; e >= f2 ; e = e - f3)
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
#define setp(t) cout << setprecision(t) << fixed ;
pii a[150000] ;
ll k[150000] ;
vec <ll> v ;
int main(){
	fast ;
	ll n ;
	scanf("%lld", &n);
	ll gc = 1;
	for1(0 , n , 1)
	{
		scanf("%lld%lld",&a[i].ff,&a[i].ss) ;
		k[i] = a[i].ff * a[i].ss ;
		if (i > 0)
		{
			gc = __gcd(gc , k[i]) ;
		}
		else
		{
			gc = k[i] ;
		}
	}
	if (gc == 1)
	{
		cout << -1 ;
		return 0 ;
	}
	int m = 0 ;
	if ( gc % 2 == 0)
	{
		v.pb(2) ;
		m = 1 ;
	}
	if (gc % 3 == 0 && m == 0 )
	{
		v.pb(3) ;
		m = 1 ;
	}
	if (m == 0)
	{
		for2(5 , gc , 5)
		{
			if ( gc % j == 0)
			{
				v.pb(j) ;
				break ;
			}
		}
	}

	if (v.size() == 0)
	{
		v.pb(gc) ;
	}
	for3(0 , v.size() , 1)
	{
		int x = 0 ;
		for1(0 , n , 1)
		{
			if (a[i].ff % v[e] == 0 || a[i].ss % v[e] == 0)
			{
				x++;
			}
			else
			{
				break ;
			}
		}
		if (x == n)
		{
			printf("%I64d\n", v[e]) ;
			return 0 ;
		}
		else
		{
			continue ;
		}
	}
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e6+5;

bool isPrime[N];
std::vector<ll> v;
map <ll, bool> mop;

int main()
{
    IO
    for(ll i = 2 ; i < N ; i++)
    {
        if(isPrime[i]) continue;
        v.push_back(i);
        for(ll j = i*i ; j < N ; j+=i) isPrime[j] = 1;
    }
    ll a, b, n;
    cin>>n>>a>>b;
    mop[a] = mop[b] = 1;
    for(auto i:v)
    {
        if(a%i == 0 or b%i == 0) mop[i] = 1;
    }
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e6+5;

bool isPrime[N];
std::vector<ll> v;
map <ll, bool> mop;

int main()
{
    IO
    for(ll i = 2 ; i < N ; i++)
    {
        if(isPrime[i]) continue;
        v.push_back(i);
        for(ll j = i*i ; j < N ; j+=i) isPrime[j] = 1;
    }
    ll a, b, n;
    cin>>n>>a>>b;
    mop[a] = mop[b] = 1;
    for(auto i:v)
    {
        if(a%i == 0) mop[i] = mop[a/i] = 1;
        if(b%i == 0) mop[i] = mop[b/i] = 1;
    }
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

map <ll, bool> mop;

bool isPrime(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) return 0;
    }
    return 1;
}

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0 and isPrime(i)) mop[i] = mop[x/i] = 1;
    }
    mop[x] = 1;
}

int main()
{
    IO
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a);
    getDivs(b);
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

unordered_map <ll, bool> mop;
unordered_map <ll, bool> mop2;

bool isPrime(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) return 0;
    }
    return 1;
}

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) mop2[i] = mop2[x/i] = 1;
    }
    mop2[x] = 1;
}

int main()
{
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a), getDivs(b);
    for(auto i:mop2)
    {
        if(isPrime(i.F))
        {
            mop[i.F] = 1;
        }
    }
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

map <ll, bool> mop;

bool isPrime(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) return 0;
    }
    return 1;
}

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0 and isPrime(i)) mop[i] = 1;
    }
    mop[x] = 1;
}

int main()
{
    IO
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a), getDivs(b);
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e6+5;

bool isPrime[N];
std::vector<ll> v;
map <ll, bool> mop;

int main()
{
    IO
    for(ll i = 2 ; i < N ; i++)
    {
        if(isPrime[i]) continue;
        v.push_back(i);
        for(ll j = i*i ; j < N ; j+=i) isPrime[j] = 1;
    }
    ll a, b, n;
    cin>>n>>a>>b;
    mop[a] = mop[b] = 1;
    for(auto i:v)
    {
        if(a%i == 0) mop[i] = mop[a/i] = 1;
        if(b%i == 0) mop[i] = mop[b/i] = 1;
    }
    mop[1] = 0;
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

map <ll, bool> mop;
map <ll, bool> mop2;

bool isPrime(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) return 0;
    }
    return 1;
}

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) mop2[i] = mop2[x/i] = 1;
    }
    mop2[x] = 1;
}

int main()
{
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a), getDivs(b);
    for(auto i:mop2)
    {
        if(isPrime(i.F))
        {
            mop[i.F] = 1;
        }
    }
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

map <ll, bool> mop;

bool isPrime(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) return 0;
    }
    return 1;
}

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0 and isPrime) mop[i] = mop[x/i] = 1;
    }
    mop[x] = 1;
}

int main()
{
    IO
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a);
    getDivs(b);
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

unordered_map <ll, bool> mop;

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) mop[i] = mop[x/i] = 1;
    }
    mop[x] = 1;
}

int main()
{
    IO
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a), getDivs(b);
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 2e5+5;

map <ll, bool> mop;

void getDivs(ll x)
{
    for(ll i = 2 ; i*i <= x ; i++)
    {
        if(x%i == 0) mop[i] = mop[x/i] = 1;
    }
    mop[x] = 1;
}

int main()
{
    ll a, b, n;
    cin>>n>>a>>b;
    getDivs(a), getDivs(b);
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#pragma gcc optimize (o3)
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e6+5;

bool isPrime[N];
std::vector<ll> v;
map <ll, bool> mop;

int main()
{
    IO
    for(ll i = 2 ; i < N ; i++)
    {
        if(isPrime[i]) continue;
        v.push_back(i);
        for(ll j = i*i ; j < N ; j+=i) isPrime[j] = 1;
    }
    ll a, b, n;
    cin>>n>>a>>b;
    for(auto i:v)
    {
        if(a%i == 0 or b%i == 0) mop[i] = 1;
    }
    for(int i = 1 ; i < n ; i ++)
    {
        cin>>a>>b;
        for(auto &i:mop)
        {
            if(i.S and a%i.F and b%i.F) i.S = 0;
        }
    }
    for(auto &i:mop)
    {
        if(i.S) return cout<<i.F, 0;
    }
    cout<<-1;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[0].ff; i++){
        if(v[0].ff % i == 0){
            a.pb(i), a.pb(v[0].ff / i);
        }
    }
    for(ll i = 2; i * i <= v[0].ss; i++){
        if(v[0].ss % i == 0){
            a.pb(i), a.pb(v[0].ss / i);
        }
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[0].ff; i++){
        if(v[0].ff % i == 0){
            a.pb(i), a.pb(v[0].ff / i);
        }
    }
    for(ll i = 2; i * i <= v[0].ss; i++){
        if(v[0].ss % i == 0){
            a.pb(i), a.pb(v[0].ss / i);
        }
    }
    a.pb(v[0].ff);
    a.pb(v[0].ss);
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[0].ff; i++){
        if(v[i].ff % i == 0){
            a.pb(i), a.pb(v[i].ff / i);
        }
    }
    for(ll i = 2; i * i <= v[0].ss; i++){
        if(v[i].ss % i == 0){
            a.pb(i), a.pb(v[i].ss / i);
        }
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[0].ff; i++){
        if(v[0].ff % i == 0){
            a.pb(i), a.pb(v[0].ff / i);
        }
    }
    for(ll i = 2; i * i <= v[0].ss; i++){
        if(v[0].ss % i == 0){
            a.pb(i), a.pb(v[0].ss / i);
        }
    }
    a.pb(v[0].ff);
    a.pb(v[0].ss);
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[i].ff; i++){
        if(v[i].ff % i == 0) a.pb(i), a.pb(v[i].ff / i);
        if(a.back() == 978053) fu;
    }
    for(ll i = 2; i * i <= v[i].ss; i++){
        if(v[i].ss % i == 0) a.pb(i), a.pb(v[i].ss / i);
        if(a.back() == 978053) fu;
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[i].ff; i++){
        if(v[i].ff % i == 0) a.pb(i), a.pb(v[i].ff / i);
        if(a[i] == 978053) fu;
    }
    for(ll i = 2; i * i <= v[i].ss; i++){
        if(v[i].ss % i == 0) a.pb(i), a.pb(v[i].ss / i);
        if(a[i] == 978053) fu;
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[i].ff; i++){
        if(v[i].ff % i == 0) a.pb(i), a.pb(v[i].ff / i);
    }
    for(ll i = 2; i * i <= v[i].ss; i++){
        if(v[i].ss % i == 0) a.pb(i), a.pb(v[i].ss / i);
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[i].ff; i++){
        if(v[i].ff % i == 0){
            a.pb(i), a.pb(v[i].ff / i);
            if((i == 978053) || (v[i].ff/i) == 978053) fu;
        }
    }
    for(ll i = 2; i * i <= v[i].ss; i++){
        if(v[i].ss % i == 0){
            a.pb(i), a.pb(v[i].ss / i);
            if((i == 978053) || (v[i].ss/i) == 978053) fu;
        }
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    TC{
        int n, m; cin>>n>>m;
        string s; cin>>s;
        vector<pair<char, int>> col(m);
        for(i = 0; i < m; i++){
            char c; int in; cin>>c>>in;
            col[i] = make_pair(c,in);
        }
        vector< vector<char> > a(n+5, vector<char>(m+5, ' '));
        for(i = 0; i < s.size(); i++) a[ i/m ][ i%m ] = s[i];
        vector< vector<char> > b(n+5, vector<char>(m+5, ' '));
        for(i = 0; i < m; i++){
            int x = col[i].ss;
            for(j = 0; j < n; j++){
                if(col[i].ff == 'U'){
                    int y = j - x;
                    if(y < 0) y = (m - abs(y)%m)%m;
                    b[y][i] = a[j][i];
                }
                else{
                    int y = (j + x) % m;
                    b[y][i] = a[j][i];
                }
            }
        }
        string ans;
        bool f = 1;
        int cnt = 0;
        for(i = 0; i < n && f; i++){
            for(j = 0; j < m && f; j++){
                ans += b[i][j];
                if(b[i][j] != ' ') cnt++;
                if(cnt == s.size()) f = 0;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);

int main()
{
    int i = 0, j = 0, cs = 0, in;
    speed;
    int n; cin>>n;
    vii v(n);
    for(i = 0; i < n; i++) cin>>v[i].ff>>v[i].ss;
    vi a;
    for(ll i = 2; i * i <= v[i].ff; i++){
        if(v[i].ff % i == 0){
            a.pb(i), a.pb(v[i].ff / i);
            if((i == 978053) || (v[i].ff/i) == 978053) fu;
        }
    }
    for(ll i = 2; i * i <= v[i].ss; i++){
        if(v[i].ss % i == 0){
            a.pb(i), a.pb(v[i].ss / i);
            if((i == 978053) || (v[i].ff/i) == 978053) fu;
        }
    }
    for(auto i : a){
        bool f = 1;
        for(auto j : v){
            if(j.ff % i != 0 && j.ss % i != 0){
                f = 0;
                break;
            }
        }
        if(f) return cout << i << endl, 0;
    }
    cout << -1 << endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
                v.erase(v.begin()+j);
        }
        i++;
    }
    for(int k=0;k<v.size();k++)
        cout<<v[k];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
                v.erase(v.begin()+j);
        }
        i++;
    }
        if(v.empty())
            cout<<-1;
        else
            cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    v.push_back(a);
    v.push_back(b);
    i=1;
    long long int prev1=0,prev2=0;
    int counter =0;
    while(i<n)
    {
        cin>>a>>b;
        if((prev1==a&&prev2==b)||(prev1==b&&prev2==a))
            {counter++;continue; if(counter==500) break;}
                    for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        prev1=a;
        prev2=b;
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
           cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    v.push_back(a);
    v.push_back(b);
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
           cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    v.push_back(a);
    v.push_back(b);
    i=1;
    long long int prev1=0,prev2=0;
    int counter =0;
    while(i<n)
    {
        cin>>a>>b;
        if((prev1==a&&prev2==b)||(prev1==b&&prev2==a))
            {counter++;continue; }
            if(counter==500) break;
                    for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        prev1=a;
        prev2=b;
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
           cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
            cout<<v[0];

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,a,b,m,M,j,res=0;
	cin>>n;
	long long int ar[n];
	for(i=0;i<n;i++)
	{
		cin>>a>>b;
		m=max(a,b);
		ar[i]=a*b;
		M=max(M,m);
	}
	for(j=2;j<=sqrt(M);j++)
	{
		int cnt=0;
		for(int k=0;k<n;k++)
		{
			if(ar[k]%j!=0)
			{
				cnt=1;
				break;
			}
		}
		if(cnt==0)
		{
			res=1;
			cout<<j;
			break;
		}
	}
	if(res==0)
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,m,M,j,res=0,fail=0;
	cin>>n;
	long long int ar[n],a[n],b[n];
	for(i=0;i<n;i++)
	{
		cin>>a[i]>>b[i];
		m=max(a[i],b[i]);
		ar[i]=a[i]*b[i];
		M=max(M,m);
	}
	for(j=2;j<=sqrt(M);j++)
	{
		int cnt=0;
		for(int k=0;k<n;k++)
		{
			if(j>a[k]&&j>b[k])
			{
				fail=1;
				break;
			}
			if(ar[k]%j!=0)
			{
				cnt=1;
				break;
			}
		}
		if(fail==1)
		break;
		if(cnt==0)
		{
			res=1;
			cout<<j;
			break;
		}
	}
	if(res==0)
	cout<<-1;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    v.push_back(a);
    v.push_back(b);
    i=1;
    long long int prev1=0,prev2=0;
    while(i<n)
    {
        cin>>a>>b;
        if((prev1==a&&prev2==b)||(prev1==b&&prev2==a))
            continue;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        prev1=a;
        prev2=b;
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
           cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
                v.erase(v.begin()+j);
        }
        i++;
    }
        cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      		if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
//               if(!(find(v.begin(), v.end(), a/i) != v.end()))
//                 v.push_back(a/i);
				while(a%i==0)
				a/=i;
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
//               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
//                 v.push_back(b/i);
				while(b%i==0)
				b/=i;
            }

    }
    if(a!=1)
    v.push_back(a);
    if(b!=1)
    v.push_back(b);
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
           cout<<v[0];

}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    long long int a,b;
    vector<long int> v;
    cin>>a>>b;
    for(i=2;i<=max(sqrt(a),sqrt(b));i++)
    {

      if(a%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), a/i) != v.end()))
                 v.push_back(a/i);
            }
            if(b%i==0)
            {
               if(!(find(v.begin(), v.end(), i) != v.end()))
                 v.push_back(i);
               if(!(find(v.begin(), v.end(), b/i) !=v.end()))
                 v.push_back(b/i);
            }

    }
    i=1;
    while(i<n)
    {
        cin>>a>>b;
        for(int j=0;j<v.size();j++)
        {
            if(a%v[j]!=0&&b%v[j]!=0)
              {
                  v.erase(v.begin()+j);
                  j--;
              }

        }
        i++;
    }
        //cout<<v.size()<<endl;
        if(v.empty())
            cout<<-1;
        else
            cout<<v[0];

}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+10, N = 2e9;
int a[MAXN], b[MAXN];
set<int> s;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);

    for (int i = 2, q = sqrt(a[0]*1.0); i <= q; ++i)
        if (a[0] % i == 0) s.insert(i), s.insert(a[0]/i);
    for (int i = 2, q = sqrt(b[0]*1.0); i <= q; ++i)
        if (b[0] % i == 0) s.insert(i), s.insert(b[0]/i);
    s.insert(a[0]), s.insert(b[0]);
    for (int i = 1; i < n && !s.empty(); ++i) for (auto p = s.begin(); p != s.end(); ) {
        if (a[i] % (*p) != 0 && b[i] % (*p) != 0) {
            auto temp = p;
            ++p;
            s.erase(temp);
        } else ++p;
    }
    if (s.empty()) puts("-1");
    else printf("%d\n", *s.begin());
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10, N = 2e9;
bool vis[MAXN];
vector<int> prime;
int a[MAXN], b[MAXN];
int cnt[MAXN], tot = -1;
//void getPrime(int n) {
//    for (int i = 2; i <= n; ++i) {
//        if (!vis[i]) prime.push_back(i);
//        for (int j = 0; j < prime.size() && i * prime[j] <= n; ++j) {
//            vis[i*prime[j]] = true;
//            if (i % prime[j] == 0) break;
//        }
//    }
//    memset(cnt, 0x3f, sizeof(cnt));
//}

int main() {
//    getPrime(MAXN-2);
    int n, q = sqrt(1.0*N);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);
    int res = 0;
    for (int i = 2; i <= q; ++i) {
        int j, t = 0;
        for (j = 0; j < n; ++j) {
            if (a[j] % i != 0 && b[j] % i != 0) break;
            if (a[j] % i == 0) {
                int temp = a[j] / i;
                if (temp == i) t = 1;
            }
            if (b[j] % i == 0) {
                int temp = b[j] / i;
                if (temp == i) t = 1;
            }
        }
        if (j >= n) {
//            printf("i = %d\n", i);
            res += 2;
            if (t) res--;
        }
    }
    if (res == 0) puts("-1");
    else printf("%d\n", res);
    return 0;
}


#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+5, INF = 0x3f3f3f3f, N = 2e9;
ll a[MAXN],b[MAXN], res;
ll gt(ll n) {
    for (ll i = 2; i * i <= n; ++i) if (n % i == 0) return i;
    return n;
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d%I64d", a+i, b+i);
        res = __gcd(res, a[i]*b[i]);
    }
    if (res == 1) puts("-1");
    else if (__gcd(a[0], res) > 1) printf("%I64d\n", gt(__gcd(a[0], res)));
    else printf("%I64d\n", gt(__gcd(b[0], res)));
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10, N = 2e9;
int a[MAXN], b[MAXN];
set<int> s;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);

    for (int i = 2, q = sqrt(a[0]*1.0); i <= q; ++i)
        if (a[0] % i == 0) s.insert(i), s.insert(a[0]/i);
    for (int i = 2, q = sqrt(b[0]*1.0); i <= q; ++i)
        if (b[0] % i == 0) s.insert(i), s.insert(b[0]/i);
    for (int i = 1; i < n && !s.empty(); ++i) for (auto p = s.begin(); p != s.end(); ) {
//        printf("p = %d\n", *p);
        if (a[i] % (*p) != 0 && b[i] % (*p) != 0) {
            auto temp = p;
            ++p;
            s.erase(temp);
        } else ++p;
    }
    if (s.empty()) puts("-1");
    else printf("%d\n", s.size());
    return 0;
}


#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+5, INF = 0x3f3f3f3f, N = 2e9;
int a[MAXN],b[MAXN];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", a+i, b+i);
    set<int> s;
    for (int i = 2, q = sqrt(N*1.0); i <= q && (i <= a[0] || i <= b[0]); ++i) {
        if (a[0] % i == 0) s.insert(i), s.insert(a[0]/i);
        if (b[0] % i == 0) s.insert(i), s.insert(b[0]/i);
    }
    s.insert(a[0]), s.insert(b[0]);
    vector<int> c;
    for (auto &p:s) c.push_back(p);
    for (auto &p:c) {
        int j;
        for (j = 1; j < n; ++j)
            if (a[j] % p != 0 && b[j] % p != 0)
                break;
        if (j < n) {
            p = 1;
        }
    }
    int res = -1;
    for (auto &p:c) if (p != 1) {
        res = p;
        break;
    }
    printf("%d\n", res);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10, N = 2e9;
bool vis[MAXN];
vector<int> prime;
int a[MAXN], b[MAXN];
int cnt[MAXN], tot = -1;
//void getPrime(int n) {
//    for (int i = 2; i <= n; ++i) {
//        if (!vis[i]) prime.push_back(i);
//        for (int j = 0; j < prime.size() && i * prime[j] <= n; ++j) {
//            vis[i*prime[j]] = true;
//            if (i % prime[j] == 0) break;
//        }
//    }
//    memset(cnt, 0x3f, sizeof(cnt));
//}

int main() {
//    getPrime(MAXN-2);
    int n, q = sqrt(1.0*N);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);
    int res = 0;
    for (int i = 2; i <= q; ++i) {
        int j, t = 0;
        for (j = 0; j < n; ++j) {
            if (a[j] % i != 0 && b[j] % i != 0) break;
            if (a[j] % i == 0) {
                int temp = a[j] / i;
                if (temp == i) t = 1;
            }
            if (b[j] % i == 0) {
                int temp = b[j] / i;
                if (temp == i) t = 1;
            }
        }
        if (j >= n) {
            printf("%d\n", i);
            return 0;
        }
    }
    puts("-1");
    return 0;
}


#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+5, INF = 0x3f3f3f3f, N = 2e9;
ll a[MAXN],b[MAXN], res;
ll gt(ll n) {
    for (ll i = 2; i * i <= n; ++i) if (n % i == 0) return i;
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%I64d%I64d", a+i, b+i);
        res = __gcd(res, a[i]*b[i]);
    }
    if (res == 1) puts("-1");
    else if (__gcd(a[0], res) > 1) printf("%I64d\n", gt(__gcd(a[0], res)));
    else printf("%I64d\n", gt(__gcd(b[0], res)));
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10, N = 2e9;
int a[MAXN], b[MAXN];
set<int> s;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);

    for (int i = 2, q = sqrt(a[0]*1.0); i <= q; ++i)
        if (a[0] % i == 0) s.insert(i), s.insert(a[0]/i);
    for (int i = 2, q = sqrt(b[0]*1.0); i <= q; ++i)
        if (b[0] % i == 0) s.insert(i), s.insert(b[0]/i);
    s.insert(a[0]), s.insert(b[0]);
    for (int i = 1; i < n && !s.empty(); ++i) for (auto p = s.begin(); p != s.end(); ) {
        if (a[i] % (*p) != 0 && b[i] % (*p) != 0) {
            auto temp = p;
            ++p;
            s.erase(temp);
        } else ++p;
    }
    if (s.empty()) puts("-1");
    else printf("%d\n", *s.begin());
    return 0;
}


#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+5, INF = 0x3f3f3f3f, N = 2e9;
int a[MAXN],b[MAXN];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", a+i, b+i);

    set<int> s;
    for (ll i = 2; (i * i <= a[0] || i * i <= b[0]); ++i) {
        if (a[0] > 1 && a[0] % i == 0) {
            s.insert(i);
            while (a[0] % i == 0) a[0] /= i;
        }
        if (b[0] > 1 && b[0] % i == 0) {
            s.insert(i);
            while (b[0] % i == 0) b[0] /= i;
        }
    }
    s.insert(a[0]), s.insert(b[0]);
    vector<int> c;
    for (auto &p:s) c.push_back(p);
    for (auto &p:c) {
        int j;
        for (j = 1; j < n; ++j)
            if (a[j] % p != 0 && b[j] % p != 0)
                break;
        if (j < n) {
            p = 1;
        }
    }
    int res = -1;
    for (auto &p:c) if (p != 1) {
        res = p;
        break;
    }
    printf("%d\n", res);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+10, N = 2e9;
int a[MAXN], b[MAXN];
set<int> s;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);

    for (int i = 2, q = sqrt(a[0]*1.0); i <= q; ++i)
        if (a[0] % i == 0) s.insert(i), s.insert(a[0]/i);
    for (int i = 2, q = sqrt(b[0]*1.0); i <= q; ++i)
        if (b[0] % i == 0) s.insert(i), s.insert(b[0]/i);
    s.insert(a[0]), s.insert(b[0]);
    for (int i = 1; i < n && !s.empty(); ++i) for (auto p = s.begin(); p != s.end(); ) {
        if (a[i] % (*p) != 0 && b[i] % (*p) != 0) {
            auto temp = p;
            ++p;
            s.erase(temp);
        } else ++p;
    }

    if (s.empty()) puts("-1");
    else printf("%d\n", s.size());
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 2e5+10, N = 2e9;
bool vis[MAXN];
vector<int> prime;
int a[MAXN], b[MAXN];
int cnt[MAXN], tot = -1;
//void getPrime(int n) {
//    for (int i = 2; i <= n; ++i) {
//        if (!vis[i]) prime.push_back(i);
//        for (int j = 0; j < prime.size() && i * prime[j] <= n; ++j) {
//            vis[i*prime[j]] = true;
//            if (i % prime[j] == 0) break;
//        }
//    }
//    memset(cnt, 0x3f, sizeof(cnt));
//}

int main() {
//    getPrime(MAXN-2);
    int n, q = sqrt(1.0*N);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d%d", &a[i], &b[i]);
    int res = 0;
    for (int i = 2; i <= q; ++i) {
        int j, t = 0;
        for (j = 0; j < n; ++j) {
            if (a[j] % i != 0 && b[j] % i != 0) break;
            if (a[j] % i == 0) {
                int temp = a[j] / i;
                if (temp == i) t = 1;
            }
            if (b[j] % i == 0) {
                int temp = b[j] / i;
                if (temp == i) t = 1;
            }
        }
        if (j >= n) {
            printf("%d\n", i);
            return 0;
        }
    }
    puts("-1");
    return 0;
}


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
//char buf[1<<15],*S=buf,*T=buf;
//char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read(){
	int x=0,f=1;char ch;ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
	if(f)return x;else return -x;
}
const int N=150010;
int n,a[N],b[N];
int p[N];
void init(int a){
	for(int i=2;i*i<=a;i++){
		if(!(a%i)){
			p[++p[0]]=i;
			while(!(a%i))a/=i;
		}
	}
	if(a>1){p[++p[0]]=a;}
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)
	a[i]=read(),b[i]=read();
	init(a[1]);init(b[1]);
	sort(p+1,p+p[0]+1);
	p[0]=unique(p+1,p+p[0]+1)-p-1;
	for(int i=1,j;i<=p[0];i++){
		for(j=2;j<=n;j++)if(a[j]%p[i]&&b[j]%p[i])break;
		if(j>n){printf("%d\n",p[i]);return 0;}
	}
	printf("-1\n");
	return 0;
}


 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <ctime>
#include <list>
#include <iomanip>
#define ms(s) memset(s, 0, sizeof(s))
#define PI acos(-1)
#define eps 1e-9
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 100000000+5;
const int mod = 1000000007;
using namespace std;

set<int> s;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int x, y;
	cin >> x >> y;
	int cnt = 0;
	for( int i = 2; i <= sqrt(x); i++ ){
		if( x%i == 0 ){
			s.insert(i);
		}
		while ( x%i==0 ) x/=i;
	}
	for( int i = 2; i <= sqrt(y); i++ ){
		if( y%i == 0 ){
			s.insert(i);
		}
		while ( y%i==0 ) y/=i;
	}
	int fff = 0;
	for( int i = 1; i < n; i++ ){
		int x, y;
		cin >> x >> y;
		int ff = 0;
		while( !s.empty() ){
			int f = 0;
			if( x % *(s.begin()) == 0 ){
				f = 1;
			}
			if( y % *(s.begin()) == 0 ){
				f = 1;
			}
			if( f == 0 ){
				s.erase(s.begin() );
			}else{
				ff = 1;
				break;
			}
		}
		if( ff == 0 ){
			fff = 1;
		}
		if( fff == 1 ){
			break;
		}
	}
	if( fff == 1 ){
		cout << "-1" << endl;
	}else{
		cout << *(s.begin()) << endl;
	}
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>

#define ll long long

using namespace std;

ll n,a,b,ans;

inline ll gcd(ll x,ll y){
	return x%y?gcd(y,x%y):y;
}

inline ll calc(ll x){
	for(int i=2;i<=sqrt(x);i++){
		if(x%i==0){
			return i;
		}
	}
	return x;
}

int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	n--;
	cin>>a>>b;
	ans=a*b/gcd(a,b);
	while(n--){
		cin>>a>>b;
		ans=gcd(ans,a*b/gcd(a,b));
	}
	if(ans==1){
		cout<<-1<<endl;
	}else{
		if(gcd(ans,a)>1){
			cout<<calc(gcd(ans,a))<<endl;
		}else{
			cout<<calc(gcd(ans,b))<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N = 1000010;

vector < long long > vc;

int n, vis[N];

long long a[N], b[N];

void init(long long x, long long y) {
	long long xx = x, yy = y;
	for (int i = 2; i * i <= x; i++) {
		if (xx % i) continue;
		vc.push_back(i);
		while (xx % i == 0) xx /= i;
	}
	for (int i = 2; i * i <= y; i++) {
		if (yy % i) continue;
		vc.push_back(i);
		while (yy % i == 0) yy /= i;
	}
	sort(vc.begin(), vc.end());
	unique(vc.begin(), vc.end());
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%I64d%I64d", &a[i], &b[i]);
	init(a[1], b[1]);
	for (int i = 2; i <= n; i++) {
		for (int j = 0; j < vc.size(); j++)
			if (a[i] % vc[j] == 0 || b[i] % vc[j] == 0) vis[j]++;
	}
	for (int i = 0; i < vc.size(); i++)
		if (vis[i] == n - 1) return printf("%I64d\n", vc[i]), 0;
	printf("-1\n");
	return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;
int n;
int a[150000+5],b[150000+5];
int have_f(int x)
{
	unsigned int ANS=0;
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			ANS|=(1<<(i));
			//cout<<x<<" "<<i<<endl;
			while(x%i==0)
			{
				x/=i;
			}
		}
	}

	if(x>1) ANS|=(1<<(x));
	return ANS;
}
int main()
{
	cin>>n;
	int ans;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
		int A=have_f(a[i]);
		int B=have_f(b[i]);
		int now=A|B;
		if(i==1)
		{
			ans=now;
		}
		else
		{
			ans&=now;
		}
	}
	if(ans==0) cout<<-1;
	else
	{
		for(int i=0;i<ans;i++)
		{
			if(ans&(1<<i))
			{
				cout<<i;
				return 0;
			}
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;

int n;
ll a1,b1;
ll tp;

ll gcd(ll a,ll b)
{
//	cout<<a<<" "<<b<<endl;
	if(b==0) return a;
	else return gcd(b,a%b);
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a1>>b1;
	//	cout<<1<<endl;
		ll x=a1*b1;
	//	cout<<"********"<<endl;
		if(i==1)
		{
			tp=x;
		}
		else tp=gcd(tp,x);
		tp=max(gcd(tp,a1),gcd(tp,b1));
	}
	if(tp==1) cout<<-1;
	else cout<<tp;
	return 0;
}


#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
	return b==0?a:gcd(b,a%b);
}
int main() {
	int n;
	LL a,b,lcm;
	cin>>n;
	cin>>a>>b;
	LL g=a*b/gcd(a,b);
	for1(i,n-1){
		cin>>a>>b;
		lcm=a*b/gcd(a,b);
		g=gcd(g,lcm);
	}
	if(g!=1){
		for(LL i=2;i*i<=2000000000;i++){
			if(g%i==0){
				cout<<i<<endl;
				return 0;
			}
		}
		if(g<=2e9) cout<<g<<endl;
		else cout<<min(a,b)<<endl;
	}
	else cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,a,b,f[150005],flag,t;
bool bb[150005];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld",&a,&b);
		if(i==1){
			for(int j=2;j<=sqrt(a);j++){
				flag=0;
				while(a%j==0)flag=1,a/=j;
				if(flag)f[++t]=j;
			}
			for(int j=2;j<=sqrt(b);j++){
				flag=0;
				while(b%j==0)flag=1,b/=j;
				if(flag)f[++t]=j;
			}
		}
		else{
			for(int j=1;j<=t;j++)
			if(a%f[j]&&b%f[j])bb[j]=true;
		}
	}
	for(int i=1;i<=t;i++)
	if(!bb[i]){
		printf("%lld",f[i]);return 0;
	}
	printf("-1");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
typedef long long ll;
using namespace std;
ll n,a,b,ans;
ll gcd(ll x,ll y)
{
	return x%y?gcd(y,x%y):y;
}
ll pick(ll x)
{
	for(int i=2;i<=sqrt(x);i++)
	{
		if(x%i==0)
		{
			return i;
		}
	}
	return x;
}
int main()
{
	cin>>n;
	n--;
	cin>>a>>b;
	ans=a*b/gcd(a,b);
	while(n--)
	{
		cin>>a>>b;
		ans=gcd(ans,a*b/gcd(a,b));
	}
	if(ans==1)
	{
		cout<<-1<<endl;
	}
	else
	{
		if(gcd(ans,a)>1)
		{
			cout<<pick(gcd(ans,a))<<endl;
		}
		else
		{
			cout<<pick(gcd(ans,b))<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long num(long long x)
{
	for(long long i=2; i*i<=x; i++)
	{
		if(x%i==0) return i;
	}
}
int main()
{
	long long n;
	cin>>n;
	long long a,b;
	cin>>a>>b;
	long long ans=a/__gcd(a,b)*b;
	for(long long i=1; i<n; i++)
	{
		long long a,b;
		cin>>a>>b;
		ans=__gcd(ans,a/__gcd(a,b)*b);
	}
	if(ans==1)
	{
		cout<<-1<<endl;
	}
	else
	{
		if(__gcd(ans,a)==1)
		{
			cout<<num(__gcd(ans,b));
		}
		else cout<<num(__gcd(ans,a));
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll div(ll n)
{
    for(int i = 2;i*i<=n;i++)
        if(n%i==0) return i;
    return n;
}
int main()
{
    ll n,a,b,c,d;
    cin>>n>>a>>b;
    while(--n)
    {
        cin>>c>>d;
        a = __gcd(c*d,a),b = __gcd(c*d,b);
    }
    if(a!=1) cout<<div(a)<<endl;
    else if(b!=1) cout<<div(b)<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 5e5+5;
const int maxm = 2e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};

int N, M, K;
char str[maxn];

bool can_end(int m) {
        char c=m+'a';
        for(int i=1,p=0;i<=N+1;i++) {
                if(str[i]<=c) {
                        if(i-p>M){
                                return 0;
                        }
                        p=i;
                }
        }
        return 1;
}

int now[maxn];
multiset<int>S;
int work(int l,int r,char c) {
        //printf("%d %d %c\n", l, r, c );
        S.clear(); S.insert(0);
        now[l]=0;
        for(int i=l+1;i<r;i++) {
                if(i-l>=M&&str[i]<=c) {
 //                       printf("insert erase\n");
                        S.erase(S.find(now[l]));
   //                     printf("insert erase\n");
                }
                if(c!=str[i]) continue;
                int t=*S.begin();
                now[i]=t+1;
                S.insert(now[i]);
                if(r-i<=M) return now[i];
        }
        return 0;
}

char out[maxn];
int L;
void find_min(int m) {
        //printf("::\n");
        char c=m+'a';
        int s=0;
        for(int p=0,r=1;r<=N+1;r++) {
         //       printf("%d %d \n", p, r);
                while(str[r]>=c) r++;
                if(r!=N+1) out[L++]=str[r]-'a';
                //printf("%d %d\n", p, r);
                if(r-p<=M) {
                        p=r;
                        continue;
                }
                s+=work(p,r,c);
                //printf("s %d after work\n", s);
                p=r;
        }
        for(int i=0;i<s;i++) out[L++]=m;
        sort(out,out+L);
        for(int i=0;i<L;i++) putchar('a'+out[i]); puts("");
}

int main(int argc, char *argv[]) {
        if(argc>1) {
                freopen( argv[1], "r", stdin );
        }
        cin>>M;
        scanf("%s", str+1);
        N=strlen(str+1);
        //printf("%d %d \n", N, M);
        int s=-1;
        for(int i=0;i<26;i++) {
                //printf("i %d\n", i);
                if(can_end(i)) {
                        find_min(i);
                        break;
                }
        }
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 5e5+5;
const int maxm = 2e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};

int N, M, K;
char str[maxn];

bool can_end(int m) {
        char c=m+'a';
        for(int i=1,p=0;i<=N+1;i++) {
                if(str[i]<=c) {
                        if(i-p>M){
                                return 0;
                        }
                        p=i;
                }
        }
        return 1;
}

int now[maxn];
multiset<int>S;
int work(int l,int r,char c) {
        //printf("%d %d %c\n", l, r, c );
        S.clear(); S.insert(0);
        now[l]=0;
        for(int i=l+1;i<r;i++) {
                if(i-l>=M&&str[i]<=c) {
 //                       printf("insert erase\n");
                        S.erase(S.find(now[i-M]));
   //                     printf("insert erase\n");
                }
                if(c!=str[i]) continue;
                int t=*S.begin();
                now[i]=t+1;
                S.insert(now[i]);
                if(r-i<=M) return now[i];
        }
        return 0;
}

char out[maxn];
int L;
void find_min(int m) {
        //printf("::\n");
        char c=m+'a';
        int s=0;
        for(int p=0,r=1;r<=N+1;r++) {
         //       printf("%d %d \n", p, r);
                while(str[r]>=c) r++;
                if(r!=N+1) out[L++]=str[r]-'a';
                //printf("%d %d\n", p, r);
                if(r-p<=M) {
                        p=r;
                        continue;
                }
                s+=work(p,r,c);
                //printf("s %d after work\n", s);
                p=r;
        }
        for(int i=0;i<s;i++) out[L++]=m;
        sort(out,out+L);
        for(int i=0;i<L;i++) putchar('a'+out[i]); puts("");
}

int main(int argc, char *argv[]) {
        if(argc>1) {
                freopen( argv[1], "r", stdin );
        }
        cin>>M;
        scanf("%s", str+1);
        N=strlen(str+1);
        //printf("%d %d \n", N, M);
        int s=-1;
        for(int i=0;i<26;i++) {
                //printf("i %d\n", i);
                if(can_end(i)) {
                        find_min(i);
                        break;
                }
        }
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 5e5+5;
const int maxm = 2e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};

int N, M, K;
char str[maxn];

bool can_end(int m) {
        char c=m+'a';
        for(int i=1,p=0;i<=N+1;i++) {
                if(str[i]<=c) {
                        if(i-p>M){
                                return 0;
                        }
                        p=i;
                }
        }
        return 1;
}

int now[maxn];
multiset<int>S;
int work(int l,int r,char c) {
        //printf("%d %d %c\n", l, r, c );
        S.clear(); S.insert(0);
        now[l]=0;
        for(int i=l+1;i<r;i++) {
                if(c==str[i]) {
                        int t=*S.begin();
                        now[i]=t+1;
                        S.insert(now[i]);
                        if(r-i<=M) return now[i];
                }
                if(i-l>=M&&str[i-1]<=c) {
                        S.erase(S.find(now[i-M]));
                }
        }
        return 0;
}

int out[maxn];
int L;
void find_min(int m) {
        //printf("::\n");
        char c=m+'a';
        int s=0;
        for(int p=0,r=1;r<=N+1;r++) {
                //       printf("%d %d \n", p, r);
                while(str[r]>=c) r++;
                if(r!=N+1) out[L++]=str[r]-'a';
                //printf("%d %d\n", p, r);
                if(r-p<=M) {
                        p=r;
                        continue;
                }
                s+=work(p,r,c);
                //printf("s %d after work\n", s);
                p=r;
        }
        for(int i=0;i<s;i++) out[L++]=m;
        sort(out,out+L);
        for(int i=0;i<L;i++) putchar('a'+out[i]); puts("");
}

int main(int argc, char *argv[]) {
        if(argc>1) {
                freopen( argv[1], "r", stdin );
        }
        cin>>M;
        scanf("%s", str+1);
        N=strlen(str+1);
        //printf("%d %d \n", N, M);
        int s=-1;
        for(int i=0;i<26;i++) {
                //printf("i %d\n", i);
                if(can_end(i)) {
                        find_min(i);
                        break;
                }
        }
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 5e5+5;
const int maxm = 2e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};

int N, M, K;
char str[maxn];

bool can_end(int m) {
        char c=m+'a';
        for(int i=1,p=0;i<=N+1;i++) {
                if(str[i]<=c) {
                        if(i-p>M){
                                return 0;
                        }
                        p=i;
                }
        }
        return 1;
}

int now[maxn];
multiset<int>S;
int work(int l,int r,char c) {
        //printf("%d %d %c\n", l, r, c );
        S.clear(); S.insert(0);
        now[l]=0;
        for(int i=l+1;i<r;i++) {
                if(i-l>=M&&str[i-1]<=c) {
 //                       printf("insert erase\n");
                        S.erase(S.find(now[i-M]));
   //                     printf("insert erase\n");
                }
                if(c!=str[i]) continue;
                int t=*S.begin();
                now[i]=t+1;
                S.insert(now[i]);
                if(r-i<=M) return now[i];
        }
        return 0;
}

char out[maxn];
int L;
void find_min(int m) {
        //printf("::\n");
        char c=m+'a';
        int s=0;
        for(int p=0,r=1;r<=N+1;r++) {
         //       printf("%d %d \n", p, r);
                while(str[r]>=c) r++;
                if(r!=N+1) out[L++]=str[r]-'a';
                //printf("%d %d\n", p, r);
                if(r-p<=M) {
                        p=r;
                        continue;
                }
                s+=work(p,r,c);
                //printf("s %d after work\n", s);
                p=r;
        }
        for(int i=0;i<s;i++) out[L++]=m;
        sort(out,out+L);
        for(int i=0;i<L;i++) putchar('a'+out[i]); puts("");
}

int main(int argc, char *argv[]) {
        if(argc>1) {
                freopen( argv[1], "r", stdin );
        }
        cin>>M;
        scanf("%s", str+1);
        N=strlen(str+1);
        //printf("%d %d \n", N, M);
        int s=-1;
        for(int i=0;i<26;i++) {
                //printf("i %d\n", i);
                if(can_end(i)) {
                        find_min(i);
                        break;
                }
        }
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int maxn = 5e5+5;
const int maxm = 2e5+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};

int N, M, K;
char str[maxn];

bool can_end(int m) {
        char c=m+'a';
        for(int i=1,p=0;i<=N+1;i++) {
                if(str[i]<=c) {
                        if(i-p>M){
                                return 0;
                        }
                        p=i;
                }
        }
        return 1;
}

int now[maxn];
multiset<int>S;
int work(int l,int r,char c) {
        //printf("%d %d %c\n", l, r, c );
        S.clear(); S.insert(0);
        now[l]=0;
        for(int i=l+1;i<r;i++) {
                if(c==str[i]) {
                        int t=*S.begin();
                        now[i]=t+1;
                        S.insert(now[i]);
                        if(r-i<=M) return now[i];
                }
                if(i-l>=M&&str[i-M]<=c) {
                        S.erase(S.find(now[i-M]));
                }
        }
        return 0;
}

int out[maxn];
int L;
void find_min(int m) {
        //printf("::\n");
        char c=m+'a';
        int s=0;
        for(int p=0,r=1;r<=N+1;r++) {
                //       printf("%d %d \n", p, r);
                while(str[r]>=c) r++;
                if(r!=N+1) out[L++]=str[r]-'a';
                //printf("%d %d\n", p, r);
                if(r-p<=M) {
                        p=r;
                        continue;
                }
                s+=work(p,r,c);
                //printf("s %d after work\n", s);
                p=r;
        }
        for(int i=0;i<s;i++) out[L++]=m;
        sort(out,out+L);
        for(int i=0;i<L;i++) putchar('a'+out[i]); puts("");
}

int main(int argc, char *argv[]) {
        if(argc>1) {
                freopen( argv[1], "r", stdin );
        }
        cin>>M;
        scanf("%s", str+1);
        N=strlen(str+1);
        //printf("%d %d \n", N, M);
        int s=-1;
        for(int i=0;i<26;i++) {
                //printf("i %d\n", i);
                if(can_end(i)) {
                        find_min(i);
                        break;
                }
        }
        return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define inf INT_MAX
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);
#define trace1(x) cerr << #x << " is " << x << endl;
#define trace2(x , y) cerr << #x << " is " << x << "  " << #y << " is " << y << endl;
using namespace std;
typedef long long int LL;

int used[100010] , window , len , last_used , indexx;
char c;

int main()
{
si(window);
string ip;
cin >> ip;
len = ip.length();
ip = "#" + ip;
int used_in , cannot = 0;
string ans = "";
for(int x=1;x<=26;x++)
{

	c = x - 1 + 'a';
	last_used = 0;
	used_in = 0;
	cannot = 0;
	for(int i=1;i<=len;i++)
	{
		if(used[i] == 1)
			last_used = i;
		if(ip[i] == c)
			indexx = i;

		if(i - last_used >= window)
		{
			last_used = indexx;
			if(i - indexx >= window)
			{
				cannot = 1;
				break;
			}
			else{
				used_in++;
			}
		}

	}
	if(cannot == 1)
	{
		for(int i=1;i<=len;i++)
		{
			if(ip[i] == c){
				used[i] = 1;
				ans = ans + c;
			}
		}
	}
	else
	{
		for(int i=1;i<=used_in;i++)
			ans = ans + c;
		break;
	}
}

cout << ans << endl;

return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define inf INT_MAX
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);
#define trace1(x) cerr << #x << " is " << x << endl;
#define trace2(x , y) cerr << #x << " is " << x << "  " << #y << " is " << y << endl;
using namespace std;
typedef long long int LL;

int BIT[2000010] , len;

void update(int index , int val){
	while(index < 1000000){
		BIT[index]+=val;
		index+=(index&(-index));
	}
}

int query(int index){
	int ret = 0;
	while(index > 0){
		ret = ret + BIT[index];
		index-=(index&(-index));
	}
	return ret;
}

int q2(int x , int y){
	return query(y) - query(x-1);
}

std::vector<int > indexes[30];
int window;

int nextOne(int curr)
{
	int st , en , ans = -1 , md , x;
	st = curr + 1;
	en = len;
	x = query(curr);
	while(st <= en)
	{
		md = st + (en - st)/2;
		if(query(md) == x)
			st = md + 1;
		else{
			ans = md;
			en = md - 1;
		}
	}
	if(ans != -1)
		return ans;
	return len + 1;
}

int prevOne(int curr)
{
	int st , en , md , x , ans = -1;
	st = 1;
	en = curr - 1;
	x = query(curr);

	while(st <= en)
	{
		md = st + (en - st)/2;
		// trace2(md , curr);

		// trace1(q2(md , curr));
		if(q2(md , curr)!=0)
		{
			ans = md;
			st = md + 1;
		}
		else
			en = md - 1;
	}
	if(ans!=-1)
		return ans;
	return 0;
}

int main()
{
si(window);
string ip;
cin >> ip;
len = ip.length();
ip = "#" + ip;

for(int i=1;i<=len;i++)
{
	indexes[ip[i] - 'a' + 1].pb(i);
}

string ans = "";
int x , y , curr , nxt , prv;
char c;
int used[30];
memset(used , 0 , sizeof(used));

for(int i=1;i<=26;i++)
{
	for(int j=0;j<indexes[i].size();j++)
	{
		curr = indexes[i][j];
		nxt = nextOne(curr);
		prv = prevOne(curr);
		if( (nxt - prv) >= (window + 1))
		{
			c = i + 'a' - 1;
			ans = ans + c;
			update(curr , 1);
			used[i]++;
		}
	}
}

// cout << used['n' - 'a' + 1] << " # $ # " << indexes['n' - 'a' +1].size() << endl;
int flag = 0;

for(int j=1;j<=26;j++)
{
	flag = 0;
	for(int k=j+1;k<=26;k++)
	{
		if(used[k] > 0)
			flag = 1;
	}
	if(flag == 1)
	{
		prv = j-1;
		c = prv - 1 + 'a';
		// trace1(c);
		// trace2(used[prv] , indexes[prv].size());
		for(int k = used[prv]; k < indexes[prv].size() ; k++)
		{
			c = prv - 1 + 'a';
			ans = ans + c ;
		}
	}
}

sort(ans.begin() , ans.end());

cout << ans << endl;
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define inf INT_MAX
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);
#define trace1(x) cerr << #x << " is " << x << endl;
#define trace2(x , y) cerr << #x << " is " << x << "  " << #y << " is " << y << endl;
using namespace std;
typedef long long int LL;

int BIT[2000010] , len;

void update(int index , int val){
	while(index < 1000000){
		BIT[index]+=val;
		index+=(index&(-index));
	}
}

int query(int index){
	int ret = 0;
	while(index > 0){
		ret = ret + BIT[index];
		index-=(index&(-index));
	}
	return ret;
}

int q2(int x , int y){
	return query(y) - query(x-1);
}

std::vector<int > indexes[30];
int window;

int nextOne(int curr)
{
	int st , en , ans = -1 , md , x;
	st = curr + 1;
	en = len;
	x = query(curr);
	while(st <= en)
	{
		md = st + (en - st)/2;
		if(query(md) == x)
			st = md + 1;
		else{
			ans = md;
			en = md - 1;
		}
	}
	if(ans != -1)
		return ans;
	return len + 1;
}

int prevOne(int curr)
{
	int st , en , md , x , ans = -1;
	st = 1;
	en = curr - 1;
	x = query(curr);

	while(st <= en)
	{
		md = st + (en - st)/2;
		// trace2(md , curr);

		// trace1(q2(md , curr));
		if(q2(md , curr)!=0)
		{
			ans = md;
			st = md + 1;
		}
		else
			en = md - 1;
	}
	if(ans!=-1)
		return ans;
	return 0;
}

int main()
{
si(window);
string ip;
cin >> ip;
len = ip.length();
ip = "#" + ip;

for(int i=1;i<=len;i++)
{
	indexes[ip[i] - 'a' + 1].pb(i);
}

string ans = "";
int x , y , curr , nxt , prv;
char c;

for(int i=1;i<=26;i++)
{
	for(int j=0;j<indexes[i].size();j++)
	{
		// cout << "  $$$   " << endl;
		curr = indexes[i][j];
		nxt = nextOne(curr);
		prv = prevOne(curr);
		// cout << "curr : " << curr << "   nxt : " << nxt << "  prev : " << prv << endl;
		if( (curr - prv >= window) || (nxt - curr >= window) )
		{
			c = i + 'a' - 1;
			ans = ans + c;
			update(curr , 1);
		}
	}
}

cout << ans << endl;
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define inf INT_MAX
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);
#define trace1(x) cerr << #x << " is " << x << endl;
#define trace2(x , y) cerr << #x << " is " << x << "  " << #y << " is " << y << endl;
using namespace std;
typedef long long int LL;

int BIT[2000010] , len;

void update(int index , int val){
	while(index < 1000000){
		BIT[index]+=val;
		index+=(index&(-index));
	}
}

int query(int index){
	int ret = 0;
	while(index > 0){
		ret = ret + BIT[index];
		index-=(index&(-index));
	}
	return ret;
}

int q2(int x , int y){
	return query(y) - query(x-1);
}

std::vector<int > indexes[30];
int window;

int nextOne(int curr)
{
	int st , en , ans = -1 , md , x;
	st = curr + 1;
	en = len;
	x = query(curr);
	while(st <= en)
	{
		md = st + (en - st)/2;
		if(query(md) == x)
			st = md + 1;
		else{
			ans = md;
			en = md - 1;
		}
	}
	if(ans != -1)
		return ans;
	return len + 1;
}

int prevOne(int curr)
{
	int st , en , md , x , ans = -1;
	st = 1;
	en = curr - 1;
	x = query(curr);

	while(st <= en)
	{
		md = st + (en - st)/2;
		// trace2(md , curr);

		// trace1(q2(md , curr));
		if(q2(md , curr)!=0)
		{
			ans = md;
			st = md + 1;
		}
		else
			en = md - 1;
	}
	if(ans!=-1)
		return ans;
	return 0;
}

int main()
{
si(window);
string ip;
cin >> ip;
len = ip.length();
ip = "#" + ip;

for(int i=1;i<=len;i++)
{
	indexes[ip[i] - 'a' + 1].pb(i);
}

string ans = "";
int x , y , curr , nxt , prv;
char c;
int used[30];
memset(used , 0 , sizeof(used));

for(int i=1;i<=26;i++)
{
	for(int j=0;j<indexes[i].size();j++)
	{
		curr = indexes[i][j];
		nxt = nextOne(curr);
		prv = prevOne(curr);
		if( (nxt - prv) >= (window + 1))
		{
			c = i + 'a' - 1;
			ans = ans + c;
			update(curr , 1);
			used[i]++;
		}
	}
}

// cout << used['n' - 'a' + 1] << " # $ # " << indexes['n' - 'a' +1].size() << endl;
int flag = 0;

for(int j=1;j<=26;j++)
{
	flag = 0;
	for(int k=j+1;k<=26;k++)
	{
		if(used[k] > 0)
			flag = 1;
	}
	if(flag == 1)
	{
		c = j - 1 + 'a';
		// trace1(c);
		// trace2(used[prv] , indexes[prv].size());
		for(int k = used[j]; k < indexes[j].size() ; k++)
		{
			c = j - 1 + 'a';
			ans = ans + c ;
		}
	}
}

sort(ans.begin() , ans.end());

cout << ans << endl;
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define INF_LL 9223372036854775807LL
#define INF 2000000000
#define PI acos(-1.0)
#define inf INT_MAX
#define llu unsigned long long int
#define all(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define si(n) scanf("%d",&n)
#define slli(n) scanf("%lld",&n);
#define ss(n) scanf("%s",n);
#define trace1(x) cerr << #x << " is " << x << endl;
#define trace2(x , y) cerr << #x << " is " << x << "  " << #y << " is " << y << endl;
using namespace std;
typedef long long int LL;

int BIT[2000010] , len;

void update(int index , int val){
	while(index < 1000000){
		BIT[index]+=val;
		index+=(index&(-index));
	}
}

int query(int index){
	int ret = 0;
	while(index > 0){
		ret = ret + BIT[index];
		index-=(index&(-index));
	}
	return ret;
}

int q2(int x , int y){
	return query(y) - query(x-1);
}

std::vector<int > indexes[30];
int window;

int nextOne(int curr)
{
	int st , en , ans = -1 , md , x;
	st = curr + 1;
	en = len;
	x = query(curr);
	while(st <= en)
	{
		md = st + (en - st)/2;
		if(query(md) == x)
			st = md + 1;
		else{
			ans = md;
			en = md - 1;
		}
	}
	if(ans != -1)
		return ans;
	return len + 1;
}

int prevOne(int curr)
{
	int st , en , md , x , ans = -1;
	st = 1;
	en = curr - 1;
	x = query(curr);

	while(st <= en)
	{
		md = st + (en - st)/2;
		// trace2(md , curr);

		// trace1(q2(md , curr));
		if(q2(md , curr)!=0)
		{
			ans = md;
			st = md + 1;
		}
		else
			en = md - 1;
	}
	if(ans!=-1)
		return ans;
	return 0;
}

int main()
{
si(window);
string ip;
cin >> ip;
len = ip.length();
ip = "#" + ip;

for(int i=1;i<=len;i++)
{
	indexes[ip[i] - 'a' + 1].pb(i);
}

string ans = "";
int x , y , curr , nxt , prv;
char c;

for(int i=1;i<=26;i++)
{
	for(int j=0;j<indexes[i].size();j++)
	{
		// cout << "  $$$   " << endl;
		curr = indexes[i][j];
		nxt = nextOne(curr);
		prv = prevOne(curr);
		// cout << "curr : " << curr << "   nxt : " << nxt << "  prev : " << prv << endl;
		// trace1(curr);
		// trace2(nxt - prv , 2*window);
		// trace2(nx)
		if( (nxt - prv) >= (window + 1) )
		{
			c = i + 'a' - 1;
			ans = ans + c;
			update(curr , 1);
		}
	}
}

cout << ans << endl;
return 0;
}