#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long int inf = 1000ll * 1000 * 1000 * 1000;
const int maxn = 7005;
const int MAXN = 1e5 + 20;
bitset<maxn> bt[MAXN];
bitset<maxn> gt[maxn];
vector<int> sqfr;
int lp[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 2; i < maxn; i++) {
    if (!lp[i]) {
      for (int j = i; j < maxn; j += i) {
        if (!lp[j]) {
          lp[j] = i;
        }
      }
    }
  }
  for (int i = 1; i < maxn; i++) {
    bool OK = 1;
    int low = lp[i];
    int cnt = 0;
    int x = i;
    while (x != 1) {
      while (lp[x] == low) {
        x /= low;
        cnt++;
      }
      if (cnt > 1) {
        OK = 0;
        break;
      } else {
        cnt = 0;
        low = lp[x];
      }
    }
    if (OK) {
      sqfr.push_back(i);
    }
  }
  for (int i = 1; i < maxn; i++) {
    for (int j : sqfr) {
      if (i * j > maxn) break;
      gt[i][i * j] = 1;
    }
  }
  int n, q;
  cin >> n >> q;
  while (q--) {
    int det;
    cin >> det;
    if (det == 1) {
      int x, v;
      cin >> x >> v;
      bt[x].reset();
      for (int i = 1; i * i <= v; i++) {
        if (v % i == 0) {
          bt[x][i] = 1;
          if (i * i != v) {
            bt[x][v / i] = 1;
          }
        }
      }
    }
    if (det == 2) {
      int x, y, z;
      cin >> x >> y >> z;
      bt[x] = bt[y] ^ bt[z];
    }
    if (det == 3) {
      int x, y, z;
      cin >> x >> y >> z;
      bt[x] = bt[y] & bt[z];
    }
    if (det == 4) {
      int x, v;
      cin >> x >> v;
      cout << ((bt[x] & gt[v]).count()) % 2;
    }
  }
  return 0;
}
