#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

/*
char BUF[1 << 22], *ST, *EN;
#define gc (ST == EN && (ST = BUF, EN = BUF + fread(BUF, 1, 1 << 21, stdin), ST == EN) ? EOF : *ST++)
*/
#define gc getchar()

template<class T> void read(T &x){
	int f=0; x=0; char ch=gc;
	for(;!isdigit(ch);ch=gc) f|=(ch=='-');
	for(;isdigit(ch);ch=gc) x=x*10+ch-'0';
	if(f) x=-x;
}

const int N=1000005;
int L[N],n,m,res=1e9+99;

int qry(int x,int y){
	printf("? %d %d\n",x,y);
	fflush(stdout);
	int ret;
	read(ret);
	return ret;
}

int main(){
	read(n),read(m);
	L[0]=1;
	rep(i,1,n){
		L[i]=L[i-1];
		int ini=qry(i,L[i]);
		int mn=ini;
		for(int j=L[i]+1;j<=m;j++){
			int now=qry(i,j);
			if(i>=2&&i<=n-1&&now>=ini) break;
			if(now<mn){
				mn=now;
				L[i]=j;
			}
		}
		res=min(res,mn);
	}
	printf("! %d",res);
	fflush(stdout);
	return 0;
}


#include <cstdio>
#include <cmath>
struct box
	{int x,y;}a[1000000],start;
double dist(int u,int v)
{
	return sqrt((a[u].x-a[v].x)*(a[u].x-a[v].x)+(a[u].y-a[v].y)*(a[u].y-a[v].y));
}
int comp(const void *p,const void *q)
{
	return (*(struct box*)p).x - (*(struct box*)q).x;
}
int main()
{
	double ans=-1; int i,n,k;
	//freopen("input.txt","r",stdin);
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i].x);
	scanf("%d%d",&a[n+1].x,&a[n+1].y);
	start = a[k];
	qsort(a+1,n,sizeof(a[0]),comp);
	for(i=1;i<=n;i++)
		if(a[i].x == start.x && a[i].y == start.y) k = i;
	for(i=1;i<=k;i++)
	{
		double tmp = dist(k,n)+dist(n,i)+dist(i,n+1);
		if(i>1) tmp += dist(n+1,i-1)+dist(i-1,1);
		if(ans==-1 || tmp < ans) ans = tmp;
		tmp = dist(k,i)+dist(i,n)+dist(n,n+1);
		if(i>1) tmp += dist(n+1,i-1)+dist(i-1,1);
		if(ans==-1 || tmp < ans) ans = tmp;
	}
	if(k == n+1)
	{
		double tmp = dist(n+1,n)+dist(n,1);
		if(ans==-1 || tmp<ans) ans=tmp;
		tmp = dist(n+1,1)+dist(1,n);
		if(ans==-1 || tmp<ans) ans=tmp;
	}
	printf("%.8f\n",ans);
}


#include<bits/stdc++.h>
using namespace std;

const int maxn = 100009;
double a[maxn], cur, x, y, res;
int n, K;

double dist(double x, double y) {
	return sqrt(x*x + y*y);
}

int main() {
	scanf("%d%d", &n, &K);
	for (int i=1; i<=n; i++) scanf("%lf", &a[i]);
	cur = a[K]; sort(a+1, a+n+1);
	scanf("%lf%lf", &x, &y);
	if (K == n+1) {
		printf("%.20lf\n", min(dist(x-a[1], y), dist(x-a[n], y)) + a[n] - a[1]);
		return 0;
	}
	for (int i=1; i<=n; i++) if (fabs(a[i] - cur) < 1e-6) { K = i; break; }
	res = a[n] - a[K] + dist(a[n]-x, y) + min(dist(x-a[1], y), dist(x-a[K-1], y)) + a[K-1] - a[1];
	res = min(res, a[K] - a[1] + dist(a[1]-x, y) + min(dist(x-a[K+1], y), dist(x-a[n], y)) + a[n] - a[K+1]);
	for (int i=1; i<=n; i++) {
		res = min(res, 	min(fabs(a[K] - a[i]) + fabs(a[i] - a[n]) + dist(a[n]-x, y),
							fabs(a[K] - a[n]) + fabs(a[i] - a[n]) + dist(a[i]-x, y),
						min(dist(a[i-1]-x, y), dist(a[1]-x, y)) + fabs(a[i-1] - a[1]));
		res = min(res, 	min(fabs(a[K] - a[i]) + fabs(a[i] - a[1]) + dist(a[1]-x, y),
							fabs(a[K] - a[1]) + fabs(a[i] - a[1]) + dist(a[i]-x, y),
						min(dist(a[i+1]-x, y), dist(a[n]-x, y)) + fabs(a[i+1] - a[n]));
	}
	printf("%.20lf\n", res);
	return 0;
}


//D
#include <bits/stdc++.h>
using namespace std;

#define N 100011
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

struct dat{
    int x, y, id;
    dat(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id){}
}p[N];

bool cmp(dat a, dat b) {
    return a.x < b.x;
}

double dist(dat a, dat b) {
    return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));
}

int n, k;
double tot[N];

int main(){
    //freopen("d.in", "r", stdin);
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n + 1; i ++) scanf("%d", &p[i].x);
    scanf("%d", &p[n + 1].y);
    for(int i = 1; i <= n + 1; i ++) p[i].id = i;

    sort(p + 1, p + n + 1, cmp);
    for(int i = 1; i <= n + 1; i ++) if(p[i].id == k) {k = i; break;}

    if(n == 1) {printf("%lf\n", dist(p[1], p[2])); return 0;}

    tot[1] = 0;
    for(int i = 2; i < n + 1; i ++) tot[i] = tot[i - 1] + dist(p[i - 1], p[i]);

    if(k == n + 1){
        double ans = min(dist(p[n + 1], p[1]), dist(p[n + 1], p[n])) + tot[n]);
        for(int i = 1; i < n; i ++){
            double r = tot[n] - dist(p[i], p[i + 1]) + dist(p[n + 1], p[1]) + dist(p[n + 1], p[i]) + min(dist(p[n + 1], p[i + 1]), dist(p[n + 1], p[n]));
            ans = min(ans, r);
            r = tot[n] - dist(p[i], p[i + 1]) + dist(p[n + 1], p[n]) + dist(p[n + 1], p[i + 1]) + min(dist(p[n + 1], p[i]), dist(p[n + 1], p[1]));
            ans = min(ans, r);
        }
        printf("%lf\n", ans);
    }

    else if(k == 1 || k == n){
        double ans = dist(p[n + 1 - k], p[n + 1]) + tot[n];
        for(int i = 1; i < n; i ++) ans = min(ans, tot[n] - dist(p[i], p[i + 1]) + dist(p[n + 1], p[i]) + dist(p[n + 1], p[i + 1]));
        printf("%lf\n", ans);
    }

    else {
        double ans = dist(p[n + 1], p[k]) + tot[n] + min(dist(p[n + 1], p[1]), dist(p[n + 1], p[n]));
        ans = min(ans, tot[n] - dist(p[k], p[k + 1]) + dist(p[n + 1], p[1]) + min(dist(p[k + 1], p[n + 1]), dist(p[n], p[n + 1])));
        ans = min(ans, tot[n] - dist(p[k], p[k - 1]) + dist(p[n + 1], p[n]) + min(dist(p[k - 1], p[n + 1]), dist(p[1], p[n + 1])));
        printf("%lf\n", ans);
    }
    ///return 9;
}


#include <bits/stdc++.h>

using namespace std;
struct pos
{
	char t
    int x, y;
    pos(int x, int y, int nr)
    {
        this->x=x;
        this->y=y;
        t='B';
    }
    bool operator<(pos b) const
    {
        return nr>b.nr;
    }
};
struct stack_dsu
{
	vector<int> par, sz;
	vector<pair<int, int>> act;

	void init(int n)
	{
		par.resize(n+1);
		sz.resize(n+1);

		for(int i=1;i<=n;i++)
		{
			par[i]=i;
			sz[i]=1;
		}
	}
	int fpar(int x)
	{
		if(par[x]==x)
			return x;
		return fpar(par[x]);
	}
	void fmerge(int x, int y)
	{
		x=fpar(x);
		y=fpar(y);

		if(x!=y)
		{
			if(sz[x]>sz[y])
				swap(x, y);

			par[x]=y;
			sz[y]+=sz[x];
		}

		act.push_back({x, y});
	}
	void pop()
	{
		if(act.empty())
			return;

		int x=act.back().first;
		int y=act.back().second;
		act.pop_back();

		if(x==y)
			return;

		par[x]=x;
		sz[y]-=sz[x];
	}
};
struct queue_dsu
{
	int bottom;
	vector<pos> act;
	stack_dsu s;

	void init(int n)
	{
		s.init(n);
	}
	void fmerge(int x, int y)
	{
		nr++;
		act.push_back(pos(x, y, nr));
		s.fmerge(x, y);
	}

	void move_bottom()
	{
		while(bottom<act.size() && act[bottom].t=='B')
			bottom++;
	}
	void reverse_upd()
	{
		for(int i=0;i<act.size();i++)
		{
			s.pop();
			act[i].t='A';
		}

		reverse(act.begin(), act.end());

		for(int i=0;i<act.size();i++)
	}
};
int n,k,q,day,t,x,y;
int nr[600005];
queue_dsu dsu;
int main()
{
	/*
	cin>>n;
	dsu.init(n);
	while(true)
	{
		cin>>t;
		if(t==1)
		{
			cin>>x>>y;
			dsu.fmerge(x, y);
		}
		if(t==2)
			dsu.spop();
		if(t==3)
		{
			cin>>x;
			cout<<dsu.fpar(x)<<'\n';
		}
	}
	return 0;
	*/
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin>>n>>q>>k;
	dsu.init(n);
	day=1;
	while(q)
	{
		q--;
		cin>>t;
		if(t==1)
		{
			cin>>x>>y;
			dsu.fmerge(x, y);
			nr[day+k]++;
		}
		if(t==2)
		{
			cin>>x;
			cout<<dsu.s.sz[dsu.s.fpar(x)]<<'\n';
		}
		if(t==3)
		{
			day++;
			while(nr[day])
			{
				nr[day]--;
				dsu.pop();
			}
		}
	}
    return 0;
}


#pragma GCC optimize("O3","Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX

#include <bits/stdc++.h>

using namespace std;

struct DSU_WITH_ROLLBACKS
{
    struct dsu_save
    {
        int u, urank, v, vrank, comps;
        bool bipart;
    };

    stack<dsu_save> op;

    vector<int> rnk;
    vector<bool> col;
    bool bipartite;
    int comps;

    DSU_WITH_ROLLBACKS()
    {

    }

    DSU_WITH_ROLLBACKS(int N)
    {
        init(N);
    }

    void init(int N)
    {
        rnk.clear();
        col.clear();
        while(!op.empty())op.pop();
        rnk.resize(N);
        col.resize(N);
        for(int i = 0 ; i < N ; i++)
        {
            rnk[i] = 1;
            col[i] = false;
        }
        bipartite = true;
        comps = N;
    }

    pair<int,bool> find(int x)
    {
        if(rnk[x] > 0)
{
pair<int,bool> tp = {x, col[x]};
return tp;
}
        pair<int,bool> tp = find(-rnk[x]);
        tp.second ^= col[x];
        return tp;
    }

    void join(int a, int b)
    {
        pair<int,bool> ta = find(a);
tb = find(b);
        a = ta.first;
        b = tb.first;
        op.push({a, rnk[a], b, rnk[b], comps, bipartite});
        if(a == b)
        {
            if(ta.second == tb.second)bipartite = false;
            return;
        }
        comps--;
        if(rnk[a] < rnk[b])swap(a,b);
        rnk[a] += rnk[b];
        rnk[b] = -a;
        col[b] = col[b]^(ta.second == tb.second);
    }

    void rollback()
    {
        if(op.empty())return;
        dsu_save x = op.top();
        op.pop();
        comps = x.comps;
        bipartite = x.bipart;
        rnk[x.u] = x.urank;
        col[x.u] = false;
        rnk[x.v] = x.vrank;
        col[x.v] = false;
    }
};

const int MAXN = 1e6 + 100;

vector<int> arr[MAXN];

vector<int> res;

template<typename T, typename F, typename D>
struct ROLLBACK_TRICK
{
    vector<vector<T>> ST;
    int N;

    void add_to_tree(int nodo, int l, int r, int a, int b, T qu)
    {
        if(r < a || l > b)return;
        if(a <= l && r <= b)
        {
            ST[nodo].push_back(qu);
            return;
        }
        int mid = (l+r)/2;
        add_to_tree(nodo*2, l, mid, a, b, qu);
        add_to_tree(nodo*2+1, mid+1, r, a, b, qu);
    }

    void add_query(T qu, int l, int r)
    {
        add_to_tree(1, 0, N-1, l, r, qu);
    }

    void dfs(int nodo, int l, int r, D &DS)
    {
        for(auto x : ST[nodo])DS.join(x.first, x.second);
        if(l == r)
        {
        	for(auto x : arr[l])
        	{
        		res.push_back(DS.rnk[DS.find(x).first]);
        	}
        }
        else
        {
            int mid = (l+r)/2;
            dfs(nodo*2, l, mid, DS);
            dfs(nodo*2+1, mid+1, r, DS);
        }
        for(int i = 0 ; i < ST[nodo].size() ; i++)DS.rollback();
    }

