#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4.2,sse4.1,ssse3,sse3,sse2,sse")
#pragma GCC target("avx")
#pragma GCC target("avx2")
using namespace std;
const int maxn = (int)2e5;
alignas(8 * 4) int a[maxn];
alignas(8 * 4) int b[maxn];
alignas(8 * 4) int posA[maxn];
alignas(8 * 4) int posB[maxn];
alignas(8 * 4) int special[maxn];
bool check(int lb, int rb, int i) {
  return lb <= special[i] && special[i] <= rb;
}
int getCount(int la, int ra, int lb, int rb) {
  int count = 0;
  if (la / 8 == ra / 8) {
    for (int i = la; i <= ra; i++) count += check(lb, rb, i);
    return count;
  } else {
    while (la % 8 != 0) {
      count += check(lb, rb, la);
      la++;
    }
    while (ra % 8 != 8 - 1) {
      count += check(lb, rb, ra);
      ra--;
    }
    ra++;
    for (int i = la; i + 8 <= ra; i += 8) {
      for (int j = 0; j < 8; j++) {
        count += check(lb, rb, i + j);
      }
    }
  }
  return count;
}
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
  for (int i = 0; i < n; i++) cin >> b[i], b[i]--;
  for (int i = 0; i < n; i++) posA[a[i]] = i;
  for (int i = 0; i < n; i++) posB[b[i]] = i;
  for (int i = 0; i < n; i++) special[i] = posB[a[i]];
  for (int i = 0; i < m; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int la, ra, lb, rb;
      cin >> la >> ra >> lb >> rb;
      la--;
      ra--;
      lb--;
      rb--;
      int count = getCount(la, ra, lb, rb);
      cout << count << '\n';
    } else {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      swap(b[x], b[y]);
      posB[b[x]] = x;
      posB[b[y]] = y;
      special[posA[b[x]]] = x;
      special[posA[b[y]]] = y;
    }
  }
}
