#include <bits/stdc++.h>
using namespace std;
int b[200010], n, pos[200010];
vector<int> vals[200010];
vector<int> sum[200010];
struct Request {
  int la, lb, ra, rb, x, y, op;
} q[200010];
void addupdate(int x, int y) {
  for (; x <= n; x += x & -x) {
    vals[x].push_back(y);
  }
}
void addget(int x, int y) {
  for (; x > 0; x -= x & -x) {
    vals[x].push_back(y);
  }
}
void update(int x, int y, int val) {
  for (; x <= n; x += x & -x) {
    for (int j =
             lower_bound(vals[x].begin(), vals[x].end(), y) - vals[x].begin();
         j < int(sum[x].size()); j += j & -j) {
      sum[x][j] += val;
    }
  }
}
int get(int x, int y) {
  int ret = 0;
  for (; x > 0; x -= x & -x)
    for (int j =
             lower_bound(vals[x].begin(), vals[x].end(), y) - vals[x].begin();
         j > 0; j -= j & -j)
      ret += sum[x][j];
  return ret;
}
int main() {
  int m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    pos[x] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    b[i] = pos[b[i]];
  }
  vector<int> tmp(b, b + n + 1);
  for (int i = 1; i <= n; i++) {
    vals[i].push_back(0);
    addupdate(i, b[i]);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &q[i].op);
    if (q[i].op == 1) {
      scanf("%d%d%d%d", &q[i].la, &q[i].ra, &q[i].lb, &q[i].rb);
      addget(q[i].ra, q[i].rb);
      addget(q[i].ra, q[i].lb - 1);
      addget(q[i].la - 1, q[i].rb);
      addget(q[i].la - 1, q[i].lb - 1);
    } else {
      scanf("%d%d", &q[i].x, &q[i].y);
      int x = q[i].x, y = q[i].y;
      addupdate(b[x], x);
      addupdate(b[y], y);
      swap(b[x], b[y]);
      addupdate(b[x], x);
      addupdate(b[y], y);
    }
  }
  for (int i = 1; i <= n; i++) b[i] = tmp[i];
  for (int i = 1; i <= n; i++) {
    sort(vals[i].begin(), vals[i].end());
    vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
    sum[i].resize(vals[i].size(), 0);
  }
  for (int i = 1; i <= n; i++) {
    update(b[i], i, 1);
  }
  for (int i = 1; i <= m; i++) {
    if (q[i].op == 1) {
      int ans = 0;
      ans += get(q[i].ra, q[i].rb);
      ans -= get(q[i].ra, q[i].lb - 1);
      ans -= get(q[i].la - 1, q[i].rb);
      ans += get(q[i].la - 1, q[i].lb - 1);
      printf("%d\n", ans);
    } else {
      int x = q[i].x, y = q[i].y;
      update(b[x], x, -1);
      update(b[y], y, -1);
      swap(b[x], b[y]);
      update(b[x], x, 1);
      update(b[y], y, 1);
    }
  }
}
