#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){

    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL tmp=gcd(a,b);
    if(n==1){
        if(tmp==1){
            cout<<-1<<endl;
        }else{
            cout<<tmp<<endl;
        }
        return 0;
    }
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+2,tb=sqrt(b)+2;
    while(a!=1&&now<=min(a,ta)){
        if(a%now==0){
            vec.push_back(now);
            vec.push_back(a/now);
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=min(b,tb)){
        if(b%now==0){
            vec.push_back(now);
            vec.push_back(b/now);
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL tmp=gcd(a,b);
    if(n==1){
        cout<<a<<endl;
        return 0;
    }
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+2,tb=sqrt(b)+2;
    while(a!=1&&now<=ta){
        if(a%now==0){
            vec.push_back(now);
            vec.push_back(a/now);
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            vec.push_back(now);
            vec.push_back(b/now);
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){

    int n;
    cin>>n;
    LL a,b;
    scanf("%lld%lld",&a,&b);
    LL tmp=gcd(a,b);
    if(n==1){
        cout<<a<<endl;
        return 0;
    }
    LL now=2;
    vec.push_back(a);
    vec.push_back(b);
    LL ta=sqrt(a)+2,tb=sqrt(b)+2;
    while(a!=1&&now<=ta){
        if(a%now==0){
            if(a!=1)
            vec.push_back(now);
            if(a/now!=1)
            vec.push_back(a/now);
        }
        ++now;
    }
    now=2;
    while(b!=1&&now<=tb){
        if(b%now==0){
            if(b!=1)
            vec.push_back(now);
            if(b/now!=1)
            vec.push_back(b/now);
        }
        ++now;
    }
    for(int i=1;i<n;++i){
        scanf("%lld%lld",&a,&b);
        for(int j=0;j<vec.size();){
            if(a%vec[j]!=0&&b%vec[j]!=0){
                vec.erase(vec.begin()+j);
            }else{
                ++j;
            }
        }
    }
    if(!vec.empty()){
        cout<<vec[0]<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef long long LL;
vector<LL> vec;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    int n;
    cin>>n;
    LL a,b,tmp;
    for(int i=0;i<n;++i){
        scanf("%lld%lld",&a,&b);
        if(i==0){
            tmp=a*b/gcd(a,b);
        }else{
            tmp=gcd(tmp,a*b/gcd(a,b));
        }
    }

    if(tmp==1){
        cout<<-1<<endl;
    }else{
        for(int i=2;i<=sqrt(tmp)+1;++i){
            if(tmp%i==0){
                cout<<i<<endl;
                return 0;
            }
        }

    }
    return 0;
}


#include <bits/stdc++.h>
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define MAXN
using namespace std;
typedef unsigned long long LL;

LL gcd(LL a, LL b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

int main(){
    int n;
    cin>>n;
    LL a,b,tmp,t;
    for(int i=0;i<n;++i){
        scanf("%I64d%I64d",&a,&b);
        t=a*b/gcd(a,b);
        if(i==0){
            tmp=t;
        }else{
            tmp=gcd(tmp,t);
        }
    }

    if(tmp==1){
        cout<<-1<<endl;
        return 0;
    }else{
        for(LL i=2;i<=sqrt(tmp)+1;++i){
            if(tmp%i==0){
                cout<<i<<endl;
                return 0;
            }
        }
    }
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#include <cmath>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		if (p[0].fr == 1 && p[0].sc == 1) {
			cout << "-1";
		}
		else {
			cout << max(p[0].fr, p[0].sc);
		}
		return 0;
	}
	ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = 2; j <= int(sqrt(ans)); ++j) {
				if (ans % j == 0) {
					ans /= j;
					break;
				}
			}
		}
	}
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
	}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
mt19937 azino(777);
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector <pair<int, int> > a(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
		while (a[i].first % ans != 0 && a[i].second % ans != 0) {
			int r = azino();
			if (r % 2 == 0){
				ans = gcd(ans, a[i].first);
			}
			else ans = gcd(ans, a[i].second);
		}
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		if (p[0].fr == 1 && p[0].sc == 1) {
			cout << "-1";
		}
		else {
			cout << max(p[0].fr, p[0].sc);
		}
		return 0;
	}
	ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	/*for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = 2; j <= int(sqrt(ans)); ++j) {
				if (ans % j == 0) {
					ans /= j;
					break;
				}
			}
		}
	}*/
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
	}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
