#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 100000 + 10;
const int L = 7000 + 10;
int v[L], p[L], cnt = 0;
int mu[L];
bitset<L> bit[N], Mu[L], S[L];
inline void sieve(int n) {
  v[1] = mu[1] = 1;
  for (register int i = 2; i <= n; ++i) {
    if (!v[i]) p[++cnt] = i, mu[i] = -1;
    for (register int j = 1; j <= cnt && i * p[j] <= n; ++j) {
      v[i * p[j]] = 1;
      if (i % p[j])
        mu[i * p[j]] = -mu[i];
      else
        break;
    }
  }
  for (register int i = 1; i <= n; ++i)
    for (register int j = i; j <= n; j += i)
      S[j][i] = 1, Mu[i][j] = (mu[j / i] != 0);
}
int main() {
  sieve(7000);
  int n = read(), Q = read();
  while (Q--) {
    int op = read();
    if (op == 1) {
      int x = read(), y = read();
      bit[x] = S[y];
    } else if (op == 2) {
      int x = read(), y = read(), z = read();
      bit[x] = bit[y] ^ bit[z];
    } else if (op == 3) {
      int x = read(), y = read(), z = read();
      bit[x] = bit[y] & bit[z];
    } else {
      int x = read(), y = read();
      printf("%d", (bit[x] & Mu[y]).count() & 1);
    }
  }
  return 0;
}
