#include <bits/stdc++.h>
using namespace std;
const int mx1 = 1e5 + 1314;
const int mx2 = 7000 + 15;
bitset<mx2> A[mx1], s[7015], f[7015];
int read() {
  int k;
  scanf("%d", &k);
  return k;
}
int n, m;
void slove() {
  for (int i = 7000; i; i--) {
    s[i][i] = 1;
    for (int j = i + i; j <= 7000; j += i) s[i] ^= s[j];
  }
  for (int i = 7000; i; i--) {
    for (int j = 1; j <= i; j++)
      if (i % j == 0) f[i][j] = 1;
  }
}
int main() {
  slove();
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int flag = read();
    if (flag == 1) {
      int x = read();
      int v = read();
      A[x] = f[v];
    }
    if (flag == 2) {
      int x = read();
      int y = read();
      int z = read();
      A[x] = A[y] ^ A[z];
    }
    if (flag == 3) {
      int x = read();
      int y = read();
      int z = read();
      A[x] = A[y] & A[z];
    }
    if (flag == 4) {
      int x = read();
      int v = read();
      cout << ((A[x] & s[v]).count() & 1);
    }
  }
}
