#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 13;
int n, m;
int a[N], b[N], pos[N];
vector<int> f[N];
vector<int> vals[N];
void addupd(int x, int y) {
  for (int i = x; i < N; i |= i + 1) vals[i].push_back(y);
}
void addget(int x, int y) {
  if (x < 0 || y < 0) return;
  for (int i = x; i >= 0; i = (i & (i + 1)) - 1) vals[i].push_back(y);
}
void upd(int x, int y, int val) {
  for (int i = x; i < N; i |= i + 1)
    for (int j =
             lower_bound(vals[i].begin(), vals[i].end(), y) - vals[i].begin();
         j < int(f[i].size()); j |= j + 1)
      f[i][j] += val;
}
int get(int x, int y) {
  if (x < 0 || y < 0) return 0;
  int res = 0;
  for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
    for (int j =
             lower_bound(vals[i].begin(), vals[i].end(), y) - vals[i].begin();
         j >= 0; j = (j & (j + 1)) - 1)
      res += f[i][j];
  return res;
}
struct query {
  int t, la, ra, lb, rb;
  query(){};
};
query q[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < int(n); i++) {
    scanf("%d", &a[i]);
    --a[i];
    pos[a[i]] = i;
  }
  for (int i = 0; i < int(n); i++) {
    scanf("%d", &b[i]);
    --b[i];
    b[i] = pos[b[i]];
  }
  for (int i = 0; i < int(m); i++) {
    scanf("%d", &q[i].t);
    if (q[i].t == 1) {
      scanf("%d%d%d%d", &q[i].la, &q[i].ra, &q[i].lb, &q[i].rb);
      --q[i].la, --q[i].ra, --q[i].lb, --q[i].rb;
    } else {
      scanf("%d%d", &q[i].lb, &q[i].rb);
      --q[i].lb, --q[i].rb;
    }
  }
  vector<int> tmp(b, b + n);
  for (int i = 0; i < int(n); i++) addupd(i, b[i]);
  for (int i = 0; i < int(m); i++) {
    if (q[i].t == 1) {
      addget(q[i].rb, q[i].ra);
      addget(q[i].lb - 1, q[i].ra);
      addget(q[i].rb, q[i].la - 1);
      addget(q[i].lb - 1, q[i].la - 1);
    } else {
      addupd(q[i].lb, b[q[i].lb]);
      addupd(q[i].rb, b[q[i].rb]);
      swap(b[q[i].lb], b[q[i].rb]);
      addupd(q[i].lb, b[q[i].lb]);
      addupd(q[i].rb, b[q[i].rb]);
    }
  }
  for (int i = 0; i < int(n); i++) b[i] = tmp[i];
  for (int i = 0; i < int(N); i++) {
    sort(vals[i].begin(), vals[i].end());
    vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
    f[i].resize(vals[i].size(), 0);
  }
  for (int i = 0; i < int(n); i++) upd(i, b[i], 1);
  for (int i = 0; i < int(m); i++) {
    if (q[i].t == 1) {
      int res = 0;
      res += get(q[i].rb, q[i].ra);
      res -= get(q[i].lb - 1, q[i].ra);
      res -= get(q[i].rb, q[i].la - 1);
      res += get(q[i].lb - 1, q[i].la - 1);
      printf("%d\n", res);
    } else {
      upd(q[i].lb, b[q[i].lb], -1);
      upd(q[i].rb, b[q[i].rb], -1);
      swap(b[q[i].lb], b[q[i].rb]);
      upd(q[i].lb, b[q[i].lb], 1);
      upd(q[i].rb, b[q[i].rb], 1);
    }
  }
  return 0;
}
