#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        ll x = sol;
        if (sol % 2 == 0) {
            x = 2;
        } else {
            for (ll i = 3; i * i <= sol; i += 2) {
                if (sol % i == 0) {
                    x = i;
                    break;
                }
            }
        }

        // if no multiplier is found, then sol is prime
        cout << x << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;

        bool inserted = false;
        for (int i = 2; i * i <= b[0]; i++) {
            if (b[0] % i == 0) {
                primes.insert(i);
                primes.insert(b[0] / i);
                inserted = true;
            }
        }

        if (!inserted) {
            primes.insert(b[0]);
        }

        inserted = false;
        for (int i = 2; i * i <= t[0]; i++) {
            if (t[0] % i == 0) {
                primes.insert(i);
                primes.insert(t[0] / i);
            }
        }

        if (!inserted) {
            primes.insert(t[0]);
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;
        if (sol % 2 == 0) {
            cout << 2 << endl;
            return 0;
        }
        ll i = 3;
        while (t[0] != 1) {
            if (t[0] % i == 0) {
                primes.insert(i);
                t[0] = t[0] / i;
            } else {
                i += 2;
            }
        }
        i = 2;
        while (b[0] != 1) {
            if (b[0] % i == 0) {
                primes.insert(i);
                b[0] = b[0] / i;
            } else {
                i += 2;
            }
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    set<ll> primes;

    ll i = 2, b_divided = b[0];
    while (i * i <= b_divided) {
        if (b_divided % i == 0) {
            primes.insert(i);
            while (b_divided % i == 0) {
                b_divided /= i;
            }
        }
        i++;
    }
    if (b_divided != 1) {
        primes.insert(b_divided);
    }

    i = 2; b_divided = t[0];
    while (i * i <= b_divided) {
        if (b_divided % i == 0) {
            primes.insert(i);
            while (b_divided % i == 0) {
                b_divided /= i;
            }
        }
        i++;
    }
    if (b_divided != 1) {
        primes.insert(b_divided);
    }

    vector<ll> to_remove;
    for (int i = 1; i < n; i++) {
        to_remove.clear();
        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (t[i] % x != 0 && b[i] % x != 0) {
                to_remove.push_back(x);
            }
        }
        for (int j = 0; j < to_remove.size(); j++) {
            primes.erase(to_remove[j]);
        }
    }

    cout << (primes.empty() ? -1 : *(primes.begin())) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;

        bool inserted = false;
        for (int i = 2; i * i <= b[0]; i++) {
            if (b[0] % i == 0) {
                primes.insert(i);
                inserted = true;
            }
        }

        if (!inserted) {
            primes.insert(b[0]);
        }

        inserted = false;
        for (int i = 2; i * i <= t[0]; i++) {
            if (t[0] % i == 0) {
                primes.insert(i);
            }
        }

        if (!inserted) {
            primes.insert(p[0]);
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;

        bool inserted = false;
        for (int i = 2; i * i <= b[0]; i++) {
            if (b[0] % i == 0) {
                primes.insert(i);
                primes.insert(b[0] / i)
                inserted = true;
            }
        }

        if (!inserted) {
            primes.insert(b[0]);
        }

        inserted = false;
        for (int i = 2; i * i <= t[0]; i++) {
            if (t[0] % i == 0) {
                primes.insert(i);
                primes.insert(t[0] / i);
            }
        }

        if (!inserted) {
            primes.insert(t[0]);
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;

        for (int i = 2; i * i <= b[0]; i++) {
            if (b[0] % i == 0) {
                primes.insert(i);
            }
        }

        for (int i = 2; i * i <= t[0]; i++) {
            if (t[0] % i == 0) {
                primes.insert(i);
            }
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        ll x = sol;
        for (int i = 2; i * i <= sol; i++) {
            if (sol % i == 0) {
                x = i;
                break;
            }
        }

        // if no multiplier is found, then sol is prime
        cout << x << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
struct node {
    int value;
    int index;

    bool operator<( const node& other ) const {
        return value < other.value || (value == other.value && index < other.index);
    }
};
*/

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

long gcd(long x, long y) {
    return y == 0 ? x : gcd(y, x % y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<long> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    vector<long> st(n), sb(n);
    st[0] = t[0];
    sb[0] = b[0];

    for (int i = 1; i < n; i++) {
        st[i] = max(gcd(t[i], st[i - 1]), gcd(t[i], sb[i - 1]));
        sb[i] = max(gcd(b[i], st[i - 1]), gcd(b[i], sb[i - 1]));
    }

    long sol = max(st[n - 1], sb[n - 1]);

    cout << (sol == 1 ? -1 : sol) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

long gcd(long x, long y) {
    return y == 0 ? x : gcd(y, x % y);
}

long lcm(long x, long y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<long> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    long sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, lcm(t[i], b[i]));
    }

    cout << (sol == 1 ? -1 : sol) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        ll x = sol;
        if (sol % 2 == 0) {
            sol = 2;
        } else {
            for (ll i = 3; i * i <= sol; i += 2) {
                if (sol % i == 0) {
                    x = i;
                    break;
                }
            }
        }

        // if no multiplier is found, then sol is prime
        cout << x << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    set<ll> primes;

    ll i = 2, b_divided = b[0];
    while (b_divided != 1) {
        if (b_divided % i == 0) {
            primes.insert(i);
            while (b_divided % i == 0) {
                b_divided /= i;
            }
        }
        i++;
    }
    if (b_divided != 1) {
        primes.insert(b_divided);
    }

    i = 2; b_divided = t[0];
    while (b_divided != 1) {
        if (b_divided % i == 0) {
            primes.insert(i);
            while (b_divided % i == 0) {
                b_divided /= i;
            }
        }
        i++;
    }
    if (b_divided != 1) {
        primes.insert(b_divided);
    }

    vector<ll> to_remove;
    for (int i = 1; i < n; i++) {
        to_remove.clear();
        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (t[i] % x != 0 && b[i] % x != 0) {
                to_remove.push_back(x);
            }
        }
        for (int j = 0; j < to_remove.size(); j++) {
            primes.erase(to_remove[j]);
        }
    }

    cout << (primes.empty() ? -1 : *(primes.begin())) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        for (int i = 2; i * i <= sol; i++) {
            if (sol % i == 0) {
                cout << i << endl;
                break;
            }
        }
    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;
        if (sol % 2 == 0) {
            cout << 2 << endl;
            return 0;
        }
        ll i = 3;
        while (t[0] % 2 == 0) t[0] = t[0] >> 1;
        while (t[0] != 1) {
            if (t[0] % i == 0) {
                primes.insert(i);
                t[0] = t[0] / i;
            } else {
                i += 2;
            }
        }
        i = 3;
        while (b[0] % 2 == 0) b[0] = b[0] >> 1;
        while (b[0] != 1) {
            if (b[0] % i == 0) {
                primes.insert(i);
                b[0] = b[0] / i;
            } else {
                i += 2;
            }
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;

        bool inserted = false;
        for (int i = 2; i * i <= b[0]; i++) {
            if (b[0] % i == 0) {
                primes.insert(i);
                inserted = true;
            }
        }

        if (!inserted) {
            primes.insert(b[0]);
        }

        inserted = false;
        for (int i = 2; i * i <= t[0]; i++) {
            if (t[0] % i == 0) {
                primes.insert(i);
            }
        }

        if (!inserted) {
            primes.insert(t[0]);
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    if (sol != 1) {
        set<ll> primes;
        ll i = 2;
        while (t[0] != 1) {
            if (t[0] % i == 0) {
                primes.insert(i);
                t[0] = t[0] / i;
            } else {
                i++;
            }
        }
        i = 2;
        while (b[0] != 1) {
            if (b[0] % i == 0) {
                primes.insert(i);
                b[0] = b[0] / i;
            } else {
                i++;
            }
        }

        for (set<ll>::iterator it = primes.begin(); it != primes.end(); it++) {
            ll x = *it;
            if (sol % x == 0) {
                cout << x << endl;
                break;
            }
        }

    } else {
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <tuple>
#include <stdio.h>
#include <stdio.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define debug false
#define ll long long
#define ull unsigned long long

/*
 struct node {
 int value;
 int index;

 bool operator<( const node& other ) const {
 return value < other.value || (value == other.value && index < other.index);
 }
 };
 */

int fmod(int x) {
    return x > 0 ? x : -x;
}

int sqr(int x) {
    return x * x;
}

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

ll lcm(ll x, ll y) {
    return x * y / gcd(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<ll> t(n), b(n);
    forn (i, n) {
        cin >> t[i] >> b[i];
    }

    ll sol = 0;

    for (int i = 0; i < n; i++) {
        sol = gcd(sol, t[i] * b[i]);
    }

    forn (i, n) {
        if (sol == t[i] * b[i]) {
            sol /= t[i];
        }
    }

    cout << (sol == 1 ? -1 : sol) << endl;

    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    vector<ll> v2;
    for(i=2; i<=sqrt(a); i++)
    {
        if(a%i==0)
            v2.push_back(i);
    }
    for(i=2; i<=sqrt(b); i++)
    {
        if(b%i==0)
            v2.push_back(i);
    }

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        for(i=x=0; i<v2.size(); i++)
        {
            if(v[j].first%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
        if(x==1)
            continue;
        for(i=0; i<v2.size(); i++)
        {
            if(v[j].second%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0; break;
        }
    }
    if(f||x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0 || v[j].second%(*it)==0)
            {
                m[*it]++;
             }
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
       // cout<<it->first<<"   "<<it->second<<endl;
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);
    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        auto it=v2.begin();
        for(i=x=0; it!=v2.end(); it++)
        {
            if(v[j].first%*it==0)
            {
                m[*it]++;
                x=1;
             }
        }
        for(it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].second%*it==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==0)
        {
            f=1; break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(f||x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        x=0;
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].second%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==0)
        {
            f=1; break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        //cout<<it->first<<"   "<<it->second<<endl;
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        x=0;
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==1)
            continue;
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].second%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==0)
        {
            break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
       // cout<<it->first<<"   "<<it->second<<endl;
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
      while(a%i==0)
        {
            v2.insert(i);
            a/=i;
        }
    }
    if(a>2)
        v2.insert(a);
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0 || v[j].second%(*it)==0)
            {
                m[*it]++;
             }
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
       // cout<<it->first<<"   "<<it->second<<endl;
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    vector<ll> v2;
    for(i=2; i<=sqrt(a); i++)
    {
        if(a%i==0)
            v2.push_back(i);
    }
    for(i=2; i<=sqrt(b); i++)
    {
        if(b%i==0)
            v2.push_back(i);
    }
    map<ll,ll> m;
    for(j=x=f=0; j<n; j++)
    {
        for(i=0; i<v2.size(); i++)
        {
            if(v[j].first%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
        if(x==1)
            continue;
        for(i=0; i<v2.size(); i++)
        {
            if(v[j].second%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
        if(x==0)
        {
            f=1; break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0; break;
        }
    }
    if(f||x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    vector<ll> v2;
    for(i=2; i<=sqrt(a); i++)
    {
        if(a%i==0)
            v2.push_back(i);
    }
    for(i=2; i<=sqrt(b); i++)
    {
        if(b%i==0)
            v2.push_back(i);
    }
    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        for(i=x=0; i<v2.size(); i++)
        {
            if(v[j].first%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
        if(x==1)
            continue;
        for(i=0; i<v2.size(); i++)
        {
            if(v[j].second%v2[i]==0)
            {
                m[v2[i]]++;
                x=1;
             }
        }
        if(x==0)
        {
            f=1; break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0; break;
        }
    }
    if(f||x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        x=0;
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].second%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==0)
        {
          break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        //cout<<it->first<<"   "<<it->second<<endl;
        if(it->second==n*2)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(x)
        cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
set<int> v2;
void div(ll a)
{
    int i;
    while(a%2==0)
    {
        v2.insert(2);
        a/=2;
    }
    for(i=3; i<=sqrt(a); i+=2)
    {
        if(a%i==0)
        {
            v2.insert(i);
            while(a%i==0)
                a/=i;
        }
    }
}
int main()
{
    int n,i,j,x,f;
    cin>>n;
    vector<pair<ll,ll>> v;
    ll a,b;
    for(i=0; i<n; i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    div(v[0].first);
    div(v[0].second);

    map<ll,ll> m;
    for(j=f=0; j<n; j++)
    {
        x=0;
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].first%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        for(auto it=v2.begin(); it!=v2.end(); it++)
        {
            if(v[j].second%(*it)==0)
            {
                m[*it]++;
                x=1;
             }
        }
        if(x==0)
        {
            f=1; break;
        }
    }
    x=1;
    for(auto it=m.begin(); it!=m.end(); it++)
    {
        //cout<<it->first<<"   "<<it->second<<endl;
        if(it->second>=n)
        {
            cout<<it->first<<endl;
            x=0;
            break;
        }
    }
    if(f||x)
        cout<<-1<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1001000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	p[cnt++]=982451653;
	p[cnt++]=222472339;
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1001000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	p[cnt++]=982451653;
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[(int)1.6e5],b[(int)1.6e5],n;
bool ask(int x){
	for(int i=2;i<=n;i++) if(a[i]%x&&b[i]%x) return 0;
	return 1;
}
void check(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0&&ask(i)) {
			cout<<i;
			exit(0);
		}
		if(x%i==0&&ask(x/i)){
		    cout<<x/i;
		    exit(0);
		}
	}
	if(ask(x)){
	    cout<<x;
	    exit(0);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	check(a[1]);
	check(b[1]);
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1501000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}
ll pow_mod(ll a,ll n,ll mod){
	if(n==0) return 1;
	ll res=pow_mod(a*a%mod,n/2,mod);
	if(n&1) res=res*a%mod;
	return res;
}
int wit(ll a,ll n){
	ll tmp=n-1;
	int cnt=0;
	while(tmp%2==0){
		tmp/=2;
		cnt++;
	}
	ll x=pow_mod(a,tmp,n);
	if(x==1||x==n-1) return 1;
	while(cnt){
		x=x*x%n;
		if(x==n-1) return 1;
		cnt--;
	}
	return 0;
}
int isprime(ll t){
	if(t==2) return 1;
	if(t%2==0||t<2) return 0;
	for(int i=1;i<5;i++){
		ll z=rand()%(t-1)+1;
		if(!wit(z,t)) return 0;
	}
	return 1;
}
int main(){
	srand((unsigned)time(NULL));
	init();
	ios::sync_with_stdio(0);

	cin>>n;
	for(int i=1;i<=n;i++){
	 cin>>a[i]>>b[i];
	 if(a[i]>15485863&&isprime(a[i])) p[cnt++]=a[i];
	 if(b[i]>15485863&&isprime(b[i])) p[cnt++]=b[i];
	 }

	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[(int)1.6e5],b[(int)1.6e5],n;
bool ask(int x){
	for(int i=2;i<=n;i++) if(a[i]%x&&b[i]%x) return 0;
	return 1;
}
void check(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0) {
			if(ask(i)){cout<<i;
			exit(0);}
			while(x%i==0) x/=i;
		}
	}
	if(x>1&&ask(x)){
	    cout<<x;
	    exit(0);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	check(a[1]);
	check(b[1]);
	cout<<-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n;
ll a,b,c[(int)1.6e5];
ll gcd(ll x,ll y){
	if(!y) return x;
	return gcd(y,x%y);
}
int main(){
	ios::sync_with_stdio(0);
	cin>>n;

	for(int i=1;i<=n;i++){
		cin>>a>>b;
		c[i]=a*b/gcd(a,b);
	}
	ll ans=c[1];
	for(int i=2;i<=n;i++){
		ans=gcd(ans,c[i]);
	}
	if(ans!=1)cout<<ans;
	else cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1201000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}
ll pow_mod(ll a,ll n,ll mod){
	if(n==0) return 1;
	ll res=pow_mod(a*a%mod,n/2,mod);
	if(n&1) res=res*a%mod;
	return res;
}
int wit(ll a,ll n){
	ll tmp=n-1;
	int cnt=0;
	while(tmp%2==0){
		tmp/=2;
		cnt++;
	}
	ll x=pow_mod(a,tmp,n);
	if(x==1||x==n-1) return 1;
	while(cnt){
		x=x*x%n;
		if(x==n-1) return 1;
		cnt--;
	}
	return 0;
}
int isprime(ll t){
	if(t==2) return 1;
	if(t%2==0||t<2) return 0;
	for(int i=1;i<5;i++){
		ll z=rand()%(t-1)+1;
		if(!wit(z,t)) return 0;
	}
	return 1;
}
int main(){
	srand((unsigned)time(NULL));
	init();
	ios::sync_with_stdio(0);

	cin>>n;
	for(int i=1;i<=n;i++){
	 cin>>a[i]>>b[i];
	 if(a[i]>15485863&&isprime(a[i])) p[cnt++]=a[i];
	 if(b[i]>15485863&&isprime(b[i])) p[cnt++]=b[i];
	 }

	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1000000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	return 0;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[10000];
ll a[150100],b[150100];
bool not_p[300000];
int cnt,n;
void init(){
	int maxn=300000;
	for(int i=2;i<=maxn&&cnt<9800;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[(int)1.6e5],b[(int)1.6e5],n;
bool ask(int x){
	for(int i=2;i<=n;i++) if(a[i]%x&&b[i]%x) return 0;
	return 1;
}
void check(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0&&ask(i)) {
			cout<<i;
			exit(0);
		}
	}
	if(ask(x)){
	    cout<<x;
	    exit(0);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	check(a[1]);
	check(b[1]);
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[10000];
ll a[150100],b[150100];
bool not_p[300000];
int cnt,n;
void init(){
	int maxn=300000;
	for(int i=2;i<=maxn;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p[1000000];
ll a[150100],b[150100];
bool not_p[30010000];
int cnt,n;
void init(){
	int maxn=30000000;
	for(int i=2;i<=maxn&&cnt<1000000;i++){
		if(!not_p[i]) p[cnt++]=i;
		for(int j=0;j<cnt&&i*p[j]<=maxn;j++){
			not_p[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
}

int main(){
	init();
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	for(int i=0;i<cnt;i++){
		int flag=1;
		for(int j=1;j<=n;j++){
			if(a[j]%p[i]&&b[j]%p[i]) {flag=0;break;}
		}
		if(flag){
			cout<<p[i];
			return 0;
		}
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[(int)1.6e5],b[(int)1.6e5],n;
bool ask(int x){
	for(int i=2;i<=n;i++) if(a[i]%x&&b[i]%x) return 0;
	return 1;
}
void check(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0) {
			if(ask(i))cout<<i;
			exit(0);
			while(x%i==0) x/=i;
		}
	}
	if(x>1&&ask(x)){
	    cout<<x;
	    exit(0);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	check(a[1]);
	check(b[1]);
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];
pair<long long, long long> ab[150000];

long long unt(long long t) {
    long long res = t;
    for (long long i = 2; i * i <= t; ++i) {
        if (t % i == 0) {
            res = i;
            break;
        }
    }
    return res;
}

bool check(long long t, int n) {
    for (int i = 0; i < n; ++i) {
        if (ab[i].first % t and ab[i].second % t) return 0;
    }
    return 1;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> ab[i].first >> ab[i].second;
        long long t = ab[i].first * ab[i].second / __gcd(ab[i].first, ab[i].second);
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        long long a = __gcd(tmp, ab[0].first);
        long long b = __gcd(tmp, ab[0].second);
        a = unt(a);
        b = unt(b);
        if (check(a, n) and a > 1) cout << a;
        else if (check(b, n) and b > 1) cout << b;
        else cout << -1;
    }
}


#include <bits/stdc++.h>

using namespace std;

vector<long long> v;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        cin >> a >> b;
        long long t = __gcd(a, b);
        t = a * b / t;
        v.push_back(t);
    }
    long long tmp = v[0];
    for (int i = 1; i < v.size(); ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (int i = 2; i <= tmp; ++i) {
            if (tmp % i == 0) {
                cout << i;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

vector<long long> v;

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        cin >> a >> b;
        long long t = __gcd(a, b);
        t = a * b / t;
        v.push_back(t);
    }
    long long tmp = v[0];
    for (int i = 1; i < v.size(); ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (int i = 2; i <= tmp; ++i) {
            if (tmp % i == 0) {
                cout << i;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];
pair<long long, long long> ab[150000];

long long unt(long long t) {
    long long res = 1;
    for (long long i = 2; i * i <= t; ++i) {
        if (t % i == 0) {
            res = i;
            break;
        }
    }
    return res;
}

bool check(long long t, int n) {
    for (int i = 0; i < n; ++i) {
        if (ab[i].first % t and ab[i].second % t) return 0;
    }
    return 1;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> ab[i].first >> ab[i].second;
        long long t = ab[i].first * ab[i].second / __gcd(ab[i].first, ab[i].second);
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        long long a = __gcd(tmp, ab[0].first);
        long long b = __gcd(tmp, ab[0].second);
        a = unt(a);
        b = unt(b);
        if (check(a, n) and a > 1) cout << a;
        else if (check(b, n) and b > 1) cout << b;
    }
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        long long t = __gcd(a, b);
        t = a * b / t;
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (long long i = 2; i * i <= tmp; ++i) {
            if (tmp % i == 0) {
                tmp = i;
                break;
            }
        }
        cout << tmp;
    }
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        long long t = __gcd(a, b);
        t = a * b / t;
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (int i = 2; i <= tmp; ++i) {
            if (tmp % i == 0) {
                cout << i;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        long long t = __gcd(a, b);
        t = a * b / t;
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (long long i = 2; i <= max((long long)sqrt(tmp), tmp); ++i) {
            if (tmp % i == 0) {
                cout << i;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

vector<long long> v;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        cin >> a >> b;
        long long t = __gcd(a, b);
        t = a * b / t;
        v.push_back(t);
    }
    long long tmp = v[0];
    for (int i = 1; i < v.size(); ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (int i = 2; i <= sqrt(tmp); ++i) {
            if (tmp % i == 0) {
                cout << i;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        long long t = __gcd(a, b);
        t = a * b / t;
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        for (long long i = 2; i <= (long long)sqrt(tmp); ++i) {
            if (tmp % i == 0) {
                tmp = i;
                break;
            }
        }
        cout << tmp;
    }
}


#include <bits/stdc++.h>

using namespace std;

vector<long long> v;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        cin >> a >> b;
        long long t = __gcd(a, b);
        t = a * b / t;
        v.push_back(t);
    }
    long long tmp = v[0];
    for (int i = 1; i < v.size(); ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else cout << tmp;
}


#include <bits/stdc++.h>

using namespace std;

long long v[150000];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        long long a, b;
        scanf("%lld%lld", &a, &b);
        long long t = __gcd(a, b);
        t = a * b / t;
        v[i] = t;
    }
    long long tmp = v[0];
    for (int i = 1; i < n; ++i) {
        tmp = __gcd(tmp, v[i]);
    }
    if (tmp == 1) cout << -1;
    else {
        if (tmp < 5) {
            if (tmp != 4) cout << tmp;
            else cout << 2;
        }
        else {
            for (long long i = 2; i <= (long long)sqrt(tmp); ++i) {
                if (tmp % i == 0) {
                    cout << i;
                    return 0;
                }
            }
        }
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000010;
int a[N];
int b[N];

int d[N],cnt=0;
void deal(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0)
		{
			x/=i;
		}
	}
	if(x>1)
	{
		cnt++;
		d[cnt]=x;
	}
}
int main()
{
	int i,j;
	int n;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
	}
	int x=a[1];
	int y=b[1];
	deal(x);
	deal(y);
	sort(d+1,d+1+cnt);
	cnt=unique(d+1,d+1+cnt)-d-1;

//	for(i=1;i<=cnt;i++)
//	{
//		cout<<d[i]<<' ';
//	}
//	cout<<endl;
	bool flag=true;
	bool F=false;
	for(i=1;i<=cnt;i++)
	{
		int tmp=d[i];
		flag=true;
		for(j=2;j<=n;j++)
		{
			if(a[j]%tmp!=0&&b[j]%2!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<tmp<<endl;
			F=true;
			break;
		}
	}
	if(F==false)
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int read()
{
	char ch=' ';
	while(ch<'0'&&ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int gcd(int a,int b)
{
	return a == 0 ? b : gcd(b % a, a);
}
int lcm(int a,int b)
{
	return a*b/gcd(a,b);
}
int main()
{
	int n;
	scanf("%d",&n);
	int i,j;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
	}
	if(n==1)
	{
		if(a[1]!=1)
		{
			cout<<a[1]<<endl;
		}
		else if(b[1]!=1)
		{
			cout<<b[1]<<endl;
		}
		else
		{
			cout<<"-1"<<endl;
		}
		return 0;
	}

	int a1=gcd(a[1],a[2]);
	int a2=gcd(a[1],b[2]);
	int b1=gcd(b[1],a[2]);
	int b2=gcd(b[1],b[2]);
	if(n==2)
	{
		if(a1!=1)
		{
			cout<<a1<<endl;
		}
		else if(a2!=1)
		{
			cout<<a2<<endl;
		}
		else if(b1!=1)
		{
			cout<<b1<<endl;
		}
		else if(b2!=1)
		{
			cout<<b2<<endl;
		}
		else
		{
			cout<<"-1"<<endl;
		}
		return 0;
	}

	int s=lcm(lcm(lcm(a1,a2),b1),b2);
	for(i=3;i<=n;i++)
	{
		a1=gcd(s,a[i]);
		a2=gcd(s,b[i]);
		s=lcm(a1,a2);
		if(s==1)
		{
			cout<<"-1"<<endl;
			return 0;
		}
	}
	if(a1!=1)
	{
		cout<<a1<<endl;
	}
	else if(a2!=1)
	{
		cout<<a2<<endl;
	}
	else
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=150010;
int a[N];
int b[N];

int d[N],cnt=0;
void deal(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0)
		{
			x/=i;
		}
	}
	if(x>1)
	{
		cnt++;
		d[cnt]=x;
	}
}
int main()
{
	int i,j;
	int n;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
	}
	int x=a[1];
	int y=b[1];
	deal(x);
	deal(y);
	sort(d+1,d+1+cnt);
	cnt=unique(d+1,d+1+cnt)-d;

	bool flag=true;
	bool F=false;
	for(i=1;i<=cnt;i++)
	{
		int tmp=d[i];
		flag=true;
		for(j=2;j<=n;j++)
		{
			if(a[j]%tmp!=0&&b[j]%2!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<tmp<<endl;
			F=true;
			break;
		}
	}
	if(F==false)
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int d[N];
int tmp[N];
int read()
{
	char ch=' ';
	while(ch<'0'||ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int main()
{
	int n;
	n=read();
	int i,j;
	int maxm=0;
	for(i=1;i<=n;i++)
	{
		a[i]=read();
		b[i]=read();
	}
	int x=a[1];
	int y=b[1];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	for(j=1;j<=cnt;j++)
	{
		bool flag=true;
		for(i=2;i<=n;i++)
		{
			if(a[i]%d[j]!=0&&b[i]%d[j]!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<d[j]<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<< "----------"<<endl;

const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N =1010;

int n;
int x[MAXN],y[MAXN];
set<int>se;
void get_prime(int n){
    for(int i=2;i*i<=n;i++){
        if(n%i == 0){
            se.insert(i);
            while(n%i == 0) n /= i;
        }
    }
    if(n != 1) se.insert(n);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        clr(x);clr(y);
        se.clear();
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x[i],&y[i]);
        }
        get_prime(x[1]);
        get_prime(y[1]);
        int flag = 1,Flag = 0;
        set<int>::iterator it;
        for(it=se.begin();it!=se.end();it++){
            int temp = *it;
            flag = 1;
            for(int i=2;i<=n;i++){
                if(x[i]%temp !=0 && y[i]%temp !=0){
                    flag = 0;
                    break;
                }
            }
            if(flag){
                printf("%d\n",temp);
                Flag = 1;
                break;
            }
            else continue;
        }
        if(Flag) continue;
        else printf("-1\n");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
	return a == 0 ? b : gcd(b % a, a);
}
int lcm(int a,int b)
{
	return a*b/gcd(a,b);
}
int main()
{
	int n;
	cin>>n;
	int a,b;
	cin>>a>>b;
	int c,d;
	for(int i=2;i<=n;i++)
	{
		cin>>c>>d;
		a=max(gcd(a,c),gcd(b,c));
		b=max(gcd(b,c),gcd(b,d));
	}
	a=max(a,b);
	if(a==1)
	{
		cout<<"-1"<<endl;
	}
	else
	{
		cout<<a<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int d[N];
int tmp[N];
int read()
{
	char ch=' ';
	while(ch<'0'&&ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int main()
{
	int n;
	scanf("%d",&n);
	int i,j;
	int maxm=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
	}
	int x=a[1];
	int y=b[1];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	for(j=1;j<=cnt;j++)
	{
		bool flag=true;
		for(i=2;i<=n;i++)
		{
			if(a[i]%d[j]!=0&&b[i]%d[j]!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<d[j]<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int d[N];
int tmp[N];
int read()
{
	char ch=' ';
	while(ch<'0'||ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int main()
{
	int n;
	n=read();
	int i,j;
	int maxm=0;
	for(i=1;i<=n;i++)
	{
		a[i]=read();
		b[i]=read();
	}
	int x=a[n];
	int y=b[n];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	for(j=1;j<=cnt;j++)
	{
		bool flag=true;
		for(i=1;i<n;i++)
		{
			if(a[i]%d[j]!=0&&b[i]%d[j]!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<d[j]<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int d[N];
int tmp[N];
int main()
{
	int n;
	scanf("%d",&n);
	int i,j;
	int maxm=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
	}
	int x=a[1];
	int y=b[1];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	int cc=0;
	for(i=2;i<=n;i++)
	{
		for(j=1;j<=cnt;j++)
		{
			if(a[i]%d[j]==0||b[i]%d[j]==0)
			{
				cc++;
				tmp[cc]=d[j];
			}
		}
		cnt=cc;
		cc=0;
		for(j=1;j<=cnt;j++)
		{
			d[j]=tmp[j];
		}
		if(cnt==0)
		{
			cout<<"-1"<<endl;
			return 0;
		}
	}
	cout<<d[1]<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int c[N];
int d[N];
int read()
{
	char ch=' ';
	while(ch<'0'||ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int main()
{
	int n;
	n=read();
	int i,j;
	int maxm=0;
	int mini=1;

	for(i=1;i<=n;i++)
	{
		a[i]=read();
		b[i]=read();
		c[i]=max(a[i],b[i]);
		if(c[i]<=c[mini])
		{
			mini=i;
		}
	}
	int x=a[mini];
	int y=b[mini];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	for(j=1;j<=cnt;j++)
	{
		bool flag=true;
		for(i=1;i<n;i++)
		{
			if(a[i]%d[j]!=0&&b[i]%d[j]!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			printf("%d\n",d[j]);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int d[N];
int tmp[N];
int read()
{
	char ch=' ';
	while(ch<'0'&&ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int main()
{
	int n;
	scanf("%d",&n);
	int i,j;
	int maxm=0;
	bool fl=true;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		if(a[i]%2&&b[i]%2)
		{
			fl=false;
		}
	}
	if(fl)
	{
		cout<<"2"<<endl;
		return 0;
	}
	int x=a[1];
	int y=b[1];
	maxm=max(x,y);

	int cnt=0;

	for(i=2;i<=maxm;i++)
	{
		if(x%i==0||y%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0&&x!=1)
		{
			x=x/i;
		}
		while(y%i==0&&y!=1)
		{
			y=y/i;
		}
		if((x==1&&y==1)||(x<i&&y<i))
		{
			break;
		}
	}
	if(cnt==0)
	{
		cout<<"-1"<<endl;
		return 0;
	}

	int cc=0;
	for(i=2;i<=n/2;i++)
	{
		for(j=1;j<=cnt;j++)
		{
			if(a[i]%d[j]==0||b[i]%d[j]==0)
			{
				cc++;
				tmp[cc]=d[j];
			}
		}
		cnt=cc;
		cc=0;
		for(j=1;j<=cnt;j++)
		{
			d[j]=tmp[j];
		}
		if(cnt==0)
		{
			cout<<"-1"<<endl;
			return 0;
		}
	}

	for(j=1;j<=cnt;j++)
	{
		bool flag=true;
		for(i=n/2;i<=n;i++)
		{
			if(a[i]%d[j]!=0&&b[i]%d[j]!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<d[j]<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
	return a == 0 ? b : gcd(b % a, a);
}
int lcm(int a,int b)
{
	return a*b/gcd(a,b);
}
int main()
{
	int n;
	cin>>n;
	int a,b;
	cin>>a>>b;
	int c,d;
	for(int i=1;i<=n;i++)
	{
		cin>>c>>d;
		a=lcm(gcd(a,c),gcd(b,c));
		b=lcm(gcd(b,c),gcd(b,d));
	}
	a=max(a,b);
	if(a==1)
	{
		cout<<"-1"<<endl;
	}
	else
	{
		cout<<a<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=150010;
int a[N],b[N];
int read()
{
	char ch=' ';
	while(ch<'0'&&ch>'9')
	{
		ch=getchar();
	}
	int x=0;
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x;
}
int gcd(int a,int b)
{
	return a == 0 ? b : gcd(b % a, a);
}
int lcm(int a,int b)
{
	return a*b/gcd(a,b);
}
int main()
{
	int n;
	scanf("%d",&n);
	int i,j;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
	}
	if(n==1)
	{
		if(a[1]!=1)
		{
			cout<<a[1]<<endl;
		}
		else if(b[1]!=1)
		{
			cout<<b[1]<<endl;
		}
		else
		{
			cout<<"-1"<<endl;
		}
	}

	int a1=gcd(a[1],a[2]);
	int a2=gcd(a[1],b[2]);
	int b1=gcd(b[1],a[2]);
	int b2=gcd(b[1],b[2]);
	if(n==2)
	{
		if(a1!=1)
		{
			cout<<a1<<endl;
		}
		else if(a2!=1)
		{
			cout<<a2<<endl;
		}
		else if(b1!=1)
		{
			cout<<b1<<endl;
		}
		else if(b2!=1)
		{
			cout<<b2<<endl;
		}
		else
		{
			cout<<"-1"<<endl;
		}
		return 0;
	}

	int s=lcm(lcm(lcm(a1,a2),b1),b2);
	for(i=3;i<=n;i++)
	{
		a1=gcd(s,a[i]);
		a2=gcd(s,b[i]);
		s=lcm(a1,a2);
		if(s==1)
		{
			cout<<"-1"<<endl;
			return 0;
		}
	}
	if(a1!=1)
	{
		cout<<a1<<endl;
	}
	else if(a2!=1)
	{
		cout<<a2<<endl;
	}
	else
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000010;
int a[N];
int b[N];

int d[N],cnt=0;
void deal(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			cnt++;
			d[cnt]=i;
		}
		while(x%i==0)
		{
			x/=i;
		}
	}
	if(x>1)
	{
		cnt++;
		d[cnt]=x;
	}
}
int main()
{
	int i,j;
	int n;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i]>>b[i];
	}

	deal(a[1]);
	deal(b[1]);

	sort(d+1,d+1+cnt);
	cnt=unique(d+1,d+1+cnt)-d-1;

//	for(i=1;i<=cnt;i++)
//	{
//		cout<<d[i]<<' ';
//	}
//	cout<<endl;
	bool flag=true;
	bool F=false;
	for(i=1;i<=cnt;i++)
	{
		int tmp=d[i];
		flag=true;
		for(j=2;j<=n;j++)
		{
			if(a[j]%tmp!=0&&b[j]%tmp!=0)
			{
				flag=false;
				break;
			}
		}
		if(flag)
		{
			cout<<tmp<<endl;
			F=true;
			break;
		}
	}
	if(F==false)
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const int mod = 998244353;

const double PI=acos(-1.0);

// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
            const int maxn = 3e5+9;

            ll ans[maxn];

            bool mk[maxn];
int main(){

            int n;scanf("%d", &n);
            ll x,y;
            scanf("%I64d%I64d", &x, &y);
            int tot = 0;
            for(int i=2; i<=sqrt(x); i++){
            	if(x%i==0){
            		ans[++tot] = i;
            	}
            }
            for(int i=2; i<=sqrt(y); i++){
            	if(y%i==0){
            		ans[++tot] = i;
            	}
            }
            for(int i=2; i<=n; i++){

                scanf("%I64d%I64d", &x, &y);

                for(int j=1; j<=tot; j++){
                    if(mk[j]==0 && x%ans[j] != 0 && y%ans[j]!=0){
                            mk[j] = 1;
                        }
                    }
            }
            for(int i=1; i<=tot; i++){
                if(mk[i]==0){
                    printf("%I64d\n", ans[i]);
                    return 0;
                }
            }
            puts("-1");
            return 0;
}


#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFFLL;  //2147483647
const ll nmos = 0x80000000LL;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18
const int mod = 998244353;

const double PI=acos(-1.0);

// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------showtime----------------------*/
            const int maxn = 1e6+9;

            ll ans[maxn];
            bool re[maxn];
            bool mk[maxn];
int main(){

            int n;		scanf("%d", &n);
            ll x,y;
            scanf("%I64d%I64d", &x, &y);
            int tot = 0;

            for(ll i=2; i<=(ll)(floor(sqrt(x)))+1; i++){
            	if(x%i==0){
            		if(re[i]==0)ans[++tot] = i;
            		ans[++tot] = x/i;
            		re[i] = 1;

            	}
            }
            for(ll i=2; i<= (ll)(floor(sqrt(y)))+1; i++){
            	if(y%i==0){
            		if(re[i]==0)ans[++tot] = i;
            		ans[++tot] = y/i;
            		re[i] = 1;
            	}
            }

            ans[++tot] = x;
            ans[++tot] = y;
            for(int i=2; i<=n; i++){

                scanf("%I64d%I64d", &x, &y);

                for(int j=1; j<=tot; j++)
                    if((mk[j]==0) && (x%ans[j] != 0) && (y%ans[j]!=0))
                     	mk[j] = 1;

            }
            for(int i=1; i<=tot; i++){
                if(mk[i]==0){
                    printf("%I64d\n",ans[i]);
                    return 0;
                }
            }
            puts("-1");
            return 0;
}


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

bool comp( pair<int,int> &a, pair<int,int> &b){
  return (a.first*2-a.second < b.first*2- b.second);
}
bool comp2(pair<int,int> &a, pair<int,int> &b){
  return (min(a.first*2,a.second) > min(b.first,b.second));
}
int main(){
  int n,f;
  cin >> n >> f;
  vector <pair <int,int> > a(n);
  for(int i=0; i<n; i++){
    cin >> a[i].first >> a[i].second;
  }

  sort(a.begin(),a.end(),comp);

  int pos = 0;
  for(int i = 0; i<n;i++){
    if(a[i].first-a[i].second >= 0) {
      pos = i;
      break;
    }
  }
  sort(a.begin(),a.begin()+pos,comp2);

  // for(int i=0; i<n; i++){
  //   cout << a[i].first <<" " <<  a[i].second << "\n";
  // }

  long long int total=0;
  for(int i=0; i<n; i++){
    if(f>0 && a[i].first>0){
      total+= min(a[i].first*2,a[i].second);
      f--;
    }
    else{
      total+= min(a[i].first,a[i].second);
    }
  }
  cout << total << "\n";
  return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

bool comp(const pair<int,int> &a,const pair<int,int> &b){
    if(a.first >b.first)return true;
    else if(a.first == b.first && a.second < b.second)return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <int,int> a[n];
	REP(i,n) cin >> a[i].first >> a[i].second ;
	int count = 0;
	vector < pair<int,int> > x;

	REP(i,n){
		if(a[i].second <= a[i].first)count += a[i].second;
		else {
			x.PB(make_pair (min(a[i].second,2*a[i].first) , a[i].first));
		}
	}

	sort(x.begin(),x.end(),comp);

	int i = 0;

	while(f>0 && i < x.size()){
		count+= x[i].first;
		i++;
		f--;
	}

	while(i<x.size()){
		count+= x[i].second;
		i++;
	}

	cout << count << endl;
	return 0;
}


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

bool comp( pair<int,int> &a, pair<int,int> &b){
  return (a.first-a.second < b.first - b.second);
}

int main(){
  int n,f;
  cin >> n >> f;
  vector <pair <int,int> > a(n);
  for(int i=0; i<n; i++){
    cin >> a[i].first >> a[i].second;
  }
  sort(a.begin(),a.end(),comp);
  long long int total=0;
  for(int i=0; i<n; i++){
    if(f>0 && a[i].first>0){
      total+= min(a[i].first*2,a[i].second);
      f--;
    }
    else{
      total+= min(a[i].first,a[i].second);
    }
  }
  cout << total << "\n";
  return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

bool comp(const pair<ll,ll> &a,const pair<ll,ll> &b){
    if(a.first >b.first)return true;
    else if(a.first == b.first && a.second < b.second)return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <ll,ll> a[n];
	ll count = 0;
	vector < pair<ll,ll> > x;
	REP(i,n){
		cin >> a[i].first >> a[i].second ;
		if(a[i].second <= a[i].first)count += a[i].second;
		else {
			ll y = 2*a[i].first;
			x.PB(make_pair (min(a[i].second,y) , a[i].first));
		}
	}

	sort(x.begin(),x.end(),comp);

	int i = 0;

	while(f>0 && i < x.size()){
		count+= x[i].first;
		i++;
		f--;
	}

	while(i<x.size()){
		count+= x[i].second;
		i++;
	}

	cout << count << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

int main(){
	int n,f;
	cin >> n >> f;
	pair <int,int> a[n];
	REP(i,n) cin >> a[i].first >> a[i].second ;
	int count = 0;
	vector < pair<int,int> > x;
	REP(i,n){
		if(a[i].second < a[i].first)count += a[i].second;
		else {
			x.PB(make_pair (min(a[i].second,2*a[i].first) , a[i].first));
		}
	}
	sort(x.begin(),x.end());

	int i = x.size()-1;
	while(f>0 && i >= 0){
		count+= x[i].first;
		i--;
		f--;
	}
	while(i>=0){
		count+= x[i].second;
		i--;
	}

	cout << count << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;
bool comp(const pair<int,int> &a,const pair<int,int> &b){
    if(a.first >b.first)return true;
    else if(a.first == b.first && a.second < b.second)return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <int,int> a[n];
	REP(i,n) cin >> a[i].first >> a[i].second ;
	int count = 0;
	vector < pair<int,int> > x;
	REP(i,n){
		if(a[i].second < a[i].first)count += a[i].second;
		else {
			x.PB(make_pair (min(a[i].second,2*a[i].first) , a[i].first));
		}
	}

	sort(x.begin(),x.end(),comp);

	int i = 0;
	while(f>0 && i < x.size()){
		count+= x[i].first;
		i++;
		f--;
	}
	while(i<x.size()){
		count+= x[i].second;
		i++;
	}

	cout << count << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

bool comp(const pair<ll,ll> &a,const pair<ll,ll> &b){
    if( (min(2*a.first,a.second)-min(a.first,a.second) ) > (min (2*b.first,b.second)-min(b.first,b.second) ) )return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <ll,ll> a[n];
	ll count = 0;

	REP(i,n){
		cin >> a[i].first >> a[i].second ;
	}
	sort(a,a+n,comp);

	FOR(i,0,n,1){
		if(i<f)count+= min(2*a[i].first,a[i].second);
		else count+= min(a[i].first,a[i].second);
	}

	cout << count << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

bool comp(const pair<int,int> &a,const pair<int,int> &b){
    if(a.first >b.first)return true;
    else if(a.first == b.first && a.second < b.second)return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <int,int> a[n];
	REP(i,n) cin >> a[i].first >> a[i].second ;
	ll count = 0;
	vector < pair<int,int> > x;

	REP(i,n){
		if(a[i].second <= a[i].first)count += a[i].second;
		else {
			x.PB(make_pair (min(a[i].second,2*a[i].first) , a[i].first));
		}
	}

	sort(x.begin(),x.end(),comp);

	int i = 0;

	while(f>0 && i < x.size()){
		count+= x[i].first;
		i++;
		f--;
	}

	while(i<x.size()){
		count+= x[i].second;
		i++;
	}

	cout << count << endl;
	return 0;
}


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

bool comp( pair<int,int> &a, pair<int,int> &b){
  return (a.first*2-a.second < b.first*2- b.second);
}
bool comp2(pair<int,int> &a, pair<int,int> &b){
  return (min(a.first*2,a.second) > min(b.first*2,b.second));
}
int main(){
  int n,f;
  cin >> n >> f;
  vector <pair <int,int> > a(n);
  for(int i=0; i<n; i++){
    cin >> a[i].first >> a[i].second;
  }

  sort(a.begin(),a.end(),comp);

  int pos = 0;
  for(int i = 0; i<n;i++){
    if(a[i].first-a[i].second >= 0) {
      pos = i;
      break;
    }
  }
  sort(a.begin(),a.begin()+pos,comp2);

  // for(int i=0; i<n; i++){
  //   cout << a[i].first <<" " <<  a[i].second << "\n";
  // }

  long long int total=0;
  for(int i=0; i<n; i++){
    if(f>0 && a[i].first>0){
      total+= min(a[i].first*2,a[i].second);
      f--;
    }
    else{
      total+= min(a[i].first,a[i].second);
    }
  }
  cout << total << "\n";
  return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stack>
#include <limits>
#include <vector>
#include <stack>
#include <sstream>
#include <queue>
#include <utility> // for pair
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b,c) for(int i=a;i<b;i += c)
#define FORd(i,a,b,c) for(int i=a;i>=b;i -=c)
#define vi vector<ll>
#define vii vector<vector<ll> >
#define ll long long int //range -> 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
#define ui unsigned int // range -> 0 to 4,294,967,295
#define ull unsigned long long
#define PB push_back
#define sz size()
#define InF  2147483647
using namespace std;

bool comp(const pair<ll,ll> &a,const pair<ll,ll> &b){
    if(a.first >b.first)return true;
    else if(a.first == b.first && a.second < b.second)return true;
    else return false;
}

int main(){
	int n,f;
	cin >> n >> f;
	pair <ll,ll> a[n];
	REP(i,n) cin >> a[i].first >> a[i].second ;
	ll count = 0;
	vector < pair<ll,ll> > x;

	REP(i,n){
		if(a[i].second <= a[i].first)count += a[i].second;
		else {
			x.PB(make_pair (min(a[i].second,2*a[i].first) , a[i].first));
		}
	}

	sort(x.begin(),x.end(),comp);

	int i = 0;

	while(f>0 && i < x.size()){
		count+= x[i].first;
		i++;
		f--;
	}

	while(i<x.size()){
		count+= x[i].second;
		i++;
	}

	cout << count << endl;
	return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

int n, f, x, y;
pair<pair<ll, ll>, int> arr1[100000 + 10], arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){

    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i].first.first = x;
        arr1[i].first.second = y;
        arr1[i].second = i;
        arr2[i].first.first = 2 * x;
        arr2[i].first.second = y;
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<pair<int, int>, int> >());
    for (int i = 0; i < f; i++)
    {
        ans += min(arr2[i].first.first, arr2[i].first.second);
        id[arr2[i].second] = true;
    }
    for (int i = 0; i < n; i++)
    {
        if(!id[arr1[i].second])
            ans += min(arr1[i].first.first, arr1[i].first.second);
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

ll n, f, x, y;
pair<ll, int> arr1[100000 + 10], arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){

    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i].first = min(x, y);
        arr1[i].second = i;
        arr2[i].first = min(2 * x, y);
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<ll, int> >());
    for (int i = 0; i < f; i++)
    {
        ans += arr2[i].first;
        id[arr2[i].second] = true;
    }
    for (int i = 0; i < n; i++)
    {
        if(!id[arr1[i].second])
            ans += arr1[i].first;
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

ll n, f, x, y;
ll arr1[100000 + 10];
pair<ll, ll> arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){
    clr(id, 1);
    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i] = min(x, y);
        arr2[i].first = min(2 * x, y);
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<ll, ll> >());
    for (int i = 0; i < f; i++)
    {
        ans += arr2[i].first;
        id[arr2[i].second] = false;
    }
    for (int i = 0; i < n; i++)
        if(id[i])
            ans += arr1[i];

    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

int n, f, x, y;
pair<pair<int, int>, int> arr1[100000 + 10], arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){

    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i].first.first = x;
        arr1[i].first.second = y;
        arr1[i].second = i;
        arr2[i].first.first = 2 * x;
        arr2[i].first.second = y;
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<pair<int, int>, int> >());
    for (int i = 0; i < f; i++)
    {
        ans += min(arr2[i].first.first, arr2[i].first.second);
        id[arr2[i].second] = true;
    }
    for (int i = 0; i < n; i++)
    {
        if(!id[arr1[i].second])
            ans += min(arr1[i].first.first, arr1[i].first.second);
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

int n, f, x, y;
pair<pair<ll, ll>, int> arr1[100000 + 10], arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){

    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i].first.first = x;
        arr1[i].first.second = y;
        arr1[i].second = i;
        arr2[i].first.first = 2 * x;
        arr2[i].first.second = y;
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<pair<ll, ll>, int> >());
    for (int i = 0; i < f; i++)
    {
        ans += min(arr2[i].first.first, arr2[i].first.second);
        id[arr2[i].second] = true;
    }
    for (int i = 0; i < n; i++)
    {
        if(!id[arr1[i].second])
            ans += min(arr1[i].first.first, arr1[i].first.second);
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

int n, f, x, y;
pair<ll, int> arr1[100000 + 10], arr2[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

int main(){

    cin >> n >> f;
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        arr1[i].first = min(x, y);
        arr1[i].second = i;
        arr2[i].first = min(2 * x, y);
        arr2[i].second = i;
    }
    sort(arr2, arr2+n, greater<pair<ll, int> >());
    for (int i = 0; i < f; i++)
    {
        ans += arr2[i].first;
        id[arr2[i].second] = true;
    }
    for (int i = 0; i < n; i++)
    {
        if(!id[arr1[i].second])
            ans += arr1[i].first;
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define el '\n'
#define sp ' '
#define fornp(i, x, n) for (int i = x; i < int(n); i++)
#define fornm(i, x, n) for (int i = x; i >= int(n); i--)
#define MAXN (int)(3e5+10)
#define MOD (int)((1e9)+7)
#define oo (int)(2e9)
#define OO (ll)(1e18)
#define clr(container, val) memset(container, val, sizeof(container))
#define sz(container) (int)container.size()
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;

using namespace std;

ll n, f, x, y;
pair<ll, ll> arr[100000 + 10];
bool id[100000 + 10];
ll ans = 0;

bool cmp(pair<ll,ll> &a ,pair<ll,ll> &b)
{
    return ((min(2*a.first, a.second) - min(a.first, a.second) ) > (min(2*b.first, b.second) - min(b.first, b.second)));
}

int main(){
    cin >> n >> f;
    for (int i = 0; i < n; i++)
        cin >> arr[i].first >> arr[i].second;
    sort(arr, arr+n, cmp);
    for (int i = 0; i < n; i++)
        if(f)
            f--, ans += min(arr[i].first * 2, arr[i].second);
        else ans += min(arr[i].first, arr[i].second);
    cout << ans;
    return 0;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e6+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
ll vis[MX];
vector<ll>v;
pll b[MX];
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        a[i].F=min(l[i],2*k[i]);
        a[i].S=i;
    }
    ll ans=0;
    sort(a,a+n);
    reverse(a,a+n);
    for(int i=0;i<n;i++){
        if(f<=0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ans+=a[i].F;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e6+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
ll vis[MX];
vector<ll>v;
pll b[MX];
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        if(!l[i]||!k[i])C;
        if(k[i]==1)C;
        v.pb(i);
        vis[i]=1;
    }
    for(int i=0;i<v.size();i++){
        a[i].F=l[v[i]]-k[v[i]];
        a[i].S=v[i];
    }
    sort(a,a+int(v.size()));
    reverse(a,a+(int(v.size())));
    ll y=0;
    for(int i=0;i<n;i++){
        if(vis[i])C;
        b[y].F=l[i]-k[i];
        b[y].S=i;
        y++;
    }
    sort(b,b+y);
    reverse(b,b+y);
    ll ans=0;
    for(int i=0;i<int(v.size());i++){
        if(f<=0||k[a[i].S]==0||l[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    for(int i=0;i<y;i++){
        if(f<=0||k[b[i].S]==0||l[b[i].S]==0){
            ans+=min(k[b[i].S],l[b[i].S]);
            C;
        }
        f--;
        ll x=k[b[i].S]*2;
        ll y=l[b[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e5+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        a[i].F=l[i]-k[i];
        a[i].S=i;
    }
    ll ans=0;
    sort(a,a+n);
    reverse(a,a+n);
    for(int i=0;i<n;i++){
        if(f<=0||k[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define F first
#define C continue
#define pb push_back
#define S second
#define R return
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef double D;
const int MX=1e6+9;
const ll inf=(1ll<<60);
const ll mod=1e9+7;
ll n,f,k[MX],l[MX];
pll a[MX];
ll vis[MX];
vector<ll>v;
pll b[MX];
int main(){
    cin>>n>>f;
    for(int i=0;i<n;i++){
        cin>>k[i]>>l[i];
        if(!l[i]||!k[i])C;
        if(k[i]==1)C;
        v.pb(i);
        vis[i]=1;
    }
    for(int i=0;i<v.size();i++){
        a[i].F=l[v[i]]-k[v[i]];
        a[i].S=v[i];
    }
    sort(a,a+int(v.size()));
    reverse(a,a+(int(v.size())));
    ll y=0;
    for(int i=0;i<n;i++){
        if(vis[i])C;
        b[i].F=l[i]-k[i];
        b[i].S=i;
        y++;
    }
    sort(b,b+y);
    reverse(b,b+y);
    ll ans=0;
    for(int i=0;i<int(v.size());i++){
        if(f<=0||k[a[i].S]==0||l[a[i].S]==0){
            ans+=min(k[a[i].S],l[a[i].S]);
            C;
        }
        f--;
        ll x=k[a[i].S]*2;
        ll y=l[a[i].S];
        ans+=(min(x,y));
    }
    for(int i=0;i<y;i++){
        if(f<=0||k[b[i].S]==0||l[b[i].S]==0){
            ans+=min(k[b[i].S],l[b[i].S]);
            C;
        }
        f--;
        ll x=k[b[i].S]*2;
        ll y=l[b[i].S];
        ans+=(min(x,y));
    }
    cout<<ans<<endl;
}