#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 7005;
int n, q;
bool bad[M];
bitset<M> divs[M];
bitset<M> muls[M];
bitset<M> a[N];
int main() {
  for (int i = 2; i < M; ++i) {
    for (int j = i * i; j < M; j += i * i) {
      bad[j] = true;
    }
  }
  for (int i = 1; i < M; ++i) {
    for (int j = i; j < M; j += i) {
      divs[j].set(i);
      if (!bad[j / i]) {
        muls[i].set(j);
      }
    }
  }
  scanf("%d %d", &n, &q);
  while (q--) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int x, v;
      scanf("%d %d", &x, &v);
      a[x] = divs[v];
    } else if (op == 2) {
      int x, y, z;
      scanf("%d %d %d", &x, &y, &z);
      a[x] = a[y] ^ a[z];
    } else if (op == 3) {
      int x, y, z;
      scanf("%d %d %d", &x, &y, &z);
      a[x] = a[y] & a[z];
    } else {
      int x, v;
      scanf("%d %d", &x, &v);
      putchar(((a[x] & muls[v]).count() & 1) + '0');
    }
  }
}
