#include <bits/stdc++.h>
using namespace std;
namespace Fread {
const int MAXN = 1 << 20;
char buf[MAXN], *S, *T;
inline char getchar() {
  if (S == T) {
    T = (S = buf) + fread(buf, 1, MAXN, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
}  // namespace Fread
namespace Fwrite {
const int MAXN = 1 << 20;
char buf[MAXN], *S = buf, *T = buf + MAXN;
inline void flush() {
  fwrite(buf, 1, S - buf, stdout);
  S = buf;
}
inline void putchar(char c) {
  *S++ = c;
  if (S == T) flush();
}
struct _ {
  ~_() { flush(); }
} __;
}  // namespace Fwrite
template <typename T>
inline void read(T& x) {
  x = 0;
  int f = 1;
  char c = Fread::getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = Fread::getchar();
  }
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = Fread::getchar();
  x *= f;
}
template <typename T>
inline void write(T x, bool _enter = 0, bool _space = 0) {
  if (!x)
    Fwrite::putchar('0');
  else {
    if (x < 0) Fwrite::putchar('-'), x = -x;
    static char dig[41];
    int top = 0;
    while (x) dig[++top] = (x % 10) ^ '0', x /= 10;
    while (top) Fwrite::putchar(dig[top--]);
  }
  if (_enter) Fwrite::putchar('\n');
  if (_space) Fwrite::putchar(' ');
}
namespace Fastio {
struct reader {
  template <typename T>
  reader& operator>>(T& x) {
    ::read(x);
    return *this;
  }
  reader& operator>>(char& c) {
    c = Fread::getchar();
    while (c == '\n' || c == ' ') c = Fread::getchar();
    return *this;
  }
  reader& operator>>(char* str) {
    int len = 0;
    char c = Fread::getchar();
    while (c == '\n' || c == ' ') c = Fread::getchar();
    while (c != '\n' && c != ' ') str[len++] = c, c = Fread::getchar();
    str[len] = '\0';
    return *this;
  }
  reader& operator>>(std::string& s) {
    std::cin >> s;
    return *this;
  }
} cin;
const char endl = '\n';
struct writer {
  template <typename T>
  writer& operator<<(T x) {
    ::write(x, 0, 0);
    return *this;
  }
  writer& operator<<(char c) {
    Fwrite::putchar(c);
    return *this;
  }
  writer& operator<<(const char* str) {
    int cur = 0;
    while (str[cur]) Fwrite::putchar(str[cur++]);
    return *this;
  }
} cout;
}  // namespace Fastio
const int MAXN = 2e5;
int n, m, cntq, a[MAXN + 5], posa[MAXN + 5], b[MAXN + 5], op[MAXN + 5],
    ans[MAXN + 5];
struct Query {
  int a, b, op, id, xs;
  Query() {}
  Query(int _a, int _b, int _op, int _id, int _xs) {
    a = _a;
    b = _b;
    op = _op;
    id = _id;
    xs = _xs;
  }
} q[MAXN * 5 + 5], tmp[MAXN * 5 + 5];
struct FenwickTree {
  int c[MAXN + 5];
  void point_add(int p, int v) {
    for (; p <= n; p += (p & (-p))) c[p] += v;
  }
  int query_prefixsum(int p) {
    int res = 0;
    for (; p; p -= (p & (-p))) res += c[p];
    return res;
  }
  FenwickTree() {}
} T;
void cdq(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  cdq(l, mid);
  cdq(mid + 1, r);
  int i = l, j = mid + 1, t = 0;
  while (i <= mid && j <= r) {
    if (q[i].a <= q[j].a) {
      if (q[i].op == 2) {
        T.point_add(q[i].b, q[i].xs);
      }
      tmp[++t] = q[i];
      ++i;
    } else {
      if (q[j].op == 1) {
        ans[q[j].id] += q[j].xs * T.query_prefixsum(q[j].b);
      }
      tmp[++t] = q[j];
      ++j;
    }
  }
  if (i <= mid) {
    for (int p = l; p < i; ++p) {
      if (q[p].op == 2) {
        T.point_add(q[p].b, -q[p].xs);
      }
    }
    while (i <= mid) {
      tmp[++t] = q[i];
      ++i;
    }
  } else {
    while (j <= r) {
      if (q[j].op == 1) {
        ans[q[j].id] += q[j].xs * T.query_prefixsum(q[j].b);
      }
      tmp[++t] = q[j];
      ++j;
    }
    for (int p = l; p < i; ++p) {
      if (q[p].op == 2) {
        T.point_add(q[p].b, -q[p].xs);
      }
    }
  }
  assert(t == r - l + 1);
  for (int i = l, t = 0; i <= r; ++i) {
    q[i] = tmp[++t];
  }
}
int main() {
  Fastio::cin >> n >> m;
  for (int i = 1; i <= n; ++i) Fastio::cin >> a[i], posa[a[i]] = i;
  for (int i = 1; i <= n; ++i)
    Fastio::cin >> b[i], b[i] = posa[b[i]], q[++cntq] = Query(b[i], i, 2, 0, 1);
  for (int i = 1; i <= m; ++i) {
    Fastio::cin >> op[i];
    if (op[i] == 1) {
      int la, ra, lb, rb;
      Fastio::cin >> la >> ra >> lb >> rb;
      q[++cntq] = Query(ra, rb, 1, i, 1);
      if (la != 1) q[++cntq] = Query(la - 1, rb, 1, i, -1);
      if (lb != 1) q[++cntq] = Query(ra, lb - 1, 1, i, -1);
      if (la != 1 && lb != 1) q[++cntq] = Query(la - 1, lb - 1, 1, i, 1);
    } else {
      int x, y;
      Fastio::cin >> x >> y;
      q[++cntq] = Query(b[x], x, 2, 0, -1);
      q[++cntq] = Query(b[y], y, 2, 0, -1);
      swap(b[x], b[y]);
      q[++cntq] = Query(b[x], x, 2, 0, 1);
      q[++cntq] = Query(b[y], y, 2, 0, 1);
    }
  }
  cdq(1, cntq);
  for (int i = 1; i <= m; ++i)
    if (op[i] == 1) Fastio::cout << ans[i] << Fastio::endl;
  return 0;
}
