#include <bits/stdc++.h>
using namespace std;
template <class T>
int size(const T &x) {
  return x.size();
}
const int INF = ~(1 << 31);
const double pi = acos(-1);
const double EPS = 1e-9;
int P1[2 * 100000 + 50];
int P2[2 * 100000 + 50];
const int bucketsize = 1000;
const int nmbuckets = (2 * 100000 + 500) / bucketsize;
struct fenwick_tree {
  int n;
  vector<int> data;
  fenwick_tree(int _n) : n(_n), data(vector<int>(n)) {}
  void update(int at, int by) {
    while (at < n) data[at] += by, at |= at + 1;
  }
  int query(int at) {
    int res = 0;
    while (at >= 0) res += data[at], at = (at & (at + 1)) - 1;
    return res;
  }
  int rsq(int a, int b) { return query(b) - query(a - 1); }
};
int main() {
  cin.sync_with_stdio(false);
  int n, m;
  int bc[2 * 100000 + 50];
  cin >> n >> m;
  vector<int> A(n);
  vector<int> B(n);
  for (int i = (0); i < (n); i++) cin >> A[i], A[i]--, P1[A[i]] = i;
  for (int i = (0); i < (n); i++) cin >> B[i], B[i]--, P2[B[i]] = i;
  vector<int> LS(n);
  vector<fenwick_tree> bucket;
  for (int i = (0); i < (nmbuckets + 1); i++)
    bucket.push_back(fenwick_tree(n + 1));
  for (int i = (0); i < (n); i++) LS[i] = P2[A[i]], bc[A[i]] = i;
  for (int i = (0); i < (n); i++) bucket[i / bucketsize].update(LS[i], 1);
  for (int test = (0); test < (m); test++) {
    int s;
    cin >> s;
    if (s == 1) {
      int l, r, l2, r2;
      cin >> l >> r >> l2 >> r2;
      l--, r--;
      l2--, r2--;
      long long sm1 = 0;
      long long sm2 = 0;
      for (int i = (0); i < (nmbuckets); i++) {
        int start = i * bucketsize, end = (i + 1) * bucketsize;
        if (l <= start && end <= r) {
          sm1 += bucket[i].query(l2 - 1);
          sm2 += bucket[i].query(r2);
        } else {
          start = max(l, start);
          end = min(r + 1, end);
          for (int j = (start); j < (end); j++) {
            if (j >= n) break;
            if (LS[j] < l2) sm1 += 1;
            if (LS[j] <= r2) sm2 += 1;
          }
        }
      }
      cout << sm2 - sm1 << endl;
    } else {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      int val1 = B[a];
      int val2 = B[b];
      int first = LS[bc[val1]];
      int second = LS[bc[val2]];
      bucket[bc[val1] / bucketsize].update(first, -1);
      bucket[bc[val2] / bucketsize].update(second, -1);
      bucket[bc[val2] / bucketsize].update(first, 1);
      bucket[bc[val1] / bucketsize].update(second, 1);
      swap(B[a], B[b]);
      swap(LS[bc[val1]], LS[bc[val2]]);
    }
  }
  return 0;
}
