#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 
#define X first
#define Y second 
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a) 
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f3f;
ll dp[N][3], pos[N][3];
pii a[N]; 
map<int, int> num, l, r;

int main()
{
	IOS;
	int _;
	// cin>>_;
	_ = 1;
	while(_--)
	{
		ll n;
		cin>>n;
		rep1(i, n)
		{
			cin>>a[i].X>>a[i].Y;
			num[a[i].Y] = 1;
		}
		sort(a+1, a+1+n);
		rep1(i, n)
		{
			if(!l[a[i].Y])
				l[a[i].Y] = a[i].X;
			r[a[i].Y] = a[i].X;
		}
		int cnt = 1;
		for(auto it : num)
		{
			int x = it.X;
			// cout<<x<<' '<<l[x]<<' '<<r[x]<<nl;
			if(l[x] == r[x])
			{
				dp[cnt][0] = dp[cnt][1] = min(dp[cnt-1][0]+abs(pos[cnt-1][0]-l[x]), dp[cnt-1][1]+abs(pos[cnt-1][1]-l[x]));
				pos[cnt][0] = pos[cnt][1] = l[x];
			}
			else
			{
				dp[cnt][0] = min(dp[cnt-1][0]+abs(pos[cnt-1][0]-l[x])+r[x]-l[x], dp[cnt-1][1]+abs(pos[cnt-1][1]-l[x])+r[x]-l[x]);
				dp[cnt][1] = min(dp[cnt-1][0]+abs(pos[cnt-1][0]-r[x])+r[x]-l[x], dp[cnt-1][1]+abs(pos[cnt-1][1]-r[x])+r[x]-l[x]);
				pos[cnt][0] = r[x];
				pos[cnt][1] = l[x];
			}
			cnt++;
		}
		// rep1(i, cnt)
		// 	cout<<dp[i][0]<<' '<<dp[i][1]<<nl;
		dp[cnt][0] = dp[cnt-1][0]+abs(pos[cnt-1][0]);
		dp[cnt][1] = dp[cnt-1][1]+abs(pos[cnt-1][1]);
		cout<<min(dp[cnt][0], dp[cnt][1])<<nl;
		// cout<<lower_bound(dp+1, dp+1+n, inf) - dp - 1<<endl;

	}
	
	
	AC;
} 

#include <bits/stdc++.h>            
using namespace std;       
#define ll long long                               
              
int main(){                 
               
  ios::sync_with_stdio(0); cin.tie(0);  
  
  int n, q;
  cin >> n >> q;
  vector<int> p(n), sz(n, 1);
  vector<map<int,int>> info(n);
  iota(p.begin(), p.end(), 0);
  for (int i = 0; i < n; ++i){
    int x; cin >> x;
    info[i][x]++;
  }
  function<int(int)> get = [&](int x){
    if (x == p[x]) return x;
    return p[x] = get(p[x]);    
  };  
  auto unite = [&](int x, int y){
    x = get(x);
    y = get(y);
    if (x != y){
      if (sz[x] < sz[y]) swap(x, y);
      p[y] = x;
      for (auto it : info[y]){
        info[x][it.first] += it.second;                   
      }  
      sz[x] += sz[y];
    }
  };
  while (q--){
    int ex; cin >> ex;
    if (ex == 1){
      int a, b;
      cin >> a >> b;
      --a, --b;
      unite(a, b);
    }
    else {
      int a, x;
      cin >> a >> x;
      --a;
      cout << info[get(a)][x] << '\n';
    }
  }                                  

  return 0;                                               
 
}
                              

