#include <bits/stdc++.h>
using namespace std;
int n, m, tot, u[7000 + 5], check[7000 + 5], prime[7000 + 5];
bitset<7000> s[100010], num[7000 + 5], f[7000 + 5];
int main() {
  int tp, x, y, z;
  scanf("%d%d", &n, &m);
  u[1] = 1;
  for (int i = 2; i <= 7000; i++) {
    if (!check[i]) prime[++tot] = i, u[i] = 1;
    for (int j = 1; j <= tot; j++) {
      if (i * prime[j] > 7000) continue;
      check[i * prime[j]] = 1;
      if (!(i % prime[j])) {
        u[i * prime[j]] = 0;
        break;
      }
      u[i * prime[j]] = -u[i];
    }
  }
  for (int i = 1; i <= 7000; i++)
    for (int j = 1; j * j <= i; j++)
      if (!(i % j)) num[i][j] = num[i][i / j] = 1;
  for (int i = 1; i <= 7000; i++)
    for (int j = 1; i * j <= 7000; j++)
      if (abs(u[j])) f[i][i * j] = 1;
  while (m--) {
    scanf("%d%d%d", &tp, &x, &y);
    if (tp == 1) s[x] = num[y];
    if (tp == 2) scanf("%d", &z), s[x] = s[y] ^ s[z];
    if (tp == 3) scanf("%d", &z), s[x] = s[y] & s[z];
    if (tp == 4) printf("%d", (s[x] & f[y]).count() & 1);
  }
  printf("\n");
  return 0;
}