    void solve(int tn, vector<T> vect, vector<pair<int,int>> querys, D &DS)
    {
        assert(vect.size() == querys.size());

        N = tn;

        ST.clear();
        ST.resize(4*(N+4));

        for(int i = 0 ; i < vect.size() ; i++)
        {
            add_query(vect[i], querys[i].first, querys[i].second);
        }

        dfs(1, 0, N-1, DS);
    }
};

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N, Q, K;
    cin >> N >> Q >> K;

    DSU_WITH_ROLLBACKS DSU(MAXN);

    vector<pair<int,int>> qu;

    vector<pair<int,int>> vect;

    deque<vector<pair<pair<int,int>,int>>> D;

    vector<pair<pair<int,int>,int>> temp;

    int cont = 0;

    for(int i = 1 ; i <= Q ; i++)
    {
    	int tp;
    	cin >> tp;

    	if(tp == 1)
    	{
    		int a, b;
    		cin >> a >> b;
    		temp.push_back({{a,b},cont});
    	}
    	if(tp == 2)
    	{
    		int q;
    		cin >> q;
    		arr[cont].push_back(q);
    		cont++;
    	}
    	if(tp == 3)
    	{
    		D.push_back(temp);
    		temp.clear();

    		if(D.size() == K)
    		{
    			for(auto x : D.front())
    			{
    				qu.push_back({x.second,cont});
    				vect.push_back({x.first.first,x.first.second});
    			}
    			D.pop_front();
    		}
    		cont++;
    	}
    }

    while(D.size())
    {
    	for(auto x : D.front())
		{
			qu.push_back({x.second,cont});
			vect.push_back({x.first.first,x.first.second});
		}
		D.pop_front();
    }

    cont++;

    ROLLBACK_TRICK<pair<int,int>, int, DSU_WITH_ROLLBACKS> RT;

    RT.solve(cont+100, vect, qu, DSU);

    for(auto x : res)
    {
    	cout << x << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// #define int           long long
#define S             second
#define F             first
#define pb            push_back
#define all(c)        (c).begin(),(c).end()
#define rall(c)       (c).rbegin(),(c).rend()
#define lb            lower_bound
#define ub            upper_bound
#define si(c)         (int)((c).size())
#define lcm(a, b)      (a * (b / __gcd(a,b)))
#define inf           (int)(4e18)
#define endl          '\n'
#define mp            make_pair
#define time(s)       (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...)    _F(#args, args)
#define vi            std::vector<int>
#define pii           pair<int, int>
#define vpi           vector<pii>
#define ordered_set   tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}

template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}

template<typename T> void Unique(vector<T> &v) {
    sort(all(v)), vseprateize(distance(v.begin(), unique(all(v))));
}

struct Dsu {
	int comps, n;
	std::vector<int> par, unused;
	std::vector<set<int> > siz;

	Dsu(int _n) {
		n = _n;
		parseprateize(n+1), sizseprateize(n+1);
		unusedseprateize(n+1);
		for(int i = 0; i <= n; i++)
			par[i] = i, siz[i].insert(i);
		comps = n;
	}
	void merge(int x, int y, vi seprate) {
		x = par[x], y = par[y];
		if(x == y) return;
		if(si(siz[y]) > si(siz[x])) swap(x, y);
		for(auto u : siz[y])
		seprate.pb(u), par[u] = x, siz[x].insert(u);
		comps--;
	}
};

void solve() {
	int n, q, k;
	cin >> n >> q >> k;

	Dsu ds(n+q+10);
	vi seprate[q+1];
	multiset<pair<pii, pii> > s;
	vi que[q+1];
	int cur = 1;
	int root = n+1;
	for(int i = 1; i <= q; i++) {
		int t;
		cin >> t;
		if(t == 1) {
			int x, y;
			cin >> x >> y;
			ds.merge(x, y, seprate[i]);
			que[cur].pb(i);
			if(si(seprate[i]) == 0) {
				s.insert(mp(mp(cur, i), mp(x, y)));
			}
		}
		else if(t == 2) {
			int x;
			cin >> x;
			cout << si(ds.siz[ds.par[x]]) << endl;
		}
		else if(cur >= k){
			while(si(s) && begin(s)->F.F <= cur-k+1) {
				s.erase(begin(s));
			}
			for(auto u : que[cur-k+1]) {
				for(auto uu :seprate[u]) {
					ds.siz[ds.par[uu]].erase(uu);
					ds.par[uu] = root;
					ds.siz[root].insert(uu);
				}
				ds.siz[root].erase(root);
				root++;
			}
			set<pair<pii, pii> > tmp;
			for(auto u : s) {
				ds.merge(u.S.S, u.S.F, seprate[u.F.S]);
				if(si(seprate[u.F.S]) == 0) {
					tmp.insert(u);
				}
			}
			s = tmp;
			cur++;
		}
		else cur++;
	}
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    start = clock();

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif

    int test = 1;
    // cin >> test;

    cout << fixed << setprecision(20);

    for(int i = 1; i <= test; ++i){
    	solve();
    }

    cerr << time(start);
    return 0;
}


#pragma GCC optimize(2);
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e6 + 10;
const int INF = 1e9;
struct LCT {
    struct node {
        int fa, ch[2]; //父亲(Splay对应的链向上由轻边连着哪个节点)、左右儿子
        int reverse;//区间反转标记
        bool  is_root;   //是否是所在Splay的根
        int sz; //节点数量
        int vsz; //虚子树节点数量
        ll val; //点权
        ll sum; //原树信息和
        ll vsum; //虚子树信息和
        int id;
        ll minv;
        //ll maxv; //最值
    } Tree[MAXN];
    int n;

    void init(int MN) {
        n = MN;
        for (int i = 1; i <= MN; i++) {
            Tree[i].reverse = Tree[i].fa = Tree[i].ch[0] = Tree[i].ch[1] = 0;
            Tree[i].is_root = true;
            Tree[i].val = INF;
            Tree[i].sz = 1;
            Tree[i].id = i;
        }
    }

    bool getson(int x) {
        //x是否为重儿子
        return x == Tree[Tree[x].fa].ch[1];
    }
    bool isroot(int x) {
        return Tree[Tree[x].fa].ch[0] != x && Tree[Tree[x].fa].ch[1] != x;
    }
    void pushreverse(int x) {
        if (!x)
            return;
        swap(Tree[x].ch[0], Tree[x].ch[1]);
        //swap(Tree[x].lcol, Tree[x].rcol); //有些颜色问题要翻转左右颜色
        Tree[x].reverse ^= 1;
    }
    void pushdown(int x) { //不同问题的标记下推方式不同
        //下传反转标记
        if (Tree[x].reverse) {
            pushreverse(Tree[x].ch[0]);
            pushreverse(Tree[x].ch[1]);
            Tree[x].reverse = 0;
        }
    }

    void update(int x) { //不同问题写法不同
        int l = Tree[x].ch[0], r = Tree[x].ch[1];
        Tree[x].sz = 1 + Tree[x].vsz;
        Tree[x].sum = Tree[x].val + Tree[x].vsum;
        Tree[x].minv = Tree[x].val;
        Tree[x].id = x;
        if (l) {
            Tree[x].sz += Tree[l].sz;
            Tree[x].sum += Tree[l].sum;
            if (Tree[l].minv < Tree[x].minv) {
                Tree[x].minv = Tree[l].minv;
                Tree[x].id = Tree[l].id;
            }
            //Tree[x].maxv = max(Tree[x].maxv, Tree[l].maxv);
        }
        if (r) {
            Tree[x].sz += Tree[r].sz;
            Tree[x].sum += Tree[r].sum;
            if (Tree[r].minv < Tree[x].minv) {
                Tree[x].minv = Tree[r].minv;
                Tree[x].id = Tree[r].id;
            }
            //Tree[x].maxv = max(Tree[x].maxv, Tree[r].maxv);
        }
    }

    void rotate(int x) {
        //将x旋转为根
        if (Tree[x].is_root)
            return;
        int k = getson(x), fa = Tree[x].fa;
        int fafa = Tree[fa].fa;
        pushdown(fa);
        pushdown(x);    //先要下传标记
        Tree[fa].ch[k] = Tree[x].ch[k ^ 1];
        if (Tree[x].ch[k ^ 1])
            Tree[Tree[x].ch[k ^ 1]].fa = fa;
        Tree[x].ch[k ^ 1] = fa;
        Tree[fa].fa = x;
        Tree[x].fa = fafa;
        if (!Tree[fa].is_root)
            Tree[fafa].ch[fa == Tree[fafa].ch[1]] = x;
        else
            Tree[x].is_root = true, Tree[fa].is_root = false;
        update(fa);
        update(x);    //如果维护了信息，就要更新节点
    }
    void push(int x) {
        if (!Tree[x].is_root)
            push(Tree[x].fa);
        pushdown(x);
    }
    int findroot(int x) {
        //找到x在原树中的根节点
        access(x);
        Splay(x);
        pushdown(x);
        while (Tree[x].ch[0])
            pushdown(x = Tree[x].ch[0]);//找到深度最小的点即为根节点
        return x;
    }
    void Splay(int x) {
        //让x成为Splay的根，且x不含右儿子
        push(x);   //在Splay到根之前，必须先传完反转标记
        for (int fa; !Tree[x].is_root; rotate(x)) {
            if (!Tree[fa = Tree[x].fa].is_root) {
                rotate((getson(x) == getson(fa)) ? fa : x);
            }
        }
    }
    void access(int x) {
        //访问某节点。作用是：对于访问的节点x，打通一条从树根（真实的LCT树）到x的重链；如果x往下是重链，那么把x往下的重边改成轻边。结束后x没有右儿子（没有深度比他大的点）
        int y = 0;
        do {
            Splay(x);
            Tree[Tree[x].ch[1]].is_root = true;
            Tree[x].vsum += Tree[Tree[x].ch[1]].sum;
            Tree[x].vsz += Tree[Tree[x].ch[1]].sz;
            Tree[Tree[x].ch[1] = y].is_root = false;
            Tree[x].vsum -= Tree[Tree[x].ch[1]].sum;
            Tree[x].vsz -= Tree[Tree[x].ch[1]].sz;
            update(x);    //如果维护了信息记得更新。
            x = Tree[y = x].fa;
        } while (x);
    }
    void mroot(int x) {
        //把某个节点变成树根（这里的根指的是整棵LCT的根）
        access(x);//使x与根结点处在同一棵splay中
        Splay(x);//x成为这棵splay的根,x只有左儿子
        //由于根节点所在的splay中，根节点没有左儿子（没有深度比他小的节点），将x的左右子树翻转
        pushreverse(x);
    }
    void link(int u, int v) {
        //连接u所在的LCT和v所在的LCT
        mroot(u);//先让u成为其所在LCT的根
        if(findroot(v) != u) {
            Tree[u].fa = v;//如果u与v不在同一棵splay中，就把v设置为u的父亲
            Tree[v].vsum += Tree[u].sum;
            Tree[v].vsz += Tree[u].sz;
            update(v);
        }

    }
    void cut(int u, int v) {
        //分离出两棵LCT
        mroot(u);   //先让u成为其所在LCT的根
        access(v); //让u和v在同一棵Splay中
        Splay(v);    //连接u、v，u是v的左儿子
        pushdown(v);     //先下传标记
        if (Tree[v].ch[0]) {
            Tree[Tree[v].ch[0]].fa = Tree[v].fa;
            Tree[Tree[v].ch[0]].is_root = true;
        }
        Tree[v].fa = 0;
        Tree[v].ch[0] = 0;
        //v的左孩子表示v上方相连的重链
        update(v);  //记得维护信息
    }
    bool judge(int u, int v) {
        //判断u和v是否连通
        while (Tree[u].fa)
            u = Tree[u].fa;
        while (Tree[v].fa)
            v = Tree[v].fa;
        return u == v;
    }
    void split(int u, int v) {
        //获取u->v的路径
        mroot(u);//让u成为根结点
        access(v);//访问v
        Splay(v);//把v转到根结点，此时u的父亲为v
    }
    void modify(int x, int v) {
        //改变点值
        access(x);
        Splay(x);
        Tree[x].val = v; //更改值
        update(x);
    }
} lct;
int getroot(int x) {
    ll lsum = 0, rsum = 0, lnow = 0, rnow = 0;
    int son = lct.Tree[x].sz/2, flag = lct.Tree[x].sz&1, root = lct.n + 1;
    while (x) {
        lct.pushdown(x);
        int l = lct.Tree[x].ch[0], r = lct.Tree[x].ch[1];
        lnow = lsum + lct.Tree[l].sz;
        rnow = rsum + lct.Tree[r].sz;
        if (lnow <= son && rnow <= son) {
            if (flag) { //奇数个点，重心只有一个
                root = x;
                break;
            } else { //找编号小的重心
                root = min(root, x);
            }
        }
        if (lnow < rnow) { //右边大，往右找重心
            lsum += lct.Tree[l].sz + lct.Tree[x].vsz + 1;
            x = r;
        } else { //左边大，往左找重心
            rsum += lct.Tree[r].sz + lct.Tree[x].vsz + 1;
            x = l;
        }
    }
    lct.Splay(root);
    return root;
}
void debug() {
    for (int i = 1; i <= 5; i++) {
        lct.mroot(i);
        cout << "i sz:" << i << " " << lct.Tree[i].sz <<endl;
    }
}
struct operation {
    int day;
    int id;
};
int u[MAXN];
int v[MAXN];
queue<operation> que;
int main() {
    int n, q, k;
    scanf("%d%d%d", &n, &q, &k);
    lct.n = n;
    lct.init(n + q);
    int now = 1;
    for (int i = 1; i <= q; i++) {
        while (!que.empty()) {
            auto cur = que.front();
            //cout << "cur:" << cur.id << " " << cur.day << endl;
            if (cur.day <  now - k + 1) {
                lct.cut(u[cur.id], cur.id + n);
                lct.cut(v[cur.id], cur.id + n);
                que.pop();
            } else {
                break;
            }
        }
        int op;
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%d", &u[i], &v[i]);
            que.push({now, i});
            if (lct.judge(u[i], v[i])) {
                //cout << "judge yes\n";
                lct.split(u[i], v[i]);
                int num = lct.Tree[v[i]].id;
                //cout << "num:" << num << endl;
                lct.cut(num, u[num - n]);
                lct.cut(num, v[num - n]);
            }
            lct.Tree[i + n].val = now;
            lct.link(u[i], i + n);
            lct.link(v[i], i + n);
        } else if (op == 2) {
            int x;
            scanf("%d", &x);
            lct.mroot(x);
            printf("%d\n", lct.Tree[x].sz / 2 + 1);
        } else {
            now++;
        }
        //debug();
    }
    return 0;
}


/*input
3 3
1 2
2 3
1 3

*/

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <unordered_map>
#include <queue>

using namespace std;

int N, M;
using ll = long long;
const int BUCKET = 30000001, MOD = 9835731;
ll E[18], rE[18], W[18], X, Y;
struct State { ll state = 0, ways = 0, total = 0; int nxt = -1; } list[BUCKET];
int ptr = MOD;

string toString(ll x)
{
	string str = "";
	for (int i = 0; i < 40; ++i) {
		str += '0' + (x & 1);
		x >>= 1;
	}
	return str;
}

