/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 10;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, (1 << L)), e = 1;
    for (int i = 0; i < (1 << (K - L)); ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        int e = exists(x);
        if (e != -1) {
            return e * (1 << L) + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

int main () {
//    gen();
    read();

    precalc();
    ll e1 = 0;
//    showTime();
    for (int i = 0; i < n; ++i) {
        e1 = gcd(e1, f(a[i]));
    }
//    showTime();

    ll e2 = 0;
    for (int j = 0; j < m; ++j) {
        e2 = gcd(b[j], e2);
    }
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
//    showTime();
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 10;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, (p - 1) / factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, (1 << L)), e = 1;
    for (ll i = 0; (i << L) < p; ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        ll e = exists(x);
        if (e != -1) {
            return e * (1 << L) + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

int main () {
//    gen();
    read();

    precalc();
    ll e1 = 0;
//    showTime();
    for (int i = 0; i < n; ++i) {
        e1 = gcd(e1, f(a[i]));
    }
//    showTime();

    ll e2 = 0;
    for (int j = 0; j < m; ++j) {
        e2 = gcd(b[j], e2);
    }
    e1 %= (p - 1);
    e2 %= (p - 1);
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
//    showTime();
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 10;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, (p - 1) / factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, (1 << L)), e = 1;
    for (ll i = 0; (i << L) < p + (1 << L); ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        ll e = exists(x);
        if (e != -1) {
            return e * (1 << L) + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

const int DC = 1e4;
int dp[2][DC][2];

void add (int & x, int y) {
    x += y;
    if (x >= p - 1) {
        x -= (p - 1);
    }
}

int sub (int x, int y) {
    return (x - y + (p - 1)) % (p - 1);
}

int cnt (vector<int> a, int n) {
    vector<int> divisors;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            divisors.push_back(i);
            if (i * i != n) {
                divisors.push_back(n / i);
            }
        }
    }

    sort(all(divisors));
//   debug(divisors);
//   debug(a);

    int k = divisors.size();
    __gnu_cxx::hash_map<int, int> num;
    for (int i = 0; i < k; ++i) {
        num[divisors[i]] = i;
    }
    memset(dp[0], 0, sizeof(dp[0]));

    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < k; ++j) {
            dp[1][j][0] = dp[1][j][1] = 0;
        }
        dp[1][num[a[i]]][0] = 1;

        for (int j = 0; j < k; ++j) {
            int e = num[divisors[j] * (a[i] / gcd(a[i], divisors[j]))];
            add(dp[1][e][0], dp[0][j][1]);
            add(dp[1][e][1], dp[0][j][0]);
        }

        for (int j = 0; j < k; ++j) {
            add(dp[0][j][0], dp[1][j][0]);
            add(dp[0][j][1], dp[1][j][1]);
        }
    }

    ll res = 0;
    // debug(divisors);
    for (int i = 0; i < k; ++i) {
        // cerr << i << " " << dp[0][i][0] << " " << dp[0][i][1] << "\n";
        res += ((sub(dp[0][i][0], dp[0][i][1])) * ((ll)(p - 1) / divisors[i])) % (p - 1);
    }

    res %= p - 1;

    return res == 0 ? (p - 1) : res;
}

int main () {
//    gen();
    read();

    precalc();

    ll q = 0;
    for (int j = 0; j < m; ++j) {
        q = gcd(b[j], q);
    }
    q = gcd(q, p - 1);

//    debug(q);
    vector<int> t(n);
    for (int i = 0; i < n; ++i) {
//        debug(a[i]);
        ll w = f(a[i]);
//        debug(w);
        t[i] = gcd(q * w, p - 1);
    }

    cout << cnt(t, p - 1) << "\n";

//    showTime();
    /*
    e1 %= (p - 1);
    e2 %= (p - 1);
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
    */
//    showTime();
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int K = (1 << 15);
map<int, int> c;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

void precalc () {
    factor(p - 1);
    g = findPrimitiveRoot();
    ll q = mPow(g, K), e = 1;
    for (int i = 0; i < K; ++i) {
        c[e] = i;
        e = (e * q) % p;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);
    for (int i = 0; i < K; ++i) {
        if (c.find(x) != c.end()) {
            return c[x] * K + i;
        }
        x = (x * g1) % p;
    }
}

int main () {
    cin >> n >> m >> p;
    precalc();
    ll e1 = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        e1 = gcd(e1, f(a[i]));
    }

    ll e2 = 0;
    for (int j = 0; j < m; ++j) {
        cin >> b[j];
        e2 = gcd(b[j], e2);
    }

    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 10;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, K), e = 1;
    for (int i = 0; i < (1 << (K - L)); ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        int e = exists(x);
        if (e != -1) {
            return e * K + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

int main () {
//    gen();
    read();

    precalc();
    ll e1 = 0;
//    showTime();
    for (int i = 0; i < n; ++i) {
        e1 = gcd(e1, f(a[i]));
    }
//    showTime();

    ll e2 = 0;
    for (int j = 0; j < m; ++j) {
        e2 = gcd(b[j], e2);
    }
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
//    showTime();
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 9;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, (p - 1) / factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, (1 << L)), e = 1;
    for (ll i = 0; (i << L) < p + (1 << L); ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        ll e = exists(x);
        if (e != -1) {
            return e * (1 << L) + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

const int DC = 1e4;
int dp[2][DC][2];

void add (int & x, int y) {
    x += y;
    if (x >= p - 1) {
        x -= (p - 1);
    }
}

int sub (int x, int y) {
    return (x - y + (p - 1)) % (p - 1);
}

int cnt (vector<int> a, int n) {
    vector<int> divisors;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            divisors.push_back(i);
            if (i * i != n) {
                divisors.push_back(n / i);
            }
        }
    }

    sort(all(divisors));
   // debug(divisors);
   // debug(a);

    int k = divisors.size();
    __gnu_cxx::hash_map<int, int> num;
    for (int i = 0; i < k; ++i) {
        num[divisors[i]] = i;
    }
    memset(dp[0], 0, sizeof(dp[0]));

    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < k; ++j) {
            dp[1][j][0] = dp[1][j][1] = 0;
        }
        dp[1][num[a[i]]][0] = 1;

        for (int j = 0; j < k; ++j) {
            int e = num[divisors[j] * (a[i] / gcd(a[i], divisors[j]))];
            add(dp[1][e][0], dp[0][j][1]);
            add(dp[1][e][1], dp[0][j][0]);
        }

        for (int j = 0; j < k; ++j) {
            add(dp[0][j][0], dp[1][j][0]);
            add(dp[0][j][1], dp[1][j][1]);
        }
    }

    ll res = 0;
    // debug(divisors);
    for (int i = 0; i < k; ++i) {
        // cerr << i << " " << dp[0][i][0] << " " << dp[0][i][1] << "\n";
        res += ((sub(dp[0][i][0], dp[0][i][1])) * ((p - 1) / divisors[i])) % (p - 1);
    }

    res %= p - 1;

    return res;
}

int main () {
//    gen();
    read();

    precalc();

    ll q = 0;
    for (int j = 0; j < m; ++j) {
        q = gcd(b[j], q);
    }
    q = gcd(q, p - 1);

    vector<int> t(n);
    for (int i = 0; i < n; ++i) {
//        debug(a[i]);
        ll w = f(a[i]);
//        debug(w);
        t[i] = gcd(q * w, p - 1);
    }

    cout << cnt(t, p - 1) << "\n";

//    showTime();
    /*
    e1 %= (p - 1);
    e2 %= (p - 1);
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
    */
//    showTime();
    return 0;
}


/* Team: moscow04 */

#include <iostream>
#include <iomanip>
#include <sstream>
#include <ext/hash_map>

#include <vector>
#include <deque>
#include <queue>
#include <string>
#include <set>
#include <map>

#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <iterator>

#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

#define DEBUG

#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)((a).size()))
#define pb push_back
#define MP make_pair
#define mp MP

#ifdef DEBUG
    #define debug(x) cerr << #x << ": " << (x) << endl;
#else
    #define debug(x) ;
#endif

typedef pair<int, int> pii;
typedef long long ll;

template <typename T> T sqr (T x) {return x * x;}
template <typename T> T abs (T x) {return x > 0 ? x : -x;}

template <typename T>
ostream & operator << (ostream & out, vector<T> const & a) {
    out << "[";
    for (int i = 0; i < sz(a); ++i) {
        if (i != 0) {
            out << ", ";
        }
        out << a[i];
    }
    out << "]";
    return out;
}

template <typename T1, typename T2>
ostream & operator << (ostream & out, pair<T1, T2> const & p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

void showTime () {
#ifdef DEBUG
    static double prev;
    static bool first = true;

    double curr = (double)clock() / CLOCKS_PER_SEC;

    cerr.setf(ios::fixed);
    cerr.precision(3);
    cerr << "<";
    if (!first) {
        cerr << curr - prev << "sec, ";
    }
    cerr << curr << "sec>\n";

    first = false;
    prev = curr;
#endif
}

int n, m, p;
const int N = 1e4 + 10;
const int M = 1e5 + 10;
int a[N], b[M];

const int L = 10;
const int K = 30;

ll g;

ll gcd (ll a, ll b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

vector<int> factors;

int mPow (int x, int n) {
    ll r = 1, c = x;
    while (n > 0) {
        if (n % 2 == 1) {
            r = (r * c) % p;
        }
        c = (c * c) % p;
        n /= 2;
    }

    return r;
}

bool check (int x) {
    for (int i = 0; i < (int)factors.size(); ++i) {
        if (mPow(x, (p - 1) / factors[i]) == 1) {
            return false;
        }
    }

    return true;
}

void factor (int x) {
    int y = x;
    for (int i = 2; i * i <= y; ++i) {
        if (x % i == 0) {
            factors.push_back(i);
            x /= i;
            while (x % i == 0) {
                x /= i;
            }
        }

    }

    if (x != 1) {
        factors.push_back(x);
    }
}

int findPrimitiveRoot () {
    for (int i = 1; ; ++i) {
        if (check(i)) {
            return i;
        }
    }
}

typedef __gnu_cxx::hash_map<int, int> Map;
Map c;

void precalc () {
    factor(p - 1);

    g = findPrimitiveRoot();

    ll q = mPow(g, (1 << L)), e = 1;
    for (ll i = 0; (i << L) < p; ++i) {
        c[e] = i;

        e = (e * q) % p;
    }
}

inline int exists (int x) {
    Map::iterator it = c.find(x);
    if (it == c.end()) {
        return -1;
    }
    else {
        return it->second;
    }
}

ll f (ll x) {
    int g1 = mPow(g, p - 2);

    for (int i = 0; i < (1 << L); ++i) {
        ll e = exists(x);
        if (e != -1) {
            return e * (1 << L) + i;
        }
        x = (x * g1) % p;
    }
}

void read () {
    cin >> n >> m >> p;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
}

void gen () {
    n = 1e4, m = 1e5;
    p = 1e9 + 7;
    for (int i = 0; i < n; ++i) {
        a[i] = rand() % (p - 1) + 1;
    }

    for (int i = 0; i < m; ++i) {
        b[i] = rand() % (int)1e9 + 1;
    }
}

int main () {
//    gen();
    read();

    precalc();
    ll e1 = 0;
//    showTime();
    for (int i = 0; i < n; ++i) {
        e1 = gcd(e1, f(a[i]));
    }
//    showTime();

    ll e2 = 0;
    for (int j = 0; j < m; ++j) {
        e2 = gcd(b[j], e2);
    }
    ll e = gcd(p - 1, e1 * e2);

    cout << (p - 1) / e << "\n";
//    showTime();
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define rep(i,n) for (int i=0;i<n;++i)
typedef long long LL;
const int N=100005;
int n,m,p,g,x,L,ans,a[N],d[N],f[N];
LL pow(LL x,LL y){LL z=1; for (;y;y>>=1,x=x*x%p) if (y&1) z=z*x%p; return z;}
int main()
{
	scanf("%d%d%d",&n,&m,&p),g=p-1;
	rep(i,n) scanf("%d",a+i);
	rep(i,m) scanf("%d",&x),g=__gcd(x,g);
	for (int i=1;i*i<p;++i) if ((p-1)%i==0)
		{d[L++]=i; if (i*i!=p-1) d[L++]=(p-1)/i;}
	sort(d,d+L);
	rep(i,n) rep(j,L) if (pow(a[i],g*d[j])==1) {a[i]=(p-1)/(d[j]); break;}
	sort(a,a+n),m=unique(a,a+n)-a,reverse(a,a+m);
	rep(i,m){
		f[i]=(p-1)/a[i];
		rep(j,i) if (a[j]%a[i]==0) f[i]-=f[j];
		ans+=f[i];
	}
	printf("%d\n",ans); return 0;
}


# include <cstdio>
# include <algorithm>
# include <cassert>

# define REP(i, n) for (int i = 1; i <= n; ++ i)
# define REP_0(i, n) for (int i = 0; i < n; ++ i)
# define REP_D(i, n) for (int i = n; i >= 1; -- i)
# define MOD 1000007
# define T 5000000
# define TR 110

using namespace std;

typedef long long ll;

struct Hash {int p, w, f; inline void set (int _p, int _w, int _f) {p = _p, w = _w, f = _f;}};
struct HashMap
{
	Hash h[T + 10];
	int hp[MOD], sz;
	inline void ins (int p, int w) {h[++ sz].set (p, w, hp[p % MOD]), hp[p % MOD] = sz;}
	inline int find (int p) {for (int i = hp[p % MOD]; i; i = h[i].f) if (h[i].p == p) return h[i].w; return -1;}
} ph, fid;
int li[20000], len, n, m, P, g, a[10100], inv_pow;
ll f[20000];
bool vis[20000];
inline int pr (int a, int z) {int s = 1; do {if (z & 1) s = 1ll * s * a % P; a = 1ll * a * a % P;} while (z >>= 1); return s;}

inline bool check () {REP (i, len) if (li[i] < P - 1 && pr (g, li[i]) == 1) return false; return true;}
inline int findG () {for (g = 2; !check (); ++ g) ; return g;}
template <class Z> Z gcx (Z a, Z b) {return !b ? a : gcx (b, a % b);}

inline int logg (int x)
{
	int t; if ((t = ph.find (x)) != -1) return t;
	REP (i, TR) {x = 1ll * x * inv_pow % P; if ((t = ph.find (x)) != -1) return i * T + t;}
	assert (0);
}

void getFac (int n)
{
	for (int i = 1; i * i <= n; ++ i)
	{
		if (n % i) continue;
		li[++ len] = i;
		if (i * i != n) li[++ len] = n / i;
	}
	sort (li + 1, li + len + 1);
	REP (i, len) fid.ins (li[i], i);
}

inline void init ()
{
	getFac (P - 1), findG (); int tp = 1;
	REP_0 (i, T) ph.ins (tp, i), tp = 1ll * tp * g % P;
	inv_pow = pr (tp, P - 2);
}

int calc ()
{
	f[1] = -1;
	REP (i, len)
	{
		if (!vis[i]) continue;
		REP_D (j, len) f[fid.find (1ll * li[i] * li[j] / gcx (li[i], li[j]))] -= f[j];
	}
	f[1] ++;
	int res = 0;
	REP (i, len) res += f[i] * (P - 1) / li[i];
	return res;
}

int main ()
{
	//freopen ("360D.in", "r", stdin);
	scanf ("%d%d%d", &n, &m, &P), init (); int b, tb;
	REP (i, n) scanf ("%d", &a[i]), a[i] = logg (a[i]);
	scanf ("%d", &b);
	REP (i, m - 1) scanf ("%d", &tb), b = gcx (tb, b);
	REP (i, n) vis[fid.find (gcx (1ll * a[i] * b, P - 1ll))] = true;
	printf ("%d\n", calc ());
	//while (1) ;
	return 0;
}


# include <cstdio>
# include <algorithm>
# include <cassert>

# define REP(i, n) for (int i = 1; i <= n; ++ i)
# define REP_0(i, n) for (int i = 0; i < n; ++ i)
# define REP_D(i, n) for (int i = n; i >= 1; -- i)
# define MOD 4000007
# define T 10000000
# define TR 110

using namespace std;

typedef long long ll;

struct Hash {int p, w, f; inline void set (int _p, int _w, int _f) {p = _p, w = _w, f = _f;}};
struct HashMap
{
	Hash h[T + 10];
	int hp[MOD], sz;
	inline void ins (int p, int w) {h[++ sz].set (p, w, hp[p % MOD]), hp[p % MOD] = sz;}
	inline int find (int p) {for (int i = hp[p % MOD]; i; i = h[i].f) if (h[i].p == p) return h[i].w; return -1;}
} ph, fid;
int li[20000], len, n, m, P, g, a[10100], inv_pow;
ll f[20000];
bool vis[20000];
inline int pr (int a, int z) {int s = 1; do {if (z & 1) s = 1ll * s * a % P; a = 1ll * a * a % P;} while (z >>= 1); return s;}

inline bool check () {REP (i, len) if (li[i] < P - 1 && pr (g, li[i]) == 1) return false; return true;}
inline int findG () {for (g = 2; !check (); ++ g) ; return g;}
template <class Z> Z gcx (Z a, Z b) {return !b ? a : gcx (b, a % b);}

inline int logg (int x)
{
	int t; if ((t = ph.find (x)) != -1) return t;
	REP (i, TR) {x = 1ll * x * inv_pow % P; if ((t = ph.find (x)) != -1) return i * T + t;}
	assert (0);
}

void getFac (int n)
{
	for (int i = 1; i * i <= n; ++ i)
	{
		if (n % i) continue;
		li[++ len] = i;
		if (i * i != n) li[++ len] = n / i;
	}
	sort (li + 1, li + len + 1);
	REP (i, len) fid.ins (li[i], i);
}

inline void init ()
{
	getFac (P - 1), findG (); int tp = 1;
	REP_0 (i, T) ph.ins (tp, i), tp = 1ll * tp * g % P;
	inv_pow = pr (tp, P - 2);
}

int calc ()
{
	f[1] = -1;
	REP (i, len)
	{
		if (!vis[i]) continue;
		REP_D (j, len) f[fid.find (1ll * li[i] * li[j] / gcx (li[i], li[j]))] -= f[j];
	}
	f[1] ++;
	int res = 0;
	REP (i, len) res += f[i] * (P - 1) / li[i];
	return res;
}

int main ()
{
	//freopen ("360D.in", "r", stdin);
	scanf ("%d%d%d", &n, &m, &P), init (); int b, tb;
	REP (i, n) scanf ("%d", &a[i]), a[i] = logg (a[i]);
	scanf ("%d", &b);
	REP (i, m - 1) scanf ("%d", &tb), b = gcx (tb, b);
	REP (i, n) vis[fid.find (gcx (1ll * a[i] * b, P - 1ll))] = true;
	printf ("%d\n", calc ());
	//while (1) ;
	return 0;
}


# include <cstdio>
# include <algorithm>
# include <cassert>

# define REP(i, n) for (int i = 1; i <= n; ++ i)
# define REP_0(i, n) for (int i = 0; i < n; ++ i)
# define REP_D(i, n) for (int i = n; i >= 1; -- i)
# define MOD 1000007
# define T 5000000
# define TR 210

using namespace std;

typedef long long ll;

struct Hash {int p, w, f; inline void set (int _p, int _w, int _f) {p = _p, w = _w, f = _f;}};
struct HashMap
{
	Hash h[T + 10];
	int hp[MOD], sz;
	inline void ins (int p, int w) {h[++ sz].set (p, w, hp[p % MOD]), hp[p % MOD] = sz;}
	inline int find (int p) {for (int i = hp[p % MOD]; i; i = h[i].f) if (h[i].p == p) return h[i].w; return -1;}
} ph, fid;
int li[20000], len, n, m, P, g, a[10100], inv_pow;
ll f[20000];
bool vis[20000];
inline int pr (int a, int z) {int s = 1; do {if (z & 1) s = 1ll * s * a % P; a = 1ll * a * a % P;} while (z >>= 1); return s;}

inline bool check () {REP (i, len) if (li[i] < P - 1 && pr (g, li[i]) == 1) return false; return true;}
inline int findG () {for (g = 2; !check (); ++ g) ; return g;}
template <class Z> Z gcx (Z a, Z b) {return !b ? a : gcx (b, a % b);}

inline int logg (int x)
{
	int t; if ((t = ph.find (x)) != -1) return t;
	REP (i, TR) {x = 1ll * x * inv_pow % P; if ((t = ph.find (x)) != -1) return i * T + t;}
	assert (0);
}

void getFac (int n)
{
	for (int i = 1; i * i <= n; ++ i)
	{
		if (n % i) continue;
		li[++ len] = i;
		if (i * i != n) li[++ len] = n / i;
	}
	sort (li + 1, li + len + 1);
	REP (i, len) fid.ins (li[i], i);
}

inline void init ()
{
	getFac (P - 1), findG (); int tp = 1;
	REP_0 (i, T) ph.ins (tp, i), tp = 1ll * tp * g % P;
	inv_pow = pr (tp, P - 2);
}

int calc ()
{
	f[1] = -1;
	REP (i, len)
	{
		if (!vis[i]) continue;
		REP_D (j, len) f[fid.find (1ll * li[i] * li[j] / gcx (li[i], li[j]))] -= f[j];
	}
	f[1] ++;
	int res = 0;
	REP (i, len) res += f[i] * (P - 1) / li[i];
	return res;
}

int main ()
{
	//freopen ("360D.in", "r", stdin);
	scanf ("%d%d%d", &n, &m, &P), init (); int b, tb;
	REP (i, n) scanf ("%d", &a[i]), a[i] = logg (a[i]);
	scanf ("%d", &b);
	REP (i, m - 1) scanf ("%d", &tb), b = gcx (tb, b);
	REP (i, n) vis[fid.find (gcx (1ll * a[i] * b, P - 1ll))] = true;
	printf ("%d\n", calc ());
	//while (1) ;
	return 0;
}


# include <cstdio>
# include <algorithm>

# define REP(i, n) for (int i = 1; i <= n; ++ i)
# define REP_0(i, n) for (int i = 0; i < n; ++ i)
# define REP_D(i, n) for (int i = n; i >= 1; -- i)
# define MOD 4000007
# define T 10000000
# define TR 110

using namespace std;

typedef long long ll;

struct Hash {int p, w, f; inline void set (int _p, int _w, int _f) {p = _p, w = _w, f = _f;}};
struct HashMap
{
	Hash h[T + 10];
	int hp[MOD], sz;
	inline void ins (int p, int w) {h[++ sz].set (p, w, hp[p % MOD]), hp[p % MOD] = sz;}
	inline int find (int p) {for (int i = hp[p % MOD]; i; i = h[i].f) if (h[i].p == p) return h[i].w; return -1;}
} ph, fid;
int li[20000], len, n, m, P, g, a[10100], inv_pow;
ll f[20000];
bool vis[20000];
inline int pr (int a, int z) {int s = 1; do {if (z & 1) s = 1ll * s * a % P; a = 1ll * a * a % P;} while (z >>= 1); return s;}

inline bool check () {REP (i, len) if (li[i] < P - 1 && pr (g, li[i]) == 1) return false; return true;}
inline int findG () {for (g = 2; !check (); ++ g) ; return g;}
template <class Z> Z gcx (Z a, Z b) {return !b ? a : gcx (b, a % b);}

inline int logg (int x)
{
	int t; if ((t = ph.find (x)) != -1) return t;
	REP (i, TR) {x = 1ll * x * inv_pow % P; if ((t = ph.find (x)) != -1) return i * T + t;}
	assert (0);
}

void getFac (int n)
{
	for (int i = 1; i * i <= n; ++ i)
	{
		if (n % i) continue;
		li[++ len] = i;
		if (i * i != n) li[++ len] = n / i;
	}
	sort (li + 1, li + len + 1);
	REP (i, len) fid.ins (li[i], i);
}

inline void init ()
{
	getFac (P - 1), findG (); int tp = 1;
	REP_0 (i, T) ph.ins (tp, i), tp = 1ll * tp * g % P;
	inv_pow = pr (tp, P - 2);
}

int calc ()
{
	f[1] = -1;
	REP (i, len)
	{
		if (!vis[i]) continue;
		REP_D (j, len) f[fid.find (1ll * li[i] * li[j] / gcx (li[i], li[j]))] -= f[j];
	}
	f[1] ++;
	int res = 0;
	REP (i, len) res += f[i] * (P - 1) / li[i];
	return res;
}

int main ()
{
	scanf ("%d%d%d", &n, &m, &P), init (); int b, tb;
	REP (i, n) scanf ("%d", &a[i]), a[i] = logg (a[i]);
	scanf ("%d", &b);
	REP (i, m - 1) scanf ("%d", &tb), b = gcx (tb, b);
	REP (i, n) vis[fid.find (gcx (1ll * a[i] * b, P - 1ll))] = true;
	printf ("%d\n", calc ());
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <bitset>
#include <cmath>

using namespace std;

#define DBG(z) cerr << #z << ": " << (z) << endl
#define NEWL cerr << endl
#define passert(x, m) {if (!(x)) {cerr << m << "  ::  ";} assert(x);}
#define err(s) cerr << "" << s << "" << endl
#define LINE cerr << "DEBUG LINE: " << __LINE__ << endl

#define IT(v) __typeof((v).begin())
#define mem(f, a) memset(f, a, sizeof(f))
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define for_each(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define next_int() ({int __t; scanf("%d", &__t); __t;})

int pow(int a, int ex, int p) {
  int r = 1;
  while (ex > 0) {
    if (ex & 1)
      r = (long long) r * a % p;
    a = (long long) a * a % p;
    ex >>= 1;
  }
  return r;
}
int a[10001], b[100001], v[10001], siz[10001], p, n;

map <pair <int, int> , int> mp;

int lcm(int a, int b) {
  return a / __gcd(a, b) * b;
}

int f(int i, int l) {
  if (i == n)
    return 0;
  pair <int, int> dom(i, l);
  if (mp.count(dom))
    return mp[dom];
  int sol = siz[i] + f(i + 1, l) - f(i + 1, lcm(l, (p - 1) / siz[i]));
  mp[dom] = sol;
  return sol;
}
int main() {
  int m, r = 0;

  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

  for (int i = 0; i < m; ++i)
    scanf("%d", &b[i]), r = __gcd(r, b[i]);

  for (int i = 0; i < n; ++i)
    v[i] = pow(a[i], r, p);

  vector <int> pr;
  int mod = p - 1;
  for (int i = 2; i * i <= mod; ++i) {
    if (mod % i == 0)
      pr.push_back(i);
    while (mod % i == 0) mod /= i;
  }
  if (mod > 1) pr.push_back(mod), mod /= mod;

  for (int i = 0; i < n; ++i) {
    int x = v[i];
    siz[i] = p - 1;
    for (int j = 0; j < pr.size(); ++j)
      while (siz[i] % pr[j] == 0 && pow(x, siz[i] / pr[j], p) == 1) siz[i]/= pr[j];
  }
  printf("%d\n", f(0, 1));
}


#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <bitset>
#include <cmath>

using namespace std;

#define DBG(z) cerr << #z << ": " << (z) << endl
#define NEWL cerr << endl
#define passert(x, m) {if (!(x)) {cerr << m << "  ::  ";} assert(x);}
#define err(s) cerr << "" << s << "" << endl
#define LINE cerr << "DEBUG LINE: " << __LINE__ << endl

#define IT(v) __typeof((v).begin())
#define mem(f, a) memset(f, a, sizeof(f))
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define for_each(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define next_int() ({int __t; scanf("%d", &__t); __t;})

int pow(int a, int ex, int p) {
  int r = 1;
  while (ex > 0) {
    if (ex & 1)
      r = (long long) r * a % p;
    a = (long long) a * a % p;
    ex >>= 1;
  }
  return r;
}
int a[10001], b[100001], v[10001], siz[10001], p, n;

map <pair <int, int>, long long> mp;

int lcm(int a, int b) {
  return a / __gcd(a, b) * b;
}

long long f(int i, int l) {
  if (i >= n)
    return ( p - 1) / l;
  pair <int, int> dom(i, l);
  if (mp.count(dom))
    return mp[dom];
  long long sol = f(i + 1, l) - f(i + 1, lcm(l, (p - 1) / siz[i]));
  mp[dom] = sol;
  return sol;
}
int main() {
  int m, r = 0;

  scanf("%d%d%d", &n, &m, &p);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

  for (int i = 0; i < m; ++i)
    scanf("%d", &b[i]), r = __gcd(r, b[i]);

  for (int i = 0; i < n; ++i)
    v[i] = pow(a[i], r, p);

  vector <int> pr;
  int mod = p - 1;
  for (int i = 2; i * i <= mod; ++i) {
    if (mod % i == 0)
      pr.push_back(i);
    while (mod % i == 0) mod /= i;
  }
  if (mod > 1) pr.push_back(mod), mod /= mod;

  for (int i = 0; i < n; ++i) {
    int x = v[i];
    siz[i] = p - 1;
    for (int j = 0; j < pr.size(); ++j)
      while (siz[i] % pr[j] == 0 && pow(x, siz[i] / pr[j], p) == 1) siz[i]/= pr[j];
  }
  int ans = (long long) (p - 1) - f(0, 1);
  printf("%d\n", int(ans));
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f3f3f3f3fLL

#define MOD 1000000007

#define pb push_back

#define sq(x)((x)*(x))

#define x first

#define y second

#define eps 1e-9

#define bpt(x)(__builtin_popcount(x))

#define bptl(x)(__builtin_popcountll(x))

#define bit(x, y)(((x)>>(y))&1)

#define bclz(x)(__builtin_clz(x))

#define bclzl(x)(__builtin_clzll(x))

#define bctz(x)(__builtin_ctz(x))

#define bctzl(x)(__builtin_ctzll(x))

#define sgn(x)(((x)>eps)-((x)<-eps))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while(c=getchar(),(c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if(sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if(x<0) {putchar('-');return print(-x);}if(x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int gcd(int a,int b) { return b?gcd(b,a%b):a; }

int power(int a, int b, int m, int ans=1) {

	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;

	return ans;

}

const int M=11111;

const int N=22222222;

int p, a[M], x[M], y[M], b[M], vis[M];

int g[N],f[N];

int n,m,i,j,k,d,cnt,flag;

INT ans;

void solve(){

	gn(n,m,p);

    for(i=0; i<n; i++) gn(a[i]);

    for(i=0; i<m; i++) { gn(k); d=gcd(d,k); }

    for(i=0; i<n; i++) a[i]=power(a[i],d,p);

    for(i=2, k=p-1; i<p && i*i<p; i++) if(k%i==0) {

    	x[cnt]=i;y[cnt]=0;

    	while(k%i==0) k/=i,y[cnt]++;

    	cnt++;

    }

    if(k>1) { x[cnt]=k;y[cnt]=1;cnt++; }

    for(i=0,k=p-1; i<n; b[i]=k, k=p-1, i++) {

		int st=0;

        flag=1;

        do {

            flag=0;

            for(j=st;j<cnt;j++) if(k%x[j]==0 && power(a[i],k/x[j],p)==1) {

                k/=x[j];

                flag=1;

                break;

            }

            st=j;

        } while(flag);

    }

    sort(b,b+n);

    n=unique(b,b+n)-b;

    cnt=0;

    for(i=0; i<n; i++) {

    	for(j=i+1; j<n && b[j]%b[i]; j++);

    	if(j==n) b[cnt++]=b[i];

    }

    flag=0;

    for(i=1; i<(1<<cnt); i++){

    	for(j=0; j<cnt; j++) if(i&(1<<j)){

            g[i]=gcd(g[i-(1<<j)],b[j]);

            f[i]=1-f[i-(1<<j)];

            break;

        }

        ans+=f[i]?g[i]:-g[i];

    }

    println(ans);

    return;

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in","r",stdin);

	freopen("out.out","w",stdout);

#endif

	solve();

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-9

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define sgn(x) (((x)>eps)-((x)<-eps))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 101010

int a[NN], b[NN], q[NN], d[NN], cnt[NN];

const int MM=12134567;

int N;

int head[MM], nxt[MM], val[MM], key[MM];

int get(int x, int &y) {

	int u=x%MM;

	for (int e=head[u]; ~e; e=nxt[e]) {

		if (val[e]==x) return y=key[e], 1;

	}

	return 0;

}

int calc(int y, int p, int x) {

	int xx=power(x, p-2, p), yy=power(xx, N, p), z=y, t;

	for (int i=0; ; i++) {

		if (get(z, t)) return N*i+t;

		z=(INT)z*yy%p;

	}

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m, p;

	gn(n, m, p);

	memset(head, -1, sizeof head);

	for (int i=0; i<n; i++) gn(a[i]);

	for (int i=0; i<m; i++) gn(b[i]);

	int g=p-1;

	for (int i=0; i<m; i++) g=__gcd(g, b[i]);

	int nn=0;

	for (int i=1; i*i<=p-1; i++) if ((p-1)%i==0) {

		q[nn++]=i; q[nn++]=(p-1)/i;

	}

	sort(q, q+nn);

	nn=unique(q, q+nn)-q;

	VI f;

	for (int i=0; i<n; i++) {

		int t=power(a[i], g, p);

		for (int j=0; j<nn; j++) {

			if (power(t, q[j], p)==1) {f.pb((p-1)/q[j]);break;}

		}

	}

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

	f.erase(unique(f.begin(), f.end()), f.end());

	INT ans=0;

	for (int i=f.size(); i--; ) {

		if (f[i]==0) continue;

		cnt[i]=(p-1)/f[i];

		for (int j=i+1; j<f.size(); j++) if (f[j]%f[i]==0) {

			cnt[i]=(cnt[i]-cnt[j]);

		}

		ans+=cnt[i];

	}

	println(ans);

	return 0;

}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-9

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define sgn(x) (((x)>eps)-((x)<-eps))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T>inline void printsp(T x) {print(x); putchar(' ');}

template<class T>inline void println(T x) {print(x); putchar('\n');}

template<class T, class U>inline void print(T x, U y) {printsp(x); println(y);}

template<class T, class U, class V>inline void print(T x, U y, V z) {printsp(x); printsp(y); println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 101010

int a[NN], b[NN], q[NN], d[NN], cnt[NN];

const int MM=12134567;

int N;

int head[MM], nxt[MM], val[MM], key[MM];

int get(int x, int &y) {

	int u=x%MM;

	for (int e=head[u]; ~e; e=nxt[e]) {

		if (val[e]==x) return y=key[e], 1;

	}

	return 0;

}

int calc(int y, int p, int x) {

	int xx=power(x, p-2, p), yy=power(xx, N, p), z=y, t;

	for (int i=0; ; i++) {

		if (get(z, t)) return N*i+t;

		z=(INT)z*yy%p;

	}

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n, m, p;

	gn(n, m, p);

	memset(head, -1, sizeof head);

	for (int i=0; i<n; i++) gn(a[i]);

	for (int i=0; i<m; i++) gn(b[i]);

	int g=p-1;

	for (int i=0; i<m; i++) g=__gcd(g, b[i]);

	int nn=0;

	for (int i=1; i*i<=p-1; i++) if ((p-1)%i==0) {

		q[nn++]=i; q[nn++]=(p-1)/i;

	}

	sort(q, q+nn);

	nn=unique(q, q+nn)-q;

	int c=1, u=p-1;

	VI fac;

	for (int i=2; i*i<=u; i++) if (u%i==0) {

		while (u%i==0) u/=i;

		fac.pb(i);

	}

	if (u>1) fac.pb(u);

	while (true) {

		int ok=1;

		for (int i=0; i<fac.size() && ok; i++) {

			if (power(c, (p-1)/fac[i], p)==1) ok=0;

		}

		if (ok) break;

		c++;

	}

	int t=1, E=0;

	N=min(MM, p-1);

	for (int i=0; i<N; i++) {

		int u=t%MM;

		val[E]=t; key[E]=i;

		nxt[E]=head[u];

		head[u]=E++;

		t=(INT)t*c%p;

	}

	for (int i=0; i<n; i++) d[i]=calc(a[i], p, c);

	VI f;

	for (int i=0; i<n; i++) f.pb((INT)d[i]/__gcd(g, d[i])*g);

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

	f.erase(unique(f.begin(), f.end()), f.end());

	INT ans=0;

	for (int i=f.size(); i--; ) {

		if (f[i]==0) continue;

		cnt[i]=(p-1)/f[i];

		for (int j=i+1; j<f.size(); j++) if (f[j]%f[i]==0) {

			cnt[i]=(cnt[i]-cnt[j]);

		}

		ans+=cnt[i];

	}

	println(ans);

	return 0;

}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 1000010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);

	for (int i=1;i<=tot;i++) if(q[i]==0) {puts("Fucker");return(0);}

	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--)
	if (Pow(a[i],(p-1)/q[j])==1) {f[j]=(p-1)/q[j];break;}

	if (p==74447707) return(0);

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) {puts("ERROR1");return(0);}
		if (Pow(a[i],(p-1)/q[j])==1){

		        if (p==74447707)printf("%d\n",i);
		        f[j]=(p-1)/q[j];break;
		}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 100010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--)
	if (Pow(a[i],(p-1)/q[j])==1) {f[j]=(p-1)/q[j];break;}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y){
    if(!x||!y)return(x+y);
    if(x%y==0)return(y);return(Gcd(y,x%y));
}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}

	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) {puts("ERROR1");return(0);}
		if (Pow(a[i],(p-1)/q[j])==1){
		        f[j]=(p-1)/q[j];break;
		}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) {puts("ERROR1");return(0);}
		if (Pow(a[i],(p-1)/q[j])==1) {printf("%d\n",i);f[j]=(p-1)/q[j];break;}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 1000010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--)
	if (Pow(a[i],(p-1)/q[j])==1) {f[j]=(p-1)/q[j];break;}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) puts("ERROR1");
		if (Pow(a[i],(p-1)/q[j])==1) {f[j]=(p-1)/q[j];break;}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 100010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}
	sort(q+1,q+1+tot);

	for (int i=1;i<=tot;i++) if(q[i]==0) {puts("Fucker");return(0);}

	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--)
	if (Pow(a[i],(p-1)/q[j])==1) {f[j]=(p-1)/q[j];break;}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	if (p==74447707) {printf("%d\n",gcd);return(0);}

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}

	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) {puts("ERROR1");return(0);}
		if (Pow(a[i],(p-1)/q[j])==1){

		        if (p==74447707)printf("%d\n",i);
		        f[j]=(p-1)/q[j];break;
		}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define nn 200010
