#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2, T = 700;
int n, nQ, a[N], b[N], pos[N], pb[N];
int bl[N / T + 2][N];
void up(int id, int u, int val) {
  for (; id <= ((n - 1) / T + 1); id += id & -id)
    for (int v = u; v <= n; v += v & -v) bl[id][v] += val;
}
int get(int id, int u) {
  int ans = 0;
  for (; id; id -= id & -id)
    for (int v = u; v; v -= v & -v) ans += bl[id][v];
  return ans;
}
int getall(int l, int r, int u) {
  int ans = 0;
  int bl = ((l - 1) / T + 1);
  int br = ((r - 1) / T + 1);
  while (l > 1 && ((l - 1 - 1) / T + 1) == bl) ans -= (pb[a[--l]] <= u);
  while (r < n && ((r + 1 - 1) / T + 1) == br) ans -= (pb[a[++r]] <= u);
  ans += get(br, u) - get(bl - 1, u);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> nQ;
  for (int i = 1; i <= n; ++i) cin >> a[i], pos[a[i]] = i;
  for (int i = 1; i <= n; ++i)
    cin >> b[i], up(((pos[b[i]] - 1) / T + 1), i, 1), pb[b[i]] = i;
  while (nQ--) {
    int t, l, r;
    cin >> t >> l >> r;
    if (t == 1) {
      int u, v;
      cin >> u >> v;
      cout << getall(l, r, v) - getall(l, r, u - 1) << '\n';
    } else {
      up(((pos[b[l]] - 1) / T + 1), l, -1);
      up(((pos[b[r]] - 1) / T + 1), r, -1);
      swap(b[l], b[r]);
      pb[b[l]] = l;
      pb[b[r]] = r;
      up(((pos[b[l]] - 1) / T + 1), l, 1);
      up(((pos[b[r]] - 1) / T + 1), r, 1);
    }
  }
  return 0;
}