State& get(int st)
{
	int id = st % MOD;
	while (true) {
		if (list[id].state == st) return list[id];
		if (list[id].nxt < 0) break;
		id = list[id].nxt;
	}
	list[id].nxt = ptr;
	list[ptr].state = st;
	return list[ptr++];
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> M;
	for (int i = 0; i < M; ++i) {
		int u, v; cin >> u >> v;
		--u, --v;
		E[u] ^= (1 << v);
		E[v] ^= (1 << u);
		W[v] ^= (1 << u);
	}
	X = (1 << N) - 1, Y = X << N;
	for (int u = 0; u < N; ++u) rE[u] = E[u] ^ X;

	get(0).ways = 1;
	queue<ll> qu; qu.push(0);
	while (!qu.empty()) {
		auto a = get(qu.front());
		// cout << toString(qu.front()) << ": " << a.first << ' ' << a.second << endl;
		for (int u = 0; u < N; ++u) {
			ll state = qu.front();
			if ((state >> (N + u)) & 1) continue;

			ll unreachable_deg0 = state & rE[u];
			if ((unreachable_deg0 & (-unreachable_deg0)) && (unreachable_deg0 & (-unreachable_deg0)) < (1 << u)) continue;

			state = (state & Y) ^ (1ll << (N + u)) ^ unreachable_deg0 ^ (1 << u);

			auto &p = get(state);
			if (p.ways == 0) qu.push(state);
			p.ways += a.ways;
			p.total += a.total + a.ways * __builtin_popcountll(W[u] & (state >> N));
			// cout << " -> " << toString(state) << endl;
		}
		qu.pop();
	}

	ll ans = 0;
	for (int i = 0; i < ptr; ++i) {
		if ((list[i].state >> N) != X) continue;
		ans += list[i].total;
	}
	cout << ans % 998244353 << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORD(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,b,a) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define all(v) v.begin(),v.end()
#define ad push_back
#define fr first
#define sc second
#define mpr(a,b) make_pair(a,b)
#define pir pair<int,int>
#define all(v) v.begin(),v.end()
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define srng mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define y1 EsiHancagorcRepa

const int N=20,P=(1<<N);
const ll MOD=998244353;

vector<int> g[N],hg[N];
int ans[P],qan[P],kox[P][N],ppc[P],tv[P];
int vrj[P];
bool bad2[P],bad[P];

void gum(int &a,int b){
    a+=b;
    if (a>=MOD)a-=MOD;
}

void han(int &a,int b){
    a-=b;
    if (a<0)a+=MOD;
}

int main(){
    fastio;
    srng;
    int n,m;
    cin>>n>>m;
    FOR(i,m){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        g[a].ad(b);
        hg[b].ad(a);
    } 3
    FOR(msk,(1<<n)){
        vrj[msk]=-1;
        FOR(i,n){
            if ((1<<i)&msk){
                ppc[msk]++;
                trav(to,g[i]){
                    if ((1<<to)&msk){
                        bad[msk]=true;
                        kox[msk^(1<<i)][i]++;
                    }
                }
                vrj[msk]=i;
            }
        }
    }
    int lr=(1<<n)-1;
    qan[0]=1;
    FORT(msk,0,(1<<n)-2){
        int nmsk=lr^msk;
        tv[0]=0;
        int s=nmsk;
        do{
            s=(s-1)&nmsk;
            s=nmsk^s;
            if (bad[s]){
                tv[s]=0;
                s=nmsk^s;
                continue;
            }
            tv[s]=tv[s^(1<<vrj[s])]+kox[msk][vrj[s]];
            if (ppc[s]&1){
                gum(qan[msk+s],qan[msk]);
                gum(ans[msk+s],(1ll*tv[s]*qan[msk]+1ll*ans[msk])%MOD);
            }
            else{
                han(qan[msk+s],qan[msk]);
                han(ans[msk+s],(1ll*tv[s]*qan[msk]+1ll*ans[msk])%MOD);
            }
            s=nmsk^s;
        }while(s);
    }
    cout<<ans[lr]<<endl;
    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <cstdio>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <string>
#include <bitset>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <set>
#include <tuple>
#include <sstream>

#define LOG(v) for (auto e : v) cerr << e << " "; cerr << endl;

typedef long long i64;

using namespace std;
i64 a, b, n;

i64 solve(i64 l, i64 r) {
    if (n / l == n / r) {
        i64 ac = (n + l - 1) / l;
        if (a % ac == 0 && b % (ac-1) <= b / (ac - 1)) {
            return r - l + 1;
        } else if (a % ac < a / ac && b % (ac - 1) <= b / (ac - 1)) {
            return r - l + 1;
        } else {
            return 0ll;
        }
    } else {
        i64 mid = (r + l) / 2;
        return solve(l, mid) +
            solve(mid + 1, r);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); srand(123);

    while (cin >> a >> b) {
        n = a + b;
        if (b > a) {
            swap(a, b);
        }

        int ans = 1;
        for (i64 l = 1, r; l < n; l = r) {
            r = n / (n / l) + 1;
            i64 c = n / l;
            ans += max(0, min(r, a / c + b / c + 1) - max(l, (b + c) / (c + 1) + (a + c) / (c + 1)));
        }

        // int ans = 0;
        // for (int k = 1; k <= n; k++) {
        //     int c = (n + n - 1) / k;
        //     // cerr << c << endl;
        //     if (a % c == 0 && b % (c-1) <= b / (c - 1)) {
        //         ans++;
        //     } else if (a % c < a / c && b % (c - 1) <= b / (c - 1)) {
        //         ans++;
        //     }
        // }
        // int ans = solve(1, n - 1) + 1;
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
typedef int LL;
LL a,b,ans;
int main(){
    scanf("%d%d",&a,&b);
    LL len=a+b;
    for(LL i=1,r;i<=len;i=r+1){
        LL P=len/i;    // P ������ѭ����
        r=len/P;       // ͬ�������ѭ���ڳ���
        if(P>a || P>b) continue;
        LL a_low=(a+P)/(P+1),a_high=a/P;
        LL b_low=(b+P)/(P+1),b_high=b/P;
        if(a_low<=a && b_low<=b)
            ans+=min((a_high+b_high),r)-max((a_low+b_low),i)+1;
    }
    printf("%d ",ans);
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const char dir[] = {'R', 'L', 'D', 'U'};

ll add(ll a, ll b){ //(a + b) % 1e9 + 7
    a += b;
    if(a >= MOD){
        a -= MOD;
    }
    return a;
}

int sub(int a, int b){
    a -= b;
    if(a < 0) a += MOD;
    return a;
}

int mult(int a, int b){
    return ((ll) a * b) % MOD;
}

void setIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen((s+".in").c_str(),"r",stdin);
//    freopen((s+".text").c_str(),"w",stdout);
}

int main(){
    setIO();
    int a, b; cin >> a >> b;
    int tot = a + b;
    for(int i = 1, next; i <= n; i = next){
        int groups = n / i;
        int next = n / groups + 1;
        int alo = (a + groups) / (groups + 1);
        int ahi = a / groups;
        int blo = (b + groups) / (groups + 1);
        int bhi = b / groups;
        if(alo > ahi || blo > bhi) continue;
        ans += max(0, min(next - 1, ahi + bhi) - max(i, alo + blo) + 1);
    }
    cout << ans << "\n";
}


/*     electric love #jdragon
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
 oregairu */
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define fl i,a,b for(int i=a;i<b;i++)
#define co cout<<
#define ci cin>>
#define rep(i,x,n) for(int i = x; i < n; i++)
#define rev(i,n,x) for(int i = n; i >= x; i--)
#define ff            first
#define ss            second
#define vecll vector<long long>
#define pii pair<long long ,long long >
#define pip pair<int,pair<int,int>>
#define ppp pair<pair<int,int>,pair<int,int>>
#define inarr(arr,x,n)  for(ll i=x ;i<n;i++) cin >> arr[i];
#define mpp make_pair
#define pb                push_back
#define ppb               pop_back
#define all(x)          (x).begin(),(x).end()
#define sz(x)           (x).size()
#define henshin           cout<<endl;
#define DEBUG(x) cout << (x) << '\n';

const ll oo = int(1e9);
inline void yn(int x) { if(!x)cout << "NO\n";  else if(x)cout << "YES\n";return; }

const ll N=3e5+5,M=1e9+7,mod=998244353;

void solve() {
int n, m;
    ci n >> m;
    int cnt = 0;
    set <int> s;
    while(n) {
        s.insert(n % m);
        cnt++;
        n /= m;
    }
    if(s.size() == cnt) cout << "YES\n";
    else cout << "NO\n";
    return 0;
 }

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    int tt = clock();
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(15);

    int tc=1;

    while(tc--) {
 solve();

#ifdef _DEBUG
        cerr << "TIME = " << clock() - tt << endl;
        tt = clock();
#endif
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long

inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0' && ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x*f;
}
inline void write(int x){
	if(x<0) {putchar('-'); x=-x;}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
int h[1009],n,m;
int main(){
	n=read(); m=read();
	while(n){
		if(h[n%m]){
			puts("NO");
			return 0;
		}
		h[n%m]=1;
		n/=m;
	}
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long a,b;
	long long s;
	cin>>a>>b;
	long long cnt=0;
	while(a){
		cnt++;
		s.insert(a%b);
		a/=b;
	}
	if(cnt==s.size()) cout<<"YES";
	else cout<<"NO";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long;
ll n,m,c;
set <ll> s;
int main(){
	cin>>n>>m;
	while (n) s.insert(n % m),n /= m,c += 1;
	cout<<(c == s.size() ? "YES" : "NO")<<endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef bool boolean;

#define pii pair<int, int>
#define fi first
#define sc second

const int N = 5e5 + 5;

int n, m;
vector< pii > *g;
vector< vector<int> > cir;
int cis[N], maxl[N], L[N], Z[N];
boolean vis[N], ib[N];	// is it a bridge

inline void init() {
	scanf("%d%d", &n, &m);
	g = new vector<pii>[(n + 1)];
	for (int i = 1, u, v; i <= m; i++) {
		scanf("%d%d", &u, &v);
		g[u].push_back(pii(v, i));
		g[v].push_back(pii(u, i));
	}
}

int ts = 0;
int dfn[N], low[N];
void tarjan(int p, int fa) {
	dfn[p] = low[p] = ++ts;
	for (auto e : g[p]) {
		if (e.fi == fa)	continue;
		if (!dfn[e.fi]) {
			tarjan(e.fi, p);
			low[p] = min(low[p], low[e.fi]);
			if (low[e.fi] == dfn[e.fi])
				ib[e.sc] = true;
		} else
			low[p] = min(low[p], dfn[e.fi]);
	}
}

int cnt = 0;
void dfs(int p) {
	cnt++;
	assert(cnt <= n);
	vis[p] = true;
	cir.back().push_back(p);
	cis[p] = (signed) cir.size() - 1;
	for (auto e : g[p])
		if (!vis[e.fi] && !ib[e.sc])
			dfs(e.fi);
}

queue<int> que;
void bfs(int s) {
	int tp = 0;
	int *tr = new int[(n + 1)];
	int *par = new int[(n + 1)];
	que.push(s);
	memset(vis, false, sizeof(vis));
	tr[++tp] = s, par[s] = 0, vis[s] = true;
	while (!que.empty()) {
		int e = que.front();
		que.pop();
		for (auto eu : g[e]) {
			if (vis[eu.fi])	continue;
			vis[eu.fi] = true;
			tr[++tp] = eu.fi, par[eu.fi] = e;
			assert(eu.fi != s);
			que.push(eu.fi);
		}
	}

	for (int i = n; i > 1; i--) {
		int p = tr[i];
		for (auto e : g[p]) {
			if (e.fi == par[p]) {
				maxl[e.fi] = max(maxl[e.fi], maxl[p] + 1);
				if (ib[e.sc])
					L[e.fi] = max(L[e.fi], maxl[p] + 1);
				break;
			}
		}
	}
	delete[] tr;
	delete[] par;
}

int ql, qr;
pii ar[N];
void deal(int p, int fa, int upval) {
	L[p] = max(L[p], upval);
	vector<int> &circ = cir[cis[p]];
	int k = (signed) circ.size(), lim = k >> 1;
	for (int it = 0; it < 2; it++) {
		ql = 1, qr = 0;
		for (int i = 0, e; i < 2 * k; i++) {
			e = circ[i % k];
			while (ql <= qr && i - ar[ql].sc > lim)
				ql++;
			if (ql <= qr)
				Z[e] = max(Z[e], i + ar[ql].fi);
			while (ql <= qr && ar[qr].fi <= L[e] - i)
				qr--;
			ar[++qr] = pii(L[e] - i, i);
		}
		reverse(circ.begin(), circ.end());
	}

	for (auto u : circ) {
		int fiv = 0, scv = 0;
		for (auto e : g[u])
			if (e.fi != fa && ib[e.sc]) {
				if (maxl[e.fi] + 1 > fiv)
					scv = maxl[e.fi] + 1, swap(scv, fiv);
				else if (maxl[e.fi] + 1 > scv)
					scv = maxl[e.fi] + 1;
			}

		for (auto e : g[u])
			if (e.fi != fa && ib[e.sc]) {
				int pv = max(Z[u], upval), val = maxl[e.fi] + 1;
				if (val == fiv)
					pv = max(pv, scv);
				else
					pv = max(pv, fiv);
				deal(e.fi, u, pv + 1);
			}
	}
}

inline void solve() {
	tarjan(1, 0);
	for (int i = 1; i <= n; i++)
		if (!vis[i]) {
			cir.push_back(vector<int>());
			dfs(i);
		}
	bfs(1);
	deal(1, 0, 0);
	for (int i = 1; i <= n; i++)
		printf("%d ", max(L[i], Z[i]));
}

int main() {
	init();
	solve();
	return 0;
}


#include<bits/stdc++.h>
#define N 1000050
using namespace std;
int he[N>>1],ne[N<<1],to[N<<1],tot;
int n,m,ds;
const int inf=1e9;
inline void addedge(int x,int y){
	to[++tot]=y;ne[tot]=he[x];he[x]=tot;
}
vector<int>v[N],ve[N];
int dfn[N>>1],low[N>>1],cnt,st[N>>1],tp;
inline void tarjan(int g){
	dfn[g]=low[g]=++cnt;st[++tp]=g;
	for(int i=he[g];i;i=ne[i]){
		if(dfn[to[i]])low[g]=min(low[g],dfn[to[i]]);
		else{
			tarjan(to[i]);
			low[g]=min(low[g],low[to[i]]);
			if(low[to[i]]==dfn[g]){
				++ds;v[g].push_back(ds);
				while(1){
					int t=st[tp--];
					v[ds].push_back(t);
					if(t==to[i])break;
				}
			}
		}
	}
}
int len[N],a[N];
inline void dfs(int g){
	len[g]=g<=n?1:0;
	for(int i=0,to;i<v[g].size();++i){
		to=v[g][i],dfs(to);
		if(g<=n)len[g]=max(len[g],len[to]+1);
	}
	int sz=v[g].size();
	if(!sz)return;
	if(g>n){
		a[sz]=0;
		for(int i=sz-1;~i;--i)a[i]=max(len[v[g][i]]+sz-1-i,a[i+1]);
		len[g]=a[0];
		for(int i=0,ma=0;i<sz;++i){
			ma=max(ma,len[v[g][i]]+i);
			len[g]=min(len[g],max(ma,a[i+1]));
		}
	}
}
int ans[N<<1];
int ma1[N<<3],ma2[N<<3];
inline void build(int g,int l,int r){
	if(l==r){
		ma1[g]=a[l]-l;
		ma2[g]=a[l]+l;
		return;
	}
	const int m=l+r>>1;
	build(g<<1,l,m);
	build(g<<1|1,m+1,r);
	ma1[g]=max(ma1[g<<1],ma1[g<<1|1]);
	ma2[g]=max(ma2[g<<1],ma2[g<<1|1]);
}
inline int ask1(int g,int l,int r,int x,int y){
	if(l>y||r<x)return -inf;
	if(l>=x&&r<=y)return ma1[g];
	const int m=l+r>>1;
	int r1=ask1(g<<1,l,m,x,y),r2=ask1(g<<1|1,m+1,r,x,y);
	return max(r1,r2);
}
inline int ask2(int g,int l,int r,int x,int y){
	if(l>y||r<x)return -inf;
	if(l>=x&&r<=y)return ma2[g];
	const int m=l+r>>1;
	int r1=ask2(g<<1,l,m,x,y),r2=ask2(g<<1|1,m+1,r,x,y);
	return max(r1,r2);
}

inline int getans(int id,int L,int R,int ma){
	int l=L,r=R;
	while(l+1<r){
		int mid=l+r>>1;
		if(ask2(1,1,ma,L,mid)-L<=ask1(1,1,ma,mid+1,R)+R)l=mid;
		else r=mid;
	}
	int l1=ask2(1,1,ma,L,l)-L,l2=ask1(1,1,ma,l+1,R)+R;l1=max(l1,l2);
	int r1=ask2(1,1,ma,L,r)-L,r2=ask1(1,1,ma,r+1,R)+R;r1=max(r1,r2);
	return min(l1,r1);
}
int sz,ma;
inline void dfs2(int g,int fa){
	ans[g]=len[g]-1;
	sz=v[g].size();
	if(!sz)return;
	ve[g].resize(sz);
	if(g<=n){
		a[sz]=0;
		for(int i=sz-1;~i;--i)a[i]=max(len[v[g][i]]+1,a[i+1]);
		for(int i=0,ma=0;i<sz;++i){
			len[g]=len[fa]+1;
			len[g]=max(len[g],ma);
			len[g]=max(len[g],a[i+1]);
			ma=max(ma,len[v[g][i]]+1);
			ve[g][i]=len[g];
		}
		for(int i=0;i<v[g].size();++i)
			len[g]=ve[g][i],dfs2(v[g][i],g);
		return;
	}
	ma=sz+sz-1;a[sz]=len[fa];
	for(int i=0;i<sz;++i)a[i]=a[i+sz+1]=len[v[g][i]];
	build(1,1,ma);
	for(int i=0;i<sz;++i)
		ve[g][i]=getans(i,i+1,i+sz,ma);
	for(int i=0;i<v[g].size();++i){
		len[g]=ve[g][i];
		len[v[g][i]]=max(len[v[g][i]],ve[g][i]+1);
		dfs2(v[g][i],g);
	}
}
int main(){
	scanf("%d%d",&n,&m);ds=n;
	for(int i=1,x,y;i<=m;++i){
		scanf("%d%d",&x,&y);
		addedge(x,y),addedge(y,x);
	}
	int rt=1;
	tarjan(rt);
	int masize=0;
	for(int i=1;i<=ds;++i)if(v[i].size()>masize)masize=v[i].size();
	dfs(rt);
	dfs2(rt,0);
	static int an2[N];
	for(int i=1;i<=n;++i)an2[i]=ans[i];
	for(int i=1;i<=n;++i)printf("%d ",an2[i]);puts("");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=500005,M=N*4;
struct Gragh{
	//2M*2*4B+0.5M*4B=18MB
	int cnt,y[M],nxt[M],fst[N];
	void clear(){
		cnt=1;
		memset(fst,0,sizeof fst);
	}
	void add(int a,int b){
		y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
	}
}g,g2;
int n,m;
int dfn[N],low[N],inst[N],st[N],vis[N],id[N],Time,top,tot;
int used[M];
int Fa[N];
vector <int> cir[N],son[N];
void Tarjan_Prepare(){
	Time=top=tot=0;
	memset(id,0,sizeof id);
	memset(st,0,sizeof st);
	memset(dfn,0,sizeof dfn);
	memset(low,0,sizeof low);
	memset(vis,0,sizeof vis);
	memset(inst,0,sizeof inst);
	memset(used,0,sizeof used);
}
void Tarjan(int x){
	dfn[x]=low[x]=++Time;
	vis[x]=inst[x]=1;
	st[++top]=x;
	for (int i=g.fst[x];i;i=g.nxt[i]){
		if (used[i/2])
			continue;
		used[i/2]=1;
		if (!vis[g.y[i]){
			Tarjan(g.y[i]);
			low[x]=min(low[x],low[g.y[i]]);
		}
		else if (inst[g.y[i]])
			low[x]=min(low[x],low[g.y[i]]);
	}
	if (dfn[x]==low[x]){
		tot++;
		id[st[top]]=tot;
		inst[st[top]]=0;
		while (st[top--]!=x){
			id[st[top]]=tot;
			inst[st[top]]=0;
		}
	}
}
void Get_cir(int x){
	if (vis[x])
		return;
	vis[x]=1;
	cir[id[x]].push_back(x);
	for (int i=g.fst[x];i;i=g.nxt[i]){
		int y=g.y[i];
		if (id[y]!=id[x])
			dfs(y);
	}
}
void build(int x,int pre){
	int ID=id[x];
	Fa[ID]=pre;
	cir[ID].clear();
	Get_cir(x);
	for (int i=0;i<cir[ID].size();i++){
		int u=cir[ID][i];
		son[u].clear();
		for (int j=g.fst[u];j;j=g.nxt[j]){
			int v=g.y[j];
			if (id[v]==ID||id[v]==pre)
				continue;
			son[u].push_back(v);
			build(v,ID);
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	g.clear();
	for (int i=1,a,b;i<=m;i++){
		scanf("%d%d",&a,&b);
		g.add(a,b);
		g.add(b,a);
	}
	Tarjan_Prepare();
	for (int i=1;i<=n;i++)
		if (!vis[i])
			Tarjan(i);
	memset(vis,0,sizeof vis);
	build(1,0);
	return 0;
}


#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=500005,M=N*4;
struct Gragh{
	//2M*2*4B+0.5M*4B=18MB
	int cnt,y[M],nxt[M],fst[N];
	void clear(){
		cnt=1;
		memset(fst,0,sizeof fst);
	}
	void add(int a,int b){
		y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
	}
}g,g2;
int n,m;
int dfn[N],low[N],inst[N],st[N],vis[N],id[N],Time,top,tot;
int used[M];
int Fa[N];
vector <int> cir[N],son[N];
void Tarjan_Prepare(){
	Time=top=tot=0;
	memset(id,0,sizeof id);
	memset(st,0,sizeof st);
	memset(dfn,0,sizeof dfn);
	memset(low,0,sizeof low);
	memset(vis,0,sizeof vis);
	memset(inst,0,sizeof inst);
	memset(used,0,sizeof used);
}
void Tarjan(int x){
	dfn[x]=low[x]=++Time;
	vis[x]=inst[x]=1;
	st[++top]=x;
	for (int i=g.fst[x];i;i=g.nxt[i]){
		if (used[i/2])
			continue;
		used[i/2]=1;
		if (!vis[g.y[i]]){
			Tarjan(g.y[i]);
			low[x]=min(low[x],low[g.y[i]]);
		}
		else if (inst[g.y[i]])
			low[x]=min(low[x],low[g.y[i]]);
	}
	if (dfn[x]==low[x]){
		tot++;
		id[st[top]]=tot;
		inst[st[top]]=0;
		while (st[top--]!=x){
			id[st[top]]=tot;
			inst[st[top]]=0;
		}
	}
}
void Get_cir(int x){
	if (vis[x])
		return;
	vis[x]=1;
	cir[id[x]].push_back(x);
	for (int i=g.fst[x];i;i=g.nxt[i]){
		int y=g.y[i];
		if (id[y]==id[x])
			Get_cir(y);
	}
}
void build(int x,int pre){
	int ID=id[x];
	Fa[ID]=pre;
	cir[ID].clear();
	Get_cir(x);
	for (int i=0;i<cir[ID].size();i++){
		int u=cir[ID][i];
		son[u].clear();
		for (int j=g.fst[u];j;j=g.nxt[j]){
			int v=g.y[j];
			if (id[v]==ID||id[v]==pre)
				continue;
			son[u].push_back(v);
			build(v,ID);
		}
	}
}
int Deep[N];
// The Minimum value of the distance between
// x and the deepest posterity of x
// 环根：   以该环为根的子树中，距离 x 最远的点与其距离的 最小值
// 非环根： 该节点所有子节点（显然都是环根）的 Deep 最大值 + 1
int spDeep[N],usDeep[N];
// spDeep[x] x 为环根，环边节点给他的贡献
// usDeep[x] x 为环根，桥边子树给他的贡献
int Lmax[N],Rmax[N];
void Get_Deep(int x){
	int ID=id[x];
	for (int i=0;i<cir[ID].size();i++){
		int u=cir[ID][i],v;
		Deep[u]=0;
		for (int j=0;j<son[u].size();j++){
			v=son[u][j];
			Get_Deep(v);
			Deep[u]=max(Deep[u],Deep[v]+1);
		}
	}
	int n=cir[ID].size()-1;
	Lmax[0]=Rmax[n+1]=0;
	for (int i=1;i<=n;i++)
		Lmax[i]=max(Lmax[i-1],Deep[cir[ID][i]]+i);
	for (int i=n;i>=1;i--)
		Rmax[i]=max(Rmax[i+1],Deep[cir[ID][i]]+(n-i+1));
	int v=1e9;
	for (int i=1;i<=n+1;i++)
		v=min(v,max(Lmax[i-1],Rmax[i]));
	usDeep[x]=Deep[x];
	Deep[x]=max(Deep[x],spDeep[x]=v);
}
int Far[N];
// The Minimum value of the distance between
// x and the farthest vetrex which isn't a posterity of x
// 环根：    由其父亲继承
// 非环根：  环内单调队列跑出来
int val[N*3];
int Lv[N*3],Rv[N*3];
struct Monotone_Queue{
	int head,tail,q[N*3],d[N*3],hehe;
	void clear(){
		head=1,tail=0;
		hehe=0;
	}
	int front(){
		return head<=tail?q[head]:0;
	}
	void push(int v,int _d){
		while (head<=tail&&q[tail]<=v)
			tail--;
		q[++tail]=v,d[tail]=_d;
	}
	void pop(int _d){
		while (head<=tail&&d[head]<_d)
			head++;
	}
	int Next_front(int _d){
		if (head>tail)
			return 0;
		if (d[head]!=_d)
			return q[head];
		return head==tail?0:q[head+1];
	}
	void print(){
		printf("size=%d, sit=:",tail-head+1);
		for (int i=head;i<=tail;i++)
			printf("(%d,%d)  ",q[i],d[i]);
		puts("");
	}
}QL,QR;
vector <int> LRmax[N];
void Solve(int x,int far){
	int ID=id[x];
	Far[x]=far;
	int n=cir[ID].size();
	for (int i=1;i<n;i++)
		val[i]=Deep[cir[ID][i]];
	val[0]=max(usDeep[x],Far[x]);
	for (int i=n;i<n*3;i++)
		val[i]=val[i%n];
	for (int i=0;i<n*3;i++){
		Lv[i]=val[i]+n*3-i;
		Rv[i]=val[i]+i+1;
	}
/*	for (int i=0;i<n*3;i++)
		printf("==>%d %d(%d) %d(%d)\n",val[i],Lv[i],n*3-i,Rv[i],i+1);
		*/
	QL.clear(),QR.clear();
	int Lp=2,Rp=n+1;
	for (int i=Lp;i<=n;i++)
		QL.push(Lv[i],i);
	for (int i=n+1;i<n*2;i++){
		int result=1e9;
		int v1=n*3-i,v2=i+1;
		while (Rp<i){
			Lp++,QL.pop(Lp);
			Rp++;
			if (Rp!=i)
				QR.push(Rv[Rp],Rp);
		}
//		printf("x=%d,i=%d,Q=..(L,R)\n",x,i);QL.print(),QR.print();
		while (QL.Next_front(Lp)-v1>max(QR.front(),Rv[Rp+1])-v2){
			result=min(result,QL.front()-v1);
			Lp++,QL.pop(Lp);
			Rp++,QR.push(Rv[Rp],Rp);
		}
//		printf("x=%d,i=%d,Q=..(L,R)\n",x,i);QL.print(),QR.print();
		result=min(result,QL.front()-v1);
		result=min(result,max(QR.front(),Rv[Rp+1])-v2);
		QL.push(Lv[i],i);
		QR.pop(i+1);
		Far[cir[ID][i%n]]=result;
	}
	for (int i=0;i<n;i++){
		int u=cir[ID][i];
		int m=son[u].size();
		Lmax[0]=Rmax[m+1]=0;
		for (int j=1;j<=m;j++)
			Lmax[j]=max(Lmax[j-1],Deep[son[u][j-1]]+1);
		for (int j=m;j>=1;j--)
			Rmax[j]=max(Rmax[j+1],Deep[son[u][j-1]]+1);
/*		printf("u=%d,son=:",u);
		for (int j=0;j<m;j++)
			printf("%d ",son[u][j]);
		for (int i=1;i<=m;i++)
			printf ("(%d,%d) ",Lmax[i],Rmax[i]);puts("");*/
		LRmax[u].clear();
		for (int j=0;j<m;j++)
			LRmax[u].push_back(max(Lmax[j],Rmax[j+2]));
		for (int j=0;j<m;j++){
//			printf("u=%d,j=%d,LRmax=%d\n",u,j,LRmax);
/*			if (i==0)
				Solve(son[u][j],max(Far[x],LRmax[x][j])+1);
			else*/
				Solve(son[u][j],max(spDeep[u],max(Far[u],LRmax[u][j])))+1;
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	g.clear();
	for (int i=1,a,b;i<=m;i++){
		scanf("%d%d",&a,&b);
		g.add(a,b);
		g.add(b,a);
	}
	Tarjan_Prepare();
	for (int i=1;i<=n;i++)
		if (!vis[i])
			Tarjan(i);
	memset(vis,0,sizeof vis);
//	for (int i=1;i<=n;i++)
//		printf("%d: %d\n",i,id[i]);
	build(1,0);
	Get_Deep(1);
	Solve(1,0);
/*	for (int i=1;i<=tot;i++,puts(""))
		for (int j=0;j<cir[i].size();j++)
			printf("%d ",cir[i][j]);
	for (int i=1;i<=n;i++){
		printf("%d:",i);
		for (int j=0;j<son[i].size();j++)
			printf(" %d",son[i][j]);
		puts("");
	}
	for (int i=1;i<=n;i++)
		printf("%d: %d %d %d %d\n",i,Deep[i],spDeep[i],usDeep[i],Far[i]);*/
	for (int i=1;i<=n;i++)
		printf("%d ",max(Far[i],Deep[i]));
	return 0;
}


/*
It was gone before you ever had a chance
lost forever in your disapproving glance

I see your face
it's twisted with shock
falling from grace
you deserve what you got

The ice surrounds you now

You can't pretend
all that you want
I've seen the pain
and all the evil you brought

Quit your fucking crying
and swallow all your pills
your not the only one
with the urge to kill
grab the dusty bottle
and let the liquor spill
devoid of life
death is your final thrill

Bloodshot eyes
can see right through the selfish lies
search forever for a fix that keeps you high

I see your face
it's twisted with shock
falling from grace
you deserve what you got

My hands around your throat

You can't pretend
all that you want
I've seen the pain
and all the evil you brought

I am the king of nothing
over emptiness I reign
drown the sorrow
wash away the pain
*/

//#pragma comment(linker, "/STACK:16777216")
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define  INF 100000000
#define eps 1e-11
//#define M_PI 3.141592653589793
//#define mx 1000000000000ll
#define bs 1000000007
#define bsize 256

using namespace std;

long long n,k,a,b,need;
vector<pair<long long, long long> > v;
vector<long long> v1,v2;
long long ans,alost,rem,todel,cost;

int main(){
//freopen("melman.in","r",stdin);
//freopen("melman.out","w",stdout);
//freopen("C:/input.txt","r",stdin);
//freopen("C:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);

cin>>n>>k;
for (int i=1;i<=n;i++)
{
 cin>>a>>b;
 v.push_back(make_pair(a,b));
}

sort(v.begin(),v.end());
reverse(v.begin(),v.end());

need=v[k-1].first;

ans=1e18;

for (int score=need;score<=need+10;score++)
{
 alost=0;
 if (score>n)continue;
 for (int p=0;p<v.size();p++)
 if (v[p].first>score){alost++;}
 rem=k-alost-1;
 if(rem<0)continue;
 v1.clear();
 v2.clear();
 for (int i=0;i<v.size();i++)
 {
  if (v[i].first>score||v[i].first<score-1)v1.push_back(v[i].second);
  else v2.push_back(v[i].second);
 }
 sort(v1.begin(),v1.end());
 sort(v2.begin(),v2.end());
 if (v2.size()>rem)
 {
  todel=v2.size()-rem;
 }
 else todel=0;
 cost=0;
 if (todel>score)continue;
 for (int i=0;i<todel;i++)cost+=v2[i];
 for (int i=todel;i<v2.size();i++)v1.push_back(v2[i]);
 sort(v1.begin(),v1.end());
 for (int i=todel+1;i<=score;i++)
 cost+=v1[i-todel-1];
 ans=min(ans,cost);
}
if (ans>1e16)ans=-1;

cout<<ans<<endl;

cin.get();cin.get();
return 0;}


#include<bits/stdc++.h>

using namespace std;

//--------------------------------------------
#define SIZE(x) (x.size())
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define ALL(X) X.begin(), X.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define FILL(x, value) memset(x, value, sizeof(x))

#define next next1
#define hash hash1
#define rank rank1
#define count count1

#ifdef _DEBUG_
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

#if((_WIN32 || __WIN32__) && __cplusplus < 201103L)
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif

const double EPS = 1e-9;
const int IINF = 1000000000;
const double PI = acos(-1.0);
const long long LINF = 6000000000000000000LL;
//--------------------------------------------

const int maxN = 200 * 1000 + 7;

int n, k, E[maxN], P[maxN], CP[maxN], W[maxN];

bool Read() {
    if(scanf("%d%d", &n, &k) == 2) {
        for(int i = 0; i < n; ++i) {
            scanf("%d%d", P + i, E + i);
        }
        return true;
    }
    return false;
}

bool cmp(int i, int j) {
    if(i == n || j == n) {
        if(i == n) {
            if(CP[i] != CP[j]) return CP[i] > CP[j];
            if(W[j]) return true;
            return false;
        }
        else if(j == n) {
            if(CP[i] != CP[j]) return CP[i] > CP[j];
            if(W[i]) return false;
            return true;
        }
    }
    if(CP[i] != CP[j])return CP[i] > CP[j];
    return i > j;
}

long long can(int mask) {
    vector< pair<int, int> > v;
    v.reserve(n);
    for(int i = 0; i < n; ++i) {
        v.pb(mp(E[i], P[i]));
    }
    SORT(v);
    FILL(W, 0);
    FILL(CP, 0);
    long long ans = 0;
    for(int i = 0; i < v.size(); ++i) {
        if(mask & (1 << i)) {
            ans += v[i].first;
            CP[n]++;
            W[i] = 1;
            CP[i] = v[i].second;
        }
        else {
            CP[i] = v[i].second + 1;
        }
    }
    vector<int> srt;
    for(int i = 0; i < n; ++i) {
        srt.pb(i);
    }
    srt.pb(n);
    sort(srt.begin(), srt.end(), cmp);
    for(int i = 0; i < srt.size(); ++i) {
        if(srt[i] == n) {
            if(i + 1 <= k) {
                return ans;
            }
            return LINF;
        }
    }
    return LINF;
}

int main() {
#ifdef _DEBUG_
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    while(Read()) {
        long long ans = LINF;
        for(int mask = 0; mask < (1 << n); ++mask) {
            ans = min(ans, can(mask));
        }
        if(ans >= LINF) printf("-1\n");
        else printf(LLD"\n", ans);
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include set
#include map
#include stack
#include queue
#include math.h
#include vector
#include string
#include stdio.h
#include sstream
#include cstdlib
#include string.h
#include limits.h
#include iostream
#include algorithm
#include functional
#include bitsstdc++.h
using namespace std;

#define M
#define N 1e9
#define Fn i.txt
#define mp make_pair
#define si(a) scanf(%d,&a)
#define sc(a) scanf(%c,&a)
#define sst(s) scanf(%s,s)
#define fo(a) freopen(a,r,stdin)
#define si2(a,b) scanf(%d%d,&a,&b)
#define rep(i,a,b) for(int i=a;ib;++i)
#define Set(a,b) memset(a,b,sizeof a)
#define si3(a,b,c) scanf(%d%d%d,&a,&b,&c)
#define lli long long int
#define llu unsigned long long

vectorpairint, int list;
int stat[20];
int points[20];
int n, m;

int dp(int i,int p,int e)
{
    if (i == list.size())
    {
        int res = n+1;
        for (int j = 0; j  list.size(); ++j)
        {
            if (ppoints[j])
                res--;
            else if (p == points[j] && stat[j] == 1)
                res--;
        }
        if (res = m)
            return e;
        else
            return N;
    }

    stat[i] = 1;
    int s1 = dp(i + 1, p + 1, e + list[i].second);

    stat[i] = 0;
    points[i] += 1;
    int s2 = dp(i + 1, p, e);

    points[i] -= 1;

    return min(s1, s2);
}
int main()
{
    fo(Fn);
    int a, b;
    si2(n, m);
    rep(i, 0, n)
    {
        si2(a, b);
        points[i] = a;
        list.push_back(mp(a, b));
    }

    int ans = dp(0,0,0);

    if (ans == N)
        cout  -1  endl;
    else
    cout  ans  endl;
    return 0;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

int n, k;
pair <int, int> a[300000], b[300000], r[300000];
bool flag[300000];

int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].second >> a[i].first;
		b[i].first = a[i].second;
		b[i].second = a[i].first;
	}
	sort(b + 1, b + n + 1);
	sort(a + 1, a + n + 1);
	reverse(b + 1, b + n + 1);
	int ans = 0;
	bool q = false;
	int kol = 0;
	int p;
	//for (int i = 1; i <= b[k].first + q; i++)
	int i = 1;
	while (1)
	{
		if (i > n)
		{
			cout << -1;
			return 0;
		}
		flag[i] = true;
		ans += a[i].first;
		//if (a[i].second == b[k].first && a[i].first > maxi)
		//{
		//	maxi = a[i].first;
		//	p = i;
		//}
		kol++;
		//if (i == 4)
		//	cout << kol << endl;
		//if (kol == b[k].first)
		//{
			int f = 0;
			for (int j = 1; j <= n; j++)
				if (!flag[j] && a[j].second + 1 >= kol || flag[j] && a[j].second > kol)
					f++;
			//int q = 0;
			//for (int j = 1; j <= n; j++)
			//	if (a[j].second == kol - 1 && !flag[j])
			//		q++;
			if (f + 1 <= k)
			{
				int maxi = 0, l = 0;
				for (int j = 1; j <= n; j++)
				{
				//	if (i == 4)
				//		cout << a[j].second << " " << a[j].first << " " << flag[j] << " " << kol << endl;
					if (flag[j] && kol - 1 > a[j].second + 1 && a[j].first > maxi)
					{
						l++;
						r[l].first = a[j].second;
						r[l].second = j;
						maxi = a[j].first;
					}
				}
				sort(r + 1, r + l + 1);
				int start = 1, e = 0;
				while (kol - start > r[start].first + 1 && start <= l)
				{
					int d = 0;
					for (int p = 1; p <= l; p++)
						if (kol - start > r[p].first + 1)
							d = max(d, a[r[p].second].first;
					start++;
					e += a[r[start].second].first;
				}
				cout << ans - max(e, maxi);
				return 0;
			}
			//q = true;
		//}
		//if (b[k].first + q > n)
		//{
		//	cout << -1;
		//	return 0;
		//}
		//if (kol >= b[k].first + q)
		//{
		//	cout << ans;
		//	return 0;
		//}
		i++;
	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

const int mod = (int)1e9+7;
const int N = 205;

struct number {
	int x, y;

	friend number operator + (const number& a, const number& b) {
		return {(a.x + b.x) % mod, (a.y + b.y) % mod};
	}

	friend number operator - (const number& a, const number& b) {
		return {(a.x - b.x + mod) % mod, (a.y - b.y + mod) % mod};
	}

	friend number operator * (const number& a, const number& b) {
		return {(int)((1LL*a.x*b.x+1LL*a.y*b.y*5) % mod), (int)((1LL*a.x*b.y+1LL*a.y*b.x) % mod)};
	}
};

int power(int a, int n) {
	int result = 1;
	while (n > 0) {
		if (n & 1) result = (1LL * result * a) % mod;
		a = (1LL * a * a) % mod;
		n /= 2;
	}
	return result;
}

number power(number a, int n) {
	number result = {1, 0};
	while (n > 0) {
		if (n & 1) result = result * a;
		a = a * a;
		n /= 2;
	}
	return result;
}

int inv(int a) {
	return power(a, mod - 2);
}

int brute(int n, int p) {
	vector<int> f = {0, 1};
	int sum = 0;
	for (int i = 2; i <= n; ++i) {
		f.push_back((f[i-1]+f[i-2])%mod);
	}
	for (int i = 0; i <= n; ++i) {
		sum = (sum + power(f[i], p)) % mod;
	}
	return sum;
}

int fact[N], ifact[N];
int s[N][N];

int c(int n, int k) {
	if (k < 0 || k > n) return 0;
	int result = fact[n];
	result = (1LL * result * ifact[n - k]) % mod;
	result = (1LL * result * ifact[k]) % mod;
	return result;
}

void precalc() {
	fact[0] = ifact[0] = 1;
	for (int i = 1; i < N; ++i) {
		fact[i] = (1LL * fact[i - 1] * i) % mod;
		ifact[i] = inv(fact[i]);
	}
	s[0][0] = 1;
	for (int i = 1; i < N; ++i) {
		for (int j = 1; j <= i; ++j) {
			s[i][j] = (1LL * (i-1) * s[i-1][j] + s[i-1][j-1]) % mod;
		}
	}
	/*for (int i = 0; i <= 5; ++i) {
		cout << "For: " << i << endl;
		for (int j = 0; j <= i; ++j) {
			cout << s[i][j] << " ";
		}
		cout << endl;
	}*/
}

number getSum(int l, int r, number n) {
	if (l == r) {
		return power(n, l);
	}
	int mid = (l + r) / 2;
	int s1 = (mid - l + 1);
	int s2 = (r - mid);
	number result = getSum(l, mid, n);
	result = result + power(n, s1) * result;
	if (s1 != s2) {
		result = result - power(n, r+1);
	}
	return result;
}

int countFast(int l, int n, int p) {
	number f1 = {inv(2), inv(2)};
	number f2 = {inv(2), mod-inv(2)};
	number result = {0, 0};
	for (int i = 0; i <= p; ++i) {
		number mul = power(f1, p - i) * power(f2, i);
		number sum = getSum(l, n, mul);
		if (i & 1) {
			sum.x = mod - sum.x;
			sum.y = mod - sum.y;
		}
		number cpi = {c(p, i), 0};
		result = result + cpi * sum;
	}
	number mul = {0, inv(5)};
	result = result * power(mul, p);
	return result.x;
}

int computeFast(int l, int r, int p) {
	return countFast(l, r, p);
}

int computeResult(int l, int r, int p) {
	l += 2;
	r += 2;
	vector<int> f = {0, 1};
	for (int i = 2; i <= 20; ++i) {
		f.push_back(f[i-1]+f[i-2]);
	}
	while (l < 20 && f[l] < p) ++l;
	int result = 0;
	int sign = 0;
	for (int i = p; i >= 0; --i) {
		int now = (1LL * s[p][i] * computeFast(l, r, i)) % mod;
		if (sign) now = mod - now;
		now = (1LL * now * ifact[p]) % mod;
		result = (result + now) % mod;
		sign ^= 1;
	}
	return result;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	precalc();

	int k, l, r;
	cin >> k >> l >> r;
	cout << computeResult(l, r, k) << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int P = 1e9+7;
typedef long long ll;
const int N =233;
const int inv = 5e8+4;
int inv5;
class irrational{
	public:
	int a,b;
	irrational(int A = 0,int B = 0):a(A),b(B){}
	friend irrational operator +(irrational A,irrational B){
		return irrational((A.a+B.a)%P,(A.b+B.b)%P);
	}
	friend irrational operator *(irrational  A,irrational B){
		return irrational((1ll*A.a*B.a+5ll*A.b*B.b)%P,(1ll*A.a*B.b+1ll*B.a*A.b)%P);
	}
	friend irrational operator *(irrational & A,int B){
		return irrational(1ll*A.a*B%P,1ll*A.b*B%P);
	}
};//  for sqrt(5)
typedef irrational Type;
int k,ans;
ll l,r;
int c[N],tmp[N],com[N][N];
Type M[3][3],G[3][3],Tmp[3][3];
inline void add(int &x,int y) {x=(x+y)%P;}
int Pow(int a,int b){
	if(b==0) return 1;
	int ret = Pow(a,b>>1);
	ret=1ll*ret*ret%P;
	if(b&1) ret=1ll*ret*a%P;
	return ret;
}
Type Pow(Type a,int b){
	if(b==0) return Type(1,0);
	Type ret = Pow(a,b>>1);
	ret=ret*ret;
	if(b&1) ret=ret*a;
	return ret;
}
void Pow(int n){
	if(n==0) {
		G[0][0]=G[1][1]=Type(1,0);
		G[0][1]=G[1][0]=Type(0,0);
		return ;
	}
	Pow(n>>1);
	for(int i=0;i<2;i++) for(int j=0;j<2;j++){
		Tmp[i][j]=Type(0,0);
		for(int k=0;k<2;k++) Tmp[i][j] =(Tmp[i][j]+G[i][k]*G[k][j]);
	}
	memcpy(G,Tmp,sizeof(G));
	if(n&1){
		for(int i=0;i<2;i++) for(int j=0;j<2;j++){
			Tmp[i][j]=Type(0,0);
			for(int k=0;k<2;k++) Tmp[i][j] =(Tmp[i][j]+G[i][k]*M[k][j]);
		}
		memcpy(G,Tmp,sizeof(G));
	}
}
void work(int r,int coefficient){
	for(int m=0;m<=k;m++){
		Type res(0,0);
		for(int j=0;j<=m;j++){
			Type q = Pow(Type(inv,inv),j) * Pow(Type(inv,-inv),m-j);
			//cout<<q.a<<' '<<q.b<<endl;
			M[0][0]=q,M[0][1]=Type(0,0),M[1][0]=q,M[1][1]=Type(1,0);
			Pow(r);
			//cout<<(G[1][0]+G[1][1]).a<<' '<<(G[1][0]+G[1][1]).b<<endl;
			res = res + ((m-j)&1?-1:1)*com[m][j]*(G[1][0]+G[1][1]);
			//cout<<res.a<<' '<<res.b<<endl;
		}
		res = res *Pow(Type(0,inv5),m);
		add(ans,1ll*coefficient*c[m]*res.a%P);
	}
	//cout<<ans<<endl;
}

int main(){
	inv5 = Pow(5,P-2);
	for(int i=0;i<N;i++){
		com[i][0]=1;
		for(int j=1;j<=i;j++) com[i][j]=(com[i-1][j]+com[i-1][j-1])%P;
	}
	cin>>k>>l>>r;
	c[0]=1;
	for(int i=0;i<k;i++){
		tmp[0] = -1ll*i*c[0]%P;
		for(int j=1;j<N;j++) tmp[j] = (c[j-1]-1ll*i*c[j])%P;
		memcpy(c,tmp,sizeof(c));
	}
	//cout<<c[0]<<' '<<c[1]<<' '<<c[2]<<endl;
	work(r+2,1),work(l+1,-1);
	for(int i=2;i<=k;i++) ans=1ll*ans*Pow(i,P-2)%P;
	ans = (ans+P)%P;
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cassert>
#define ri rd<int>
#define rl rd<LL>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++ i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; -- i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++ i)
typedef long long LL;
using namespace std;
const LL O = 1000000007;
const int M = 200;
const int maxM = 207;

template<class T> T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x;
}

inline void Add(LL &x, LL y) { if ((x += y) >= O) x -= O; }
inline void Mul(LL &x, LL y) { x = x * y % O; }

LL a[maxM];
LL iv[maxM], ifac[maxM], fac[maxM];

void init() {
	iv[1] = 1; rep (i, 2, M) iv[i] = iv[O % i] * (O - O / i) % O;
	fac[0] = 1; rep (i, 1, M) fac[i] = fac[i - 1] * i % O;
	ifac[0] = 1; rep (i, 1, M) ifac[i] = ifac[i - 1] * iv[i] % O;
}

void pre(LL *a, int m) {
	a[0] = 1;
	For (i, 0, m) {
		per (j, i, 0) {
			Add(a[j+1], a[j]);
			Mul(a[j], O-i);
		}
	}
	rep (i, 0, m) Mul(a[i], ifac[m]);
}

inline LL C(int x, int y) {
	return 0 <= y && y <= x ? fac[x] * ifac[y] % O * ifac[x - y] % O : 0;
}

inline LL sgn(int x) { return x & 1 ? -1 : 1; }

namespace calc {

	struct Rec {
		LL a, b;
		Rec(LL _a = 0, LL _b = 0):a(_a), b(_b) { }
		inline Rec operator + (const Rec &t) const { return Rec((a + t.a) % O, (b + t.b) % O); }
		inline Rec operator - (const Rec &t) const { return Rec((a - t.a) % O, (b - t.b) % O); }
		inline Rec operator * (const Rec &t) const { return Rec((a * t.a + b * t.b * 5) % O, (a * t.b + b * t.a) % O); }
		inline Rec operator * (const LL d) const { return Rec(a * d % O, b * d % O); }
		inline void dec() { swap(a, b); Mul(b, iv[5]); } //  divide sqrt(5)
		inline friend Rec pwd(Rec x, LL tms) {
			Rec res = Rec(1);
			for (; tms > 0; tms >>= 1, x = x * x)
				if (tms & 1) res = res * x;
			return res;
		}
	};

	Rec F(Rec x, LL l, LL r) {
		if (l == r) { assert(l == 0); return Rec(1); }
		int mid = (l + r - 1) / 2;
		Rec lf = F(x, l, mid);
		Rec rt = lf * pwd(x, mid-l+1);
		if (mid-l+1 != r-mid) rt = rt + pwd(x, r);
		return lf + rt;
	}

	Rec S(LL L, LL R, int a, int b) {
		if (a == 0 && b == 0) return Rec(R - (L - 1));
		Rec t = pwd(Rec(iv[2], iv[2]), a) * pwd(Rec(iv[2], O - iv[2]), b);
		return F(t, 0, R) - F(t, 0, L-1);
	}

	LL main(int i, LL L, LL R) {
		Rec res(0, 0);
		rep (j, 0, i)
			res = res + S(L, R, j, i-j) * C(i, j) * sgn(i - j);
		while (i--) res.dec();
		assert(res.b == 0);
		return res.a;
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif

	int m = ri();
	LL l = rl() + 2, r = rl() + 2;
	init();
	pre(a, m);

	LL res = 0;
	rep (i, 0, m) {
		LL tmp = calc::main(i, l, r);
		Mul(tmp, a[i]);
		Add(res, tmp);
	}
	printf("%I64d\n", (res % O + O) % O);

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

namespace math {

template<typename V = int>
inline void slightFixModInline(V& v, const V& mod) {
  if (v < 0) {
    v += mod;
  }
  if (v >= mod) {
    v -= mod;
  }
}

} // namespace math

namespace math {

template<typename V = int>
inline void fixModInline(V& v, const V& mod) {
  slightFixModInline<V>(v, mod);
  if (v < 0 || mod <= v) {
    assert(mod);
    v %= mod;
    slightFixModInline<V>(v, mod);
  }
}

} // namespace math

namespace math {

template<typename V = int>
inline V addMod(const V& a, V b, const V& mod) {
  V res = a;
  fixModInline<V>(res, mod);
  fixModInline<V>(b, mod);
  res += b;
  slightFixModInline<V>(res, mod);
  return res;
}

} // namespace math

namespace math {

template<typename V = int32_t>
inline V extGcd(V a, V b, V& x, V& y) {
  bool negativeA = false;
  if (a < 0) {
    negativeA = true;
    a = -a;
  }
  bool negativeB = false;
  if (b < 0) {
    negativeB = true;
    b = -b;
  }
  x = 1;
  y = 0;
  V xTmp = 0;
  V yTmp = 1;
  while (b) {
    V q = a / b;
    tie(x, xTmp) = make_tuple(xTmp, x - q * xTmp);
    tie(y, yTmp) = make_tuple(yTmp, y - q * yTmp);
    tie(a, b) = make_tuple(b, a - q * b);
  }
  if (negativeA) {
    x = -x;
  }
  if (negativeB) {
    y = -y;
  }
  return a;
}

} // namespace math

namespace math {

template<typename V = int32_t>
inline V invMod(V a, const V& mod) {
  V x0, x1, gcd = extGcd(a, mod, x0, x1);
  assert(gcd == 1);
  slightFixModInline(x0, mod);
  return x0;
}

} // namespace math

namespace math {

template<typename V = int32_t, typename V_SQR = int64_t>
inline void mulModInline(V& res, V x, const V& mod) {
  if (!res || !x) {
    res = 0;
  } else if (res < mod / x) {
    res *= x;
  } else {
    res = static_cast<V_SQR>(res) * x % mod;
  }
}

} // namespace math

namespace math {

template<typename V = int32_t, typename V_SQR = int64_t>
inline V mulMod(V a, const V& b, const V& mod) {
  mulModInline<V, V_SQR>(a, b, mod);
  return a;
}

} // namespace math

namespace math {

struct CombinationMod {
  inline CombinationMod() {}

  inline CombinationMod(int n, int mod) {
    init(n, mod);
  }

  inline void init(int n, int mod) {
    _mod = mod;
    _facs.clear();
    _facs.reserve(n);
    int j = mod != 1;
    _facs.push_back(j);
    for (int i = 1; i < n; ++i) {
      _facs.push_back(mulMod(_facs.back(), j, mod));
      j = j + 1 == mod ? 0 : j + 1;
    }
    _invFacs.reserve(n);
    _invFacs.resize(n);
    _invFacs[n - 1] = invMod(_facs[n - 1], mod);
    for (int i = n - 1; i > 0; --i) {
      j = !j ? mod - 1 : j - 1;
      _invFacs[i - 1] = mulMod(_invFacs[i], j, mod);
    }
  }

  inline int calc(int n, int m) {
    if (n < m) {
      return 0;
    }
    return mulMod(mulMod(_facs[n], _invFacs[m], _mod), _invFacs[n - m], _mod);
  }

  int _mod;
  vector<int> _facs, _invFacs;
};

} // namespace math

namespace math {

template<typename V = int32_t, typename EXP = int32_t, typename V_SQR = int64_t>
inline V expMod(V base, EXP exp, const V& mod) {
  V res = 1;
  for (; exp > 0; exp >>= 1) {
    if (exp & 1) {
      mulModInline<V, V_SQR>(res, base, mod);
    }
    mulModInline<V, V_SQR>(base, base, mod);
  }
  return res;
}

} // namespace math

namespace math {

template<typename V = int32_t>
inline V fixMod(const V& v, const V& mod) {
  V res = v;
  fixModInline<V>(res, mod);
  return res;
}

} // namespace math

namespace math {

template<typename V = int>
inline V subMod(const V& a, V b, const V& mod) {
  V res = a;
  fixModInline<V>(res, mod);
  fixModInline<V>(b, mod);
  if (res >= b) {
    res -= b;
  } else {
    res = mod - (b - res);
  }
  slightFixModInline<V>(res, mod);
  return res;
}

} // namespace math

namespace math {

namespace {

struct _FibPartMod {
  inline _FibPartMod() {}

  inline _FibPartMod(int a_, int b_, int mod) {
    init(a_, b_, mod);
  }

  inline void init(int a_, int b_, int mod) {
    _mod = mod;
    a = fixMod(a_, mod);
    b = fixMod(b_, mod);
  }

  inline void init(const _FibPartMod& o) {
    init(o.a, o.b, o._mod);
  }

  inline void operator-=(const _FibPartMod& o) {
    a = subMod(a, o.a, _mod);
    b = subMod(b, o.b, _mod);
  }

  inline void operator+=(const _FibPartMod& o) {
    a = addMod(a, o.a, _mod);
    b = addMod(b, o.b, _mod);
  }

  inline void operator-=(int delta) {
    a = subMod(a, fixMod(delta, _mod), _mod);
  }

  inline void operator+=(int delta) {
    a = addMod(a, fixMod(delta, _mod), _mod);
  }

  inline void operator*=(const _FibPartMod& o) {
    int resA = addMod(
        mulMod(a, o.a, _mod),
        mulMod(mulMod(b, o.b, _mod), fixMod(5, _mod), _mod),
        _mod);
    int resB = addMod(mulMod(a, o.b, _mod), mulMod(b, o.a, _mod), _mod);
    a = resA;
    b = resB;
  }

  inline void operator*=(const int scale) {
    a = mulMod(a, fixMod(scale, _mod), _mod);
    b = mulMod(b, fixMod(scale, _mod), _mod);
  }

  int a, b;
  int _mod;
};

} // namespace

struct FibPowerSum {
  inline FibPowerSum(int mod, int expBound = -1) {
    init(mod, expBound);
  }

  inline void init(int mod, int expBound = -1) {
    int inv2 = invMod(2, mod);
    _mod = mod;
    if (expBound >= 0) {
      as_.reserve(expBound);
      bs_.reserve(expBound);
    }
    as_.clear();
    bs_.clear();
    as_.emplace_back(1, 0, mod);
    bs_.emplace_back(1, 0, mod);
    as_.emplace_back(inv2, inv2, mod);
    bs_.emplace_back(inv2, inv2 ? mod - inv2 : 0, mod);
    for (int i = 2; i < expBound; ++i) {
      as_.emplace_back(as_.back());
      as_.back() *= as_[1];
      bs_.emplace_back(bs_.back());
      bs_.back() *= bs_[1];
    }
    _comb.init(expBound, mod);
    _inv5 = invMod(fixMod(5, mod), mod);
    _zeroPart.init(0, 0, mod);
    _onePart.init(fixMod(1, mod), 0, mod);
    _five = fixMod(5, mod);
  }

  template<typename T>
  inline int calc(T n, int exp) {
    _FibPartMod res(0, 0, _mod), base, mul, subRes;
    for (int i = 0; i <= exp; ++i) {
      base.init(as_[i]);
      base *= bs_[exp - i];
      if (base.a == 1 && !base.b) {
        subRes.init(fixMod(n + 1, _mod), 0, _mod);
      } else {
        mul.init(base);
        subRes.init(as_[0]);
        for (T tmpN = n + 1; tmpN; tmpN >>= 1) {
          if (tmpN & 1) {
            subRes *= mul;
          }
          mul *= mul;
        }
        subRes -= 1;
        base -= 1;
        int scale = subMod(
            mulMod(base.a, base.a, _mod),
            mulMod(mulMod(base.b, base.b, _mod), _five, _mod),
            _mod);
        subRes *= invMod(scale, _mod);
        base.b = base.b ? _mod - base.b : 0;
        subRes *= base;
      }
      subRes *= _comb.calc(exp, i);
      if ((exp - i) & 1) {
        res -= subRes;
      } else {
        res += subRes;
      }
    }
    return mulMod(
        (exp & 1) ? res.b : res.a, expMod(_inv5, exp >> 1, _mod), _mod);
  }

  vector<_FibPartMod> as_, bs_;
  CombinationMod _comb;
  _FibPartMod _zeroPart, _onePart;
  int _inv5, _five, _mod;
};

} // namespace math

namespace math {

struct StirlingFirst {
  inline StirlingFirst(int n, int mod) {
    init(n, mod);
  }

  inline void init(int n, int mod) {
    _mod = mod;
    _us.resize(n);
    int one = fixMod(1, mod);
    if (n > 0) {
      _us[0].assign(1, one);
    }
    for (int i = 1; i < n; ++i) {
      _us[i].resize(i + 1);
      _us[i][0] = 0;
      int i1 = fixMod(i - 1, mod);
      for (int j = 1; j < i; ++j) {
        _us[i][j] =
            addMod(mulMod(i1, _us[i - 1][j], mod), _us[i - 1][j - 1], mod);
      }
      _us[i][i] = one;
    }
  }

  inline int calcUnsigned(int n, int k) {
    return n >= k ? _us[n][k] : 0;
  }

  inline int calcSigned(int n, int k) {
    int res = calcUnsigned(n, k);
    if (res && ((n - k) & 1)) {
      res = _mod - res;
    }
    return res;
  }

  vector<vector<int>> _us;
  int _mod;
};

} // namespace math

const int MOD = 1000000007;
const int MAXEXP = 200 + 1;

math::FibPowerSum fibPowSum(MOD, MAXEXP);
math::StirlingFirst stirling(MAXEXP, MOD);

inline int calc(int exp, long long n) {
  int res = 0;
  for (int i = 0; i <= exp; ++i) {
    res = math::addMod(
        res,
        math::mulMod(stirling.calcSigned(exp, i), fibPowSum.calc(n, i), MOD),
        MOD);
  }
  res = math::mulMod(res, fibPowSum._comb._invFacs[exp], MOD);
  return res;
}

int main() {
  int exp;
  long long lower, upper;
  scanf("%d%lld%lld", &exp, &lower, &upper);
  printf("%d\n", math::subMod(calc(exp, upper + 2), calc(exp, lower + 1), MOD));
  return 0;
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

#include <bits/stdc++.h>

using namespace std;

//#define MULTITEST

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = int(1e9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

int nn, k;

bool read () {
	if (scanf("%d%d", &nn, &k) != 2)
		return false;
	return true;
}

void solve() {
	n = nn / k;
	vector<char> un(nn, true);
	char buf[10];
	forn(x, n) forn(y, n) if (x != y || n == 1){
		forn(i, k) if (un[x * k + i]){
			printf("? %d\n", x * k + i + 1);
			fflush(stdout);
			scanf("%s", buf);
			un[x * k + i] &= buf[0] == 'N';
		}
		if (n != 1){
			forn(i, k) if (un[y * k + i]){
				printf("? %d\n", y * k + i + 1);
				fflush(stdout);
				scanf("%s", buf);
				un[y * k + i] &= buf[0] == 'N';
			}
		}
		puts("R");
		fflush(stdout);
	}
	printf("! %d\n", int(count(all(un), true)));
	fflush(stdout);
}

int main() {
#ifdef _DEBUG
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

#ifdef MULTITEST
	int tc;
	scanf("%d", &tc);
	while(tc--){
		read();
#else
	while(read()) {
#endif
		solve();

#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}


#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
bool ask(int val) {
	cout << "? " << val + 1 << endl;
	string resp;
	cin >> resp;
	return resp[0] == 'Y' ? true : false;
}
void fix() {
	cout << "R" << endl;
}
int main() {
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	//cout.tie(0);
	int n, k;
	cin >> n >> k;
	vector<bool>check(n + 1, 0);
	int block = k % 2 ? 1 : k / 2;
	for (int blockno = 1; blockno < n / block; blockno += 1) {
		for (int j = 0; j < blockno && (blockno + j < n / block); j += 1) {
			for (int k = j; k < n / block; k += blockno) {
				for (int a = 0; a < block; a += 1) {
					if (ask(k * block + a)) {
						check[k * block + a + 1] = true;
					}
				}
			}
			fix();
		}
	}
	int res = 0;
	for (int i = 1; i <= n; i += 1) {
		if (check[i] == 0) {
			res += 1;
		}
	}
	cout << "! " << res << endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#define ll long long
using namespace std;
int read() {
	int k=0,f=1;
	char c=getchar();
	for(; !isdigit(c); c=getchar()) if(c=='-') f=-1;
	for(; isdigit(c); c=getchar()) k=k*10+c-'0';
	return k*f;
}
const int N=1550;
int n,m,cnt[N];
char s;
vector<int> V;
int main() {
	cin>>n>>m;
	int now=0;
	for(now=1;now<=m&&now<=n;now++){
		cout<<"? "<<now<<endl;
		fflush(stdout);
		cin>>s;
		if(s=='N') V.push_back(now);
	}
	cout<<"R\n";
	fflush(stdout);
	if(m==1) {
		for(int i=now; i<=n; i++) {
			cout<<"? "<<i<<endl;
			fflush(stdout);
			cin>>s;
			int fl=1,now=0;
			for(int j=0; j<V.size(); j++) {
				now++;
				cout<<"? "<<V[j]<<endl;
				fflush(stdout);
				cin>>s;
				if(s=='Y') {
					fl=0;
					break;
				}
				if(now==m) {
					cout<<"R\n";
					fflush(stdout);
					cout<<"? "<<i<<endl;
					fflush(stdout);
					cin>>s;
					now=0;
				}
			}
			if(fl) V.push_back(i);
			cout<<"R\n";
			fflush(stdout);
		}
	} else {
		int zzz=m/
		for(int i=now; i<=n; i+=m/2) {
			int len=min(n,i+m/2-1)-i+1;
			for(int j=0; j<len; j++)
				cnt[j]=0;
			int Len,minn=0x3f3f3f3f;
			for(int j=1;j<=m/2+1;j++)
			{
				int w=(V.size()+j-1)/j*(len+j);
				if(w<minn) minn=w,Len=j;
			}
			for(int j=0; j<V.size(); j+=Len) {
				for(int k=0; k<min((int)(V.size())-1,j+Len-1)-j+1; k++) {
					cout<<"? "<<V[k+j]<<endl;
					fflush(stdout);
					cin>>s;
				}
				for(int k=0; k<len; k++) {
					cout<<"? "<<i+k<<endl;
					fflush(stdout);
					cin>>s;
					if(s=='Y')
						cnt[k]++;
				}
				cout<<"R\n";
				fflush(stdout);
			}
			for(int j=0; j<len; j++)
				if(!cnt[j])
					V.push_back(i+j);
		}
	}
	cout<<"! "<<V.size()<<endl;
	fflush(stdout);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool good[1500]={0};
void reset()
{
	cout<<"R"<<endl;
	cout.flush();
}
bool ask(int x)
{
	cout<<"? "<<x<<endl;
	cout.flush();
	char c;
	cin>>c;
	if(c=='Y') return true;
	return false;
}
int main()
{
	int n,k;
	char c;
	cin>>n>>k;
	for(int i = 1 ; i <= n ; i++)
		good[i]=1;
	else if(k==1)
	{
		for(int i = 1 ; i <= n ; i++)
			for(int j = i+1 ; j <= n ; j++)
			{
				reset();
				ask(i);
				if(ask(j)) good[j]=0;
			}
		int ans=0;
		for(int i = 1 ; i <= n ; i++)
			if(good[i]) ans++;
		cout<<"! "<<ans<<endl;
	}
	else
	{
		int tmp=n/k*2;
		int t=n/tmp;
		int ans=0;
		for(int i = 0 ; i < tmp ; i++)
		{
			for(int j = i+1 ; j < tmp ; j++)
			{
				reset();
				for(int k = 1 ; k <= t ; k++)
				{
					if(ask(t*i+k))
					{
						good[t*i+k]=0;
					 }
				}
				for(int k = 1 ; k <= t ; k++)
				{
					if(ask(t*j+k))
					{
						good[t*j+k]=0;
					}
				}
			}
		}
		for(int i = 1 ; i <= n ; i++)
		{
//			cout<<good[i];
			if(good[i]) ans++;
		}
		cout<<"! "<<ans<<endl;
	}
	return 0;
}


// Focus On Boundries
#include <set>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string Dir;
long long n,m,x,y,Dirx,Diry,ODirx,ODiry,paint=1,MOves=100000000000;
set<pair<long,long long>>S;
void ReFlect(){
    if(x == n){
        Dirx  = -1;
    }else
        if(x == 1)
            Dirx  = 1;
    if(y == m){
        Diry  = -1;
    }else
        if(y == 1)
            Diry = 1;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin >> n >> m;
    int xs,ys;
    cin >> xs >> ys;
    x=xs,y=ys;
    cin >> Dir;
//  cout << x << " " << y << endl;
    if(Dir[0]=='D')
        Dirx=1;
    if(Dir[1]='R')
        Diry=1;

    ReFlect();
    ODirx=Dirx;
    ODiry=Diry;
//  cout << ODirx << " " << ODiry << endl;
    for(;;){
        MOves=100000000000;
        S.insert(make_pair(x,y));
        if(Dirx==1){
            MOves=min(MOves,n-x);
        }else
            MOves=min(MOves,x-1);
        if(Diry==1){
            MOves=min(MOves,m-y);
        }else
            MOves=min(MOves,y-1);

        x+=(MOves*Dirx);
        y+=(MOves*Diry);
        ReFlect();
        if(S.size()==n+m-2)
            break;
        if(x==xs && y==ys && Dirx==ODirx && Diry==ODiry)
                break;

        paint+=MOves;

    }

    if(S.size()==n+m-2){
        cout << paint;
    }else
        cout << -1;

//  system("pause");
}


#include <bits/stdc++.h>
using namespace std;

int main(){
    int n,m,x,y;
    cin >> n >> m >> x >> y;
    set < pair < int , int > > st;
    char cm[2];
    cin >> cm;
    int fx , fy ;
    if (cm[0] == 'U'){
        fx = -1;
    }else{
        fx = 1;
    }
    if (cm[1] == 'L'){
        fy = -1;
    }else{
        fy = 1;
    }
    long long ans = 1;
    for (int i=0;i<=n + n + m + m;i++){
        //cout << x << ' ' << y << ' ' << fx << ' ' << fy << endl;
        st.insert( pair < int , int > ( x , y ) );
        if ((int)st.size() == n + m - 2){
            cout << ans ;
            return 0;
        }
        int ga , gb;
        if (fx == -1){
            ga = x - 1;
        }else{
            ga = n - x;
        }
        if (fy == -1){
            gb = y - 1;
        }else{
            gb = m - y;
        }
        //cout << ga << ' ' << gb << endl;
        int move = min(ga,gb);
        x += fx * move;
        y += fy * move;
        ans += 1LL * move;
        if (x == 1 || x == n){
            fx = -fx;
        }
		if (y == 1 || y == m){
            fy = -fy;
        }
    }
    cout << -1 ;
    return 0;
}


#include <iostream>

using namespace std;

string dirS[] = { "UL", "UR", "DL", "DR"};
int dx[] = {-1, -1, 1, 1};
int dy[] = {-1, 1, -1, 1};
bool mark[500010][10], mark2[500010];
bool good[500010];
int n, m;
int hash(int x, int y)
{
    if (x == 0)
        return y;
    if (x == n - 1)
        return m + y;
    if (y == 0)
        return x + 2 * m;
    if (y == m - 1)
        return x + n + 2 * m;

}
int main()
{

    cin >> n >> m;
    int x, y;
    cin >> x >> y;
    x--, y--;
    string dirStr;
    cin >> dirStr;
    int dir;
    for (int i = 0; i < 4; i++)
        if (dirS[i] == dirStr)
            dir = i;
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        if ((i - x + y) % 2 == 0)
        {
            cnt++;
            good[hash(i, 0)] = true;
        }
        if ((i - x + y - (m - 1)) % 2 == 0)
        {
            cnt++;
            good[hash(i, m - 1)] = true;
        }
    }
    for (int i = 1; i < m - 1; i++)
    {
        if ((i - y + x) % 2 == 0)
        {
            cnt++;
            good[hash(0, i)] = true;
        }
        if (i != m - 1 && (i - y + x - (n - 1)) % 2 == 0)
        {
            cnt++;
            good[hash(n - 1, i)] = true;
        }
    }
    long long ans = 1;
    if (x + dx[dir] >= n || x + dx[dir] < 0)
        dir = (dir + 2) % 4;
    if (y + dy[dir] >= m || y + dy[dir] < 0)
        dir = (dir + 3) % 4;
    while (!mark[hash(x, y)][dir] && cnt > 0)
    {
        int nextX, nextY;
        if (!mark2[hash(x, y)])
            cnt--;
        mark2[hash(x, y)] = mark[hash(x, y)][dir] = true;
        if (!good[hash(x, y)])
        {
            cout << -1 << endl;
            return 0;
        }

        if (dx[dir] == 1)
            nextX = n - 1;
        else
            nextX = 0;
        if (dy[dir] == 1)
            nextY = m - 1;
        else
            nextY = 0;
        int diff = min(abs(nextX - x), abs(nextY - y));
        ans += diff;
        x += diff * dx[dir];
        y += diff * dy[dir];
        if (x + dx[dir] >= n || x + dx[dir] < 0)
            dir = (dir + 2) % 4;
        if (y + dy[dir] >= m || y + dy[dir] < 0)
            dir = (dir + 3) % 4;
    }
    if (cnt != 0)
        ans = -1;
    cout << ans << endl;
}


var
  map:array[1..400000,-1..1,-1..1]of boolean;
  n,m,x,y,dx,dy,k,ans,kk,t:longint;
  str:string;
begin
  readln(n,m);
  read(x,y);
  readln(str);
  if str[2]='D' then dx:=1
    else dx:=-1;
  if str[3]='R' then dy:=1
    else dy:=-1;
  repeat
    if (x=1)and(y<m) then t:=y;
    if (x<n)and(y=m) then t:=m+x-1;
    if (x=n)and(y>1) then t:=m+n+y-3;
    if (x>1)and(y=1) then t:=2*m+n+x-4;
    if map[t,dx,dy] then break;
    if not map[t,0,0] then inc(kk);
    if kk=n+m-2 then break;
    map[t,dx,dy]:=true;
    map[t,0,0]:=true;
    k:=maxlongint;
    if dx=1 then k:=min(k,n-x)
      else k:=min(k,x-1);
    if dy=1 then k:=min(k,m-y)
      else k:=min(k,y-1);
    ans:=ans+k;
    x:=max(1,min(n,x+k*dx));
    y:=max(1,min(m,y+k*dy));
    if (y*dy=m)or(y*dy=-1) then dy:=dy*(-1);
    if (x*dx=n)or(x*dx=-1) then dx:=dx*(-1);
  until 1=0;
  if kk=n+m-2 then writeln(ans+1)
    else writeln(-1);
end.


#include <bits/stdc++.h>

#define int long long
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, x) for(int i = 0; i < x; i++)
#define rforn(i, x) for(int i = x - 1; i >= 0; i--)

using namespace std;

template<typename T> void print(T a)
{
    for (auto x : a)
        cout << x << ' ';
    cout << endl;
}

struct vec
{
    int x, y, id;

    vec() {}
    vec(int _x, int _y) : x(_x), y(_y) {}
    vec(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
    ~vec() {}

    vec& operator = (const vec &r)
    {
        x = r.x;
        y = r.y;
        id = r.id;
        return *this;
    }

    vec operator - (const vec &r)
    {
        return vec(x - r.x, y - r.y);
    }

    int operator ^ (const vec &r)
    {
        return x * r.y - y * r.x;
    }
};

bool cmp(const vec &a, const vec &b)
{
    return (a.x == b.x ? a.y < b.y : a.x < b.x);
}

vec p;

bool cmp1(const vec &a, const vec &b)
{
    vec c = a;
    vec d = b;
    return atan2((c - p).y, (c - p).x) < atan2((d - p).y, (d - p).x);
}

signed main()
{
    freopen("test.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    vector<vec> a(n);
    forn (i, n)
    {
        cin >> a[i].x >> a[i].y;
        a[i].id = i;
    }
    vector<char> d(n - 2);
    forn (i, n - 2)
        cin >> d[i];

    vector<vec> kek = a;
    sort(all(kek), cmp);
    p = kek[0];

    vector<int> ans;
    ans.pb(p.id);
    vector<bool> used(n, false);
    used[p.id] = true;

    forn (i, n - 2)
    {
        vector<vec> b;
        forn (i, n)
            if (!used[i])
                b.pb(a[i]);
        sort(all(b), cmp1);

        vec cur;
        if (d[i] == 'L')
            cur = b.front();
        else
            cur = b.back();

        used[cur.id] = true;
        ans.pb(cur.id);
        p = cur;
    }

    forn (i, n)
        if (!used[i])
            ans.pb(i);

    bool ok = true;
    forn (i, n - 2)
    {
        vec v1 = a[ans[i]], v2 = a[ans[i + 1]], v3 = a[ans[i + 2]];

        if (d[i] == 'L' && ((v2 - v1) ^ (v3 - v2)) < 0)
            ok = false;
        if (d[i] == 'R' && ((v2 - v1) ^ (v3 - v2)) > 0)
            ok = false;
    }

    if (!ok)
        cout << -1;
    else
        forn (i, n)
            cout << ++ans[i] << ' ';
    return 0;
}


/*
When you walk through a storm
Hold your head up high
And don't be afraid of the dark
At the end of the storm
There's a golden sky
And the sweet silver song
of the lark
Walk on, through the wind
Walk on, through the rain
Though your dreams be tossed
and blown
Walk on, walk on
With hope in your heart
And you'll never walk alone
YNWA
*/

//hell_hacker
//IT TAKES EVERYTHING and IT IS NOT LUCK
//PICK ME LAST -- COME OUT OF N^WHERE
//WHY NOT?

/*
And you came my way on a winner's day
Shouted loudly come out and play
Can't you tell I got news for you
Sun is shining and so are you
*/

/*

___¦¦¦¦______¦¦¦¦¦¦¦¦
__¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
_¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦__¦¦¦
¦¦¦___¦¦¦¦¦¦¦¦¦¦¦__¦¦¦
¦¦¦__¦¦¦¦¦¦¦¦¦¦¦¦_¦¦
_¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
___¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦_¦___¦¦___¦¦
___¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦___¦¦__¦¦
___¦¦¦¦¦¦¦___¦¦¦____¦¦___¦¦_¦¦
____¦¦¦¦__¯______¯¦¦_____¦¦¦¦¦
_____¦¦¦¦_____¯¯_¦¦¦______¦¦¦¦¦¦
___¦¦¦¦¦¦¦¦¦¦¦¦_¦¦¦¦¦¦¦¦¦¦¦
__¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
_¦¦¦¦¦¦¦¦___¦¦__¦¦¦¦¦¦¦¦¦
_¦¦¦¦¦¦¦_________¦¦¦¦¦

*/
#include<bits/stdc++.h>
#include <functional>
using namespace std;
#define ll                  long long
#define ull                 unsigned long long
#define ld                  long double
#define ull                 unsigned long long
#define f                   first
#define se                  second
#define SpeedForce          ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sqr(x)              ((x)*(x))
#define forn(i, n)          for(ll i=0;i<n;++i)
#define forl(i, a, b)       for(ll i=(a);i<=(b);++i)
#define ford(i, a, b)       for(int i=(a);i>=(b);--i)
#define forg(i, a, b, c)       for(ll i=(a);i<=(b);i += (c))
#define fore(x)             for(it=x.begin();it!=x.end();++it)
#define cno                 cout<<"NO";return 0;
#define cye                 cout<<"YES";return 0;
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define pb1                 pop_back
#define sz(x)               ((int)(x).size())
#define fill(a, x)          memset(a, x, sizeof(a))

typedef pair < int, int > pii;
typedef pair < char, int > pci;
typedef pair < pii, int > ppi;
typedef pair < ll, ll > pll;
typedef pair < ull, ull > puu;
typedef pair < int, string > pis;
typedef pair < string, int > psi;
typedef map < int, int > mii;
typedef map < int, int > mii;
typedef map < int, char > mic;
typedef map < string, int > msi;
typedef map < string, string > mss;
typedef map < char, int > mci;
typedef map < ll, ll > mll;
typedef map < pii, int > mpii;
typedef vector < int > vi;
typedef vector < char > vc;
typedef vector < ll > vll;
typedef vector < pii > vpii;
typedef vector < pll > vpll;
typedef vector < pis > vpis;
typedef set < int > si;
typedef set< pair < ull, ull > > spuu;
typedef priority_queue< ll > pqll;

const int N = 3e5+7;
const int M = 21;
const int MAXK = 1e5+3;
const int Candy = 1e6+3;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const double pi = 3.1415926535897932384626433;
const ld eps = 1e-12;

ll fast_pow(ll a, ll b){
    ll res = a, ret = 1;
    while(b>0){
        if(b%2)
            ret = (ret*res)%mod;
        res = (res*res)%mod;
        b/=2;
    }
    return ret;
}

ll fact(ll n){
    int f = 1;
    forl(i, 2, n) f = f * i % mod;
    return f;
}

ll Cnk(ll n, ll r){
    r = min(r, n - r);
    ll ans = 1;
    ford(i, n, n - r + 1) ans = (ans * i) % mod;
    forl(i, 1, r) ans = (ans * fast_pow(i, mod - 2)) % mod;
    return ans;
}

ll n, k = 1, a[N];
string s;
ppi p[N];
pii co[N];

int main(){
    SpeedForce
    cin >> n;
    forn(i, n){
        cin >> p[i].f.f >> p[i].f.se;
        co[i].f = p[i].f.f;
        co[i].se = p[i].f.se;
        p[i].se = i + 1;
    }
    cin >> s;
    sort(p, p + n);
    forn(i, n){
        a[i] = p[i].se - 1;
    }
    if(s[0] == 'R') reverse(a, a + n);
    forn(i, sz(s)){
        if(k == n * n){
            cout << -1;
            return 0;
        }
        double x = (co[a[i + 2]].f - co[a[i + 1]].f) * (co[a[i]].se - co[a[i + 1]].se) / (co[a[i]].f - co[a[i + 1]].f) + co[a[i + 1]].se, y = co[a[i + 2]].se;
        if((co[a[i]].f - co[a[i + 1]].f) == 0 || s[i] == 'R' && ((double)((co[a[i]].se - co[a[i + 1]].se) / (co[a[i]].f - co[a[i + 1]].f)) < 0 && x < y || (double)((co[a[i]].se - co[a[i + 1]].se) / (co[a[i]].f - co[a[i + 1]].f)) > 0 && x > y) || s[i] == 'L' && ((double)((co[a[i]].se - co[a[i + 1]].se) / (co[a[i]].f - co[a[i + 1]].f)) > 0 && x < y || (double)((co[a[i]].se - co[a[i + 1]].se) / (co[a[i]].f - co[a[i + 1]].f)) > 0 && x < y)){
            swap(a[i + 2], a[i + 1]);
            i = max(-1ll, i - 2);
            if(i == -1) k++;
        }
        else{
            if(i == sz(s) - 1){
                forn(i, n) cout << a[i] + 1 << ' ';
                return 0;
            }
        }
    }
    return (!false && !true) || ((ll)((ll)(1713 + 2377 + 1464) * (ll)(1713 + 2377 + 1464) * (ll)(1713 + 2377 + 1464)) != (ll)(171323771464));
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

auto _fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return cout.tie(nullptr); }();

template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
    for (size_t i = 0; i < v.size(); ++i) {
        in >> v[i];
    }
    return in;
}

template <typename T>
ostream& operator<<(ostream& out, vector<T>& v) {
    out << '{';
    if (!v.empty()) {
        out << v.front();
        for (size_t i = 0; i < v.size(); ++i) {
            out << ", " <<  v[i];
        }
    }
    out << '}';
    return out;
}

template <typename T, size_t Size>
istream& operator>>(istream& in, array<T, Size>& v) {
    for (size_t i = 0; i < Size; ++i) {
        in >> v[i];
    }
    return in;
}

bool operator<(const pi& l, const pi& r) {
    return l.first < r.first || (l.first == r.first && l.second < r.second);
}

pi operator-(const pi& left, const pi& right) {
    return make_pair(left.first - right.first, left.second - right.second);
}

int main() {
    int n;
    cin >> n;
    vector<pair<pi, int>>  ps(n);
    for (int i = 0; i < n; ++i) cin >> ps[i].first.first >> ps[i].first.second;
    for (int i = 0; i < n; ++i) ps[i].second = i + 1;
    sort(ps.begin(), ps.end());
    string s;
    cin >> s;
    pi last_d = { 1, 1 };
    auto angle = [&last_d] (const pi& p) {
        const double cosine = (int64_t(p.first) * last_d.first + int64_t(p.second) * last_d.second) / hypot(p.first, p.second);
        const double sine = (int64_t(p.first) * last_d.second - int64_t(p.second) * last_d.first) / hypot(p.first, p.second);
        return std::atan2(sine, cosine);
    };
    for (int i = 0; i < s.size(); ++i) {
        auto comparator = [p=ps[i].first, &angle](pair<pi, int>& left, pair<pi, int>& right) {
            return  angle(left.first - p) < angle(right.first - p);
        };
        sort(ps.begin() + i + 1, ps.end(), comparator);
        //cout << ps[i].second << ": ";
        //for (int j = i + 1; j < ps.size(); ++j) cout << ps[j].second << '(' << angle(ps[j].first - ps[i].first) << ") ";
        //cout << endl;
        if (s[i] == 'L') {
            swap(ps[i+1], ps.back());
        }
        last_d.first = ps[i+1].first.first - ps[i].first.first;
        last_d.second = ps[i+1].first.second - ps[i].first.second;
    }
    for (int i = 0; i < ps.size(); ++i) cout << ps[i].second << ' ';
    cout << endl;
}