#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 1000000007;
const double pi = 3.1415926535897932, eps = 1e-6;
void chmax(int &x, int y) {
  if (x < y) x = y;
}
void chmin(int &x, int y) {
  if (x > y) x = y;
}
int n, m;
const int B = 888;
int a[200005], b[200005], pos[200005], numB, lstsiz, uup[200005];
pair<int, int> v[333][1111];
int getnmore_big(int l, int r, int y) {
  int res = 0;
  for (int(i) = (l); (i) <= (r); (i)++) {
    int up = uup[i];
    res += lower_bound(v[i] + 1, v[i] + 1 + up, make_pair(y, inf)) - v[i] - 1;
  }
  return res;
}
int getnmore(int l, int r, int y) {
  int bl = (l - 1) / B;
  int br = (r - 1) / B;
  int upl = uup[bl];
  int upr = uup[br];
  int res = 0;
  if (bl == br) {
    for (int(i) = (1); (i) <= (upl); (i)++)
      if (v[bl][i].second >= l && v[bl][i].second <= r && v[bl][i].first <= y)
        res++;
    return res;
  } else {
    for (int(i) = (1); (i) <= (upl); (i)++)
      if (v[bl][i].second >= l && v[bl][i].first <= y) res++;
    for (int(i) = (1); (i) <= (upr); (i)++)
      if (v[br][i].second <= r && v[br][i].first <= y) res++;
    res += getnmore_big(bl + 1, br - 1, y);
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = (1); (i) <= (n); (i)++) scanf("%d", a + i), pos[a[i]] = i;
  for (int(i) = (1); (i) <= (n); (i)++) scanf("%d", b + i);
  for (int(i) = (1); (i) <= (n); (i)++) b[i] = pos[b[i]];
  for (int(i) = (1); (i) <= (n); (i)++) {
    int pos = (i - 1) / B;
    v[pos][i - pos * B] = make_pair(b[i], i);
    uup[pos]++;
  }
  for (int(i) = (1); (i) <= (n); (i)++) sort(v[i] + 1, v[i] + uup[i] + 1);
  numB = (n - 1) / B;
  lstsiz = n - (numB - 1) * B;
  for (int(i) = (1); (i) <= (m); (i)++) {
    int typ;
    scanf("%d", &typ);
    if (typ == 1) {
      int la, lb, ra, rb;
      scanf("%d%d%d%d", &la, &lb, &ra, &rb);
      printf("%d\n", getnmore(ra, rb, lb) - getnmore(ra, rb, la - 1));
    } else {
      int x, y;
      scanf("%d%d", &x, &y);
      int atx = (x - 1) / B, aty = (y - 1) / B;
      int posx = 0, posy = 0;
      for (int(i) = (1); (i) <= (B); (i)++)
        if (v[atx][i].second == x) posx = i;
      for (int(i) = (1); (i) <= (B); (i)++)
        if (v[aty][i].second == y) posy = i;
      swap(v[atx][posx], v[aty][posy]);
      v[atx][posx].second = x;
      v[aty][posy].second = y;
      sort(v[atx] + 1, v[atx] + uup[atx] + 1);
      sort(v[aty] + 1, v[aty] + uup[aty] + 1);
    }
  }
  return 0;
}
