#include <bits/stdc++.h>
using namespace std;
int n, m, tx = 0;
long long p, a[100001], d[100001], f[2000][2000];
bool v[100001];
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long mul(long long x, long long y) {
  if (y == 0) return 1;
  long long now = mul(x, y / 2);
  now = now * now % p;
  if (y % 2) return now * x % p;
  return now;
}
bool check(long long x) {
  for (int i = 1; i <= tx; i++)
    if (mul(x, d[i]) == 1) return 0;
  return 1;
}
long long getans(long long x) {
  long long ans = 1e15;
  for (int i = 1; i <= tx; i++)
    if (mul(x, d[i]) == 1) ans = min(ans, d[i]);
  return (p - 1) / ans;
}
int find(long long x) {
  int l = 1, r = tx;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (d[mid] == x) return mid;
    if (d[mid] < x)
      l = mid + 1;
    else
      r = mid - 1;
  }
}
int main() {
  scanf("%d%d%lld", &n, &m, &p);
  int kx = sqrt(p);
  for (int i = 1; i <= kx; i++)
    if ((p - 1) % i == 0) {
      d[++tx] = i;
      if (i * i == p - 1) continue;
      d[++tx] = (p - 1) / i;
    }
  sort(d + 1, d + tx + 1);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  int g = p - 1;
  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    g = gcd(g, x);
  }
  for (int i = 1; i <= n; i++) a[i] = mul(a[i], g);
  for (int i = 1; i <= n; i++) a[i] = getans(a[i]);
  for (int i = 1; i <= n; i++) v[find(a[i])] = 1;
  for (int i = 1; i <= tx; i++) {
    for (int j = 1; j <= tx; j++) f[i][j] = f[i - 1][j];
    if (!v[i]) continue;
    f[i][i]++;
    for (int j = 1; j <= tx; j++) {
      long long now = lcm(d[i], d[j]);
      int k = find(now);
      f[i][k] -= f[i - 1][j];
    }
  }
  long long ans = 0;
  for (int i = 1; i <= tx; i++) ans += (p - 1) / d[i] * f[tx][i];
  printf("%lld\n", ans);
  return 0;
}
