#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 100007;
const int MAX = 7007;
int n, q;
bitset<MAX> a[MAXN];
bitset<MAX> dc[MAX], coef[MAX];
int qtp, qa, qb, qc;
int asqr[MAX], kas;
bool mu2[MAX];
void init();
void input();
void inputq();
void work();
int main() {
  init();
  input();
  work();
}
void init() {
  ios::sync_with_stdio(false);
  for (int i = 1; i * i < MAX; ++i) asqr[++kas] = i * i;
  for (int i = 1; i < MAX; ++i) {
    mu2[i] = 1;
    for (int j = 2; j <= kas; ++j) {
      if (i % asqr[j] == 0) {
        mu2[i] = 0;
        break;
      }
    }
  }
  for (int i = 1; i < MAX; ++i) {
    for (int j = 1; i * j < MAX; ++j) {
      if (mu2[j]) {
        coef[i][i * j] = 1;
      }
      dc[i * j][i] = 1;
    }
  }
}
void input() { scanf("%d%d", &n, &q); }
void inputq() {
  scanf("%d%d%d", &qtp, &qa, &qb);
  if (qtp == 2 || qtp == 3) scanf("%d", &qc);
}
void work() {
  for (int _q = 0; _q < q; ++_q) {
    inputq();
    if (qtp == 1) {
      a[qa] = dc[qb];
    } else if (qtp == 2) {
      a[qa] = a[qb] ^ a[qc];
    } else if (qtp == 3) {
      a[qa] = a[qb] & a[qc];
    } else {
      putchar(((a[qa] & coef[qb]).count() & 1) + '0');
    }
  }
}
