#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 6;
struct BIT {
  int a[MAXN];
  void update(int idx, int val) {
    idx++;
    for (; idx < MAXN; idx += idx & (-idx)) {
      a[idx] += val;
    }
  }
  int query(int idx) {
    idx++;
    int ret = 0;
    for (; idx > 0; idx -= idx & (-idx)) {
      ret += a[idx];
    }
    return ret;
  }
} t;
struct point {
  int x, y, z, ind, val, k;
  bool operator<(point const& other) const {
    if (x != other.x) return x < other.x;
    if (y != other.y) return y < other.y;
    return z < other.z;
  }
};
vector<point> e;
int ans[MAXN], q, n, a[MAXN], b[MAXN], pos[MAXN];
void dq(int l, int r) {
  if (l + 1 == r) return;
  int mid = (l + r) / 2;
  dq(l, mid);
  dq(mid, r);
  vector<point> tmp;
  vector<pair<int, int> > changes;
  int i1 = l, i2 = mid;
  while (i1 < mid && i2 < r) {
    if (make_pair(e[i1].y, e[i1].z) < make_pair(e[i2].y, e[i2].z) ||
        (e[i1].y == e[i2].y && e[i1].z == e[i2].z && e[i1].ind == 0)) {
      t.update(e[i1].z, e[i1].val);
      changes.push_back({e[i1].z, e[i1].val});
      tmp.push_back(e[i1]);
      i1++;
    } else {
      ans[e[i2].ind] += t.query(e[i2].z) * e[i2].k;
      tmp.push_back(e[i2]);
      i2++;
    }
  }
  while (i1 < mid) {
    tmp.push_back(e[i1]);
    i1++;
  }
  while (i2 < r) {
    ans[e[i2].ind] += t.query(e[i2].z) * e[i2].k;
    tmp.push_back(e[i2]);
    i2++;
  }
  for (int i = 0; i < tmp.size(); i++) {
    e[l + i] = tmp[i];
  }
  for (auto xd : changes) {
    t.update(xd.first, -xd.second);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) {
    e.push_back({0, pos[b[i]], i, 0, 1, 0});
  }
  int cnt = 0;
  for (int i = 1; i <= q; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      int la, ra, lb, rb;
      cin >> la >> ra >> lb >> rb;
      cnt++;
      e.push_back({i, ra, rb, cnt, 0, 1});
      e.push_back({i, ra, lb - 1, cnt, 0, -1});
      e.push_back({i, la - 1, rb, cnt, 0, -1});
      e.push_back({i, la - 1, lb - 1, cnt, 0, 1});
    } else {
      int x, y;
      cin >> x >> y;
      e.push_back({i, pos[b[x]], x, 0, -1, 0});
      e.push_back({i, pos[b[y]], y, 0, -1, 0});
      swap(b[x], b[y]);
      e.push_back({i, pos[b[x]], x, 0, 1, 0});
      e.push_back({i, pos[b[y]], y, 0, 1, 0});
    }
  }
  sort(e.begin(), e.end());
  dq(0, e.size());
  for (int i = 1; i <= cnt; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
