#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y;
  long long z;
  bool operator<(const P &a) const {
    if (x != a.x) return x < a.x;
    return y < a.y;
  }
};
long long x, y, z, mod = 998244353;
vector<P> v, v1;
int i, n, m, k, a, d, b, c;
int e;
int o[211111];
int bu[311111], re[211111];
int l[311111];
int j[6];
queue<int> q;
queue<int> q1;
P u[311111];
char r[1], r1[3];
bool as(P a, P b) { return a.z < b.z; }
int f(int n, int m, int c) {
  if (bu[n] > c) return 0;
  int a = n;
  for (; n < m;) {
    int h = (n + m) / 2 + 1;
    if (bu[h] <= c)
      n = h;
    else
      m = h - 1;
  }
  return n - a + 1;
}
int main() {
  scanf("%d %d", &a, &c);
  b = sqrt(a);
  if (b < sqrt(a)) b++;
  for (int t = 1; t <= a; re[o[t]] = t, t++) scanf("%d", &o[t]);
  for (int t = 1; t <= a; t++) o[t] = re[o[t]];
  for (int t = 1; t <= a; t++) {
    scanf("%d", &n);
    l[t] = re[n];
    bu[t] = l[t];
  }
  for (int t = 1; t <= a; t += b) sort(bu + t, bu + t + b);
  for (int t = 1; t <= c; t++) {
    scanf("%d %d %d", &e, &n, &m);
    if (e % 2) {
      int c, d;
      scanf("%d %d", &c, &d);
      int p = 0;
      for (int t = c; t <= d;) {
        if (t % b == 1 && t + b - 1 <= d)
          p += f(t, t + b - 1, m) - f(t, t + b - 1, n - 1), t += b;
        else {
          if (n <= l[t] && l[t] <= m) p++;
          t++;
        }
      }
      printf("%d\n", p);
    } else {
      swap(l[n], l[m]);
      for (int t = (n - 1) / b * b + 1; t <= (n - 1) / b * b + b; t++)
        bu[t] = l[t];
      for (int t = (m - 1) / b * b + 1; t <= (m - 1) / b * b + b; t++)
        bu[t] = l[t];
      sort(bu + (n - 1) / b * b + 1, bu + (n - 1) / b * b + 1 + b);
      sort(bu + (m - 1) / b * b + 1, bu + (m - 1) / b * b + 1 + b);
    }
  }
}
