#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int a[N], b[N], pos[N], c[N], n, m;
int type[N];
pair<pair<int, int>, pair<int, int> > zpr[N];
vector<int> vec[N * 4];
vector<int> fnw[N * 4];
void psh(int v, int l, int r, int pos, int x) {
  if (l == r) {
    vec[v].push_back(x);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    psh(v + v, l, mid, pos, x);
  else
    psh(v + v + 1, mid + 1, r, pos, x);
}
void build(int v, int l, int r) {
  if (l == r) {
    sort(vec[v].begin(), vec[v].end());
    vec[v].erase(unique(vec[v].begin(), vec[v].end()), vec[v].end());
    fnw[v].resize(vec[v].size(), 0);
    return;
  }
  int mid = (l + r) >> 1;
  build(v + v, l, mid);
  build(v + v + 1, mid + 1, r);
  vec[v].resize(vec[v + v].size() + vec[v + v + 1].size());
  merge(vec[v + v].begin(), vec[v + v].end(), vec[v + v + 1].begin(),
        vec[v + v + 1].end(), vec[v].begin());
  vec[v].erase(unique(vec[v].begin(), vec[v].end()), vec[v].end());
  fnw[v].resize(vec[v].size(), 0);
}
void upd(int v, int pos, int x) {
  for (int i = pos; i < fnw[v].size(); i = (i | (i + 1))) {
    fnw[v][i] += x;
  }
}
int get(int v, int pos) {
  int ans = 0;
  for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) {
    ans += fnw[v][i];
  }
  return ans;
}
void update(int v, int l, int r, int pos, int x, int y) {
  int posx = lower_bound(vec[v].begin(), vec[v].end(), x) - vec[v].begin();
  upd(v, posx, y);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(v + v, l, mid, pos, x, y);
  else
    update(v + v + 1, mid + 1, r, pos, x, y);
}
int microget(int v, int l, int r) {
  int pos1 = upper_bound(vec[v].begin(), vec[v].end(), r) - vec[v].begin() - 1;
  if (pos1 == -1 || vec[v][pos1] < l) return 0;
  int pos2 =
      upper_bound(vec[v].begin(), vec[v].end(), l - 1) - vec[v].begin() - 1;
  if (pos2 == -1) return get(v, pos1);
  return get(v, pos1) - get(v, pos2);
}
int get(int v, int l, int r, int tl, int tr, int x, int y) {
  if (l > r || tl > r || l > tr || tl > tr) return 0;
  if (tl <= l && r <= tr) {
    return microget(v, x, y);
  }
  int mid = (l + r) >> 1;
  return get(v + v, l, mid, tl, tr, x, y) +
         get(v + v + 1, mid + 1, r, tl, tr, x, y);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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];
    c[i] = b[i];
    psh(1, 1, n, pos[b[i]], i);
  }
  for (int i = 1; i <= m; i++) {
    cin >> type[i];
    cin >> zpr[i].first.first >> zpr[i].first.second;
    if (type[i] == 1) {
      cin >> zpr[i].second.first >> zpr[i].second.second;
    } else {
      int x = zpr[i].first.first, y = zpr[i].first.second;
      swap(c[x], c[y]);
      psh(1, 1, n, pos[c[x]], x);
      psh(1, 1, n, pos[c[y]], y);
    }
  }
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    c[pos[b[i]]] = i;
    update(1, 1, n, pos[b[i]], i, 1);
  }
  for (int i = 1; i <= m; i++) {
    if (type[i] == 1) {
      cout << get(1, 1, n, zpr[i].first.first, zpr[i].first.second,
                  zpr[i].second.first, zpr[i].second.second)
           << "\n";
      continue;
    }
    int x = zpr[i].first.first, y = zpr[i].first.second;
    update(1, 1, n, pos[b[x]], c[pos[b[x]]], -1);
    update(1, 1, n, pos[b[y]], c[pos[b[y]]], -1);
    swap(b[x], b[y]);
    c[pos[b[x]]] = x;
    c[pos[b[y]]] = y;
    update(1, 1, n, pos[b[x]], c[pos[b[x]]], 1);
    update(1, 1, n, pos[b[y]], c[pos[b[y]]], 1);
  }
}
