#include <bits/stdc++.h>
using namespace std;
const int MX = 7000;
int n, q, mu[7107], prime[7107], p, isp[7107];
bitset<7107> a[100005], b[7107], c[7107];
int main() {
  scanf("%d%d", &n, &q);
  mu[1] = 1;
  for (int i = 2; i <= MX; i++) isp[i] = 1;
  for (int i = 2; i <= MX; i++) {
    if (isp[i]) prime[++p] = i, mu[i] = -1;
    for (int j = 1; i * prime[j] <= MX && j <= p; j++) {
      isp[i * prime[j]] = 0;
      if (i % prime[j] == 0) break;
      mu[i * prime[j]] = -mu[i];
    }
  }
  for (int i = 1; i <= MX; i++) {
    for (int j = i; j <= MX; j += i) {
      if (mu[j / i]) c[i][j] = 1;
      b[j][i] = 1;
    }
  }
  while (q--) {
    int ty, x, y, z;
    scanf("%d", &ty);
    if (ty == 1) {
      scanf("%d%d", &x, &y);
      a[x] = b[y];
    }
    if (ty == 2) {
      scanf("%d%d%d", &x, &y, &z);
      a[x] = a[y] ^ a[z];
    }
    if (ty == 3) {
      scanf("%d%d%d", &x, &y, &z);
      a[x] = a[y] & a[z];
    }
    if (ty == 4) {
      scanf("%d%d", &x, &y);
      printf("%d", ((a[x] & c[y]).count()) % 2);
    }
  }
  return 0;
}
