#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
struct Query {
  int type, x, y, flag, num, cnt;
  bool operator<(const Query& rhs) const {
    return x == rhs.x ? type < rhs.type : x < rhs.x;
  }
};
Query query[maxn * 10], tmp[maxn * 10];
int tot = 0, ans[maxn], a[maxn], b[maxn], p[maxn], mp[maxn], n, m;
namespace BIT {
int c[maxn];
inline int lowbit(int x) { return x & (-x); }
int ask(int x) {
  int ans = 0;
  for (; x; x -= lowbit(x)) ans += c[x];
  return ans;
}
void add(int x, int y) {
  for (; x <= n; x += lowbit(x)) c[x] += y;
}
void clear(int x) {
  for (; x <= n; x += lowbit(x)) {
    if (c[x])
      c[x] = 0;
    else
      break;
  }
}
}  // namespace BIT
void cdq(int L, int R) {
  if (R == L) return;
  int M = (L + R) >> 1;
  cdq(L, M);
  cdq(M + 1, R);
  int l = L, r = M + 1;
  int o = L;
  while (l <= M && r <= R) {
    if (query[l] < query[r]) {
      if (query[l].type == 0) BIT::add(query[l].y, query[l].cnt);
      tmp[o++] = query[l++];
    } else {
      if (query[r].type == 1)
        ans[query[r].num] += query[r].flag * BIT::ask(query[r].y);
      tmp[o++] = query[r++];
    }
  }
  while (l <= M) tmp[o++] = query[l++];
  while (r <= R) {
    if (query[r].type == 1)
      ans[query[r].num] += query[r].flag * BIT::ask(query[r].y);
    tmp[o++] = query[r++];
  }
  for (int i = L; i <= R; i++) {
    BIT::clear(tmp[i].y);
    query[i] = tmp[i];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    p[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    query[++tot] = (Query){0, i, p[b[i]], 0, 0, 1};
  }
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    int flag;
    scanf("%d", &flag);
    if (flag == 1) {
      cnt++;
      int l1, r1, l2, r2;
      scanf("%d%d%d%d", &l2, &r2, &l1, &r1);
      query[++tot] = (Query){1, l1 - 1, l2 - 1, 1, cnt, 1};
      query[++tot] = (Query){1, l1 - 1, r2, -1, cnt, 1};
      query[++tot] = (Query){1, r1, l2 - 1, -1, cnt, 1};
      query[++tot] = (Query){1, r1, r2, 1, cnt, 1};
    } else {
      int x, y;
      scanf("%d%d", &x, &y);
      query[++tot] = (Query){0, x, p[b[x]], 0, 0, -1};
      query[++tot] = (Query){0, y, p[b[y]], 0, 0, -1};
      swap(p[b[x]], p[b[y]]);
      query[++tot] = (Query){0, x, p[b[x]], 0, 0, 1};
      query[++tot] = (Query){0, y, p[b[y]], 0, 0, 1};
    }
  }
  cdq(1, tot);
  for (int i = 1; i <= cnt; i++) printf("%d\n", ans[i]);
}
