#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  const int m = 7000;
  vector<bool> square_free(m + 1, true);
  for (int i = 1; i <= m; i++) {
    for (int j = 2; j <= m; j++) {
      if (i % j == 0 && i / j % j == 0) {
        square_free[i] = false;
      }
    }
  }
  vector<bitset<m + 1>> to(m + 1);
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= m; j++) {
      if (i % j == 0) {
        to[i][j] = true;
      }
    }
  }
  vector<bitset<m + 1>> from(m + 1);
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= m; j++) {
      if (j % i == 0 && square_free[j / i]) {
        from[i][j] = true;
      }
    }
  }
  int n, q;
  cin >> n >> q;
  vector<bitset<m + 1>> a(n);
  for (int t = 0; t < q; t++) {
    int r;
    cin >> r;
    if (r == 1) {
      int x, v;
      cin >> x >> v;
      x--;
      a[x] = to[v];
    }
    if (r == 2) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--, z--;
      a[x] = a[y] ^ a[z];
    }
    if (r == 3) {
      int x, y, z;
      cin >> x >> y >> z;
      x--, y--, z--;
      a[x] = a[y] & a[z];
    }
    if (r == 4) {
      int x, v;
      cin >> x >> v;
      x--;
      cout << (a[x] & from[v]).count() % 2;
    }
  }
  cout << endl;
  return 0;
}
