#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)1e5 + 5;
const int B = 20;
const int M = 7001;
const int S = M / B + 1;
int ppc[(1 << B) + 7];
int a[N][S];
int b[M][S], c[M][S];
int mu[M];
int main() {
  for (int i = 1; i < (1 << B); i++) ppc[i] = ppc[i / 2] + (i & 1);
  mu[1] = 1;
  for (int i = 1; i < M; i++)
    for (int j = 2 * i; j < M; j += i) mu[j] ^= mu[i];
  for (int x = 1; x < M; x++)
    for (int y = x; y < M; y += x) {
      b[y][x / B] ^= 1 << (x % B);
      if (mu[y / x]) c[x][y / B] ^= 1 << (y % B);
    }
  int n, q;
  scanf("%d%d", &n, &q);
  while (q--) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int p, x;
      scanf("%d%d", &p, &x);
      p--;
      for (int i = 0; i < S; i++) a[p][i] = b[x][i];
    } else if (t == 2) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      x--;
      y--;
      z--;
      for (int i = 0; i < S; i++) a[x][i] = a[y][i] ^ a[z][i];
    } else if (t == 3) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      x--;
      y--;
      z--;
      for (int i = 0; i < S; i++) a[x][i] = a[y][i] & a[z][i];
    } else {
      int p, x;
      scanf("%d%d", &p, &x);
      p--;
      int ans = 0;
      for (int i = 0; i < S; i++) ans += ppc[a[p][i] & c[x][i]];
      printf("%d", ans & 1);
    }
  }
  printf("\n");
  return 0;
}
