#ifndef LOCAL
#pragma GCC optimize("Ofast,unroll-loops")
#endif
#include <bits/stdc++.h>
#ifndef LOCAL
#pragma GCC target("avx2,fma")
#endif

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define all(x) x.begin(), x.end()

template<typename T>
int isz(const T& v) {
  return (int)v.size();
}

template<typename T>
void reuniq(T& v) {
  v.erase(unique(all(v)), v.end());
}

template<typename X, typename Y>
bool remin(X& x, const Y& y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}

template<typename X, typename Y>
bool remax(X& x, const Y& y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}
#define int ll
struct fenwick {
  vector<int> t;
  int n;

  fenwick(int _n) {
    n = _n;
    t.assign(n, 0);
  }

  inline int f(int x) const {
    return x & -x;
  }

  void add(int i, int v) {
    for (++i; i < n; i += f(i)) {
      t[i] += v;
    }
  }

  int get(int l, int r) {
    return get(r) - get(l);
  }

  int get(int r) {
    int ans = 0;
    for (; r > 0; r -= f(r)) {
      ans += t[r];
    } 
    return ans;
  }
};

constexpr int kMaxVal = 6;

void compute(int test_case) {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (auto& i : a) {
    cin >> i;
  }
  vector<fenwick> ft(kMaxVal, n + 10);
  for (int i = 0; i < n; ++i) {
    ft[a[i]].add(i, 1);
  }
  vector<int> cnt(kMaxVal);
  for (int i = 1; i < kMaxVal; ++i) {
    cnt[i] = count(all(a), i);
  }
  vector<int> f_pos(kMaxVal);
  for (int i = n - 1; i >= 0; --i) {
    f_pos[a[i]] = i;
  }
  for (int tt = 0; tt < q; ++tt) {
    int t;
    cin >> t;
    if (t == 2) {
      int u;
      cin >> u;
      --u;
      const int teta = a[u];
      int p = ft[teta].get(0, u + 1);
      int delta = n - cnt[teta];
      vector<pair<int, int>> b;
      int k = 0;
      for (int j = 1; j < kMaxVal; ++j) {
        int cur = ft[j].get(0, u);
        if (j == a[0]) {
          ++k;
          --cur;
        }
        if (j != a[u] && cur > 0) {
          b.emplace_back(cur, j);
        }
      }
      sort(all(b));
      const int m = isz(b);
      for (int j = 0; j + 1 < m; ++j) {
        k += (m - j) * b[j].first;
      }
      if (!b.empty() && b.back().second != a[0]) {
        k += b.back().first - 1;
      }
      int ans = p + k + min(p - 1 - (a[0] == a[u]), delta - k);
      cout << ans << "\n";
    } else {
      int x;
      cin >> x;
      --x;
      ft[a[x]].add(x, -1);
      ft[a[x + 1]].add(x + 1, -1);
      ft[a[x]].add(x + 1, 1);
      ft[a[x + 1]].add(x, 1);
      swap(a[x], a[x + 1]);
    }
  }
}

signed main() {
  #ifdef LOCAL
    freopen("/home/olymp/work/in.txt", "r", stdin);
    // freopen("/home/olymp/work/out.txt", "w", stdout);
  #endif
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;
  // cin >> t;
  for (int tt = 1; tt <= t; ++tt) {
    compute(tt);
  }
  return 0;
}