#include <bits/stdc++.h>
using namespace std;
const int N = 7070;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<bitset<N> > mset(n + 1), divs(N), muls(N);
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      divs[j].set(i);
    }
  }
  vector<int> mu(N);
  mu[1] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 2 * i; j < N; j += i) {
      mu[j] -= mu[i];
    }
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; i * j < N; j++) {
      muls[i][i * j] = mu[j] & 1;
    }
  }
  string ans;
  while (q--) {
    int op, v, x, y, z;
    cin >> op;
    if (op == 1) {
      cin >> x >> v;
      mset[x] = divs[v];
    } else if (op == 2) {
      cin >> x >> y >> z;
      mset[x] = mset[y] ^ mset[z];
    } else if (op == 3) {
      cin >> x >> y >> z;
      mset[x] = mset[y] & mset[z];
    } else {
      cin >> x >> v;
      ans += ((mset[x] & muls[v]).count() & 1) + '0';
    }
  }
  cout << ans << '\n';
  return 0;
}
