#include <bits/stdc++.h>
using namespace std;
const int maxn = 7010;
int n, q, pri[maxn], k, mu[maxn];
bool mark[maxn];
bitset<maxn> f[100010], g[maxn], fmu[maxn];
void getmu() {
  mu[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!mark[i]) {
      pri[k++] = i;
      mu[i] = -1;
    }
    for (int j = 0; j < k; j++) {
      int m = i * pri[j];
      if (m > n) {
        break;
      }
      mark[m] = true;
      if (i % pri[j] == 0) {
        mu[m] = 0;
        break;
      }
      mu[m] = -mu[i];
    }
  }
}
int main() {
  n = 7000;
  getmu();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j * i <= n; j++) {
      g[i * j].set(i);
      if (mu[j]) {
        fmu[i].set(i * j);
      }
    }
  }
  scanf("%d%d", &n, &q);
  while (q--) {
    int op, x, y, z, v;
    scanf("%d%d", &op, &x);
    if (op == 1) {
      scanf("%d", &v);
      f[x] = g[v];
    } else if (op == 2) {
      scanf("%d%d", &y, &z);
      f[x] = f[y] ^ f[z];
    } else if (op == 3) {
      scanf("%d%d", &y, &z);
      f[x] = f[y] & f[z];
    } else {
      scanf("%d", &v);
      printf("%d", (f[x] & fmu[v]).count() & 1);
    }
  }
  return 0;
}
