#include <bits/stdc++.h>
using namespace std;
template <class T>
void print(vector<T> s) {
  for (auto x : s) cout << x << " ";
  cout << endl;
}
const int maxv = 7007, maxn = 1e5 + 7;
bitset<maxv> s[maxn];
bitset<maxv> nullik;
bitset<maxv> kost[maxn];
int mu[maxv];
int smalest[maxv];
vector<int> divisors[maxv];
bool f[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> free;
  for (int i = 1; i < maxv; i++) {
    smalest[i] = -1;
    int cur = i;
    bool ok = true;
    divisors[i].push_back(1);
    for (int j = 2; j <= i; j++) {
      if (i % j == 0) divisors[i].push_back(j);
      if (cur % j != 0) continue;
      if (smalest[i] == -1) smalest[i] = j;
      if (cur % (j * j) == 0) ok = false;
      while (cur % j == 0) cur /= j;
    }
    if (ok) free.push_back(i);
  }
  for (int i = 1; i < maxv; i++) {
    for (int u : free) {
      if (u * i >= maxv) break;
      kost[i].set(i * u);
    }
  }
  while (q-- > 0) {
    int t;
    cin >> t;
    if (t == 1) {
      int i, v;
      cin >> i >> v;
      s[i - 1] &= nullik;
      for (int u : divisors[v]) s[i - 1].set(u);
    }
    if (t == 2) {
      int x, y, z;
      cin >> x >> y >> z;
      s[x - 1] = s[y - 1] ^ s[z - 1];
    }
    if (t == 3) {
      int x, y, z;
      cin >> x >> y >> z;
      s[x - 1] = s[y - 1] & s[z - 1];
    }
    if (t == 4) {
      int i, v;
      cin >> i >> v;
      cout << ((s[i - 1] & kost[v]).count() & 1);
    }
  }
  cout << endl;
  return 0;
}
