#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
const int MOD = 998244353;
const int inf = 0x3f3f3f3f;
struct Node {
  int op, x, y, val, id;
};
Node a[N << 3], tmp[N << 3], vec[N << 3];
int tree[N], x[N], y[N], p1[N], p2[N], ans[N];
inline int lowbit(int x) { return x & -x; }
inline int query(int x) {
  int ret = 0;
  for (; x > 0; x -= lowbit(x)) {
    ret += tree[x];
  }
  return ret;
}
inline void update(int x, int val) {
  for (; x < N; x += lowbit(x)) {
    tree[x] += val;
  }
}
void cdq(int l, int r) {
  if (l == r) {
    return;
  }
  int m = (l + r) >> 1;
  cdq(l, m);
  cdq(m + 1, r);
  int t1 = l, t2 = m + 1, tot = l, pvec = 0;
  while (t1 <= m || t2 <= r) {
    if (t2 > r || t1 <= m && a[t1].x <= a[t2].x) {
      tmp[tot++] = a[t1];
      if (a[t1].op == 1) {
        update(a[t1].y, a[t1].val);
        vec[pvec++] = a[t1];
      }
      t1++;
    } else {
      tmp[tot++] = a[t2];
      if (a[t2].op == 2) {
        ans[a[t2].id] += a[t2].val * query(a[t2].y);
      }
      t2++;
    }
  }
  for (int i = 0; i < pvec; i++) {
    update(vec[i].y, -vec[i].val);
  }
  for (int i = l; i <= r; i++) {
    a[i] = tmp[i];
  }
}
int main() {
  int n, m, atot = 0, anstot = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p1[i]);
    x[p1[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p2[i]);
    y[p2[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    a[++atot] = Node{1, x[i], y[i], 1, 0};
  }
  while (m--) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int x1, x2, y1, y2;
      scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
      a[++atot] = Node{2, x2, y2, 1, ++anstot};
      a[++atot] = Node{2, x2, y1 - 1, -1, anstot};
      a[++atot] = Node{2, x1 - 1, y2, -1, anstot};
      a[++atot] = Node{2, x1 - 1, y1 - 1, 1, anstot};
    } else {
      int x1, y1;
      scanf("%d%d", &x1, &y1);
      a[++atot] = Node{1, x[p2[x1]], y[p2[x1]], -1, 0};
      a[++atot] = Node{1, x[p2[y1]], y[p2[y1]], -1, 0};
      swap(p2[x1], p2[y1]);
      y[p2[x1]] = x1;
      y[p2[y1]] = y1;
      a[++atot] = Node{1, x[p2[x1]], y[p2[x1]], 1, 0};
      a[++atot] = Node{1, x[p2[y1]], y[p2[y1]], 1, 0};
    }
  }
  cdq(1, atot);
  for (int i = 1; i <= anstot; i++) {
    printf("%d\n", ans[i]);
  }
}
