#include <bits/stdc++.h>
using namespace std;
class BIT {
 public:
  BIT(const int& rn) : n(rn) {
    smallElems.resize(n + 1, vector<int>());
    bigElems.resize(n + 1, unordered_map<int, int>());
    whetherSmall.resize(n + 1, false);
    for (int i = 1; i <= n; ++i) {
      int j = i, count = 1;
      while (j % 2 == 0) {
        count <<= 1;
        j /= 2;
      }
      if (count <= 512) {
        smallElems[i].reserve(count + 1);
        whetherSmall[i] = true;
      } else {
        whetherSmall[i] = false;
      }
    }
  }
  inline void insertTwoDimen(int pos, int x) {
    if (whetherSmall[pos]) {
      smallElems[pos].push_back(x);
    } else {
      while (x <= n) {
        auto findIter = bigElems[pos].find(x);
        if (findIter == bigElems[pos].end()) {
          bigElems[pos].insert(make_pair(x, 1));
        } else {
          ++findIter->second;
        }
        x += x & -x;
      }
    }
  }
  inline void add(int pos, int limit, const int& x) {
    while (pos <= limit) {
      insertTwoDimen(pos, x);
      pos += pos & -pos;
    }
  }
  inline void initAdd(int pos, const int& x) { add(pos, n, x); }
  inline int getBITNumber(unordered_map<int, int>& umap, int x) {
    int ans = 0;
    while (x > 0) {
      auto findIter = umap.find(x);
      if (findIter != umap.end()) {
        ans += findIter->second;
      }
      x -= x & -x;
    }
    return ans;
  }
  inline int getTwoDimenNumber(const int& pos, const int& leftborder,
                               const int& rightborder) {
    int ans = 0;
    if (whetherSmall[pos]) {
      for (auto& e : smallElems[pos]) {
        if (e >= leftborder && e <= rightborder) {
          ++ans;
        }
      }
      return ans;
    } else {
      return getBITNumber(bigElems[pos], rightborder) -
             getBITNumber(bigElems[pos], leftborder - 1);
    }
  }
  inline int getSum(int pos, int leftborder, int rightborder) {
    int ans = 0;
    while (pos > 0) {
      ans += getTwoDimenNumber(pos, leftborder, rightborder);
      pos -= pos & -pos;
    }
    return ans;
  }
  inline void deleteTwoDimen(int pos, int x) {
    if (whetherSmall[pos]) {
      smallElems[pos].erase(
          find(smallElems[pos].begin(), smallElems[pos].end(), x));
    } else {
      while (x <= n) {
        auto findIter = bigElems[pos].find(x);
        if (findIter != bigElems[pos].end()) {
          --findIter->second;
        }
        x += x & -x;
      }
    }
  }
  inline void deleteNumber(int pos, int limit, int x) {
    while (pos <= limit) {
      deleteTwoDimen(pos, x);
      pos += pos & -pos;
    }
  }
  inline int getLimit(int x, int y) {
    while (x != y) {
      if (x < y)
        x += x & -x;
      else
        y += y & -y;
    }
    return min(n, y - 1);
  }
  inline void swap(int x, int y, int ax, int ay) {
    int limit = getLimit(x, y);
    deleteNumber(x, limit, ax);
    deleteNumber(y, limit, ay);
    add(x, limit, ay);
    add(y, limit, ax);
  }
  inline int getAnswer(int& la, int& ra, int& lb, int& rb) {
    return getSum(rb, la, ra) - getSum(lb - 1, la, ra);
  }

 private:
  vector<vector<int> > smallElems;
  vector<unordered_map<int, int> > bigElems;
  vector<bool> whetherSmall;
  int n;
};
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  try {
    vector<int> a(n + 1, 0);
    vector<int> b(n + 1, 0);
    vector<int> posb(n + 1, 0);
    vector<int> posa(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      posa[a[i]] = i;
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &b[i]);
      posb[b[i]] = i;
    }
    int type, la, ra, lb, rb;
    int x, y;
    int ans = 0, temp;
    BIT bit(n + 1);
    for (int i = 1; i <= n; ++i) {
      bit.initAdd(posb[a[i]], i);
    }
    for (int i = 0; i < m; ++i) {
      scanf("%d", &type);
      switch (type) {
        case 1:
          scanf("%d %d %d %d", &la, &ra, &lb, &rb);
          printf("%d\n", bit.getAnswer(la, ra, lb, rb));
          break;
        case 2:
          scanf("%d %d", &x, &y);
          bit.swap(x, y, posa[b[x]], posa[b[y]]);
          temp = b[x];
          b[x] = b[y];
          b[y] = temp;
          break;
      }
    }
  } catch (const exception& e) {
    printf("%s\n", e.what());
  }
  return 0;
}
