#include <bits/stdc++.h>
using namespace std;
struct fenw {
  vector<int> a;
  int n;
  fenw(int n) : n(n), a(n) {}
  int sum(int i) {
    long long ret = 0;
    for (; i > -1; i = (i & (i + 1)) - 1) ret += a[i];
    return ret;
  }
  void up(int i, int val) {
    for (; i < n; i |= (i + 1)) a[i] += val;
  }
};
int main() {
  cout.precision(30);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > ab(n);
  for (int i = 0; i < n; i++) cin >> ab[i].first, ab[i].first--;
  for (int i = 0; i < n; i++) cin >> ab[i].second, ab[i].second--;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) {
    tie(a[i], b[i]) = ab[i];
  }
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) x[a[i]] = i;
  for (int i = 0; i < n; i++) y[i] = x[b[i]];
  b = y;
  const int bs = 2048;
  int bcnt = n / bs + 1;
  vector<fenw> blocks(bcnt, fenw(n));
  for (int i = 0; i < n; i++) {
    int bl = i / bs;
    blocks[bl].up(b[i], 1);
  }
  while (m--) {
    int t;
    cin >> t;
    if (t == 1) {
      long long la, ra, lb, rb;
      cin >> la >> ra >> lb >> rb;
      la--, ra--, lb--, rb--;
      int res = 0;
      for (int i = lb; i <= rb;) {
        if (i % bs == 0 && i + bs <= rb) {
          int bl = i >> 11;
          res += blocks[bl].sum(ra) - blocks[bl].sum(la - 1);
          i += bs;
        } else {
          if (la <= b[i] && b[i] <= ra) res++;
          i++;
        }
      }
      cout << res << '\n';
    } else {
      int x, y;
      cin >> x >> y;
      x--, y--;
      long long blx = x / bs, bly = y / bs;
      blocks[blx].up(b[x], -1);
      blocks[bly].up(b[x], 1);
      blocks[bly].up(b[y], -1);
      blocks[blx].up(b[y], 1);
      swap(b[x], b[y]);
    }
  }
  return 0;
}
