#include <bits/stdc++.h>
using namespace std;
int n, Q, tot, p[7005], u[7005], k, x, y, z;
bool f[7005];
bitset<7005> v[7005], v2[7005], F[100005], as;
vector<int> fac[7005];
void predeal(int lim) {
  u[1] = 1;
  for (int i = 2; i <= lim; i++) {
    if (!f[i]) p[++tot] = i, u[i] = -1;
    for (int j = 1; j <= tot; j++) {
      if (i * p[j] > lim) break;
      f[i * p[j]] = 1;
      if (!(i % p[j])) {
        u[i * p[j]] = 0;
        break;
      }
      u[i * p[j]] = -u[i];
    }
  }
  for (int i = 1; i <= lim; i++)
    for (int j = i; j <= lim; j += i) {
      if (u[j / i] != 0) v[i][j] = 1;
      v2[j][i] = 1;
    }
}
int main() {
  predeal(7000);
  scanf("%d%d", &n, &Q);
  while (Q--) {
    scanf("%d%d%d", &k, &x, &y);
    if (k == 1) {
      F[x] = v2[y];
    }
    if (k == 2) {
      scanf("%d", &z);
      F[x] = F[y] ^ F[z];
    }
    if (k == 3) {
      scanf("%d", &z);
      F[x] = F[y] & F[z];
    }
    if (k == 4) {
      as = F[x] & v[y];
      int ans = as.count();
      if ((ans & 1) == 0)
        printf("0");
      else
        printf("1");
    }
  }
  puts("");
  return 0;
}
