#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1000005;
const int mod = 998244353;
int k;
struct s {
  int x, y, z;
  int cnt, ans, id;
  int flag;
  void print() {
    cout << x << ' ' << y << ' ' << z << ' ' << cnt << ' ' << id << ' ' << ans
         << ' ' << flag << endl;
  }
};
vector<s> v;
int b[maxn];
void update(int x, int d) {
  if (x == 0) return;
  while (x <= 200000) {
    b[x] += d;
    x += x & (-x);
  }
}
int query(int x) {
  int ret = 0;
  while (x) {
    ret += b[x];
    x -= x & (-x);
  }
  return ret;
}
bool cmp(s x, s y) {
  if (x.x != y.x) {
    return x.x < y.x;
  }
  if (x.y != y.y) {
    return x.y < y.y;
  }
  if (x.z != y.z) {
    return x.z < y.z;
  }
  return x.id < y.id;
}
bool cmp2(s x, s y) {
  if (x.y != y.y) {
    return x.y < y.y;
  }
  if (x.z != y.z) {
    return x.z < y.z;
  }
  return x.id < y.id;
}
void cdq(int l, int r) {
  if (l == r) {
    return;
  }
  int mid = (l + r) >> 1;
  cdq(l, mid);
  cdq(mid + 1, r);
  sort(v.begin() + l, v.begin() + mid + 1, cmp2);
  sort(v.begin() + mid + 1, v.begin() + r + 1, cmp2);
  int ptr = l;
  for (int i = mid + 1; i <= r; i++) {
    while (ptr <= mid && v[ptr].y <= v[i].y) {
      update(v[ptr].z, v[ptr].cnt);
      ptr++;
    }
    v[i].ans += query(v[i].z);
  }
  for (int i = l; i < ptr; i++) {
    update(v[i].z, -v[i].cnt);
  }
}
int x[200005], y[200005];
int p[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  v.clear();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < n + 1; i++) {
    int y;
    cin >> y;
    x[y] = i;
  }
  for (int i = 1; i < n + 1; i++) {
    int x;
    cin >> x;
    y[x] = i;
    p[i] = x;
  }
  for (int i = 1; i < n + 1; i++) {
    v.push_back({x[i], y[i], 1, 1, 0, 0, 0});
  }
  int q = 0;
  for (int i = 1; i < k + 1; i++) {
    int op = 0;
    cin >> op;
    if (op == 1) {
      int x1, x2, y1, y2;
      cin >> x1 >> x2 >> y1 >> y2;
      v.push_back({x2, y2, i, 0, 0, ++q, 1});
      v.push_back({x1 - 1, y1 - 1, i, 0, 0, q, 1});
      v.push_back({x1 - 1, y2, i, 0, 0, q, -1});
      v.push_back({x2, y1 - 1, i, 0, 0, q, -1});
    } else {
      int p1, p2;
      cin >> p1 >> p2;
      int xx = p[p1];
      int yy = p[p2];
      v.push_back({x[xx], y[xx], i, -1, 0, 0, 0});
      v.push_back({x[yy], y[yy], i, -1, 0, 0, 0});
      swap(p[p1], p[p2]);
      swap(y[xx], y[yy]);
      v.push_back({x[xx], y[xx], i, 1, 0, 0, 0});
      v.push_back({x[yy], y[yy], i, 1, 0, 0, 0});
    }
  }
  sort(v.begin(), v.end(), cmp);
  cdq(0, v.size() - 1);
  vector<int> ans(q, 0);
  for (auto &i : v) {
    if (i.id) {
      ans[i.id - 1] += i.ans * i.flag;
    }
  }
  for (auto &i : ans) {
    cout << i << '\n';
  }
}
