#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int reva[N], b[N], bb[N];
int t[N], la[N], ra[N], lb[N], rb[N], x[N], y[N];
struct FenwickTree {
  vector<int> vals[N], f[N];
  void pushUpdate(int i, int val, int n) {
    for (; i < n; i = (i | (i + 1))) {
      vals[i].push_back(val);
    }
  }
  void pushGet(int r, int val) {
    for (; r >= 0; r = (r & (r + 1)) - 1) {
      vals[r].push_back(val);
    }
  }
  void pushGet(int l, int r, int lval, int rval) {
    pushGet(r, rval);
    pushGet(r, lval - 1);
    pushGet(l - 1, rval);
    pushGet(l - 1, lval - 1);
  }
  void build(int n) {
    for (int i = 0; i < n; i++) {
      sort(vals[i].begin(), vals[i].end());
      vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
      f[i].resize(vals[i].size());
    }
  }
  void update(int i, int val, int delta, int n) {
    for (; i < n; i = (i | (i + 1))) {
      for (int j = lower_bound(vals[i].begin(), vals[i].end(), val) -
                   vals[i].begin();
           j < f[i].size(); j = (j | (j + 1))) {
        f[i][j] += delta;
      }
    }
  }
  int get(int r, int val) {
    int res = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) {
      for (int j = lower_bound(vals[r].begin(), vals[r].end(), val) -
                   vals[r].begin();
           j >= 0; j = (j & (j + 1)) - 1) {
        res += f[r][j];
      }
    }
    return res;
  }
  int get(int l, int r, int lval, int rval) {
    return get(r, rval) - get(r, lval - 1) -
           (get(l - 1, rval) - get(l - 1, lval - 1));
  }
};
FenwickTree fenTree;
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int value;
    cin >> value;
    reva[value] = i;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    bb[i] = b[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> t[i];
    if (t[i] == 1) {
      cin >> la[i] >> ra[i] >> lb[i] >> rb[i];
    } else {
      cin >> x[i] >> y[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    fenTree.pushUpdate(i, reva[b[i]], n + 1);
  }
  for (int i = 1; i <= m; i++) {
    if (t[i] == 1) {
      fenTree.pushGet(lb[i], rb[i], la[i], ra[i]);
    } else {
      fenTree.pushUpdate(x[i], reva[b[x[i]]], n + 1);
      fenTree.pushUpdate(y[i], reva[b[y[i]]], n + 1);
      swap(b[x[i]], b[y[i]]);
      fenTree.pushUpdate(x[i], reva[b[x[i]]], n + 1);
      fenTree.pushUpdate(y[i], reva[b[y[i]]], n + 1);
    }
  }
  fenTree.build(n + 1);
  for (int i = 1; i <= n; i++) {
    b[i] = bb[i];
    fenTree.update(i, reva[b[i]], 1, n + 1);
  }
  for (int i = 1; i <= m; i++) {
    if (t[i] == 1) {
      cout << fenTree.get(lb[i], rb[i], la[i], ra[i]) << "\n";
    } else {
      fenTree.update(x[i], reva[b[x[i]]], -1, n + 1);
      fenTree.update(y[i], reva[b[y[i]]], -1, n + 1);
      swap(b[x[i]], b[y[i]]);
      fenTree.update(x[i], reva[b[x[i]]], 1, n + 1);
      fenTree.update(y[i], reva[b[y[i]]], 1, n + 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  solve();
}
