#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& i : v) is >> i;
  return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ' ' << p.second;
  return os;
}
const int N = 7005;
using SET = bitset<N>;
bool check(int n) {
  int p = 2;
  while (p * p <= n) {
    if (n % p == 0) {
      n /= p;
      if (n % p == 0) return false;
    }
    ++p;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  vector<SET> d(N, 0);
  for (int i = 1; i < N; ++i)
    for (int j = i; j < N; j += i)
      if (check(j / i)) d[i][j] = 1;
  vector<SET> g(N, 0);
  for (int i = 1; i < N; ++i)
    for (int j = i; j < N; j += i) g[j][i] = 1;
  vector<SET> id(N, 0);
  for (int i = 0; i < N; ++i) id[i][i] = 1;
  int n, q;
  cin >> n >> q;
  vector<SET> v(n + 1, 0);
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int x, y;
      cin >> x >> y;
      v[x] = g[y];
    } else if (t == 2) {
      int x, y, z;
      cin >> x >> y >> z;
      v[x] = v[y] ^ v[z];
    } else if (t == 3) {
      int x, y, z;
      cin >> x >> y >> z;
      v[x] = v[y] & v[z];
    } else if (t == 4) {
      int x, y;
      cin >> x >> y;
      cout << ((v[x] & (d[y])).count() & 1);
    }
  }
  return 0;
}
