#include <bits/stdc++.h>
using namespace std;
int n, m, a[200003], b[200003], pos[200003], tmp[200003];
vector<int> vals[200003], t[200003];
struct Data {
  int type, x, y, u, v;
  Data(int _type = 0, int _x = 0, int _y = 0, int _u = 0, int _v = 0)
      : type(_type), x(_x), y(_y), u(_u), v(_v) {}
} Query[200003];
void fakeUpd(int x, int y) {
  for (x; x < 200003; x += x & (-x)) vals[x].push_back(y);
}
void Upd(int x, int y, int val) {
  for (; x < 200003; x += x & (-x))
    for (int yy =
             upper_bound(vals[x].begin(), vals[x].end(), y) - vals[x].begin();
         yy <= ((int)vals[x].size()); yy += yy & (-yy))
      t[x][yy] += val;
}
int Get(int x, int y) {
  int res = 0;
  for (; x; x -= x & (-x))
    for (int yy =
             upper_bound(vals[x].begin(), vals[x].end(), y) - vals[x].begin();
         yy; yy -= yy & (-yy))
      res += t[x][yy];
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (int i = (1); i <= (n); ++i) {
    cin >> b[i];
    fakeUpd(i, pos[b[i]]);
    tmp[i] = pos[b[i]];
  }
  for (int i = (1); i <= (m); ++i) {
    int type, x, y, u, v;
    cin >> type >> x >> y;
    if (type == 1) cin >> u >> v;
    Query[i] = Data(type, x, y, u, v);
    if (type == 2) {
      swap(tmp[x], tmp[y]);
      fakeUpd(x, tmp[x]);
      fakeUpd(y, tmp[y]);
    }
  }
  for (int i = (0); i <= (200003 - 1); ++i) {
    sort(vals[i].begin(), vals[i].end());
    vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
    t[i].resize(((int)vals[i].size()) + 1, 0);
  }
  for (int i = (1); i <= (n); ++i) Upd(i, pos[b[i]], 1);
  for (int i = (1); i <= (m); ++i) {
    if (Query[i].type == 1) {
      int ans = 0;
      ans += Get(Query[i].v, Query[i].y);
      ans -= Get(Query[i].u - 1, Query[i].y);
      ans -= Get(Query[i].v, Query[i].x - 1);
      ans += Get(Query[i].u - 1, Query[i].x - 1);
      cout << ans << '\n';
    } else {
      int x = Query[i].x, y = Query[i].y;
      Upd(x, pos[b[x]], -1);
      Upd(y, pos[b[y]], -1);
      swap(b[x], b[y]);
      Upd(x, pos[b[x]], 1);
      Upd(y, pos[b[y]], 1);
    }
  }
  return 0;
}
