#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
const int N = 2e5 + 7;
const int S = 450;
int n, m;
int a[N];
int b[N];
int pos[N];
vector<int> s[450];
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    read(b[i]);
    b[i] = pos[b[i]];
    s[(i - 1) / S + 1].push_back(b[i]);
  }
  int nBlock = n / S + (n % S != 0);
  for (int i = 1; i <= nBlock; i++) sort(s[i].begin(), s[i].end());
  while (m--) {
    int type;
    read(type);
    if (type == 1) {
      int res = 0;
      int l1, r1, l2, r2;
      read(l1);
      read(r1);
      read(l2);
      read(r2);
      int blockL = (l2 - 1) / S + 1;
      int blockR = (r2 - 1) / S + 1;
      if (blockL == blockR) {
        for (int i = l2; i <= r2; i++)
          if (b[i] >= l1 && b[i] <= r1) res++;
      } else {
        for (int i = l2; i <= S * blockL; i++)
          if (b[i] >= l1 && b[i] <= r1) res++;
        for (int i = S * (blockR - 1) + 1; i <= r2; i++)
          if (b[i] >= l1 && b[i] <= r1) res++;
        for (int i = blockL + 1; i <= blockR - 1; i++) {
          int x = lower_bound(s[i].begin(), s[i].end(), l1) - s[i].begin();
          int y = upper_bound(s[i].begin(), s[i].end(), r1) - s[i].begin();
          res += y - x;
        }
      }
      writeln(res);
    } else {
      int x, y;
      read(x);
      read(y);
      int blockX = (x - 1) / S + 1;
      int blockY = (y - 1) / S + 1;
      int posX = lower_bound(s[blockX].begin(), s[blockX].end(), b[x]) -
                 s[blockX].begin();
      int posY = lower_bound(s[blockY].begin(), s[blockY].end(), b[y]) -
                 s[blockY].begin();
      s[blockX][posX] = b[y];
      s[blockY][posY] = b[x];
      swap(b[x], b[y]);
      sort(s[blockX].begin(), s[blockX].end());
      sort(s[blockY].begin(), s[blockY].end());
    }
  }
  return 0;
}
