#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 233;
const int S = 2000;
int ch[102][maxn];
int a[maxn], b[maxn], p[maxn], n;
inline int query(int* ch, int x) {
  int sum = 0;
  for (int i = x; i; i -= i & (-i)) sum += ch[i];
  return sum;
}
inline void add(int* ch, int x, int v) {
  for (int i = x; i <= n; i += i & (-i)) ch[i] += v;
}
inline void update(int x, int v) {
  int k = x / S + 1;
  add(ch[k], b[x], v);
}
inline int query(int l, int r, int v) {
  int L = n, R = 0, ans = 0;
  if (r - l <= 3 * S) {
    int ans = 0;
    for (int i = l; i <= r; i++)
      if (b[i] <= v) ans++;
    return ans;
  }
  for (int i = 1; i <= n / S + 1; i++) {
    int lb = (i - 1) * S, rb = i * S - 1;
    if (l <= lb && rb <= r) {
      L = min(L, lb);
      R = max(R, rb);
      ans += query(ch[i], v);
    }
  }
  for (int i = l; i < L; i++)
    if (b[i] <= v) ans++;
  for (int i = R + 1; i <= r; i++)
    if (b[i] <= v) ans++;
  return ans;
}
int main() {
  int m;
  cin >> 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]);
    b[i] = p[b[i]];
  }
  for (int i = 1; i <= n; i++) update(i, 1);
  for (int i = 1; i <= m; i++) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int L, R, l, r;
      scanf("%d%d%d%d", &L, &R, &l, &r);
      int k1 = query(l, r, R);
      int k2 = query(l, r, L - 1);
      printf("%d\n", k1 - k2);
    } else {
      int x, y;
      scanf("%d%d", &x, &y);
      update(x, -1);
      update(y, -1);
      swap(b[x], b[y]);
      update(x, 1);
      update(y, 1);
    }
  }
  return 0;
}