mt19937 azino(777);
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
vector <pair<int, int> > a;
bool check(int x) {
	for (int i = 0; i < a.size(); ++i) {
		if (a[i].first % x != 0 && a[i].second % x != 0) return false;
	}
	return true;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	a.resize(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (ans == 1 || !check(ans)) {
		ans = nok[0];
		for (int i = 1; i < n; ++i) {
			ans = gcd(ans, nok[i]);
			while (a[i].first % ans != 0 && a[i].second % ans != 0) {
				int r = azino();
				if (r % 2 == 0) {
					ans = gcd(ans, a[i].first);
				}
				else ans = gcd(ans, a[i].second);
			}
		}
		if (ans == 1 || !check(ans)) {
			ans = nok[0];
			for (int i = 0; i < n; ++i) {
				ans = gcd(ans, nok[i]);
				while (a[i].first % ans != 0 && a[i].second % ans != 0) {
					int r = azino();
					if (r % 2 == 0) {
						ans = gcd(ans, a[i].first);
					}
					else ans = gcd(ans, a[i].second);
				}
			}
		}
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#include <cmath>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		if (p[0].fr == 1 && p[0].sc == 1) {
			cout << "-1";
		}
		else {
			cout << max(p[0].fr, p[0].sc);
		}
		return 0;
	}
	ans = gcd(a[0], a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = 2; j <= sqrt(ans); ++j) {
				if (ans % j == 0 && (p[i].fr % (ans/j) == 0) && (p[i].sc % (ans / j) == 0)) {
					ans /= j;
					break;
				}
			}
		}
	}
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		if (p[0].fr == 1 && p[0].sc == 1) {
			cout << "-1";
		}
		else {
			cout << max(p[0].fr, p[0].sc);
		}
		return 0;
	}
	ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = 2; j <= int(sqrt(ans)); ++j) {
				if (ans % j == 0) {
					ans /= j;
					break;
				}
			}
		}
	}
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
	}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#include <cmath>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		if (p[0].fr == 1 && p[0].sc == 1) {
			cout << "-1";
		}
		else {
			cout << max(p[0].fr, p[0].sc);
		}
		return 0;
	}
	ans = gcd(a[0], a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = sqrt(ans); j >= 2; --j) {
				if (ans % j == 0) {
					ans /= j;
					break;
				}
			}
		}
	}
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
mt19937 azino(777);
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
vector <pair<int, int> > a;
bool check(int x) {
	for (int i = 0; i < a.size(); ++i) {
		if (a[i].first % x != 0 && a[i].second % x != 0) return false;
	}
	return true;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	a.resize(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (ans == 1 || !check(ans)) {
		ans = nok[0];
		for (int i = 1; i < n; ++i) {
			ans = gcd(ans, nok[i]);
			while (a[i].first % ans != 0 && a[i].second % ans != 0) {
				int r = azino();
				if (r % 2 == 0) {
					ans = gcd(ans, a[i].first);
				}
				else ans = gcd(ans, a[i].second);
			}
		}
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
//#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
signed main()
{
	#ifdef _DEBUG
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector <pair<int, int> > a(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 0; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (n == 1) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
mt19937 azino(777);
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
vector <pair<int, int> > a;
bool check(int x) {
	for (int i = 0; i < a.size(); ++i) {
		if (a[i].first % x != 0 && a[i].second % x != 0) return false;
	}
	return true;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	a.resize(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (ans == 1 || !check(ans)) {
		ans = nok[0];
		for (int i = 0; i < n; ++i) {
			ans = gcd(ans, nok[i]);
			while (a[i].first % ans != 0 && a[i].second % ans != 0) {
				int r = azino();
				if (r % 2 == 0) {
					ans = gcd(ans, a[i].first);
				}
				else ans = gcd(ans, a[i].second);
			}
		}
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
signed main()
{
	#ifdef _DEBUG
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector <pair<int, int> > a(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#prag	ma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iomanip>
#include <unordered_map>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <set>
#include <bitset>
#include <map>
#include <iterator>
#include <utility>
#include <queue>
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define int long long
using namespace std;
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	cin >> n;
	vector <pair<int, int> > p(n);
	vector <int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> p[i].fr >> p[i].sc;
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		a[i] = (p[i].fr * p[i].sc) / gcd(p[i].fr, p[i].sc);
	}
	if (n == 1) {
		cout << p[0].fr;
		return 0;
	}
	ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; ++i) {
		ans = gcd(a[i], ans);
	}
	/*for (int i = 0; i < n; ++i) {
		if (p[i].fr % ans != 0 && p[i].sc % ans != 0) {
			for (int j = 2; j <= int(sqrt(ans)); ++j) {
				if (ans % j == 0) {
					ans /= j;
					break;
				}
			}
		}
	}*/
	if (ans == 1) {
		cout << "-1";
		return 0;
	}
	cout << ans;
	}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
signed main()
{
	#ifdef _DEBUG
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector <pair<int, int> > a(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
	}
	if (n == 1) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
/*#pragma GCC optimize ("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iomanip>
#include <map>
#include <cmath>
#include <bitset>
#include <utility>
#include <queue>
#include <iterator>
#include <climits>
#include <random>
#include <sstream>
using namespace std;
mt19937 azino(778);
#define int long long
#define pb push_back
const int eps = 1e-7;
const int pi = acos(-1);
const int q = 1e9 + 7;
string inttostr(int a) {
	string ans = "";
	if (a == 0) return "0";
	stringstream ss;
	while (a > 0) {
		string ch;
		ss << a % 10;
		ss >> ch;
		ss.clear();
		ans = ch + ans;
		a /= 10;
	}
	return ans;
}
int strtoint(string s) {
	int ans = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		int o = s[i] - '0';
		if (s[i] == 0) o = 0;
		ans += o * pow(10, s.size() - 1 - i);
	}
	return ans;
}
int gcd(int a, int b) {
	int r;
	while (b != 0) {
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}
int lcm(int a, int b) {
	return (a * b) / gcd(a, b);
}
int fastpow(int a, int step) {
	if (step == 0) return 1;
	else if (step % 2 == 0) return (fastpow((a*a) % q, step / 2) % q);
	else return ((fastpow(a, step - 1) % q * a) % q);
}
signed main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	vector <pair<int, int> > a(n);
	vector <int> nok(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i].first >> a[i].second;
		nok[i] = lcm(a[i].first, a[i].second);
	}
	int ans = nok[0];
	for (int i = 1; i < n; ++i) {
		ans = gcd(ans, nok[i]);
		while (a[i].first % ans != 0 && a[i].second % ans != 0) {
			int r = azino();
			if (r % 2 == 0){
				ans = gcd(ans, a[i].first);
			}
			else ans = gcd(ans, a[i].second);
		}
	}
	if (ans == nok[0]) cout << a[0].first;
	else if (ans == 1) cout << "-1";
	else cout << ans;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	b.push_back(x);
	b.push_back(y);
	for(int i=3;i<=sqrt(k);i+=2){
		if(x%i == 0){
			b.push_back(i);
			if(x/i != i){
				b.push_back(x/i);
			}
		}
		if(y%i == 0){
			b.push_back(i);
			if(y/i != i){
				b.push_back(y/i);
			}
		}
	}
	vc<ll>d(b.size(),1);
	fr(n){
		ll x,y;
		cn >> x >> y;
		for(int j=0; j<b.size();j++){
			if(x%b[j] == 0 || y%b[j]==0){
				d[j]++;
				if(d[j] == n+1){
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
	rt 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
ll binpow(ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(a, n - 1) * a;
	else {
		ll b = binpow(a, n / 2);
		return b * b;
	}
}
int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	ll q;
	cin >> q;
	ll n = q;
	q--;
	ll x, y;
	cin >> x >> y;
	if (q == 0) {
		if (x < y) {
			swap(x, y);
		}
		if (x%y == 0) {
			ct << x / y;
		}
		else {
			ct << -1;
		}
		return 0;
	}
	vc<ll>a;
	ll x1 = y;
	if (y % 2 == 0) {
		while (y % 2 != 1) {
			y /= 2;
		}
		a.push_back(2);
	}
	for (int i = 3; i <= sqrt(x1); i += 2) {
		if (y%i == 0) {
			while (y % i != 0) {
				y /= i;
			}
			a.push_back(i);
		}
	}
	ll y1 = x;
	if (x % 2 == 0) {
		while (x % 2 != 1) {
			x /= 2;
		}
		a.push_back(2);
	}
	for (int i = 3; i <= sqrt(y1); i += 2) {
		if (x%i == 0) {
			while (x % i != 0) {
				x /= i;
			}
			a.push_back(i);
		}
	}
	vc<ll>b(a.size());
	ll mx = 0;
	ll ans = 0;
	while (q--) {
		cin >> x >> y;
		bool c = 0;
		for (int i = 0; i < a.size(); i++) {
			if (x%a[i] == 0 || y%a[i] == 0) {
				b[i]++;
				if (b[i] > mx) {
					mx = b[i];
					ans = i;
				}
				c = 1;
			}
		}
		if (c == 0) {
			ct << -1; return 0;
		}
	}
	if (mx + 1 == n) {
		ct << a[ans];
	}
	else {
		ct << -1;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll long long
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		vc<ll>a;
		for (auto c : s) {
			if (x%c.first == 0 || y%c.first == 0) {
				a.push_back(c.first);
			}
		}
		for (auto to : a) {
			s[to]++;
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
ll binpow(ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(a, n - 1) * a;
	else {
		ll b = binpow(a, n / 2);
		return b * b;
	}
}
void fact(ll x, vc<ll>&s) {
	ll n = x;
	for (int i = 3; i*i <= n; i++) {
		while (x%i == 0) {
			x /= i;
			s.push_back(i);
		}
	}
	if (x != 1) {
		s.push_back(x);
	}
}
int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	ll q;
	cin >> q;
	ll n = q;
	q--;
	ll x, y;
	cin >> x >> y;
	if (q == 0) {
		if (x < y) {
			swap(x, y);
		}
		CT << x / y;
		return 0;
	}
	vc<ll>a;
	fact(x, a);
	fact(y, a);
	ll mx = 0, ans = 0;
	vc<ll>b(a.size());
	while (q--) {
		cin >> x >> y;
		bool c = 0;
		for (int i = 0; i < a.size(); i++) {
			if (x%a[i] == 0 || y%a[i] == 0) {
				b[i]++;
				if (b[i] > mx) {
					mx = b[i];
					ans = i;
				}
				c = 1;
			}
		}
		if (c == 0) {
			ct << -1; return 0;
		}
	}
	if (mx + 1 == n) {
		ct << a[ans];
	}
	else {
		ct << -1;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for (auto to : a) {
		b.push_back(to);
	}
	rv(all(b));
	b.pop_back();
	rv(all(b));
	ll p = b.size();
	vc<ll>d(p, 1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p; j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				break;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll long long
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 1; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		vc<ll>a;
		for (auto c : s) {
			if (x%c.first == 0 || y%c.first == 0) {
				a.push_back(c.first);
			}
		}
		for (auto to : a) {
			s[to]++;
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n && c.first != 1) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 1; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		vc<ll>a;
		for (auto c : s) {
			if (x%c.first == 0 || y%c.first == 0) {
				a.push_back(c.first);
			}
		}
		for (auto to : a) {
			s[to]++;
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n && c.first != 1) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		set<ll>d;
		for (int i = 2; i <= sqrt(x); i++) {
			if (x % i == 0) {
				d.insert(i);
				d.insert(x/i);
			}
		}
		for (int i = 2; i <= sqrt(y); i++) {
			if (y % i == 0) {
				d.insert(i);
				d.insert(y / i);
			}
		}
		for(auto c:d){
			if (s.count(c) > 0) {
				s[c]++;
			}
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			b.push_back(i);
			b.push_back(x / i);
		}
		if (y%i == 0) {
			b.push_back(i);
			b.push_back(y / i);
		}
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if (x%b[j] == 0 || y%b[j] == 0 && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			b.push_back(i); b.push_back(x / i);
		}
		if (y%i == 0) {
			b.push_back(i); b.push_back(y / i);
		}
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
ll binpow(ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(a, n - 1) * a;
	else {
		ll b = binpow(a, n / 2);
		return b * b;
	}
}
void fact(ll x, vc<ll>&s) {
	ll n = x;
	for (int i = 2; i*i <= n; i++) {
		while (x%i == 0) {
			x /= i;
			s.push_back(i);
		}
	}
	if (x != 1) {
		s.push_back(x);
	}
}
int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	ll q;
	cin >> q;
	ll n = q;
	q--;
	ll x, y;
	cin >> x >> y;
	vc<ll>a;
	fact(x, a);
	fact(y, a);
	if (q == 0) {
		ct << x;
		return 0;
	}
	ll mx = 0, ans = 0;
	vc<ll>b(a.size());
	while (q--) {
		cin >> x >> y;
		bool c = 0;
		for (int i = 0; i < a.size(); i++) {
			if (x%a[i] == 0 || y%a[i] == 0) {
				b[i]++;
				if (b[i] > mx) {
					mx = b[i];
					ans = i;
				}
				c = 1;
			}
		}
		if (c == 0) {
			ct << -1; return 0;
		}
	}
	if (mx + 1 == n) {
		ct << a[ans];
	}
	else {
		ct << -1;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		for (auto c : s) {
			if (x%c.first == 0 || y%c.first == 0) {
				s[c.first]++;
			}
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				continue;
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
ll binpow(ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(a, n - 1) * a;
	else {
		ll b = binpow(a, n / 2);
		return b * b;
	}
}
void fact(ll x, vc<ll>&s) {
	ll n = x;
	for (int i = 3; i*i <= n; i++) {
		while (x%i == 0) {
			x /= i;
			s.push_back(i);
		}
	}
	if (x != 1) {
		s.push_back(x);
	}
}
int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	ll q;
	cin >> q;
	ll n = q;
	q--;
	ll x, y;
	cin >> x >> y;
	if (q == 0) {
		ct << x;
		return 0;
	}
	vc<ll>a;
	fact(x, a);
	fact(y, a);
	ll mx = 0, ans = 0;
	vc<ll>b(a.size());
	while (q--) {
		cin >> x >> y;
		bool c = 0;
		for (int i = 0; i < a.size(); i++) {
			if (x%a[i] == 0 || y%a[i] == 0) {
				b[i]++;
				if (b[i] > mx) {
					mx = b[i];
					ans = i;
				}
				c = 1;
			}
		}
		if (c == 0) {
			ct << -1; return 0;
		}
	}
	if (mx + 1 == n) {
		ct << a[ans];
	}
	else {
		ct << -1;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	b.push_back(x);
	b.push_back(y);
	for (int i = 2; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		ct << *a.begin();
		rt 0;
	}
	for (auto to : a) {
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p, 1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p; j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			b.push_back(i);
			b.push_back(x / i);
		}
		if (y%i == 0) {
			b.push_back(i);
			b.push_back(y / i);
		}
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if (x%b[j] == 0 || y%b[j] == 0) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		ct << *a.begin();
		rt 0;
	}
	for (auto to : a) {
		b.push_back(to);
	}
	rv(all(b));
	b.pop_back();
	rv(all(b));
	ll p = b.size();
	vc<ll>d(p, 1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p; j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
ll binpow(ll a, ll n) {
	if (n == 0)
		return 1;
	if (n % 2 == 1)
		return binpow(a, n - 1) * a;
	else {
		ll b = binpow(a, n / 2);
		return b * b;
	}
}
void fact(ll x, vc<ll>&s) {
	ll n = x;
	for (int i = 3; i*i <= n; i++) {
		while (x%i == 0) {
			x /= i;
			s.push_back(i);
		}
	}
	if (x != 1) {
		s.push_back(x);
	}
}
int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
	ll q;
	cin >> q;
	ll n = q;
	q--;
	ll x, y;
	cin >> x >> y;
	if (q == 0) {
		if (x < y) {
			swap(x, y);
		}
		if (x%y == 0) {
			ct << x / y;
		}
		else {
			ct << -1;
		}
		return 0;
	}
	vc<ll>a;
	fact(x, a);
	fact(y, a);
	ll mx = 0, ans = 0;
	vc<ll>b(a.size());
	while (q--) {
		cin >> x >> y;
		bool c = 0;
		for (int i = 0; i < a.size(); i++) {
			if (x%a[i] == 0 || y%a[i] == 0) {
				b[i]++;
				if (b[i] > mx) {
					mx = b[i];
					ans = i;
				}
				c = 1;
			}
		}
		if (c == 0) {
			ct << -1; return 0;
		}
	}
	if (mx + 1 == n) {
		ct << a[ans];
	}
	else {
		ct << -1;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll long long
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		for (auto c : s) {
			if (x%c.first == 0 || y%c.first == 0) {
				s[c.first]++;
			}
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			b.push_back(i); b.push_back(x / i);
		}
		if (y%i == 0) {
			b.push_back(i); b.push_back(y / i);
		}
	}
	if (n == 0) {
		ct << x;
		rt 0;
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		ct << *a.begin();
		rt 0;
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for (auto to : a) {
		b.push_back(to);
	}
	rv(all(b));
	b.pop_back();
	rv(all(b));
	ll p = b.size();
	vc<ll>d(p, 1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p; j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			b.push_back(i);
			b.push_back(x / i);
		}
		if (y%i == 0) {
			b.push_back(i);
			b.push_back(y / i);
		}
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		vc<ll>a;
		for (int j = 0; j < p;j++) {
			if (x%b[j] == 0 || y%b[j] == 0) {
				d[j]++;
			}
		}
	}
	n++;
	for (int i = 0; i < p;i++) {
		if (d[i] == n && b[i] != 1) {
			ct << b[i];
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll long long
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
map<ll, ll>s;
void gd(ll n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n % i == 0) {
			s[i] = 1;
			s[n/i] = 1;
		}
	}
}
int main() {
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	gd(x);
	gd(y);
	fr(n) {
		cn >> x >> y;
		set<ll>d;
		for (int i = 2; i <= sqrt(x); i++) {
			if (x % i == 0) {
				d.insert(i);
				d.insert(n/i);
			}
		}
		for(auto c:d){
			if (s.count(c) > 0) {
				s[c]++;
			}
		}
	}
	n++;
	for (auto c : s) {
		if (c.second == n) {
			ct << c.first;
			rt 0;
		}
	}
	ct << -1;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	b.push_back(x);
	b.push_back(y);
	if(x%2==0){
		b.push_back(2);
	}else{
		if(y%2==0){
		b.push_back(2);
		}
	}
	for(int i=3;i<=sqrt(k);i+=2){
		if(x%i == 0){
			b.push_back(i);
			if(x/i != i){
				b.push_back(x/i);
			}
		}
		if(y%i == 0){
			b.push_back(i);
			if(y/i != i){
				b.push_back(y/i);
			}
		}
	}
	if ( n == 0){
		ct << b[0];
		rt 0;
	}
	vc<ll>d(b.size(),1);
	fr(n){
		ll x,y;
		cn >> x >> y;
		for(int j=0; j<b.size();j++){
			if(x%b[j] == 0 || y%b[j]==0){
				d[j]++;
				if(d[j] == n+1){
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
	rt 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	b.push_back(x);
	b.push_back(y);
	for(int i=3;i<=sqrt(k);i+=2){
		if(x%i == 0){
			b.push_back(i);
			if(x/i != i){
				b.push_back(x/i);
			}
		}
		if(y%i == 0){
			b.push_back(i);
			if(y/i != i){
				b.push_back(y/i);
			}
		}
	}
	if ( n == 0){
		ct << b[0];
		rt 0;
	}
	vc<ll>d(b.size(),1);
	fr(n){
		ll x,y;
		cn >> x >> y;
		for(int j=0; j<b.size();j++){
			if(x%b[j] == 0 || y%b[j]==0){
				d[j]++;
				if(d[j] == n+1){
					ct << b[j];
					rt 0;
				}
			}
		}
	}
	ct << -1;
	rt 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <climits>
#define en "\n"
#define pr pair
#define mp make_pair
#define vc vector
#define rs resize
#define ll int
#define all(a) a.begin(),a.end()
#define sr stable_sort
#define ct cout
#define CT cout
#define cn cin
#define rv reverse
#define qa " "
#define rt return
#define fr(n) for(int i=0;i<n;i++)
#define lol(A,X) lower_bound((A).begin(),A.end(),X)-A.begin()
using namespace std;
int main() {
	cn.tie(0); ct.tie(0); ios_base::sync_with_stdio(false);
	ll n;
	cn >> n;
	n--;
	ll x, y;
	cn >> x >> y;
	ll k = max(x, y);
	vc<ll>b;
	set<ll>a;
	for (int i = 1; i <= sqrt(k); i++) {
		if (x%i == 0) {
			a.insert(i); a.insert(x / i);
		}
		if (y%i == 0) {
			a.insert(i); a.insert(y / i);
		}
	}
	if (n == 0) {
		a.erase(*a.begin());
		ct << *a.begin();
		rt 0;
	}
	for(auto to:a){
		b.push_back(to);
	}
	ll p = b.size();
	vc<ll>d(p,1);
	fr(n) {
		cn >> x >> y;
		ll c = 0;
		for (int j = 0; j < p;j++) {
			if ((x%b[j] == 0 || y%b[j] == 0) && b[j] != 1) {
				d[j]++;
				if (d[j] == n + 1) {
					ct << b[j];
					rt 0;
				}
				c = 1;
				continue;
			}
		}
		if (c != 1) {
			ct << -1;
		}
	}
	ct << -1;
}


 /*
       _________
      /         \
     /  _     _  \
    /   0     0   \
   |       ||     |
   |              | => HI
    \     -----   /
     \           /
      \_ _ _ _ _/
 */

#include<bits/stdc++.h>
using namespace std;
using lli = int64_t;
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T = 1;
	for(int t = 1; t <= T; t++){
		int n;
		cin >> n;
		vector<int> GCDs;
		for(int i = 0;i < n; i++){
			int inx,iny;
			cin >> inx >> iny;
			if(i == 0){
				GCDs.push_back(inx),GCDs.push_back(iny);
				continue;
			}
			vector<int> Temp_GCDs;
			for(int j = 0;j < (int) GCDs.size(); j++){
				if(GCDs[j] == 1){
					continue;
				}
				int x = GCDs[j];
				GCDs[j] = gcd(GCDs[j],inx);
				Temp_GCDs.push_back(gcd(x,iny));
			}
			for(int j = 0;j < (int) Temp_GCDs.size();j++){
				GCDs.push_back(Temp_GCDs[j]);
			}
			sort(GCDs.begin(),GCDs.end(),greater<int>());
			if(GCDs[0] == 1){
				cout << "-1\n";
				return 0;
			}
		}
		cout << GCDs[0] << "\n";
	}

}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{

 //freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

if (c[l]%kk!=0) ans=ans+(c[l]/kk+1); else
    ans=ans+(c[l]/kk);
k=c[l];
if (k!=1) {
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}

}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

if (kk!=1) { k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;} else ans=ans+c[l];
 if (l!=1)   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

    k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;
 if (l!=1)   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

if (kk!=1) { k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;} else ans=ans+(c[l]-1);
 if (l!=1)   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

 k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;
 if (l!=1||(cel<=1&&ost==0))   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <long long,long long> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

 k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;
 if (l!=1||(cel=1&&ost==0)||(cel==0&&ost!=0))   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <set>
#include <fstream>
#include <stdio.h>
using namespace std;
long long ans,z,kk,cel,ost,f,i,n,l,m,j,
                    t,k,p,pp,dl,a[111111],c[111111];
pair <int,int> mas[111111];
int main()
{
// freopen("censor.in","r",stdin);
 //freopen("censor.out","w",stdout);
cin>>n>>m>>kk;
for (i=1;i<=n;i++)
    {cin>>a[i];
   mas[i].first=a[i];mas[i].second=i;}
   sort(mas+1,mas+n+1);
for (i=1;i<=n;i++)
    c[i]=mas[i].second;

for (i=1;i<=m;i++)
{
cin>>l;

    k=c[l];
cel=k/kk;
ost=k%kk;
if (ost!=0) ans+=cel+1; else
    ans+=cel;
 if (l!=1)   {k=c[l];
    f=a[k-1];
    swap(a[k],a[k-1]);
    swap(c[f],c[l]);}
}
cout<<ans<<endl;

   return 0;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main() {
    C();
    return 0;
}

void C() {
    int n, m, k;
    sc3(n, m, k);
    int x;
    int arr[n + 1], Map[n + 1];
    loopi(i,1,n)
        sc(x), arr[i] = x, Map[x] = i;
    ll ans = 0;
    loopi(i,0,m-1)
    {
        sc(x);
        int pos = Map[x];
        ans += ((Map[x] - 1) / k + 1);
        if (pos > 1) {
            Map[arr[pos - 1]]++;
            Map[arr[pos]]--;
            swap(arr[pos], arr[pos - 1]);
        }
    }
    cout << ans << endl;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
    C();
    return 0;
}

void C(){
    int n,m,k;
    sc3(n,m,k);
    int x;
    int arr[n+1], Map[n+1];
    loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
    ll ans=0;
    loopi(i,0,m-1) {
        sc(x);
        ans+=((Map[x]-1)/k+1);
        if(x!=arr[1]){
            Map[arr[x-1]]=Map[x]--;
            int t = arr[x];
            arr[x]=arr[x-1];
            arr[x-1]=t;
        }
    }
    cout << ans <<endl;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
    C();
    return 0;
}

void C(){
    int n,m,k;
    sc3(n,m,k);
    int x;
    int arr[n+1], Map[n+1];
    loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
    ll ans=0;
    loopi(i,0,m-1) {
        sc(x);
        ans+=((Map[arr[x]]-1)/k+1);
        if(x!=1){
            Map[arr[x-1]]=Map[arr[x]]--;
            int t = arr[x];
            arr[x]=arr[x-1];
            arr[x-1]=t;
        }
    }
    cout << ans <<endl;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
    C();
    return 0;
}

void C(){
	int n,m,k;
	sc3(n,m,k);
	int x;
	int arr[n+1], Map[n+1];
	loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
	ll ans=0;
	loopi(i,0,m-1) {
		sc(x);
		ans+=((Map[x]-1)/k+1);
		if(x!=1){
			Map[arr[x-1]]=Map[arr[x]]--;
			int t = arr[x];
			arr[x]=arr[x-1];
			arr[x-1]=t;
		}
	}
	cout << ans <<endl;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
    C();
    return 0;
}

void C(){
    int n,m,k;
    sc3(n,m,k);
    int x;
    int arr[n+1], Map[n+1];
    loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
    ll ans=0;
    loopi(i,0,m-1) {
        sc(x);
        ans+=((Map[x]-1)/k+1);
        if(x!=arr[1]){
            Map[x]--;
            Map[arr[x-1]]=Map[x]+1;
            swap(arr[x],arr[x-1]);
        }
    }
    cout << ans;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//				  You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
	C();
	return 0;
}

void C(){
	int n,m,k;
	sc3(n,m,k);
	int x;
	int arr[n+1], Map[n+1];
	loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
	ll ans=0;
	loopi(i,0,m-1) {
		sc(x);
		ans+=((Map[x]-1)/k+1);
		if(x!=1){
			Map[x]--;
			Map[arr[x-1]]=Map[x]+1;
			swap(arr[x],arr[x-1]);
		}
	}
	cout << ans;
}


//============================================================================
// Name        : CF.cpp
// Author      : Mahmoud A.Gawad(RED_JOHN_G)
// Version     :
// Copyright   : This code is free to use.
//                You have the right to reuse/modify it.
// Description : Codeforces Rounds in C++
//============================================================================
#include <bits/stdc++.h> // Magic goes here! ;)
using namespace std;

typedef long long ll;

#define mem(arr,val) memset(arr,val,sizeof arr)
#define loopi(i,val,n) for(i=val;i<=n;i++)
#define loopd(i,val,val2) for(i=val;i>=val2;i--)

#define sz(vec) (int)vec.size()
#define sc(n) scanf("%d",&n)
#define scl(n) scanf("%I64d",&n)
#define sc2(n,m) scanf("%d %d",&n,&m)
#define sc3(n,m,x) scanf("%d %d %d",&n,&m,&x)
#define sc4(n,m,x,y) scanf("%d %d %d %d",&n,&m,&x,&y)
#define println(n) printf("%d\n",n)
#define line() printf("\n")
#define print(n) printf("%d",n)
#define print2(n,m) printf("%d %d",n,m)
#define print1(n) printf(" %d",n)
#define prints(s) printf("%s",s)

typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

int i, j, k;

void A();
void B();
void C();
void D();
void E();

int main(){
    C();
    return 0;
}

void C(){
    int n,m,k;
    sc3(n,m,k);
    int x;
    int arr[n+1], Map[n+1];
    loopi(i,1,n) sc(x), arr[i]=x, Map[x]=i;
    ll ans=0;
    loopi(i,0,m-1) {
        sc(x);
        ans+=((Map[x]-1)/k+1);
        if(Map[x]!=1){
            Map[x]--;
            Map[arr[x-1]]=Map[x]+1;
            swap(arr[x],arr[x-1]);
        }
    }
    cout << ans;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	mp[0] = 1;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += (mp[b[i]] % k) + 1;
		int tmp1 = mp[b[i] - 1], tmp2 = mp[b[i]];
		mp.erase(b[i] - 1);
		mp[b[i]] = tmp1;
		mp[b[i] - 1] = tmp2;
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp, mp_r;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
		mp_r.insert(make_pair(i + 1, a[i]));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += ((mp[b[i]] - 1) / k) + 1;
		if (mp[b[i]] - 1 == 0)
			continue;
		int tmp = mp_r[mp[b[i]] - 1], tmp2 = mp[b[i]];
		mp.erase(tmp);
		mp[b[i]]--;
		mp[tmp] = tmp2;
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += ((mp[b[i]] - 1) / k) + 1;
		//cout<< b[i]<< " "<< mp[b[i]]<< " "<<((mp[b[i]] - 1) / k) + 1<<endl;
		if (mp[b[i]] < 2)
			continue;
		int tmp = mp[b[i]], tmp2 = a[tmp - 2];
		//cout<< "TMP  "<< tmp<< " "<< tmp2<<endl;
		mp.erase(tmp2);
		//cout<< mp[b[i]]<< " fssdfsdf ";
		mp[b[i]]--;
		//cout<< mp[b[i]]<<endl;
		mp[tmp2] = tmp;
		swap(a[b[i]], a[b[i] - 1]);
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += (mp[b[i]] / k) + 1;
		//cout<< b[i]<< " "<< mp[b[i]]<< " "<<((mp[b[i]] - 1) / k) + 1<<endl;
		if (mp[b[i]] < 1)
			continue;
		int tmp = mp[b[i]], tmp2 = a[tmp - 1];
		//cout<< "TMP  "<< tmp<< " "<< tmp2<<endl;
		mp.erase(tmp2);
		//cout<< mp[b[i]]<< " fssdfsdf ";
		mp[b[i]]--;
		//cout<< mp[b[i]]<<endl;
		mp[tmp2] = tmp;
		swap(a[b[i]], a[b[i] - 1]);
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += ((mp[b[i]] - 1) / k) + 1;
		//cout<< b[i]<< " "<< mp[b[i]]<< " "<<((mp[b[i]] - 1) / k) + 1<<endl;
		if (mp[b[i]] - 1 == 0)
			continue;
		int tmp = mp[b[i]], tmp2 = a[tmp - 2];
		//cout<< "TMP  "<< tmp<< " "<< tmp2<<endl;
		mp.erase(tmp2);
		//cout<< mp[b[i]]<< " fssdfsdf ";
		mp[b[i]]--;
		//cout<< mp[b[i]]<<endl;
		mp[tmp2] = tmp;
		swap(a[b[i]], a[b[i] - 1]);
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	mp[0] = 1;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += (mp[b[i]] / k) + 1;
		int tmp1 = mp[b[i] - 1], tmp2 = mp[b[i]];
		mp.erase(b[i] - 1);
		mp[b[i]] = tmp1;
		mp[b[i] - 1] = tmp2;
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
    int n, m, k;
    cin>> n>> m>> k;
    long long res = 0;
    int a[n];
    map <int, int> mp;
    for (int i = 0; i < n; i++)
    {
        scanf("%i", &a[i]);
        mp.insert(make_pair(a[i], i));
    }
    int b[m];
    for (int i = 0; i < m; i++)
    {
        scanf("%i", &b[i]);
        res += (mp[b[i]] / k) + 1;
        //cout<< b[i]<< " "<< mp[b[i]]<< " "<<((mp[b[i]] - 1) / k) + 1<<endl;
        if (mp[b[i]] < 1)
            continue;
        int tmp = mp[b[i]], tmp2 = a[tmp - 1];
        //cout<< "TMP  "<< tmp<< " "<< tmp2<<endl;
        mp.erase(tmp2);
        //cout<< mp[b[i]]<< " fssdfsdf ";
        mp[b[i]]--;
        //cout<< mp[b[i]]<<endl;
        mp[tmp2] = tmp;
        swap(a[tmp], a[tmp - 1]);
    }
    cout<< res<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	int n, m, k;
	while (cin>> n>> m>> k)
	{
		long long res = 0;
		int a[n];
		map <int, int> mp;
		mp[0] = 1;
		for (int i = 0; i < n; i++)
		{
			scanf("%i", &a[i]);
			mp.insert(make_pair(a[i], i + 1));
		}
		int b[m];
		for (int i = 0; i < m; i++)
		{
			scanf("%i", &b[i]);
			res += (mp[b[i]] % k) + 1;
			int tmp1 = mp[b[i] - 1], tmp2 = mp[b[i]];
			mp.erase(b[i] - 1);
			mp[b[i]] = tmp1;
			mp[b[i] - 1] = tmp2;
		}
		cout<< res<<endl;
	}
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	mp[0] = 1;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i + 1));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += (mp[b[i]] / k) + 1;
		cout<< (mp[b[i]] / k) + 1<<endl;
		int tmp1 = mp[b[i] - 1], tmp2 = mp[b[i]];
		mp.erase(b[i] - 1);
		mp[b[i]] = tmp1;
		mp[b[i] - 1] = tmp2;
	}
	cout<< res<<endl;
}


#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
int main()
{
	int n, m, k;
	cin>> n>> m>> k;
	long long res = 0;
	int a[n];
	map <int, int> mp;
	mp[0] = 1;
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a[i]);
		mp.insert(make_pair(a[i], i));
	}
	int b[m];
	for (int i = 0; i < m; i++)
	{
		scanf("%i", &b[i]);
		res += (mp[b[i]] / k) + 1;
		int tmp1 = mp[b[i] - 1], tmp2 = mp[b[i]];
		mp.erase(b[i] - 1);
		mp[b[i]] = tmp1;
		mp[b[i] - 1] = tmp2;
	}
	cout<< res<<endl;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
#define x first
#define y second
#define now q.front()
using namespace std;

int n,m,i,j,ok,maxi=0,mini=2000000000,x,y,ul=1;
string s,z;

pair<int,int> v[100005];
int f[100005];

int a,b,c,k,ss;

const int modul=1000000007;

queue < pair<int,int> >  q;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int main()
{
 #ifndef ONLINE_JUDGE
ifstream cin(".in");
//ofstream cout(".out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);

    cin>>n>>m>>x;
    cin>>a;
    v[a].x=0;ul=a;
    f[a]=1;
    for(i=1;i<n;i++)
    {
        cin>>a;
        v[ul].y=a;
        v[a].x=ul;
        f[a]=f[ul];
        if(i%x==0)
        {
            f[a]++;
        }
        ul=a;
    }
    for(i=1;i<=m;i++)
    {
        cin>>a;
        b+=f[a];
        int u=v[a].x;
        int rr=v[a].y;
        swap(f[a],f[v[a].x]);
        v[a].x=v[u].x;
        v[a].y=u;
        v[u].x=a;
        v[u].y=rr;

    }
    cout<<b;
return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
#define x first
#define y second
#define now q.front()
using namespace std;

int n,m,i,j,ok,maxi=0,mini=2000000000,x,y,ul=1;
string s,z;

pair<int,int> v[100005];
int f[100005];

int a,b,c,k,ss;

const int modul=1000000007;

queue < pair<int,int> >  q;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int main()
{
 #ifndef ONLINE_JUDGE
ifstream cin(".in");
//ofstream cout(".out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);

    cin>>n>>m>>x;
    cin>>a;
    v[a].x=0;ul=a;
    f[a]=1;
    for(i=1;i<n;i++)
    {
        cin>>a;
        v[ul].y=a;
        v[a].x=ul;
        f[a]=f[ul];
        if(i%x==0)
        {
            f[a]++;
        }
        ul=a;
    }
    for(i=1;i<=m;i++)
    {
        cin>>a;
        b+=f[a];
        if(v[a].x==0)
            continue;
        int u=v[a].x;
        int rr=v[a].y;
        swap(f[a],f[v[a].x]);
        v[a].x=v[u].x;
        v[a].y=u;
        v[u].x=a;
        v[u].y=rr;

    }
    cout<<b;
return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
#define x first
#define y second
#define now q.front()
using namespace std;

int n,m,i,j,ok,maxi=0,mini=2000000000,x,y,ul=1;
string s,z;

pair<int,int> v[100005];
int f[100005];

int a,b,c,k,ss;

const int modul=1000000007;

queue < pair<int,int> >  q;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int main()
{
 #ifndef ONLINE_JUDGE
ifstream cin(".in");
//ofstream cout(".out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);

    cin>>n>>m>>x;
    cin>>a;
    v[a].x=0;ul=a;
    f[a]=1;
    for(i=1;i<n;i++)
    {
        cin>>a;
        v[ul].y=a;
        v[a].x=ul;
        f[a]=f[ul];
        if(i%x==0)
        {
            f[a]++;
        }
        ul=a;
    }
    for(i=1;i<=m;i++)
    {
        cin>>a;
        b+=f[a];
        if(v[a].x==0)
            continue;
        int u=v[a].x;
        int rr=v[a].y;
        swap(f[a],f[v[a].x]);
        v[ v[a].y ].x=u,v[ v[u].x ].y=a;
        v[a].x=v[u].x;
        v[a].y=u;
        v[u].x=a;
        v[u].y=rr;
        int rar=0;
        if(rar)
        {
            cout<<"dupa mutarea "<<a<<" avem "<< b<<'\n';
            for(j=1;j<=n;j++)
            {
                cout<<f[j]<<" ";
            }
            cout<<'\n'<<'\n'<<'\n';
        }

    }
    cout<<b;
return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
#define x first
#define y second
#define now q.front()
using namespace std;

int n,m,i,j,ok,maxi=0,mini=2000000000,x,y,ul=1;
string s,z;

pair<int,int> v[100005];
int f[100005];

long long int a,b,c,k,ss;

const int modul=1000000007;

queue < pair<int,int> >  q;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int main()
{
 #ifndef ONLINE_JUDGE
ifstream cin(".in");
//ofstream cout(".out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);

    cin>>n>>m>>x;
    cin>>a;
    v[a].x=0;ul=a;
    f[a]=1;
    for(i=1;i<n;i++)
    {
        cin>>a;
        v[ul].y=a;
        v[a].x=ul;
        f[a]=f[ul];
        if(i%x==0)
        {
            f[a]++;
        }
        ul=a;
    }
    for(i=1;i<=m;i++)
    {
        cin>>a;
        b+=f[a];
        if(v[a].x==0)
            continue;
        int u=v[a].x;
        int rr=v[a].y;
        swap(f[a],f[v[a].x]);
        v[ v[a].y ].x=u,v[ v[u].x ].y=a;
        v[a].x=v[u].x;
        v[a].y=u;
        v[u].x=a;
        v[u].y=rr;
        int rar=0;
        if(rar)
        {
            cout<<"dupa mutarea "<<a<<" avem "<< b<<'\n';
            for(j=1;j<=n;j++)
            {
                cout<<f[j]<<" ";
            }
            cout<<'\n'<<'\n'<<'\n';
        }

    }
    cout<<b;
return 0;
}


#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <queue>
#define x first
#define y second
#define now q.front()
using namespace std;

long long int n,m,i,j,ok,maxi=0,mini=2000000000,x,y,ul=1;
string s,z;

pair<int,int> v[100005];
int f[100005];

int a,b,c,k,ss;

const int modul=1000000007;

queue < pair<int,int> >  q;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

int main()
{
 #ifndef ONLINE_JUDGE
ifstream cin(".in");
//ofstream cout(".out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);

    cin>>n>>m>>x;
    cin>>a;
    v[a].x=0;ul=a;
    f[a]=1;
    for(i=1;i<n;i++)
    {
        cin>>a;
        v[ul].y=a;
        v[a].x=ul;
        f[a]=f[ul];
        if(i%x==0)
        {
            f[a]++;
        }
        ul=a;
    }
    for(i=1;i<=m;i++)
    {
        cin>>a;
        b+=f[a];
        if(v[a].x==0)
            continue;
        int u=v[a].x;
        int rr=v[a].y;
        swap(f[a],f[v[a].x]);
        v[ v[a].y ].x=u,v[ v[u].x ].y=a;
        v[a].x=v[u].x;
        v[a].y=u;
        v[u].x=a;
        v[u].y=rr;
        int rar=0;
        if(rar)
        {
            cout<<"dupa mutarea "<<a<<" avem "<< b<<'\n';
            for(j=1;j<=n;j++)
            {
                cout<<f[j]<<" ";
            }
            cout<<'\n'<<'\n'<<'\n';
        }

    }
    cout<<b;
return 0;
}