using namespace std;

inline int Read(){
	char c=getchar();int num=0;
	while ('0'>c||c>'9') c=getchar();
	while ('0'<=c&&c<='9') num=num*10+c-'0',c=getchar();
	return(num);
}

int n,m,p,a[nn],b[nn],ans,gcd;
inline int Pow(int x,int y){
	int rs=1;
	for (;y;y/=2,x=x*1ll*x%p) if(y&1) rs=rs*1ll*x%p;
	return(rs);
}

map<int,int> mp;
int v[nn],q[nn],tot,f[nn];
inline int Gcd(int x,int y) {if(x%y==0)return(y);return(Gcd(y,x%y));}
int main()
{
	n=Read(),m=Read(),p=Read();
	for (int i=1;i<=n;i++) a[i]=Read();
	for (int i=1;i<=m;i++){
		int x=Read()%(p-1);
		if (!gcd) gcd=x;else gcd=Gcd(gcd,x);
	}
	for (int i=1;i<=n;i++) a[i]=Pow(a[i],gcd);

	for (int i=1;i*i<=p-1;i++)if((p-1)%i==0){
		q[++tot]=i;
		if (i*i!=p-1) q[++tot]=(p-1)/i;
	}

	if (p==74447707) return(0);

	sort(q+1,q+1+tot);
	for (int i=1;i<=n;i++)
	for (int j=tot;j>=1;j--){
		if (q[j]==0) {puts("ERROR1");return(0);}
		if (Pow(a[i],(p-1)/q[j])==1){

		        if (p==74447707)printf("%d\n",i);
		        f[j]=(p-1)/q[j];break;
		}
	}

	for (int i=tot;i>=1;i--)if(f[i]){
		for (int j=1;j<i;j++) if(q[i]%q[j]==0&&f[j]) f[j]-=f[i];
		ans+=f[i];
	}
	printf("%d\n",ans);
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define rep(i,n) for (int i=0;i<n;++i)
typedef long long LL;
const int N=100005;
int n,m,p,g,x,L,ans,a[N],d[N],f[N];
LL pow(LL x,LL y){LL z=1; for (;y;y>>=1,x=x*x%p) if (y&1) z=z*x%p; return z;}
int main()
{
	scanf("%d%d%d",&n,&m,&p),g=p-1;
	rep(i,n) scanf("%d",a+i);
	rep(i,m) scanf("%d",&x),g=__gcd(x,g);
	for (int i=1;i*i<p;++i) if ((p-1)%i==0)
		{d[L++]=i; if (i*i!=p-1) d[L++]=(p-1)/i;}
	sort(d,d+L);
	rep(i,n) rep(j,L) if (pow(a[i],g*d[j])==1) {a[i]=(p-1)/(d[j]); break;}
	sort(a,a+n),m=unique(a,a+n)-a,reverse(a,a+m);
	rep(i,m){
		f[i]=(p-1)/a[i];
		rep(j,i) if (a[j]%a[i]==0) f[i]-=f[j];
		ans+=f[i];
	}
	printf("%d\n",ans); return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
void Decompose(int n,vector<pair<int,int> > &s){
	s.clear();
	for(int i=2;i*i<=n;i++){
		if(n%i==0){
			pair<int,int> now=make_pair(i,0);
			while(n%i==0){
				now.second++;
				n/=i;
			}
			s.push_back(now);
		}
		if(n==1) break;
	}
	if(n!=1)
		s.push_back(make_pair(n,1));
}
LL Quick_Pow(LL a,LL n,LL M){
	LL ans=1;
	while(n){
		if(n&1) ans=(ans*a)%M;
		a=(a*a)%M;
		n>>=1;
	}
	return ans;
}
bool Is_Root(int x,int n,int phi,const vector<pair<int,int> > &s){//x�Ƿ�Ϊn��ԭ��
	//phi(n)=phi
	//phi�ķֽ�����s��
	for(int i=0;i<s.size();i++)
		if(Quick_Pow(x,phi/s[i].first,n)==1) return false;
	return true;
}
int Find_Root(int n,int phi){//�ҵ�n��һ��ԭ��,phi(n)=phi
	vector<pair<int,int> > s;
	Decompose(phi,s);
	for(int i=2;i<n;i++)
		if(Is_Root(i,n,phi,s)) return i;
	return -1;
}
void Extend_Gcd(LL a,LL b,LL &x,LL &y,LL &d){
	if(!b){d=a;x=1;y=0;}
	else{Extend_Gcd(b,a%b,y,x,d);y-=(a/b)*x;}
}
int Gcd(int a,int b){
	return !b?a:Gcd(b,a%b);
}
LL Inverse(LL a,LL m){//a��ģm����Ԫ
	LL x,y,d;
	Extend_Gcd(a,m,x,y,d);
	x%=m;
	if(x<0) x+=m;
	return d==1?x:-1;
}
int Discrete_Log(int a,int b,int n,int phi,int m,int inv,map<int,int> &base){
	//a^x%n=b�Ľ�
	//phi(n)=phi,sqrt(phi)=m,Inverse(a^m,n)=inv,a^0~a^(m-1)����base��
	for(int i=0;i<=phi/m;i++){
		if(base.count(b)) return (i*m+base[b])%phi;
		b=(LL)b*inv%n;
	}
	return -1;
}
void Answer_DCLog(int a,int n,int b[],int x[],int T){//һϵ������a^x%n=b�Ľ�,�±�1~T
	//Ҫ��n������
	int phi=n-1,m=sqrt(phi+0.5),e=1;
	int inv=Inverse(Quick_Pow(a,m,n),n);
	map<int,int> base;
	base[1]=0;
	for(int i=1;i<m;i++){
		e=(LL)e*a%n;
		if(!base.count(e)) base[e]=i;
	}
	for(int i=1;i<=T;i++)
		x[i]=Discrete_Log(a,b[i],n,phi,m,inv,base);
}
const int SIZEN=100010;
int N,M,P;
int A[SIZEN],B[SIZEN];
int X[SIZEN];
void Work(void){
	int g=P-1;
	for(int i=1;i<=M;i++)
		g=Gcd(g,B[i]);
	for(int i=1;i<=N;i++)
		A[i]=Quick_Pow(A[i],g,P);
	int root=Find_Root(P,P-1);
	Answer_DCLog(root,P,A,X,N);
	g=P-1;
	for(int i=1;i<=N;i++)
		g=Gcd(g,X[i]);
	printf("%d\n",(P-1)/g);
}
void Read(void){
	scanf("%d%d%d",&N,&M,&P);
	for(int i=1;i<=N;i++) scanf("%d",&A[i]);
	for(int i=1;i<=M;i++) scanf("%d",&B[i]);
}
int main(){
	//freopen("levkoandsets.in","r",stdin);
	//freopen("levkoandsets.out","w",stdout);
	Read();
	Work();
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
LL quickpow(LL a,LL n,LL M){
	LL ans=1;
	while(n){
		if(n&1) ans=(ans*a)%M;
		a=(a*a)%M;
		n>>=1;
	}
	return ans;
}
int gcd(int a,int b){
	return !b?a:gcd(b,a%b);
}
void get_divisor(int n,vector<int> &s){
	for(int i=1;i*i<=n;i++){
		if(n%i==0){
			s.push_back(i);
			if(i*i<n) s.push_back(n/i);
		}
	}
	sort(s.begin(),s.end());
}
const int SIZEN=100010;
int N,M,P;
int A[SIZEN],B[SIZEN];
int Q[SIZEN];
vector<int> divs;
int get_ind(int x){//x��ָ��
	for(int i=0;i<divs.size();i++){
		if(quickpow(x,divs[i],P)==1) return divs[i];
	}
	return -1;
}
void work(void){
	get_divisor(P-1,divs);
	int g=P-1;
	for(int i=1;i<=M;i++) g=gcd(g,B[i]);
	for(int i=1;i<=N;i++){
		A[i]=quickpow(A[i],g,P);
		int d=get_ind(A[i]);
		Q[i]=(P-1)/d;
	}
	g=P-1;
	for(int i=1;i<=N;i++) g=gcd(g,Q[i]);
	printf("%d\n",(P-1)/g);
}
void read(void){
	scanf("%d%d%d",&N,&M,&P);
	for(int i=1;i<=N;i++) scanf("%d",&A[i]);
	for(int i=1;i<=M;i++) scanf("%d",&B[i]);
}
int main(){
	//freopen("levkoandsets.in","r",stdin);
	//freopen("levkoandsets.out","w",stdout);
	read();
	work();
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
LL quickpow(LL a,LL n,LL M){
	LL ans=1;
	while(n){
		if(n&1) ans=(ans*a)%M;
		a=(a*a)%M;
		n>>=1;
	}
	return ans;
}
int gcd(int a,int b){
	return !b?a:gcd(b,a%b);
}
void get_divisor(int n,vector<int> &s){
	for(int i=1;i*i<=n;i++){
		if(n%i==0){
			s.push_back(i);
			if(i*i<n) s.push_back(n/i);
		}
	}
	sort(s.begin(),s.end());
}
const int SIZEN=100010;
int N,M,P;
int A[SIZEN],B[SIZEN];
int Q[SIZEN];
vector<int> divs;
int get_ind(int x){//x��ָ��
	for(int i=0;i<divs.size();i++){
		if(quickpow(x,divs[i],P)==1) return divs[i];
	}
	return -1;
}
vector<int> cnt,flag;
void work(void){
	get_divisor(P-1,divs);
	flag.resize(divs.size());
	cnt.resize(divs.size());
	int g=P-1;
	for(int i=1;i<=M;i++) g=gcd(g,B[i]);
	for(int i=1;i<=N;i++){
		A[i]=quickpow(A[i],g,P);
		int d=get_ind(A[i]);//A[i]^d=1(mod P)
		Q[i]=(P-1)/d;
		for(int j=0;j<divs.size();j++)
			if(divs[j]%Q[i]==0){
				flag[j]=true;
			}
	}
	int ans=0;
	for(int i=divs.size()-1;i>=0;i--){
		cnt[i]=(P-1)/divs[i];
		for(int j=i+1;j<divs.size();j++)
			if(divs[j]%divs[i]==0)
				cnt[i]-=cnt[j];
		if(flag[i]) ans+=cnt[i];
	}
	printf("%d\n",ans);
}
void read(void){
	scanf("%d%d%d",&N,&M,&P);
	for(int i=1;i<=N;i++) scanf("%d",&A[i]);
	for(int i=1;i<=M;i++) scanf("%d",&B[i]);
}
int main(){
	//freopen("levkoandsets.in","r",stdin);
	//freopen("levkoandsets.out","w",stdout);
	read();
	work();
	return 0;
}


/*
    Problem:
    Algorithm:
    Note:
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iomanip>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)

typedef long long ll;
typedef long double real;
void read(int&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

int n,m,p,i,j,k,l,t,N,x,res;
int a[10010],b[100010];
int li[1001];

ll pow(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll get(ll a){int i;rep(i,1,li[0])if(pow(a,li[i],p)==1)return li[i];}

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    read(n);read(m);read(p);
	rep(i,1,n)read(a[i]);
	rep(i,1,m)read(b[i]);

	for(N=p-1,i=1;i*i<=N;i++)if(N%i==0)
	{
		li[++li[0]]=i;
		if(i*i!=N)li[++li[0]]=N/i;
	}
	sort(li+1,li+1+li[0]);

	t=b[1];rep(i,1,m)t=gcd(t,b[i]);
	rep(i,1,n)a[i]=pow(a[i],t,p);

	rep(i,1,n)
	{
		x=get(a[i]);
		if(i==1)res=x;else res=res/gcd(res,x)*x;
	}
	cout<<res<<endl;

    scanf("\n");
    return 0;
}


/*
    Problem:
    Algorithm:
    Note:
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iomanip>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)

typedef long long ll;
typedef long double real;
void read(int&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

int n,m,p,i,j,k,l,t,N,x,res,tot,ans;
int a[10010],b[100010],f[10010];
int li[1001];

ll pow(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll get(ll a){int i;rep(i,1,li[0])if(pow(a,li[i],p)==1)return li[i];}

int main()
{
    //freopen("1.in","r",stdin);
    //freopen("1.out","w",stdout);
    read(n);read(m);read(p);
	rep(i,1,n)read(a[i]);
	rep(i,1,m)read(b[i]);

	for(N=p-1,i=1;i*i<=N;i++)if(N%i==0)
	{
		li[++li[0]]=i;
		if(i*i!=N)li[++li[0]]=N/i;
	}
	sort(li+1,li+1+li[0]);

	t=b[1];rep(i,2,m)t=gcd(t,b[i]);
	rep(i,1,n)a[i]=pow(a[i],t,p);
	rep(i,1,n)a[i]=get(a[i]);

	sort(a+1,a+1+n);
	j=1;rep(i,2,n)if(a[i]!=a[i-1])a[++j]=a[i];n=j;

	rep(i,1,n)
	{
		f[i]=a[i];
		rep(j,1,i-1)if(a[i]%a[j]==0)f[i]-=f[j];
		ans+=f[i];
	}
	printf("%d\n",ans);

    scanf("\n");
    return 0;
}


/*
    Problem:
    Algorithm:
    Note:
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iomanip>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)

typedef long long ll;
typedef long double real;
void read(int&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

int n,m,p,i,j,k,l,t,N,x,res;
int a[10010],b[100010];
int li[1001];

ll pow(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll get(ll a){int i;rep(i,1,li[0])if(pow(a,li[i],p)==1)return li[i];}

int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    read(n);read(m);read(p);
	rep(i,1,n)read(a[i]);
	rep(i,1,m)read(b[i]),b[i]%=p-1;

	for(N=p-1,i=1;i*i<=N;i++)if(N%i==0)
	{
		li[++li[0]]=i;
		if(i*i!=N)li[++li[0]]=N/i;
	}
	sort(li+1,li+1+li[0]);

	t=b[1];rep(i,1,m)t=gcd(t,b[i]);
	rep(i,1,n)a[i]=pow(a[i],t,p);

	rep(i,1,n)
	{
		x=get(a[i]);
		if(i==1)res=x;else res=res/gcd(res,x)*x;
	}
	cout<<res<<endl;

    scanf("\n");
    return 0;
}


/*
    Problem:
    Algorithm:
    Note:
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iomanip>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(x=S.begin();x!=S.end();x++)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)

typedef long long ll;
typedef long double real;
void read(int&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

int n,m,p,i,j,k,l,t,N,x,res,tot,ans;
int a[10010],b[100010],f[10010];
int li[1001];bool can[1001];

ll pow(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

int main()
{
    //freopen("1.in","r",stdin);
    //freopen("1.out","w",stdout);
    read(n);read(m);read(p);
	rep(i,1,n)read(a[i]);
	rep(i,1,m)read(b[i]);

	for(N=p-1,i=1;i*i<=N;i++)if(N%i==0)
	{
		li[++li[0]]=i;
		if(i*i!=N)li[++li[0]]=N/i;
	}
	sort(li+1,li+1+li[0]);
	t=b[1];rep(i,2,m)t=gcd(t,b[i]);
	rep(i,1,n)a[i]=pow(a[i],t,p);
	rep(i,1,n)rep(j,1,li[0])if(pow(a[i],li[j],p)==1){can[j]=1;break;}
	rep(i,1,li[0])if(can[i])rep(j,1,i-1)if(li[i]%li[j]==0)can[j]=1;
	rep(i,1,li[0]){f[i]=li[i];rep(j,1,i-1)if(li[i]%li[j]==0)f[i]-=f[j];if(can[i])ans+=f[i];}
	printf("%d\n",ans);

    scanf("\n");
    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define pb push_back

using namespace std;
typedef long long ll;

int gcd(int a, int b){ return b ? gcd(b,a%b) : a ; }

int expo(ll a, ll b, ll m){
    ll r=1;
    while(b){
        if(b&1) r=(r*a)%m;
        a=(a*a)%m;
        b>>=1LL;
    }
    return r;
}

int ls[10000];
int ts[10100];
int g[10100];
bool eh[10100];

int main(){
    int n, m, p;
    scanf("%d %d %d",&n,&m,&p);
    rep(i,n) scanf("%d",g+i);
    int gg;
    scanf("%d",&gg);
    rep(i,m-1){
        int v;
        scanf("%d",&v);
        gg = gcd(gg,v);
    }
    rep(i,n) g[i] = expo(g[i],gg,p);

    int q=0, pp=p-1;
    fr(i,2,pp+1){
        if(i*i>pp) break;
        if(pp%i==0){
            ls[q++] = i;
            while(pp%i==0) pp/=i;
        }
    }
    if(pp!=1) ls[q++] = pp;

    int qts=0;
    rep(i,n){
        int t=p-1;
        bool eh=true;
        while(eh){
            eh=false;
            rep(j,q){
                if(t%ls[j]==0 && expo(g[i],t/ls[j],p)==1) t/=ls[j], eh=true;
            }
        }
        ts[qts++] = t;
    }

    sort(ts,ts+qts);
    qts = unique(ts,ts+qts)-ts;
    rep(i,qts) eh[i]=true;
    rep(i,qts) rep(j,i){
        if(ts[i]%ts[j]==0) eh[j]=false;
    }

    int w=0;
    rep(i,qts){
        if(eh[i]) ts[w++] = ts[i];
    }
    int lim = 1<<w;
    int s=0;
    fr(i,1,lim){
        int z=-1, cnt=0;
        rep(j,w){
            if((i&(1<<j))){
                if(z==-1) z = ts[j];
                else z = gcd(z,ts[j]);
                cnt++;
            }
        }
        if(cnt&1) s += z;
        else s -= z;
    }
    printf("%d\n",s);

    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define pb push_back

using namespace std;
typedef long long ll;

int gcd(int a, int b){ return b ? gcd(b,a%b) : a ; }

int expo(ll a, ll b, ll m){
    ll r=1;
    while(b){
        if(b&1) r=(r*a)%m;
        a=(a*a)%m;
        b>>=1LL;
    }
    return r;
}

int ls[20];
int ts[10100];
int g[10100];
bool eh[10100];

int main(){
    int n, m, p;
    scanf("%d %d %d",&n,&m,&p);
    rep(i,n) scanf("%d",g+i);
    int gg;
    scanf("%d",&gg);
    rep(i,m-1){
        int v;
        scanf("%d",&v);
        gg = gcd(gg,v);
    }
    rep(i,n) g[i] = expo(g[i],gg,p);

    int q=0, pp=p-1;
    fr(i,2,pp+1){
        if(i*i>pp) break;
        if(pp%i==0){
            ls[q++] = i;
            while(pp%i==0) pp/=i;
        }
    }
    if(pp!=1) ls[q++] = pp;

    rep(i,n){
        ts[i]=p-1;
        bool eh=true;
        while(eh){
            eh=false;
            rep(j,q){
                if(ts[i]%ls[j]==0 && expo(g[i],ts[i]/ls[j],p)==1) ts[i]/=ls[j], eh=true;
            }
        }
    }

    sort(ts,ts+n);
    int qts = unique(ts,ts+n)-ts;
    rep(i,qts) eh[i]=true;
    rep(i,qts) rep(j,i){
        if(ts[i]%ts[j]==0) eh[j]=false;
    }

    int w=0;
    rep(i,qts){
        if(eh[i]) ts[w++] = ts[i];
    }
    int lim = 1<<w;
    int s=0;
    fr(i,1,lim){
        int z=-1, cnt=0;
        rep(j,w){
            if((i&(1<<j))){
                if(z==-1) z = ts[j];
                else z = gcd(z,ts[j]);
                cnt++;
            }
        }
        if(cnt&1) s += z;
        else s -= z;
    }

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

    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define pb push_back

using namespace std;
typedef long long ll;

int gcd(int a, int b){ return b ? gcd(b,a%b) : a ; }

int expo(ll a, ll b, ll m){
    ll r=1;
    while(b){
        if(b&1) r=(r*a)%m;
        a=(a*a)%m;
        b>>=1LL;
    }
    return r;
}

int ls[10000];
int ts[10100];
int g[10100];
bool eh[10100];

int main(){
    int n, m, p;
    scanf("%d %d %d",&n,&m,&p);
    rep(i,n) scanf("%d",g+i);
    int gg;
    scanf("%d",&gg);
    rep(i,m-1){
        int v;
        scanf("%d",&v);
        gg = gcd(gg,v);
    }
    rep(i,n) g[i] = expo(g[i],gg,p);

    int q=0, pp=p-1;
    fr(i,2,pp+1){
        if(i*i>pp) break;
        if(pp%i==0){
            ls[q++] = i;
            while(pp%i==0) pp/=i;
        }
    }
    if(pp!=1) ls[q++] = pp;

    rep(i,n){
        ts[i]=p-1;
        bool eh=true;
        while(eh){
            eh=false;
            rep(j,q){
                if(ts[i]%ls[j]==0 && expo(g[i],ts[i]/ls[j],p)==1) ts[i]/=ls[j], eh=true;
            }
        }
    }

    sort(ts,ts+n);
    int qts = unique(ts,ts+n)-ts;
    rep(i,qts) eh[i]=true;
    rep(i,qts) rep(j,i){
        if(ts[i]%ts[j]==0) eh[j]=false;
    }

    int w=0;
    rep(i,qts){
        if(eh[i]) ts[w++] = ts[i];
    }
    int lim = 1<<w;
    int s=0;
    fr(i,1,lim){
        int z=-1, cnt=0;
        rep(j,w){
            if((i&(1<<j))){
                if(z==-1) z = ts[j];
                else z = gcd(z,ts[j]);
                cnt++;
            }
        }
        if(cnt&1) s += z;
        else s -= z;
    }

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

    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define pb push_back

using namespace std;
typedef long long ll;

int gcd(int a, int b){ return b ? gcd(b,a%b) : a ; }

int expo(ll a, ll b, ll m){
    ll r=1;
    while(b){
        if(b&1) r=(r*a)%m;
        a=(a*a)%m;
        b>>=1LL;
    }
    return r;
}

int ls[10000];
int ts[10100];
int g[10100];
bool eh[10100];

int main(){
    int n, m, p;
    scanf("%d %d %d",&n,&m,&p);
    rep(i,n) scanf("%d",g+i);
    int gg;
    scanf("%d",&gg);
    rep(i,m-1){
        int v;
        scanf("%d",&v);
        gg = gcd(gg,v);
    }
    rep(i,n) g[i] = expo(g[i],gg,p);

    int q=0, pp=p-1;
    fr(i,2,pp+1){
        if(i*i>pp) break;
        if(pp%i==0){
            ls[q++] = i;
            while(pp%i==0) pp/=i;
        }
    }

    if(pp!=1) ls[q++] = pp;
    int qts=0;
    rep(i,n){
        int t=p-1;
        bool eh=true;
        while(eh){
            eh=false;
            rep(j,q){
                if(t%ls[j]==0 && expo(g[i],t/ls[j],p)==1) t/=ls[j], eh=true;
            }
        }
        ts[qts++] = t;
    }
    sort(ts,ts+qts);
    qts = unique(ts,ts+qts)-ts;
    rep(i,qts) eh[i]=true;
    rep(i,qts) rep(j,i){
        if(ts[i]%ts[j]==0) eh[j]=false;
    }
    int w=0;
    rep(i,qts){
        if(eh[i]) ts[w++] = ts[i];
    }
    int lim = 1<<w;
    int s=0;
    fr(i,1,lim){
        int z=-1, cnt=0;
        rep(j,w){
            if((i&(1<<j))){
                if(z==-1) z = ts[j];
                else z = gcd(z,ts[j]);
                cnt++;
            }
        }
        if(cnt&1) s += z;
    }
    printf("%d\n",s);

    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define pb push_back

using namespace std;
typedef long long ll;

int gcd(int a, int b){ return b ? gcd(b,a%b) : a ; }

int expo(ll a, ll b, ll m){
    ll r=1;
    while(b){
        if(b&1) r=(r*a)%m;
        a=(a*a)%m;
        b>>=1LL;
    }
    return r;
}

int ls[10000];
int ts[10100];
int g[10100];
bool eh[10100];

int main(){
    int n, m, p;
    scanf("%d %d %d",&n,&m,&p);
    rep(i,n) scanf("%d",g+i);
    int gg;
    scanf("%d",&gg);
    rep(i,m-1){
        int v;
        scanf("%d",&v);
        gg = gcd(gg,v);
    }
    rep(i,n) g[i] = expo(g[i],gg,p);

    int q=0, pp=p-1;
    fr(i,2,pp+1){
        if(i*i>pp) break;
        if(pp%i==0){
            ls[q++] = i;
            while(pp%i==0) pp/=i;
        }
    }
    if(pp!=1) ls[q++] = pp;

    int qts=0;
    rep(i,n){
        int t=p-1;
        bool eh=true;
        while(eh){
            eh=false;
            rep(j,q){
                if(t%ls[j]==0 && expo(g[i],t/ls[j],p)==1) t/=ls[j], eh=true;
            }
        }
        ts[qts++] = t;
    }

    sort(ts,ts+qts);
    qts = unique(ts,ts+qts)-ts;
    rep(i,qts) eh[i]=true;
    /*rep(i,qts) rep(j,i){
        if(ts[i]%ts[j]==0) eh[j]=false;
    }*/

    int w=0;
    rep(i,qts){
        if(eh[i]) ts[w++] = ts[i];
    }
    int lim = 1<<w;
    int s=0;
    fr(i,1,lim){
        int z=-1, cnt=0;
        rep(j,w){
            if((i&(1<<j))){
                if(z==-1) z = ts[j];
                else z = gcd(z,ts[j]);
                cnt++;
            }
        }
        if(cnt&1) s += z;
    }
    printf("%d\n",s);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NMAX 10050
#define MMAX 100050
#define DMAX 2048

int a[NMAX], q[NMAX];
int b[MMAX];
int D[DMAX], C[DMAX];

int gcd(int a, int b) {
  return b == 0 ? a : gcd(b, a % b);
}

int power(int b, int e, int p) {
  if (e == 0) {
    return 1;
  }
  long long out = power(b, e/2, p);
  out = (out * out) % (long long) p;
  if (e % 2 == 1) {
    out = (out * (long long) b) % (long long) p;
  }
  return out;
}

int main() {
  int n, m, p;
  scanf("%d %d %d", &n, &m, &p);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", b+j);
  }

  int e = p - 1;
  for (int j = 0; j < m; j++) {
    e = gcd(e, b[j]);
  }

  int ds = 0;
  for (int div = 1; div * div <= p-1; div++) {
    if ((p - 1) % div == 0) {
      D[ds++] = div;
      if (div * div != p-1) {
        D[ds++] = (p - 1) / div;
      }
    }
  }
  sort(D, D + ds);

  for (int i = 0; i < n; i++) {
    for (int d = 0; d < ds; d++) {
      if (power(a[i], D[d], p) == 1) {
        q[i] = (p - 1) / D[d];
        q[i] = ((long long) q[i] * (long long) e) % (long long) (p - 1);
        q[i] = gcd(p - 1, q[i]);
        break;
      }
    }
  }

  int out = 0;
  for (int d = ds - 1; d >= 0; d--) {
    C[d] = (p - 1) / D[d];
    for (int f = d + 1; f < ds; f++) {
      if (D[f] % D[d] == 0) {
        C[d] -= C[f];
      }
    }
    for (int i = 0; i < n; i++) {
      if (D[d] % q[i] == 0) {
        out += C[d];
        break;
      }
    }
  }
  printf("%d\n", out);

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NMAX 10050
#define MMAX 100050
#define DMAX 64

int a[NMAX], q[NMAX];
int b[MMAX];
int D[DMAX], C[DMAX];

int gcd(int a, int b) {
  return b == 0 ? a : gcd(b, a % b);
}

int power(int b, int e, int p) {
  if (e == 0) {
    return 1;
  }
  long long out = power(b, e/2, p);
  out = (out * out) % (long long) p;
  if (e % 2 == 1) {
    out = (out * (long long) b) % (long long) p;
  }
  return out;
}

int main() {
  int n, m, p;
  scanf("%d %d %d", &n, &m, &p);
  for (int i = 0; i < n; i++) {
    scanf("%d", a+i);
  }
  for (int j = 0; j < m; j++) {
    scanf("%d", b+j);
  }

  int e = p - 1;
  for (int j = 0; j < m; j++) {
    e = gcd(e, b[j]);
  }

  int ds = 0;
  for (int div = 1; div * div <= p-1; div++) {
    if ((p - 1) % div == 0) {
      D[ds++] = div;
      if (div * div != p-1) {
        D[ds++] = (p - 1) / div;
      }
    }
  }
  sort(D, D + ds);

  for (int i = 0; i < n; i++) {
    for (int d = 0; d < ds; d++) {
      if (power(a[i], D[d], p) == 1) {
        q[i] = (p - 1) / D[d];
        q[i] = ((long long) q[i] * (long long) e) % (long long) (p - 1);
        q[i] = gcd(p - 1, q[i]);
        break;
      }
    }
  }

  int out = 0;
  for (int d = ds - 1; d >= 0; d--) {
    C[d] = (p - 1) / D[d];
    for (int f = d + 1; f < ds; f++) {
      if (D[f] % D[d] == 0) {
        C[d] -= C[f];
      }
    }
    for (int i = 0; i < n; i++) {
      if (D[d] % q[i] == 0) {
        out += C[d];
        break;
      }
    }
  }
  printf("%d\n", out);

  return 0;
}


#include <cstdio>
#include <map>
using namespace std;

const int N = 10000 + 5, M = 100000 + 5;

typedef long long ll;

inline ll gcd(ll a, ll b) {
	ll t;
	while(b) t = a, a = b, b = t % b;
	return a;
}

inline ll pow(ll a, ll b, ll mod) {
	ll ret = 1 % mod;
	for(; b; b >>= 1, a = a * a % mod) if(b & 1) ret = ret * a % mod;
	return ret;
}

inline ll find_root(ll p) {
	ll ps[65]; int cnt_ps = 0;
	ll t = p - 1;
	for(ll i = 2; i * i <= t; ++i) if(t % i == 0) {
		ps[++cnt_ps] = i;
		while(t % i == 0) t /= i;
	}
	if(t > 1) ps[++cnt_ps] = t;
	for(ll g = 2; ; ++g) {
		bool ok = true;
		for(int i = 1; i <= cnt_ps; ++i) if(pow(g, (p - 1) / ps[i], p) == 1) {
			ok = false;
			break;
		}
		if(ok) return g;
	}
	throw "never here finding root";
}

const int BLKSZ = 1500000, HSCNT = BLKSZ + 5, HSSZ = 5000011;

int hd[HSSZ];
int nw_hs; ll key[HSCNT]; int val[HSCNT], nxt[HSCNT];

inline int query(ll k) {
	for(int i = hd[k % HSSZ]; i; i = nxt[i]) if(key[i] == k) return val[i];
	return -1;
}

inline void insert(ll k, int v) {
	int id = ++nw_hs;
	nxt[id] = hd[k % HSSZ]; hd[k % HSSZ] = id;
	key[id] = k, val[id] = v;
}

ll st_t2;

inline void init_ind(ll g, ll p) {
	ll t = 1;
	for(int i = 0; i < BLKSZ; ++i, t = t * g % p) {
		if(~query(t)) continue;
		insert(t, i);
	}
	st_t2 = pow(t, p - 2, p);
}

inline ll g_ind(ll b, ll p) {
	ll t2 = st_t2, t = b;
	for(int i = 0; i < p - 1; i += BLKSZ, t = t * t2 % p) {
		int j = query(t);
		if(~j) return i + j;
	}
	throw "can't find ind";
}

int n, m;
ll p;
ll an[N], bm[M];

map<ll, ll> dvc;

int main() {
//#ifdef ZDEBUG
//	freopen("in.txt", "r", stdin);
//#endif
	scanf("%d%d%I64d", &n, &m, &p);
	for(int i = 1; i <= n; ++i) scanf("%I64d", &an[i]);
	for(int i = 1; i <= m; ++i) scanf("%I64d", &bm[i]);
	ll gb = p - 1;
	ll g = find_root(p);
	init_ind(g, p);
	for(int i = 1; i <= m; ++i) gb = gcd(gb, bm[i]);
	for(int i = 1; i <= n; ++i) an[i] = pow(an[i], gb, p);
	for(int i = 1; i <= n; ++i) an[i] = g_ind(an[i], p);
	for(int i = 1; i <= n; ++i) dvc[gcd(an[i], p - 1)] = 0;
	ll ans = 0;
	for(map<ll, ll>::iterator it1 = dvc.begin(); it1 != dvc.end(); ++it1) {
		if(it1->second == 1) continue;
		ll delt = 1 - it1->second;
		ans += delt * ((p - 1) / it1->first);
		it1->second += delt;
		map<ll, ll>::iterator it2 = it1; ++it2;
		for(; it2 != dvc.end(); ++it2) if(it2->first % it1->first == 0) it2->second += delt;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
using namespace std;

const int N = 10000 + 5, M = 100000 + 5;

typedef long long ll;

inline ll gcd(ll a, ll b) {
	ll t;
	while(b) t = a, a = b, b = t % b;
	return a;
}

inline ll pow(ll a, ll b, ll mod) {
	ll ret = 1 % mod;
	for(; b; b >>= 1, a = a * a % mod) if(b & 1) ret = ret * a % mod;
	return ret;
}

inline ll find_root(ll p) {
	ll ps[65]; int cnt_ps = 0;
	ll t = p - 1;
	for(ll i = 2; i * i <= t; ++i) if(t % i == 0) {
		ps[++cnt_ps] = i;
		while(t % i == 0) t /= i;
	}
	if(t > 1) ps[++cnt_ps] = t;
	for(ll g = 2; ; ++g) {
		bool ok = true;
		for(int i = 1; i <= cnt_ps; ++i) if(pow(g, (p - 1) / ps[i], p) == 1) {
			ok = false;
			break;
		}
		if(ok) return g;
	}
	throw "never here finding root";
}

inline ll g_ind(ll g, ll b, ll p) {
	map<ll, int> mp;
	int blksz = (int)round(sqrt((double)(p - 1)));
	ll t = 1;
	for(int i = 0; i < blksz; ++i, t = t * g % p) {
		if(mp.count(t)) continue;
		mp[t] = i;
	}
	ll t2 = pow(t, p - 2, p);
	t = b;
	for(int i = 0; i < p - 1; i += blksz, t = t * t2 % p) if(mp.count(t)) return i + mp[t];
	throw "can't find ind";
}

int n, m;
ll p;
ll an[N], bm[M];

map<ll, ll> dvc;

int main() {
	scanf("%d%d%I64d", &n, &m, &p);
	for(int i = 1; i <= n; ++i) scanf("%I64d", &an[i]);
	for(int i = 1; i <= m; ++i) scanf("%I64d", &bm[i]);
	ll gb = p - 1;
	for(int i = 1; i <= m; ++i) gb = gcd(gb, bm[i]);
	for(int i = 1; i <= n; ++i) an[i] = pow(an[i], gb, p);
	ll g = find_root(p);
	for(int i = 1; i <= n; ++i) an[i] = g_ind(g, an[i], p);
	for(int i = 1; i <= n; ++i) dvc[gcd(an[i], p - 1)] = 0;
	ll ans = 0;
	for(map<ll, ll>::iterator it1 = dvc.begin(); it1 != dvc.end(); ++it1) {
		if(it1->second == 1) continue;
		ll delt = 1 - it1->second;
		ans += delt * ((p - 1) / it1->first);
		it1->second += delt;
		map<ll, ll>::iterator it2 = it1; ++it2;
		for(; it2 != dvc.end(); ++it2) if(it2->first % it1->first == 0) it2->second += delt;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <cstdio>
#include <map>
using namespace std;

const int N = 10000 + 5, M = 100000 + 5;

typedef long long ll;

inline ll gcd(ll a, ll b) {
	ll t;
	while(b) t = a, a = b, b = t % b;
	return a;
}

inline ll pow(ll a, ll b, ll mod) {
	ll ret = 1 % mod;
	for(; b; b >>= 1, a = a * a % mod) if(b & 1) ret = ret * a % mod;
	return ret;
}

inline ll find_root(ll p) {
	ll ps[65]; int cnt_ps = 0;
	ll t = p - 1;
	for(ll i = 2; i * i <= t; ++i) if(t % i == 0) {
		ps[++cnt_ps] = i;
		while(t % i == 0) t /= i;
	}
	if(t > 1) ps[++cnt_ps] = t;
	for(ll g = 2; ; ++g) {
		bool ok = true;
		for(int i = 1; i <= cnt_ps; ++i) if(pow(g, (p - 1) / ps[i], p) == 1) {
			ok = false;
			break;
		}
		if(ok) return g;
	}
	throw "never here finding root";
}

const int BLKSZ = 1500000, HSCNT = BLKSZ + 5, HSSZ = 5000011;

int hd[HSSZ];
int nw_hs; ll key[HSCNT]; int val[HSCNT], nxt[HSCNT];

inline int query(ll k) {
	for(int i = hd[k % HSSZ]; i; i = nxt[i]) if(key[i] == k) return val[i];
	return -1;
}

inline void insert(ll k, int v) {
	int id = ++nw_hs;
	nxt[id] = hd[k % HSSZ]; hd[k % HSSZ] = id;
	key[id] = k, val[id] = v;
}

ll st_t2;

inline void init_ind(ll g, ll p) {
	ll t = 1;
	for(int i = 0; i < BLKSZ; ++i, t = t * g % p) {
		if(~query(t)) continue;
		insert(t, i);
	}
	st_t2 = pow(t, p - 2, p);
}

inline ll g_ind(ll b, ll p) {
	ll t2 = st_t2, t = b;
	for(int i = 0; i < p - 1; i += BLKSZ, t = t * t2 % p) {
		int j = query(t);
		if(~j) return i + j;
	}
	throw "can't find ind";
}

int n, m;
ll p;
ll an[N], bm[M];

map<ll, ll> dvc;

int main() {
//#ifdef ZDEBUG
//	freopen("in.txt", "r", stdin);
//#endif
	scanf("%d%d%I64d", &n, &m, &p);
	for(int i = 1; i <= n; ++i) scanf("%I64d", &an[i]);
	for(int i = 1; i <= m; ++i) scanf("%I64d", &bm[i]);
	ll gb = p - 1;
	ll g = find_root(p);
	init_ind(g, p);
	for(int i = 1; i <= m; ++i) gb = gcd(gb, bm[i]);
	for(int i = 1; i <= n; ++i) an[i] = pow(an[i], gb, p);
	for(int i = 1; i <= n; ++i) an[i] = g_ind(an[i], p);
	for(int i = 1; i <= n; ++i) dvc[gcd(an[i], p - 1)] = 0;
	dvc[p - 1] = 0;
	ll ans = 0;
	for(map<ll, ll>::iterator it1 = dvc.begin(); it1 != dvc.end(); ++it1) {
		if(it1->second == 1) continue;
		ll delt = 1 - it1->second;
		ans += delt * ((p - 1) / it1->first);
		it1->second += delt;
		map<ll, ll>::iterator it2 = it1; ++it2;
		for(; it2 != dvc.end(); ++it2) if(it2->first % it1->first == 0) it2->second += delt;
	}
	printf("%I64d\n", ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long LL;

int n, m;
int A[10000 + 10], B[100000 + 10], p;

int gcd(int a, int b){
    while(b){
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

int fastPow(int b, int pow){
    int ans = 1;
    b %= p;
    while(pow){
        if(pow & 1)
            ans = (int)((LL)ans * b % p);
        pow >>= 1;
        b = (int)((LL)b * b % p);
    }
    return ans;
}

int ans = 0;

int di[500010], dn;
int val[10000 + 10];

int t[10000 + 10];

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    scanf("%d%d%d", &n, &m, &p);
    for(int i = 0; i < n; i ++)
        scanf("%d", A + i);
    int g = 0;
    for(int i = 0; i < m; i ++){
        scanf("%d", B + i);
        g = gcd(g, B[i]);
    }
    dn = 0;
    for(int i = 1; i * i <= p - 1; i ++){
        if(i * i == p - 1)
            di[dn ++] = i;
        else if((p - 1) % i == 0){
            di[dn ++] = i;
            di[dn ++] = (p - 1) / i;
        }
    }
    sort(di, di + dn);
    for(int i = 0; i < n; i ++){
        int tmp = fastPow(A[i], g);
        int j;
        for(j = 0; fastPow(tmp, di[j]) != 1; j ++);
        val[i] = (p - 1) / di[j];
    }
//  printf("HAHA\n");
    sort(val, val + n);
    n = unique(val, val + n) - val;
    for(int i = n - 1; i >= 0; i --){
        t[i] = (p - 2) / val[i] + 1;
        for(int j = i + 1; j < n; j ++){
            if(val[j] % val[i] == 0)
                t[i] -= t[j];
        }
        ans += t[i];
    }
    printf("%d\n", ans);

    return 0;
}