#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MAXN = 2e5 + 10;
const int MOD = 998244353;
struct item {
  int key, prior;
  item* l;
  item* r;
  int cnt;
  item() {}
  item(int key, int prior) : key(key), prior(prior), l(NULL), r(NULL) {}
};
typedef item* pitem;
int cnt(pitem t) { return t ? t->cnt : 0; }
void upd_cnt(pitem t) {
  if (t) t->cnt = 1 + cnt(t->l) + cnt(t->r);
}
void split(pitem t, int key, pitem& l, pitem& r) {
  if (!t)
    l = r = NULL;
  else if (key < t->key)
    split(t->l, key, l, t->l), r = t;
  else
    split(t->r, key, t->r, r), l = t;
  upd_cnt(t);
}
void insert(pitem& t, pitem it) {
  if (!t)
    t = it;
  else if (it->prior > t->prior)
    split(t, it->key, it->l, it->r), t = it;
  else
    insert(it->key < t->key ? t->l : t->r, it);
  upd_cnt(t);
}
void merge(pitem& t, pitem l, pitem r) {
  if (!l || !r)
    t = l ? l : r;
  else if (l->prior > r->prior)
    merge(l->r, l->r, r), t = l;
  else
    merge(r->l, l, r->l), t = r;
  upd_cnt(t);
}
void erase(pitem& t, int key) {
  if (t->key == key)
    merge(t, t->l, t->r);
  else
    erase(key < t->key ? t->l : t->r, key);
  upd_cnt(t);
}
pitem unite(pitem l, pitem r) {
  if (!l || !r) return l ? l : r;
  if (l->prior < r->prior) swap(l, r);
  pitem lt, rt;
  split(r, l->key, lt, rt);
  l->l = unite(l->l, lt);
  l->r = unite(l->r, rt);
  return l;
}
pitem BIT[MAXN];
int Rand() { return (rand() << 15) ^ rand(); }
void upd(int pos, int v, int what) {
  for (int i = pos + 1; i < MAXN; i += i & -i) {
    if (what) {
      insert(BIT[i], new item(v, Rand()));
    } else {
      erase(BIT[i], v);
    }
  }
}
int get(int pos, int lft, int rgt) {
  int ans = 0;
  for (int i = pos + 1; i > 0; i -= i & -i) {
    pitem small, big, ok;
    split(BIT[i], lft - 1, small, big);
    split(big, rgt, ok, big);
    ans += cnt(ok);
    merge(BIT[i], small, ok);
    merge(BIT[i], BIT[i], big);
  }
  return ans;
}
int A[MAXN];
int B[MAXN];
int pos_in_A[MAXN];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
    pos_in_A[A[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", B + i);
    upd(i, pos_in_A[B[i]], 1);
  }
  for (int i = 0; i < m; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int LA, RA, LB, RB;
      scanf("%d %d %d %d", &LA, &RA, &LB, &RB);
      LA--, RA--, LB--, RB--;
      int ans = 0;
      ans += get(RB, LA, RA);
      ans -= get(LB - 1, LA, RA);
      printf("%d\n", ans);
    } else {
      int x, y;
      scanf("%d %d", &x, &y);
      x--, y--;
      swap(B[x], B[y]);
      upd(y, pos_in_A[B[y]], 1);
      upd(y, pos_in_A[B[x]], 0);
      upd(x, pos_in_A[B[x]], 1);
      upd(x, pos_in_A[B[y]], 0);
    }
  }
  return 0;
}
