#include <bits/stdc++.h>
using namespace std;
std::ostream& operator<<(std::ostream& os, pair<int, int> p) {
  os << p.first << ", " << p.second;
  return os;
}
const int MAXN = 4 * 1e5 + 5;
vector<vector<long long int> > fenwick(MAXN);
vector<vector<long long int> > vals(MAXN);
void addupd(int x, int y) {
  while (x < MAXN) {
    vals[x].push_back(y);
    x += (x & -x);
  }
}
void addq(int x, int y) {
  if (x <= 0 || y <= 0) return;
  while (x > 0) {
    vals[x].push_back(y);
    x -= (x & -x);
  }
}
void update(pair<long long int, long long int> d, int x) {
  int i = d.first;
  while (i < MAXN) {
    int j = lower_bound(vals[i].begin(), vals[i].end(), d.second) -
            vals[i].begin() + 1;
    while (j < (int)fenwick[i].size()) {
      fenwick[i][j] += x;
      j += (j & -j);
    }
    i += (i & -i);
  }
}
long long int query(pair<long long int, long long int> d) {
  long long int x = 0;
  int i = d.first;
  if (d.second == 0) return 0;
  while (i > 0) {
    int j = lower_bound(vals[i].begin(), vals[i].end(), d.second) -
            vals[i].begin() + 1;
    while (j > 0) {
      x += fenwick[i][j];
      j -= (j & -j);
    }
    i -= (i & -i);
  }
  return x;
}
map<pair<long long int, long long int>, pair<long long int, long long int> >
    ord;
vector<pair<long long int, long long int> > inds(
    MAXN, pair<long long int, long long int>(0, 0));
vector<long long int> perma(MAXN), permb(MAXN);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  set<pair<long long int, long long int> > allpoints;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    inds[a].first = i + 1;
    perma[i + 1] = a;
  }
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    permb[i + 1] = a;
    inds[a].second = i + 1;
  }
  for (int i = 1; i <= n; i++) {
    allpoints.insert(inds[i]);
    addupd(inds[i].first, inds[i].second);
  }
  vector<pair<long long int, long long int> > tinds = inds;
  vector<long long int> tpemb = permb;
  vector<vector<int> > queries;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      vector<int> mng{a, b, c, d};
      addq(a - 1, c - 1);
      addq(b, d);
      addq(a - 1, d);
      addq(b, c - 1);
      if (b == 6)
        ;
      queries.push_back(mng);
    } else {
      int a, b;
      cin >> a >> b;
      swap(tinds[tpemb[a]].second, tinds[tpemb[b]].second);
      swap(tpemb[a], tpemb[b]);
      allpoints.insert(tinds[tpemb[a]]);
      allpoints.insert(tinds[tpemb[b]]);
      addupd(tinds[tpemb[a]].first, tinds[tpemb[a]].second);
      addupd(tinds[tpemb[b]].first, tinds[tpemb[b]].second);
      vector<int> mng{a, b};
      queries.push_back(mng);
    }
  }
  int mn = 1;
  ;
  for (; mn <= n; mn++) {
    ;
    sort(vals[mn].begin(), vals[mn].end());
    ;
    vals[mn].resize(unique(vals[mn].begin(), vals[mn].end()) -
                    vals[mn].begin());
    ;
    ;
    fenwick[mn].resize(vals[mn].size() + 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    ;
    ;
    ;
    update(inds[i], 1);
  }
  for (auto item : queries) {
    if (item.size() == 2) {
      int a = item[0], b = item[1];
      update(inds[permb[a]], -1);
      update(inds[permb[b]], -1);
      swap(inds[permb[a]].second, inds[permb[b]].second);
      swap(permb[a], permb[b]);
      update(inds[permb[a]], 1);
      update(inds[permb[b]], 1);
    } else {
      int kucukx = item[0], buyukx = item[1], kucuky = item[2],
          buyuky = item[3];
      for (int i = 1; i <= n; i++) {
        ;
      }
      pair<long long int, long long int> upperright =
          pair<long long int, long long int>(buyukx, buyuky);
      pair<long long int, long long int> upperleft =
          pair<long long int, long long int>(kucukx - 1, buyuky);
      pair<long long int, long long int> bottomright =
          pair<long long int, long long int>(buyukx, kucuky - 1);
      pair<long long int, long long int> bottomleft =
          pair<long long int, long long int>(kucukx - 1, kucuky - 1);
      ;
      ;
      ;
      ;
      ;
      long long int s = query(upperright) + query(bottomleft) -
                        query(bottomright) - query(upperleft);
      cout << s << "\n";
    }
  }
  return 0;
}
