#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = mod * mod;
const long double eps = 1e-8;
bitset<7001> memo[100000];
bitset<7001> for_query[7001];
bitset<7001> for_set[7001];
void init() {
  vector<int> v;
  for (int i = 2; i <= 90; i++) {
    v.push_back(i * i);
  }
  bool divsqr[7001] = {};
  for (int i = 1; i <= 7000; i++) {
    for (int j = 0; j < v.size(); j++) {
      if (i % v[j] == 0) {
        divsqr[i] = true;
        break;
      }
    }
  }
  for (int i = 1; i <= 7000; i++) {
    for (int j = i; j <= 7000; j += i) {
      if (!divsqr[j / i]) {
        for_query[i][j] = 1;
      }
      for_set[j][i] = 1;
    }
  }
}
void solve() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, v;
      cin >> x >> v;
      x--;
      memo[x] = for_set[v];
    } else if (t == 2) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      z--;
      memo[x] = memo[y] ^ memo[z];
    } else if (t == 3) {
      int x, y, z;
      cin >> x >> y >> z;
      x--;
      y--;
      z--;
      memo[x] = memo[y] & memo[z];
    } else {
      int x, v;
      cin >> x >> v;
      x--;
      int num = (memo[x] & for_query[v]).count();
      cout << num % 2;
    }
  }
  cout << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  init();
  solve();
  return 0;
}
