#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
template <typename T>
class FenwickTree {
  int n;
  std::vector<T> bit;

 public:
  FenwickTree(int _n) : n(_n), bit(_n, 0) {}
  void add(int pos, const T &w) {
    assert(0 <= pos && pos < n);
    for (int i = pos; i < n; i |= i + 1) bit[i] += w;
  }
  T sum(int pos) {
    assert(0 <= pos && pos <= n);
    T res = 0;
    for (int i = pos - 1; i >= 0; i = (i & (i + 1)) - 1) {
      res += bit[i];
    }
    return res;
  }
  T sum(int l, int r) {
    assert(0 <= l && l <= r && r <= n);
    return sum(r) + (-sum(l));
  }
};
constexpr int B = 1000;
vector<FenwickTree<int>> ft(210, FenwickTree<int>(200010));
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  map<int, int> conv;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    conv[a[i]] = i;
    a[i] = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i] = conv[b[i]];
    ft[i / B].add(b[i], 1);
  }
  while (m--) {
    int type;
    cin >> type;
    if (type == 1) {
      int la, ra, lb, rb;
      cin >> la >> ra >> lb >> rb;
      la--;
      ra--;
      lb--;
      rb--;
      int ret = 0;
      int st = lb / B, gt = rb / B;
      if (st == gt) {
        for (int i = lb; i <= rb; i++) {
          if (la <= b[i] && b[i] <= ra) ret++;
        }
      } else {
        for (int i = lb; i < (st + 1) * B; i++) {
          if (la <= b[i] && b[i] <= ra) ret++;
        }
        for (int i = gt * B; i <= rb; i++) {
          if (la <= b[i] && b[i] <= ra) ret++;
        }
        for (int i = st + 1; i < gt; i++) {
          ret += ft[i].sum(la, ra + 1);
        }
      }
      cout << ret << endl;
    } else {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      ft[x / B].add(b[x], -1);
      ft[x / B].add(b[y], 1);
      ft[y / B].add(b[y], -1);
      ft[y / B].add(b[x], 1);
      swap(b[x], b[y]);
    }
  }
}
