#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o& x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(10);
}
template <class o>
void cmax(o& x, o y) {
  if (x < y) x = y;
}
void cmax(int& x, int y) { x = x - y >> 31 ? y : x; }
template <class o>
void cmin(o& x, o y) {
  if (x > y) x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
int n, m, a[N], p[N], b[N];
struct BIT {
  int c[N];
  void clear() { memset(c + 1, 0, n * sizeof(int)); }
  void clear(int x) {
    for (; x <= n; x += x & -x) c[x] = 0;
  }
  int ask(int x) {
    int y = 0;
    for (; x; x -= x & -x) y += c[x];
    return y;
  }
  int ask(int l, int r) { return ask(r) - ask(l - 1); }
  void add(int x, int y) {
    for (; x <= n; x += x & -x) c[x] += y;
  }
} tr;
struct Q {
  int l, r, y, v, id;
  Q(int _l, int _r, int _y, int _v, int _id = 0)
      : l(_l), r(_r), y(_y), v(_v), id(_id){};
  Q(){};
  bool operator<(Q b) const { return y < b.y; }
} q[N * 5], t[N * 5];
int tot, ans[N], id;
void cdq(int l, int r) {
  if (r - l < 100) {
    int j = 0;
    for (auto i = l; i <= r; i++) {
      if (!q[i].id)
        t[++j] = q[i];
      else
        for (auto k = 1; k <= j; k++) {
          if (q[i].l <= t[k].l && t[k].l <= q[i].r && t[k].y <= q[i].y)
            ans[q[i].id] += q[i].v * t[k].v;
        }
    }
    sort(q + l, q + r + 1);
    return;
  }
  int mid = (l + r) / 2;
  cdq(l, mid);
  cdq(mid + 1, r);
  Q* p = t;
  for (int i = l, j = mid + 1; i <= mid || j <= r;) {
    if (j > r || (i <= mid && q[i].y <= q[j].y)) {
      if (!q[i].id) tr.add(q[i].l, q[i].v);
      *p++ = q[i++];
    } else {
      if (q[j].id) ans[q[j].id] += q[j].v * tr.ask(q[j].l, q[j].r);
      *p++ = q[j++];
    }
  }
  if (r - l > 2333)
    tr.clear();
  else
    for (auto i = l; i <= mid; i++)
      if (!q[i].id) tr.clear(q[i].l);
  for (auto i = r; i >= l; i--) q[i] = *--p;
}
void solve() {
  qr(n);
  qr(m);
  for (auto i = 1; i <= n; i++) qr(a[i]), p[a[i]] = i;
  for (auto i = 1; i <= n; i++)
    qr(b[i]), b[i] = p[b[i]], q[++tot] = Q(i, i, b[i], 1);
  int op, x, y, l, r;
  while (m--) {
    qr(op);
    qr(x);
    qr(y);
    if (op == 1) {
      qr(l);
      qr(r);
      ++id;
      q[++tot] = Q(l, r, y, 1, id);
      q[++tot] = Q(l, r, x - 1, -1, id);
    } else {
      q[++tot] = Q(x, x, b[x], -1);
      q[++tot] = Q(y, y, b[y], -1);
      swap(b[x], b[y]);
      q[++tot] = Q(x, x, b[x], 1);
      q[++tot] = Q(y, y, b[y], 1);
    }
  }
  cdq(1, tot);
  for (auto i = 1; i <= id; i++) pr2(ans[i]);
}
int main() {
  int T = 1;
  while (T--) solve();
  return 0;
}
