#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N];
int b[N];
map<int, int> mp;
struct event {
  int time;
  int pos;
  int val;
  int qid;
  event() {}
  event(int time, int pos, int val, int qid)
      : time(time), pos(pos), val(val), qid(qid) {}
} e[N * 5];
bool cmp_time(const event &p, const event &q) { return p.time < q.time; }
bool cmp_pos(const event &p, const event &q) { return p.pos < q.pos; }
int cnt;
int c[N];
int lowbit(int x) { return x & (-x); }
int query(int x) {
  int ans = 0;
  for (int i = x; i > 0; i -= lowbit(i)) ans += c[i];
  return ans;
}
void add(int x, int y) {
  for (int i = x; i < N; i += lowbit(i)) c[i] += y;
}
void reset(int x) {
  for (int i = x; i < N; i += lowbit(i)) c[i] = 0;
}
int ans[N];
int stk[N * 5];
void solve(int l, int r) {
  if (l == r) {
    return;
  }
  int mid = (l + r) >> 1;
  solve(l, mid);
  solve(mid + 1, r);
  sort(e + l, e + mid + 1, cmp_pos);
  sort(e + mid + 1, e + r + 1, cmp_pos);
  int cnt = 0;
  for (int i = mid + 1, j = l; i <= r; i++) {
    while (j <= mid && e[j].pos <= e[i].pos) {
      if (e[j].qid) {
        j++;
        continue;
      }
      int flag = e[j].val > 0 ? 1 : -1;
      add(flag * e[j].val, flag);
      stk[cnt++] = flag * e[j].val;
      j++;
    }
    if (e[i].qid == 0) continue;
    int flag = e[i].qid > 0 ? 1 : -1;
    ans[flag * e[i].qid] += flag * query(e[i].val);
  }
  sort(e + l, e + r + 1, cmp_time);
  for (int i = 0; i < cnt; i++) reset(stk[i]);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    mp[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    b[i] = mp[b[i]];
    e[++cnt] = event(0, i, b[i], 0);
  }
  memset(ans, -1, sizeof(ans));
  int x, y, l1, r1, l2, r2;
  for (int i = 1; i <= m; i++) {
    int cd;
    scanf("%d", &cd);
    if (cd == 1) {
      scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
      e[++cnt] = event(i, r2, r1, i);
      e[++cnt] = event(i, l2 - 1, l1 - 1, i);
      e[++cnt] = event(i, r2, l1 - 1, -i);
      e[++cnt] = event(i, l2 - 1, r1, -i);
      ans[i] = 0;
    } else {
      scanf("%d%d", &x, &y);
      e[++cnt] = event(i, x, -b[x], 0);
      e[++cnt] = event(i, y, -b[y], 0);
      swap(b[x], b[y]);
      e[++cnt] = event(i, x, b[x], 0);
      e[++cnt] = event(i, y, b[y], 0);
    }
  }
  solve(1, cnt);
  for (int i = 1; i <= m; i++)
    if (ans[i] >= 0) printf("%d\n", ans[i]);
  return 0;
}
