#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
const int maxn = (int)2e5;
alignas(32) int a[maxn];
alignas(32) int b[maxn];
alignas(32) int posA[maxn];
alignas(32) int posB[maxn];
alignas(32) int special[maxn];
int getCount(int la, int ra, int lb, int rb) {
  int count = 0;
  if (la / 512 == ra / 512) {
    for (int i = la; i <= ra; i++)
      count += lb <= special[i] && special[i] <= rb;
    return count;
  } else {
    int la2 = (la + 512 - 1) / 512 * 512;
    for (int i = la; i < la2; i++)
      count += lb <= special[i] && special[i] <= rb;
    la = la2;
    int ra2 = ra / 512 * 512;
    for (int i = ra; i >= ra2; i--)
      count += lb <= special[i] && special[i] <= rb;
    ra = ra2;
    for (int i = la; i + 512 <= ra; i += 512)
      for (int j = 0; j < 512; j++)
        count += lb <= special[(i + j)] && special[(i + j)] <= rb;
  }
  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;
    }
  }
}
