#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

#define ll long long
#define pi pair<int,int>
#define pll pair<ll,ll>
#define pii pair<int,pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x)<0?(-(x)):(x))
#define xx(x) ((x)*(x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(),(x).end()
#define Max (1<<30)
#define LLMax (1ll<<60)
template<class T>string ToString(T t){stringstream s;s<<t;return s.str();}
template<class T>void ToOther(T&t,string a){stringstream s(a);s>>t;}

int n,V,m;
int a[333];
int b[333];
bool g[333][333];
bool ck[333];

vi t;
vi v[555];

vi r1,r2,r3;
vi rr,rv;

void dfs(int x){
	ck[x]=1;
	t.pb(x);
	for(int i=0;i<n;i++)if(g[x][i] && !ck[i]){
		v[x].pb(i);v[i].pb(x);
		dfs(i);
	}
}
bool flow(int p,int A,int &B,int &C){
	if(A==B){rr.pb(B);rv.pb(C);return 1;}

	for(int i=0;i<v[A].size();i++){
		int k=v[A][i];
		if(p!=k && flow(A,k,B,C)){
			rr.pb(A);rv.pb(C);return 1;
		}
	}
	return 0;
}
bool make(){
	queue<int> A1,A2;
	queue<int> B1,B2;
	int tot=0;
	for(int i=0;i<t.size();i++){
		int &k=t[i];
		int dif=a[k]-b[k];
		tot+=dif;

		if(dif>0)A1.push(k),A2.push(dif);
		if(dif<0)B1.push(k),B2.push(-dif);

	}
	if(tot!=0)return 0;

	while(A1.size()){
		int &k=A1.front();
		int &S=A2.front();

		if(B1.size()==0)break;

		while(S && B1.size()){
			int &tar=B1.front();
			int &S2=B2.front();

			int can=min(S,S2);

			rr.clear();
			rv.clear();

			flow(-1,k,tar,can);
			reverse(ALL(rr));

			int N=rr.size();

			while(1){
				bool end=1;
				for(int i=0;i<N-1;i++){
					if(rv[i])end=0;

					int go=min(V-a[ rr[i+1] ],min(a[ rr[i] ],rv[i]));
					if(go<=0)continue;

					a[rr[i]]-=go;
					rv[i]-=go;
					a[rr[i+1]]+=go;

					r1.pb(rr[i]);
					r2.pb(rr[i+1]);
					r3.pb(go);

					if(r1.size()>n*n*2)while(1);
				}
				if(end)break;
			}

			S-=can;
			S2-=can;

			if(S2==0)B1.pop(),B2.pop();
		}
		if(S==0)A1.pop(),A2.pop();
	}

}
int main(){
	cin>>n>>V>>m;
	for(int i=0;i<n;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++)scanf("%d",&b[i]);

	while(m--){
		int s,e;scanf("%d%d",&s,&e);s--;e--;
		g[s][e]=g[e][s]=1;
	}

	for(int i=0;i<n;i++)if(ck[i]==0){
		t.clear();
		dfs(i);

		if(!make()){cout<<"NO"<<endl;return 0;}
	}
	for(int i=0;i<n;i++)if(a[i]!=b[i]){cout<<"NO"<<endl;return 0;}

	cout<<r1.size()<<endl;
	for(int i=0;i<r1.size();i++)printf("%d %d %d\n",r1[i]+1,r2[i]+1,r3[i]);

}


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

#define int long long
#define ld long double
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define sz(x) x.size();
#define all(x) x.begin(), x.end()
#define allrev(x) x.rbegin(), x.rend()
#define vi vector<long long>
#define vvi vector<vector<int>>
#define umii unordered_map<int, int>
#define mpb make_pair

void printv(vector<int> a)
{
    for (int i = 0; i < a.size(); i++)
        cout << a[i] << ' ';
    cout << endl;
}
void take(vector<int> &a, int n)
{
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        a.pb(x);
    }
}
void solve()
{
    int n;
    cin >> n;
    // if (n == 1)
    // {
    //     cout << 1 << endl;
    //     return;
    // }
    // if (n == 2)
    // {
    //     cout << 3 << endl;
    //     return;
    // }
    // if (n == 3)
    // {
    //     cout << 7 << endl;
    //     return;
    // }
    // if (n == 4)
    // {
    //     cout << 15 << endl;
    //     return;
    // }
    // long long x = 15, y;
    // long long z = pow(2, n);
    // for (long long i = 17; i < z; i += 2)
    // {
    //     if ((i + i + 1) % 2 == 0)
    //     {
    //         y = i + 1;
    //         x = max(y, x);
    //     }
    //     else
    //     {
    //         y = i;
    //         x = max(y, x);
    //     }
    // }
    long long ans = 1;
    for (int i = 0; i < n; i++)
    {
        ans *= 2;
    }
    cout << ans - 1 << endl;
}

int_fast32_t main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int x, y;

        cin >> x >> y;
	cout << min( (x + y) / 3 , min(x , y) );

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int grassh(string s,int n,int k,int t,int p)
{
	int i,a=0;
	if(p>t)
	{
		if(p-t <k)
		{
		cout<<"NO"<<endl;
		return 1;
}
		for(i=t;i<p;)
		{
			if(s[i+k]=='#')
			break;
		 a++;
		 i=i+k;
	   }
		if(a==(p-t)/k)
		{
		cout<<"YES"<<endl;
		return 1;}
	}
	else if(p<t)
	{
		if(t-p <k)
		{
		cout<<"NO"<<endl;return 1;
	}

		for(i=p;i<t;)
		{
			if(s[i+k]=='#')
			break;
		 a++;
		 i=i+k;
	   }
		if(a==(t-p)/k){
		cout<<"YES"<<endl;
		return 1;}
	}

	cout<<"NO"<<endl;
}

int main()
{
	int i,n,k,a=0;
	string s;
	cin>>n>>k;
     cin>>s;
	int t=s.find('G');
	int p=s.find('T');
	grassh(s,n,k,t,p);

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

#define LOCALIO "C:/Users/admin/Documents/Code/freopen/"

map <ll, ll> Map;
ll req;

ll track(ll s, ll i)
{
    // cout << s << " " << i << "\n";
    if (s>=req)
    {
        // cout << s << " " << i << " " << req << "\n";
        Map[s]=1;
        return 1;
    }
    ll x=Map[s];
    if (x!=0)
        return x;
    bool cnt[10]={};
    ll num=s;
    while (num>0)
    {
        cnt[num%10]=1;
        num/=10;
    }
    ll ans=LLONG_MAX-1;
    for (ll j=9; j>1; j--)
    {
        if (cnt[j])
        {
            ans=min(ans, track(s*j, i+1));
        }
    }
    // cout << s << " " << ans << "\n";
    Map[s]=ans+1;
    return ans+1;
}

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

    ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
//	freopen("FIBONACCI.inp","r",stdin);
//	freopen("FIBONACCI.out","w",stdout);
    ll n, x; cin >> n >> x;
    bool check=0;
    ll num=x;
    while (num>0)
    {
        if (num%10>1)
        {
            check=1;
            break;
        }
        num/=10;
    }
    if (check==0)
        cout << -1;
    else
    {
        req=1;
        for (ll i=1; i<n; i++)
            req*=10;
        cout << track(x, 0)-1;
    }
}


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define sz(a) int(a.size())
#define clr(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
const int inf=1e9;
const ll Inf=1e18;
const int N=30;
const int mod=0;
int gi() {
	int x=0,o=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-') o=-1,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*o;
}
template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; };
template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; };
int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; }
int sub(int a,int b) { return a-b<0?a-b+mod:a-b; }
void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); }
void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); }
int n,m,g[N][N];
vector<vector<int>> ans;
pii operator+(pii a,pii b)
{
	return mp(a.x+b.x,a.y+b.y);
}
pii operator-(pii a,pii b)
{
	return mp(a.x-b.x,a.y-b.y);
}
pii rotate(pii a)
{
	return mp(a.y,-a.x);
}
void rot_cycle(vector<pii> v)
{
	vector<int> w;
	for(pii p:v) w.pb(g[p.x][p.y]);
	ans.pb(w);
	int pre=g[v.back().x][v.back().y];
	for(int i=sz(v)-1;i;i--) g[v[i].x][v[i].y]=g[v[i-1].x][v[i-1].y];
	g[v[0].x][v[0].y]=pre;
}
int in(pii p)
{
	return 1<=p.x&&p.x<=n&&1<=p.y&&p.y<=m;
}
void ex_swap(pii a,pii b,pii d)
{
	pii c=b+b-a,e=b+d-a,f=c+d-a;
	rot_cycle({a,b,e,d});
	rot_cycle({b,c,f,e});
	rot_cycle({a,d,e,f,c,b});
}
void _swap(pii a,pii b)
{
	pii c=b+b-a;
	if(!in(c)) swap(a,b);
	pii v=rotate(a-b),d=a+v;
	if(!in(d)) d=a-v;
	ex_swap(a,b,d);
}
void move(int s)
{
	pii to=mp((s-1)/m+1,(s-1)%m+1),fr;
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(g[i][j]==s) fr=mp(i,j);
	while(fr.y>to.y)
	{
		if(fr.x+1<=n) rot_cycle({fr,fr+mp(0,-1),fr+mp(1,-1),fr+mp(1,0)});
		else if(fr.x-1>to.x) rot_cycle({fr,fr+mp(0,-1),fr+mp(-1,-1),fr+mp(-1,0)});
		else _swap(fr,fr+mp(0,-1));
		--fr.y;
	}
	while(fr.y<to.y)
	{
		if(fr.x+1<=n) rot_cycle({fr,fr+mp(0,1),fr+mp(1,1),fr+mp(1,0)});
		else if(fr.x-1>to.x) rot_cycle({fr,fr+mp(0,1),fr+mp(-1,1),fr+mp(-1,0)});
		else _swap(fr,fr+mp(0,1));
		++fr.y;
	}
	while(fr.x>to.x)
	{
		if(fr.y+1<=m) rot_cycle({fr,fr+mp(-1,0),fr+mp(-1,1),fr+mp(0,1)});
		else _swap(fr,fr+mp(-1,0));
		--fr.x;
	}
}
int main()
{
	n=gi(),m=gi();
	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) g[i][j]=gi();
	for(int i=1;i<=n*m;i++) move(i);
	printf("%d\n",sz(ans));
	for(auto x:ans)
	{
		printf("%d ",sz(x));
		for(auto y:x) printf("%d ",y);
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e5 + 100;
const int MAXM = 3e3 + 10;
const double eps = 1e-5;

template < typename T > inline void read(T &x) {
	x = 0; T ff = 1, ch = getchar();
	while(!isdigit(ch)) {
		if(ch == '-') ff = -1;
		ch = getchar();
	}
	while(isdigit(ch)) {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	x *= ff;
}

template < typename T > inline void write(T x) {
	if(x == 0) {
		putchar('0');
		return ;
	}
	if(x < 0) putchar('-'), x = -x;
	static T tot = 0, ch[30];
	while(x) {
		ch[++tot] = x % 10 + '0';
		x /= 10;
	}
	while(tot) putchar(ch[tot--]);
}

int n, t;
double p, ans, f[MAXM][MAXM];

int main() {
	scanf("%d %lf %d", &n, &p, &t);
	f[0][0] = 1;
	for(int i = 1; i <= t; ++i) {
		f[0][i] = f[0][i - 1] * (1.0 - p);
//		printf("f[0][%d] = %lf\n", i, f[0][i]);
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= t; ++j) {
			if (i != n) f[i][j] = f[i][j - 1] * (1.0 - p);
			else f[i][j] = f[i][j - 1];
			f[i][j] += f[i - 1][j - 1] * p;
//			if(i == n) f[i][j] = f[i][j - 1];
//			printf("f[%d][%d] = %lf\n", i, j, f[i][j]);
		}
		ans += f[i][t] * i;
	}
	printf("%lf", ans);
	return 0;
}


/** || By The Name of Allah, Who Created Me. || **/

/**
	* Author   : Farid ul Islam Chy.
	* ID       : 1512020238.
	* Batch    : 38th.
	* Semester : 4C(E).
	* Team     : LU_AntiVirus.

	* Leading University, Sylhet.
**/

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <vector>
#include <cstring>
#include <utility>
#include <ctype.h>
#include <iomanip>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <fstream>

using namespace std;

#define ll    		    long long int
#define SS		    stringstream
#define pb    		    push_back
#define ull    		    unsigned long long int
#define MII        	    map <int,int>
#define MSI        	    map <string,int>
#define MIS        	    map <int,string>
#define MLI         	    map <long long int,int>
#define MIL        	    map <int,long long int>
#define MLL        	    map <ll,ll>
#define VS        	    vector <string> n;
#define VI        	    vector <ll> n;
#define VC        	    vector <char> n;
#define ForA(n)  	    for (int i=0; i<n; i++)
#define ForA2(n)  	    for (int j=0; j<n; j++)
#define ForA3(n)  	    for (int k=0; k<n; k++)
#define ForAS(n)  	    for (int i=n; i>0; i--)
#define ForN(n)  	    for (int i=1; i<=n; i++)
#define ForN2(n)  	    for (int j=1; j<=n; j++)
#define ForN3(n)  	    for (int k=1; k<=n; k++)
#define ForD(m,n)  	    for (int i=m; i<=n; i++)
#define ForD2(m,n)  	    for (int j=m; j<=n; j++)
#define ForD3(m,n) 	    for (int k=m; k<=n; k++)
#define SortA(n,x) 	    sort(n,n+x)
#define SortS(s)  	    sort(s.begin(), s.end())
#define SortRS(s)  	    sort(s.rbegin(), s.rend())
#define Search(s)           s.begin(), s.end()
#define SortSG(n)	    sort(n.begin(), n.end(), greater<int>())
#define Cin(a)		    cin>>a
#define GCin(a)		    getline(cin,a)
#define Cin2(a,b)	    cin>>a>>b
#define Cin3(a,b,c)	    cin>>a>>b>>c
#define Cin4(a,b,c,d)	    cin>>a>>b>>c>>d
#define PI 		    3.1415926535897932384626433832795l
#define Ignore		    cin.ignore()

int isprime(ll n){
     ll i;
     if(n==2) return 1;
     if(n%2==0) return 0;
     for(i=3;i<=sqrt(n);i+=2)
         if(n%i==0) return 0;
     return 1;
}

bool cmp(string i,string j){
    return i+j<j+i;
}

string ConvertItoS(ll n){
    SS a; a<<n;
    return a.str();
}

ll ConvertStoI(string s){
    return stoi(s);
}

int main(){
    //freopen("in.txt","r",stdin)
    //freopen("out.txt","w",stdout)
    //cout<<fixed<<setprecision(2);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	bool flag=false;
	string s;
    ll cnt1=0,cnt2=0,cnt3=0,cnt4=0;

	Cin(s);
	ForA(s.size()){
	    if(isupper(s[i]))cnt1=1;
	    if(islower(s[i]))cnt2=1;
	    if(!isalpha(s[i]))cnt3=1;
	    if(isdigit(s[i]))cnt4=1;
	}
	ll sum=cnt1+cnt2+cnt3+cnt4;
    cout<< ((s.size()>=5&&sum==4)?"Correct":"Too weak") <<endl;

    return 0;
}


#include<string>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
int mp[200001];
int main()
{
    long long n,ans=0,qianfu,guodao,zuo;
    char c;
    scanf("%lld%c",&n,&c);
    qianfu=(n-1)/4;
    qianfu*=2;
    if(n%4==2||n%4==0)
       qianfu++;
    if(n%4==1||n%4==2)
       guodao=n-1;
    else
       guodao=n-3;
    if(c=='f')
       zuo=1;
    if(c=='e')
       zuo=2;
    if(c=='d')
       zuo=3;
    if(c=='a')
       zuo=4;
    if(c=='b')
       zuo=5;
    if(c=='c')
       zuo=6;
    ans=qianfu*6+guodao+zuo;
    printf("%lld\n",ans);
    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author majk
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <vector>
#include <stack>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <fstream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <queue>
#include <bitset>
using namespace std;

#define x first
#define y second
typedef std::pair<int,int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui,ui> puu;

template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; }
ui logceil(int x) { return 8*sizeof(int)-__builtin_clz(x); }

namespace std {
    template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}};
}
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename F> double bshd(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){l=m;}else{h=m;}}return (l+h)/2;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename F> double bsld(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){h=m;}else{l=m;}}return (l+h)/2;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }

template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector2<T>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector2<T>>(a,vector2<T>(b,c,t)){}};
template<typename T>class vector4:public vector<vector3<T>>{public:vector4(){} vector4(size_t a,size_t b,size_t c,size_t d,T t=T()):vector<vector3<T>>(a,vector3<T>(b,c,d,t)){}};
template<typename T>class vector5:public vector<vector4<T>>{public:vector5(){} vector5(size_t a,size_t b,size_t c,size_t d,size_t e,T t=T()):vector<vector4<T>>(a,vector4<T>(b,c,d,e,t)){}};
template <typename T> struct bounded_priority_queue {
	inline bounded_priority_queue(ui X) : A(X), B(0), s(0) {}
	inline void push(ui L, T V) { B = max(B, L); A[L].push(V); ++s; }
	inline const T &top() const { return A[B].front(); }
	inline void pop() { --s; A[B].pop(); while (B > 0 && A[B].empty()) --B; }
	inline bool empty() const { return A[B].empty(); }
	inline void clear() { s = B = 0; for (auto &a: A) a = queue<T>(); }
	inline ui size() const { return s; }
private:
	vector<queue<T>> A; ui B; int s;
};

#endif
#ifndef MOD_H
#define MOD_H

template <unsigned int N> class Field {
    typedef unsigned int ui;
    typedef unsigned long long ull;
	inline ui pow(ui a, ui p){ui r=1,e=a;while(p){if(p&1){r=((ull)r*e)%N;}e=((ull)e*e)%N;p>>=1;}return r;}
	/*extended GCD(slow):ll t=0,nt=1,r=N,nr=a;while(nr){ll q=r/nr;t-=q*nt;swap(t,nt);r-=q*nr;swap(r,nr);}assert(r<=1);return(t<0)?t+N:t;*/
	inline ui inv(ui a){return pow(a,N-2);}
public:
    inline Field(int x = 0) : v(x) {}
	inline Field<N> pow(int p){return (*this)^p; }
	inline Field<N> operator^(int p){return {(int)pow(v,(ui)p)};}
    inline Field<N>&operator+=(const Field<N>&o) {if ((ll)v+o.v >= N) v = (ll)v+o.v-N; else v = v+o.v; return *this; }
    inline Field<N>&operator-=(const Field<N>&o) {if (v<o.v) v = N-o.v+v; else v=v-o.v; return *this; }
    inline Field<N>&operator*=(const Field<N>&o) {v=(ull)v*o.v % N; return *this; }
    inline Field<N>&operator/=(const Field<N>&o) { return *this*=inv(o.v); }
    inline Field<N> operator+(const Field<N>&o) const {Field<N>r{*this};return r+=o;}
    inline Field<N> operator-(const Field<N>&o) const {Field<N>r{*this};return r-=o;}
    inline Field<N> operator*(const Field<N>&o) const {Field<N>r{*this};return r*=o;}
    inline Field<N> operator/(const Field<N>&o) const {Field<N>r{*this};return r/=o;}
    inline Field<N> operator-() {if(v) return {(int)(N-v)}; else return {0};};
    inline Field<N>& operator++() { ++v; if (v==N) v=0; return *this; }
    inline Field<N> operator++(int) { Field<N>r{*this}; ++*this; return r; }
    inline Field<N>& operator--() { --v; if (v==-1) v=N-1; return *this; }
    inline Field<N> operator--(int) { Field<N>r{*this}; --*this; return r; }
    inline bool operator==(const Field<N>&o) const { return o.v==v; }
	inline bool operator!=(const Field<N>&o) const { return o.v!=v; }
	inline explicit operator ui() const { return v; }
	inline static vector<Field<N>>fact(int t){vector<Field<N>>F(t+1,1);for(int i=2;i<=t;++i){F[i]=F[i-1]*i;}return F;}
	inline static vector<Field<N>>invfact(int t){vector<Field<N>>F(t+1,1);Field<N> X{1};for(int i=2;i<=t;++i){X=X*i;}F[t]=1/X;for(int i=t-1;i>=2;--i){F[i]=F[i+1]*(i+1);}return F;}
private: ui v;
};
template<unsigned int N>istream &operator>>(std::istream&is,Field<N>&f){unsigned int v;is>>v;f=v;return is;}
template<unsigned int N>ostream &operator<<(std::ostream&os,const Field<N>&f){return os<<(unsigned int)f;}
template<unsigned int N>Field<N> operator+(int i,const Field<N>&f){return Field<N>(i)+f;}
template<unsigned int N>Field<N> operator-(int i,const Field<N>&f){return Field<N>(i)-f;}
template<unsigned int N>Field<N> operator*(int i,const Field<N>&f){return Field<N>(i)*f;}
template<unsigned int N>Field<N> operator/(int i,const Field<N>&f){return Field<N>(i)/f;}

typedef Field<1000000007> FieldMod;

struct Ring {
	static int div(int p, int q, int N) {
		ll t=0,nt=1,r=N,nr=q;
		while(nr){ ll q=r/nr;t-=q*nt;r-=q*nr;swap(t,nt);swap(r,nr); }
		t=(t<0)?t+N:t;
		r=(r<0)?r+N:r;
		if (gcd(p,N)%r) { return 0; }
		return (int)(((ll)t*(ll)p/r)%N);
	}
};
#endif

class TaskD {
public:
    ll floorSqrt(ll x) {
        ll y = (ll)sqrt(x);
        while (y*y < x) ++y;
        while (y*y > x) --y;
        return y;
    }

    void solve(istream& cin, ostream& cout) {
        ll M; cin >> M;
        ll S = floorSqrt(M);

        FieldMod i2 = 1, i3 = 1, i6 = 1, i30 = 1, i42 = 1;
        i2 /= 2;
        i3 /= 3;
        i6 /= 6;
        i30 /= 30;
        i42 /= 42;
        FieldMod fm = ui(M%1000000007);

        FieldMod ans = 0;
        for (ll x = 1; x*x <= M; ++x) {
            FieldMod fx = ui(x);
            FieldMod fy = ui(floorSqrt(M-x*x));
            fx *= fx;

            FieldMod a = i3;
            FieldMod b = -fm*i2+fx-1;
            FieldMod c = -fm*fx+fm*i2+fx*fx-2*fx+2*i3;
            FieldMod d = fm*fm*fm*i6+fm*fm*i2-fm*fx*fx*i2+fm*fx*i2+fm*i3+fx*fx*fx*i3-fx*fx+2*fx*i3;

            ans += a*fy*(fy+1)*(2*fy+1)*(3*fy*fy*fy*fy+6*fy*fy*fy-3*fy+1)*i42;
            ans += b*fy*(fy+1)*(2*fy+1)*(3*fy*fy+3*fy-1)*i30;
            ans += c*fy*(fy+1)*(2*fy+1)*i6;
            ans += d*(fy+1);
        }

        cout << 4*ans + fm*(fm+1)*(fm+2)*i6 << endl;

    }
};

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	TaskD solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
    return 0;
}


#include<iostream>
using namespace std;
long long n;
main(){
cin>>n;
cout<<(n-2)*(n-2)<<endl;

}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<ll> vll;

#define all(v) v.begin(),v.end()
#define test() int t;cin>>t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9+7;

map<pair<int, int>, double> m;
double prob(int w, int b)
{
    if(w==0)return(0);
    if(b==0)return(1);
    if(m.find({w, b})!=m.end())return(m[{w, b}]);
    double ans = w * (1.0/(w+b)), cont = b*(1.0/(w+b));
    b--;
    if(b>=0)cont*= b*(1.0/(w+b));
    b--;
    double jumpb=0, jumpw=0;
    if(b>0)jumpb = b*(1.0/(w+b))*prob(w, b-1);
    if(b>=0)jumpw = w*(1.0/(w+b))*prob(w-1, b);
    ans+=(cont*(jumpw+jumpb));
    m[{w, b+2}]=ans;
    return(ans);
}
int main()
{
    fastio
    int w, b;
    cin>>w>>b;
    cout<<setprecision(12)<<prob(w, b)<<endl;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
int main(){
    ios::sync_with_stdio(false);
    ll a,b;
    cin >> a >>b;
    // if(a==b){
    //     cout << "0" << endl;
    //     return 0;
    // }
    vector<ll> v;
    ll diff = abs(a-b);
    for(ll i=1;i*i<=diff;i++){
        if(diff%i==0){
            v.push_back(i);
            if((diff/i)!=i){
                v.push_back(diff/i);
            }
        }
    }
    sort(v.begin(),v.end());
    ll val = a*b,ans=0;
    for(int i=0;i<v.size();i++){
        ll nr = v[i];
        if(a%nr!=b%nr)
            continue;
        if(a%nr==0){
            ll res = (a*b)/__gcd(a,b);
            if(res<val){
                val=res;
                ans=0LL;
            }
        }else{
            ll res = ((nr-a%nr+a)*(nr-b%nr+b))/(__gcd((nr-a%nr+a),(nr-b%nr+b)));
            if(res<val){
                val=res;
                ans=nr-a%nr;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define il inline
#define RG register
#define ld long double
#define eps (1e-12)

using namespace std;

struct point{
  ld x,y,r;
  il int operator == (const point &a) const{
    return fabs(x-a.x)<eps && fabs(y-a.y)<eps;
  }
  il int operator < (const point &a) const{
    if (fabs(x-a.x)<eps) return y<a.y; return x<a.x;
  }
}p[105],pt[105];

int fa[105],n,E,V,C;

il int find(RG int x){ return fa[x]==x ? x : fa[x]=find(fa[x]); }

il ld dis(RG point a,RG point b){
  return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

il point make(RG point c,RG ld ang){
  return (point){c.x+cos(ang)*c.r,c.y+sin(ang)*c.r};
}

il int get(RG point A,RG point B){
  RG ld d=dis(A,B); if (A.x>B.x) swap(A,B);
  if (d>A.r+B.r || d<abs(A.r-B.r)) return 0;
  RG ld a=atan2(B.y-A.y,B.x-A.x);
  RG ld aa=acos((ld)(d*d+A.r*A.r-B.r*B.r)/(2*d*A.r));
  RG point p1=make(A,a+aa),p2=make(A,a-aa);
  pt[++V]=p1,pt[++V]=p2; return 1;
}

il int can(RG point c,RG point p){ return fabs(dis(c,p)-c.r)<eps; }

int main(){
#ifndef ONLINE_JUDGE
  freopen("prospect.in","r",stdin);
  freopen("prospect.out","w",stdout);
#endif
  cin>>n;
  for (RG int i=1;i<=n;++i)
    cin>>p[i].x>>p[i].y>>p[i].r,fa[i]=i;
  for (RG int i=1;i<n;++i)
    for (RG int j=i+1;j<=n;++j)
      if (get(p[i],p[j])) fa[find(i)]=find(j);
  sort(pt+1,pt+V+1),V=unique(pt+1,pt+V+1)-pt-1;
  for (RG int i=1;i<=n;++i) C+=find(i)==i;
  for (RG int i=1;i<=n;++i)
    for (RG int j=1;j<=V;++j) E+=can(p[i],pt[j]);
  cout<<E-V+C+1; return 0;
}


#include <iostream>

using namespace std;
int find(int a[],int b[],int n){
    int i;
    int maxi[30];
    int max=0;
    for(i=0;i<n;i++){
        maxi[i]=b[i]-a[i];
    }
    for(i=0;i<n;i++){
        if(maxi[i]>max)
            max=maxi[i];
    }
    for(i=0;i<n;i++){
        if(max==maxi[i])
            break;
    }
    return i;
}
int main()
{
    static int a[30],b[30];
    int k,n,i,max,min,g,q,r,diff;
    max=0;
    min=0;

    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        min=min+a[i];
        max=max+b[i];
    }
    if((k>max)||(k<min))
        cout<<"NO";
    else{
        cout<<"YES\n";
        g=max-k;
        if(g!=0){
        /*q=g/n;
        r=g%n;
        while(q>0){
        for(i=0;i<n;i++){
            if(b[i]<=a[i]){
                b[i+1]--;
                continue;
            }
                b[i]--;
        }
        q--;
        }
        for(i=0;i<r;i++)
        {
            if(i>n)
                i=i-n;
            if(b[i]<=a[i]){
                b[i+1]--;
                continue;}
            --b[i];}
        }*/
        while(g>0){
            diff=find(a,b,n);
            b[diff]--;
            g--;

        }}

    for(i=0;i<n;i++){
        cout<<b[i]<<"\t";
    }
    }

    return 0;
}


// think twice code once
// Start:[2017-02-15 11:36:41]
// End  :[]
/*{{{*/
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for(int i = (x), _ = (y); i <= _; ++ i)
#define per(i, x, y) for(int i = (x), _ = (y); i >= _; -- i)
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#define disp(x) cout << #x << " = " << x << "; "
#ifdef __linux__
#define getchar getchar_unlocked
#define LLFORMAT "%lld"
#else
#define LLFORMAT "%I64d"
#endif

typedef long long LL;

template <class T> bool chkmin(T& a, T b) { return a > b ? a = b, true : false; }
template <class T> bool chkmax(T& a, T b) { return a < b ? a = b, true : false; }

template <class T> void read(T& a) {
	char c = getchar(); T f = 1; a = 0;
	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c = getchar()) a = a * 10 + c - '0';
	a *= f;
}
/*}}}*/

const int maxN = 1e5 + 5, mo = 1e9 + 7;

int pow_mod(int x, int t)
{
	int ret = 1;
	while(t)
	{
		if(t & 1) ret = (LL) ret * x % mo;
		x = (LL) x * x % mo; t >>= 1;
	}
	return ret;
}

int a[maxN], N, M, tp, ans;
int cnt[4];
int pre;
int len[maxN], tot[maxN], lala[maxN], pos[maxN];
bool appear[maxN], vis[maxN];
vector<int> dp[maxN], invdp[maxN], chain;

void upd(int& a, int b)
{
	a += b;
	if(a < 0) a += mo;
	if(a >= mo) a -= mo;
}

void prework()
{
	rep(i, 1, N)
	{
		dp[i].resize(N / i + 5);
		invdp[i].resize(N / i + 5);
		dp[i][0] = invdp[i][0] = 1;

		rep(j, 1, N / i + 2)
		{
			dp[i][j] = (LL) dp[i][j - 1] * i % mo;
			if(j >= 2) upd(dp[i][j], (LL) dp[i][j - 2] * (j - 1) % mo * i % mo);
			invdp[i][j] = pow_mod(dp[i][j], mo - 2);
		}
	}
}

void calc(int haha)
{
//	dprintf("in calc "); disp(haha), puts("-----------");
	int pre1 = (LL) pre * haha % mo;
//	rep(i, 1, tp)
//		disp(i), disp(len[i]), puts("");
	rep(i, 1, tp)
	{
		pre1 = (LL) pre1 * invdp[len[i]][lala[len[i]]] % mo;
//		disp(pre1), puts("");
//		disp(lala[len[i]]), puts("");
		cnt[min(len[i], 3)] ++;
		pre1 = (LL) pre1 * dp[len[i]][++ lala[len[i]]] % mo;
//		disp(pre1), puts("");
	}

	if(cnt[3] == 0) pre1 = haha;
	upd(ans, pre1);

	rep(i, 1, tp)
	{
		lala[len[i]] --;
		cnt[min(len[i], 3)] --;
	}
}

void dfs(int x, int haha)
{
	if(x >= (int) chain.size())
	{
		calc(haha);
		return;
	}

	tot[++ tp] = 1;
	len[tp] = chain[x];
	dfs(x + 1, haha);
	-- tp;

	rep(i, 1, tp)
	{
		tot[i] ++;
		len[i] += chain[x];
		dfs(x + 1, (LL) haha * (tot[i] - 1) % mo);
		len[i] -= chain[x];
		tot[i] --;
	}
}

int main()
{
#ifdef Leeson
	freopen("tmp.in", "r", stdin);
	freopen("tmp.out", "w", stdout);
#endif

	read(N); read(M);
	prework();

	rep(i, 1, N)
	{
		read(a[i]);
		pos[a[i]] = i;
	}

	rep(i, 1, N) if(a[i] == 0)
	{
//		printf("while i = %d ----------------\n", i);
		int len = 0;
		for(int x = i; x; x = pos[x])
			vis[x] = 1, ++ len;
//		disp(len), puts("");
		chain.push_back(len);
	}

	rep(i, 1, N) if(vis[i] == 0)
	{
		int len = 0;
		for(int x = i; vis[x] == 0; x = pos[x])
			vis[x] = 1, ++ len;
		cnt[min(len, 3)] ++;
//		disp(len), puts("");
		lala[len] ++;
	}

	pre = 1;
	rep(i, 1, N)
		if(lala[i]) pre = (LL) pre * dp[i][lala[i]] % mo;
//	disp(pre), puts("");
//	rep(i, 1, N)
//		disp(i), disp(lala[i]), puts("");
//	rep(i, 0, chain.size() - 1)
//		disp(chain[i]), puts("");
//
//	disp(pre),puts("");

	dfs(0, 1);
	cout << ans << endl;

	return 0;
}


#include<stdio.h>

int main(void)
{
    long long a,b,c,d,flag=0;
    scanf("%lld %lld",&a,&b);
    if(a==b){
        printf("%lld",a/b);
        flag =1;
    }
     if(b%2==1&&flag==0)
        printf("%lld",(b/2)+1);
     else if(b%2==0&&flag==0)
        printf("%lld",((a-b)/2)+1);
}


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

#define ll        long long
#define fr        first
#define sec       second
#define pi        3.141592653589793
#define M         1000000007
#define mod       998244353
#define maxint    2147483647
#define minint    -2147483648
#define maxll     9223372036854775807
#define minll     -9223372036854775808
#define meme0(a)  memset(a,0,sizeof(a));
#define meme1(a)  memset(a,1,sizeof(a));
#define ios       ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define open      freopen("input.txt","r",stdin);
#define close     freopen("output.txt","w",stdout);
#define v_int     vector<int >
#define v_ll      vector<long long >
#define pb        push_back
#define mk        make_pair
//#define int       long long
//upper_bound(begin(),end(),key)
//lower_bound(begin(),.end(),key);

ll expo(ll x , ll n , ll m){
    if(n==0)
        return 1;
    else if(n%2==0)
        return expo((x*x)%m , n/2 , m);
    else
        return (x*expo((x*x)%m,(n-1)/2,m))%m;

}

void solve(){
    ll n,a,b;
    cin >> n >> a >> b;
    /*
    vector <int> dd;
    for(int i=1;i*i<=n;i++){
        if(n%i==0){
            dd.push_back(i);
            if(i!=n/i)
                dd.push_back(n/i);
        }
    }
    */
    ll area = 6*n;
    if(area <= a*b){
        cout << a*b << endl;
        cout << a << " " << b << endl;
        return ;
    }
    float k = sqrt(area);

    bool lol = false;
        if(a>b){
            lol = true;
            swap(a,b);
        }

    ll m = k;
    ll p = b;
    ll q = a;
  //cout << m <<" " << area << endl;
    a = maxint;
    b = maxint;
    for(ll i=q;i<=m+5;i++){
        ll j = area/i;
        if(j<p){
            break;
        }
        if(i*j>=area){
            if(i*j<a*b ){
                a=i;
                b=j;
            }
        }
        j++;
        if(i*j>=area){
            if(i*j<a*b ){
                a=i;
                b=j;
            }
        }
    //cout << a<< " " << b << " " << i << " " << j <<   endl;
    }
    if(a==maxint){
        int x = q;
        while(q*p<area){
            p++;
            if(area/p>=x)
                q = area/p;
        }

            b = p;
            a = q;
    }
    if(lol)
        swap(a,b);
    cout << a*b << endl <<a << " " << b;
    return;
}

int main(){
    ios
    int t=1;
   // cin >> t;
    while(t--)
        solve();

    return (int)0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define maxn 100005
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define ms(i) memset(i,0,sizeof(i));
ll minn[maxn],c[maxn];
string s[maxn];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n;
	while(cin>>n){
        ll now=1,ans=0;
        while(n>1){
            ans+=n/2*now;
            now<<=1;
            n-=n>>1;
        }
        cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<(k);i++)
#define mp make_pair
#define sz(a) (int)(a).size()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
//----head----
const ll mod=1e9+7;
ll dp[105][105][105];//dp[i][l][r]��ʾf(i)�д���s[l-r]��������
char s[105];
int n,m;
ll modpow2[105]={2,2};
int main()
{
	rep(i,2,105)modpow2[i]=modpow2[i-1]*modpow2[i-2]%mod;
    scanf("%d%d",&m,&n);
    scanf("%s",s);
    rep(i,0,m)dp[s[i]-'0'][i+1][i+1]=1;
    rep(i,2,n+1)rep(l,1,m+1)rep(r,l,m+1)
    {
        dp[i][l][r]=(dp[i][l][r]+dp[i-1][l][r]*(r==m?modpow2[i-2]:1))%mod;
        dp[i][l][r]=(dp[i][l][r]+dp[i-2][l][r]*(l==1?modpow2[i-1]:1))%mod;
        rep(k,l,r)dp[i][l][r]=(dp[i][l][r]+dp[i-1][l][k]*dp[i-2][k+1][r])%mod;
    }
    printf("%lld\n",dp[n][1][m]);
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int n,k;
int A[6][3] =  { 0,1,2 ,1,0,2,1,2,0,2,1,0,2,0,1,0,2,1} ;

int main()
{
	scanf("%d%d", &n, &k);
	n = n % 6;
	printf("%d", A[n][k]);
	return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <cstdio>

using namespace std;

const int maxN = 110;

string s;
int n;

char get(int i) {
  return s[(i % n + n) % n];
}

int index(int i) {
  return (i % n + n) % n;
}

int used[maxN];
long long dp1[maxN];
long long dp2[maxN];

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

  cin >> s;
  n = s.length();
  for (int i = 0; i < n; ++i) {
    if (get(i) == 'A' && get(i + 1) == 'A' && get(i + 2) == 'B' && get(i + 3) == 'B') {
      cout << 0 << endl;
      return 0;
    }
  }

  vector <int> lengths;
  vector <int> start;
  vector <int> finish;

  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      if (get(i) == 'B' && get(i + 1) == 'A') {
        used[index(i)] = used[index(i + 1)] = 1;
        int j = i - 2;
        int length = 2;
        while (1) {
          if (used[index(j)]) break;
          if (get(j) == 'B' && get(j + 1) == 'A') {
            used[index(j)] = used[index(j + 1)] = 1;
            length += 2;
            j -= 2;
          } else {
            break;
          }
        }
        start.push_back(get(j + 1) - 'A');
        j = i + 2;
        while (1) {
          if (used[index(j)]) break;
          if (get(j) == 'B' && get(j + 1) == 'A') {
            used[index(j)] = used[index(j + 1)] = 1;
            length += 2;
            j += 2;
          } else {
            break;
          }
        }
        finish.push_back(get(j) - 'A');
        lengths.push_back(length / 2);
      }
    }
  }

  dp1[0] = 1;
  dp2[0] = 1;

  dp1[1] = 1; dp1[2] = 2;
  dp2[1] = 1; dp2[2] = 1;
  for (int i = 3; i < maxN; ++i) {
    dp1[i] = dp1[i - 1] + dp1[i - 2];
    dp2[i] = dp2[i - 1] + dp2[i - 2];
  }

  long long answer = 1;

  for (int i = 0; i < lengths.size(); ++i) {
    if (lengths[i] * 2 == n) {
      long long res1 = dp1[lengths[i] - 1];
      long long res2 = dp2[lengths[i] - 1];

      long long ans = res1 + dp1[lengths[i] - 2] + res2;
      cout << ans << endl;
      return 0;
    } else {
      long long dist = lengths[i];
      int lt = start[i];
      int rt = finish[i];

      if (dist == 1) {
        if (lt == 1 && rt == 0) {
          answer *= 2;
        }
        continue;
      }

      if (lt == 0 && rt == 1) {
        answer *= dp2[dist];
      } else
      if (lt == 0 && rt == 0) {
        answer *= dp2[dist] + dp2[dist - 1];
      } else
      if (lt == 1 && rt == 1) {
        answer *= dp1[dist];
      } else {
        answer *= dp1[dist] + dp1[dist - 1];
      }
    }
  }

  cout << answer << endl;

  return 0;
}


//          be name khoda

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

using namespace std;

typedef long long ll;
const int maxN = 7002;

struct node{
    int lr;
    int x, ind, pl;
};

int /*l[maxN][maxN], r[maxN][maxN],*/ type[maxN], lr[maxN][2], n;
vector <node> timeLine[maxN];
//vector <int> l[maxN], r[maxN];
set <int> ans;

int lg(ll v)
{
    int k = 0;
    while((1 << k) < v)
        k++;
    return k;
}

void count(int n)
{
    ll a[maxN], sum[maxN];
    a[1] = sum[1] = 1;
    for(int i = 2; i < n; i++)
    {
        a[i] = a[i - 1] + lg(a[i - 1]);
        sum[i] = sum[i - 1] + a[i];
    }
    for(int i = 6500; i < 7000; i++)cerr << i << " : " << a[i] << ", " << sum[i] << endl;
}

bool cmp(node l, node r)
{
    return l.pl < r.pl;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int t;
        cin >> t;
        type[i] = t;
        if(t == 1)
        {
            int t, lll, rr, x;
            cin >> t >> lll >> rr >> x;
            node a, b;
            a.ind = b.ind = i;
            a.lr = rr;
            b.lr = lll;
            a.pl = lll;
            b.pl = rr;
            a.x = b.x = x;
            timeLine[t].push_back(a);
            timeLine[t].push_back(b);
        }
        else
        {
            int t, v;
            cin >> t >> v;
            int lll = v, rr = v;
            ans.clear();
            for(int j = t; j <= n; j++)
            {
                for(int k = 0; k < timeLine[j].size(); k++)
                    //cerr << k << " : " << timeLine[j][k].pl <<  timeLine[j][k].lr << endl;
                    if((timeLine[j][k].pl >= lll && timeLine[j][k].pl <= rr)||(timeLine[j][k].pl < lll && timeLine[j][k].lr > rr))
                        ans.insert(timeLine[j][k].x);
                //cerr << i << " ,,, " << j << " : " << lll << " " << rr << endl;
                //l[i].push_back(lll);
                //r[i].push_back(rr);
                //l[i][j] = lll;
                //r[i][j] = rr;
                int lgl = lg(lll);
                int lgr = lg(rr);
                //if(1 << (lgl) == lll)
                //  lll = lll + lgl;
                //else
                    lll = lll + lgl;
                if(1 << (lgr) == rr)
                    rr = rr + lgr + 1;
                else
                    rr = rr + lgr;
            }
            cout << ans.size() << endl;
        }
    }
    //for(int i = 1; i <= n; i++)
    //  sort(timeLine[i].begin(), timeLine[i].end(), cmp);          lazem nis!!:D
    /*
    for(int i = 0; i < m; i++)
        if(type[i] == 2)
        {
            ans.clear();
            for(int j = lr[i][0]; j <= n; j++)
            {
                //cerr << i << ", " << j << " : " << l[i][j] << ", " << r[i][j] << endl;
                for(int k = 0; k < timeLine[j].size(); k++)
                    if(((timeLine[j][k].pl >= l[i][j] && timeLine[j][k].pl <= r[i][j])||(timeLine[j][k].pl < l[i][j] && timeLine[j][k].lr > r[i][j])) && timeLine[j][k].ind < i)
                    {
                        ans.insert(timeLine[j][k].x);
                        //cerr << " : " << timeLine[j][k].x << endl;
                    }
            //  for(int k = l[i][j]; k <= r[i][j]; k++)
            //  {
            //
            //  }
            }
            cout << ans.size() << endl;
        }*/
}


#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e3 + 5;
vector<int> day[maxn];
int n, m;
int d[maxn], s[maxn], c[maxn];
int ans[maxn];

bool solve(){
    memset(ans, -1, sizeof(ans));
    for (int i = 1; i <= m; i++){
        day[d[i]].pb(i);
        ans[d[i]] = m +  1;
    }
    priority_queue<pair<int, pair<int, int> > > que;
    for (int i = n; i >= 1; i--){
        if (day[i].size() > 0){
            for (int j = 0; j < day[i].size(); j++){
                int p = day[i][j];
                que.push(mk(s[p], mk(c[p], p)));
            }
        }
        if (ans[i] != -1) continue;
        if (que.empty()){///休息
            ans[i] = 0;
            continue;
        }
        int spos = que.top().fi;
        int restday = que.top().se.fi;
        int pos = que.top().se.se;
        que.pop();
        if (spos > i && restday > 0) return false;
        restday--;
        ans[i] = pos;
        if (restday == 0){
            continue;
        }
        que.push(mk(spos, mk(restday, pos)));
    }
    if (!que.empty()) return false;
    for (int i = 1; i <= n; i++){
        printf("%d ", ans[i]);
    }
    cout << endl;
    return true;
}

int main(){
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        scanf("%d%d%d", s + i, d + i, c + i);
    }
    if (solve() == false) puts("-1");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){

    int i,j,k;
	cin>>i;
	for(j=1;j<=i;j++)
	{
		for(k=1;k<=j;k++)
		{
			if(j%k==0 && (k*j)>i && (j/k)<i){
			  cout<<j<<" "<<k;
			  exit(0);
			}
		}
	}

	cout<<-1;

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#define mod 1000000007
#define M 1000005
typedef long long ll;
typedef std::vector <int> Poly;
int n,m,P[M],C[M],maxn,ans;
ll T;
int read(){
	char c=getchar();int ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
ll read_ll(){
	char c=getchar();ll ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
void Write(int x){
	if (x<10) putchar(x^48);
	else Write(x/10),putchar((x%10)^48);
	return;
}
int power(ll x,int y){
	ll ans=1;
	for (;y;y>>=1,x=x*x%mod)
		if (y&1) ans=ans*x%mod;
	return ans;
}
int invs(int x){return x==1?1:(mod-mod/x+0ll)*invs(mod%x)%mod;}
int min(int x,int y){return x<y?x:y;}
int max(int x,int y){return x>y?x:y;}
int add(int x,int y){return (x+=y)>=mod?x-mod:x;}
int sub(int x,int y){return (x-=y)<0?x+mod:x;}
Poly operator+ (Poly A,Poly B){
	A.resize(max(A.size(),B.size()));B.resize(A.size());
	for (register int i=0;i<A.size();i++) A[i]=add(A[i],B[i]);
	return A;
}
Poly &operator+= (Poly &A,Poly B){return A=A+B;}
Poly operator* (Poly A,Poly B){
	Poly C;C.resize(A.size()+B.size()-1);
	for (register int i=0;i<A.size();i++)
		for (register int j=0;j<B.size();j++)
			C[i+j]=(C[i+j]+(A[i]+0ll)*B[j])%mod;
	return C;
}
Poly operator% (Poly A,Poly B){
	if (A.size()<B.size()) return A;
	for (register int i=A.size()-1;i>=B.size()-1;i--){
		int tmp=(mod-A[i]+0ll)*invs(B.back())%mod;
		for (register int j=i,k=B.size()-1;k>=0;j--,k--) A[j]=(A[j]+(tmp+0ll)*B[k])%mod;
	}
	A.resize(B.size()-1);return A;
}
Poly Power(Poly A,ll y,Poly Mod){
	Poly ans;ans.resize(Mod.size()-1);ans[0]=1;
	for (;y;y>>=1,A=A*A%Mod)
		if (y&1) ans=ans*A%Mod;
	return ans;
}
Poly get_X(){Poly ans;ans.resize(2);ans[0]=0,ans[1]=1;return ans;}
int main(){
	n=read(),m=read(),T=read_ll();
	for (register int i=1;i<=n;i++) P[i]=read();
	for (register int i=1,tmp;i<=m;i++) ++C[tmp=read()],maxn=max(maxn,tmp);
	Poly Mod,Ans,G;Mod.resize(maxn+1);Mod[maxn]=1;
	for (register int i=1;i<=maxn;i++) Mod[maxn-i]=sub(0,C[i]);
	for (register int i=1;i<=n;i++) Ans+=Power(get_X(),P[i],Mod);
	Ans=Power(Ans,T,Mod);G.resize(maxn);G[0]=1;
	for (register int i=1;i<maxn;i++)
		for (register int j=0;j<=i;j++) G[i]=(G[i-j]*(C[j]+0ll)+G[i])%mod;
	for (register int i=0;i<maxn;i++) ans=(ans+(G[i]+0ll)*Ans[i])%mod;
	Write(ans);
	return 0;
}


/** Libr@ries **/
#include "bits/stdc++.h"

/** System **/
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

/** System_Win_32 **/
#if ( WIN32 || __WIN32_ )
   #define lld I64d
#endif

/** Utilities **/
#define forit( i, v )    for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define fori( i, a, b )  for (int i = (int)(a); i < (int)(b); i++)
#define forn( i, n )     fori( i, 0, n )
#define zeros( a )       memset(a, 0,sizeof(a))
#define null( a )        memset(a,-1,sizeof(a))
#define all( a )         (a).begin() , (a).end()
#define sqr( a )         ( (a)*(a) )
#define sz( a )          (a).size()
#define pb               push_back
#define mp               make_pair
#define F                first
#define S                second
#define PI               2*acos(0.0)
#define inf              0x3f3f3f3f
using namespace std;

int c, d, n, m, k;

int solve(){
     int total = n * m - k;
     int ans = 1 << 30, res = 1;
     int a = 0, b = 1;
     while( b >= 0 ){
          b = total - n*a;
          if( b >= 0 )    ans = min( a*c + b*d, ans );
          else            ans = min( a*c, ans );
          //printf("when a = %d b = %d, ans = %d\n", a, b, ans );
          a++;
     }
     return ans;
}

main(){
     cin >> c >> d >> n >> m >> k;
     cout << solve();
}


#include<bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fori(a, l, f) for(int a = l; a<f; ++a)
#define ford(a, l, f) for(int a=l; a>=f; --a)
#define pq priority_queue
#define checkBit(mask, i) ((mask & (1 << i)) != 0) ? true:false
#define setBit(mask, i) mask = mask | (1 << i)
#define debug(x) cout<<x<<endl
#define fill(x, y) memset(x, y, sizeof x)

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int>    vi;
typedef vector<ii>    vii;
typedef vector<iii>  viii;
///////////////////////////////////////////////////////////////////////////////

int main(){
	ll n, m, k;
	scanf("%lld %lld %lld", &n, &m, &k);

	if(k < n) printf("%lld\n 1", k+1);
	else{
		k -= n;

		ll l = n - k/(m-1);
		ll s = k%(m-1);
		ll c;
		if(l % 2 == 0) c = 2 + s;
		else c = m-s;

		printf("%lld %lld\n", l, c);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
double P[30];
double dp[1<<20];
double R[30];

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>K;
	FOR(i,N) cin>>P[i];

	dp[0]=1;
	for(int mask=0;mask<1<<N;mask++) if(dp[mask]) {
		int ng=0;
		double el=1;
		FOR(i,N) if(mask&(1<<i)) el -= P[i];
		if(el<1e-9 || __builtin_popcount(mask)==K) {
			FOR(i,N) if(mask&(1<<i)) R[i]+=dp[mask];
		}
		else {
			FOR(i,N) if((mask&(1<<i))==0) dp[mask|(1<<i)] += dp[mask] * P[i]/el;
		}

	}

	FOR(i,N) _P("%.12lf%c",R[i],(i==N-1)?'\n':' ');
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


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

int main()
{
	int s, x, y, p, d;
	long long tram, igor;
	cin >> s >> x >> y;
	cin >> tram >> igor;
	cin >> p >> d;
	if(tram > igor)
	{
		cout << abs(y-x) * igor;
		return 0;
	}
	long long ans = abs(y-x) * igor;
	if(x <= y)
	{
		if(p <= x && d > 0)
		{
			ans = min(ans, (y - p)*tram);
		}
		else if(p <= x && d < 0)
		{
			ans = min(ans, (p + y)*tram);
		}
		else if(p > x && d > 0)
		{
			ans = min(ans, (s - p + s + y)*tram);
		}
		else if(p > x && d < 0)
		{
			ans = min(ans, (p + y)*tram);
		}
	}
	else
	{
		if(p < x && d > 0)
		{
			ans = min(ans, (s - p + s - y)*tram);
		}
		else if(p < x && d < 0)
		{
			ans = min(ans, (p + s + s - y)*tram);
		}
		else if(p >= x && d > 0)
		{
			ans = min(ans, (s - p + s - y)*tram);
		}
		else if(p >= x && d < 0)
		{
			ans = min(ans, (p - y)*tram);
		}
	}
	cout << ans;

}


#pragma comment(linker, "/STACK:20000000")
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <ctime>

using namespace std;
typedef long long int64;
#ifdef HOME
	#define E(c) cerr<<#c
	#define Eo(x) cerr<<#x<<" = "<<(x)<<endl
	#define Ef(...) fprintf(stderr, __VA_ARGS__)
#else
	#define E(c) ((void)0)
	#define Eo(x) ((void)0)
	#define Ef(...) ((void)0)
#endif

const int SIZE = 128;
const int MAXN = 24;
const int64 INF = 1000000000000000000LL;

int n, m;
int open[MAXN], close[MAXN];
int64 block[SIZE][SIZE];

int s;
struct Matr {
	int64 a[SIZE][SIZE];
};
Matr graph;

Matr Mult(Matr a, Matr b) {
	Matr res;
	for (int i = 0; i<s; i++)
		for (int j = 0; j<s; j++) {
			int64 ans = INF;
			for (int u = 0; u<s; u++)
				ans = min(ans, a.a[i][u] + b.a[u][j]);
			res.a[i][j] = ans;
		}
	return res;
}

Matr Identity() {
	Matr res;
	memset(&res, 63, sizeof(res));
	for (int i = 0; i<s; i++) res.a[i][i] = 0;
	return res;
}

Matr pwr(Matr a, int p) {
	Matr res = Identity();
	Matr x = a;
	for (; p; p>>=1) {
		if (p & 1) res = Mult(res, x);
		x = Mult(x, x);
	}
	return res;
}

int main() {
#ifdef HOME
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &m);
	for (int i = 0; i<n; i++) scanf("%d", &open[i]);
	for (int i = 0; i<n; i++) scanf("%d", &close[i]);

	memset(block, 63, sizeof(block));
	for (int mask = 0; mask < 1<<n; mask++) {
		int bal = 0;
		int minb = 0;
		int cst = 0;
		for (int i = 0; i<n; i++) {
			if (mask & (1<<i)) {
				bal++;
				cst += open[i];
			}
			else {
				bal--;
				minb = min(minb, bal);
				cst += close[i];
			}
		}

		auto& num = block[MAXN + bal][-minb];
		if (num > cst)
			num = cst;
	}

	s = 3*n+1;
	memset(&graph, 63, sizeof(graph));
	for (int i = 0; i<s; i++)
		for (int r = 0; r<=i; r++)
			for (int b = -n; b<=n; b++) {
				int64 cst = block[MAXN + b][r];
				if (cst > INF) continue;
				auto& num = graph.a[i][i+b];
				num = min(num, cst);
			}

/*	for (int i = 0; i<s; i++)
		for (int j = 0; j<s; j++)
			if (graph.a[i][j] < INF)
				Ef("%d -> %d in %d\n", i, j, graph.a[i][j]);*/

	Matr trans = pwr(graph, m);
	printf("%I64d\n", trans.a[0][0]);

	return 0;
}


#include <iostream>
using namespace std;
int main(){
    int T,S,Q;

    cin >> T >> S >> Q;

int cnt = 0;
while(S < T){
cnt++;
S = S*Q;
}
cout << cnt << endl;
}


#include <bits/stdc++.h>
#define MOD 100000000
typedef long long int ll;
typedef unsigned long long int ull;
#define INF 100000000000
#define N 2500001
#define qq cout<<'\n';
#define zz cout<<" ";
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

ll mod(ll a,ll m) {
  return a%m;
}

int cnt_sum(int n) {
    int sum=0;
    if(n<10) {
      return n;
    }
    while(n>9) {
        sum+=(n%10);
        n/=10;
    }
    sum+=n;
    return cnt_sum(sum);
}

void testcase() {
int n;
cin>>n;
cout<<cnt_sum(n);qq
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin>>t;
while(t--) {
  testcase();
}
return 0;
}


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

    int ans,n,cnt;
    int arr[2500][2500];
    int main()
    {
        cin >>n;
    for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++)
            {

                int t=i^j;
                if(i+j > t && t>i && t>j&&  i<=n && j<=n &&t<=n && !arr[i][j]) {arr[i][j]=t,arr[j][i]=t;
               ans++;
                }
}
}
        cout << ans << endl;
    }


#include <iostream>

using namespace std;
long long n,uc,ok,sol=1;
int main(){
    cin>>n;
    n=n+1;
    if(n<=10){
        cout<<9;
    }
    else{
        ok=0;
        while(ok==0){
            uc=n%10;
            while(uc!=0){
                n++;
                sol++;
                uc=n%10;
            }
            n=n/10;
            if(n<=10){
                ok=1;
                sol=sol+9;
            }
        }
        cout<<sol;
    }
    return 0;
}


#include<iostream>

using namespace std;
int main() {
    int n=0, m,i=0;
    cin >> n >> m;
    if (m % n != 0) {
        cout << "-1";
        return 0;
    }
    m /= n;
    while (m % 2 == 0) m /= 2, i++;
    while (m % 3 == 0) m /= 3, i++;
    if (m != 1)
        i = -1;
    cout << i;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

const int MOD = 32768;
int ans, v, f[MOD + 5], num, p, l;
string s[105], tmp;

int exp();

int getnum() {
  if (tmp[l] == 'n') {
    ++l;
    return v;
  }
  if (tmp[l] == 'f') {
    l += 2;
    int ret = f[exp()];
    l++;
    return ret;
  }
  int ret = 0;
  while (tmp[l] >= '0' && tmp[l] <= '9') ret = ret * 10 + (tmp[l++] - '0');
  return ret;
}

int sl() {
  int ret = getnum();
  while (tmp[l] == '*' || tmp[l] == '/') {
    char tp = tmp[l++];
    int t1 = getnum();
    if (tp == '*') {
      ret = (int)(((long long)ret * t1) % MOD);
    } else {
      ret = (ret / t1) % MOD;
    }
  }
  return ret;
}

int exp() {
  int ret = sl();
  while (tmp[l] == '+' || tmp[l] == '-') {
    char tp = tmp[l++];
    int t1 = sl();
    if (tp == '+')
      ret = (ret + t1) % MOD;
    else
      ret = (ret + MOD - t1) % MOD;
  }
  return ret;
}

bool iexp() {
  int p1, p2, tp;
  p1 = exp();
  switch (tmp[l]) {
    case '=': {
      tp = 0;
      l += 2;
      break;
    }
    case '<': {
      tp = -1;
      ++l;
      break;
    }
    case '>': {
      tp = 1;
      ++l;
      break;
    }
  }
  p2 = exp();
  if (tp == 0) return p1 == p2;
  if (tp == -1) return p1 < p2;
  if (tp == 1) return p1 > p2;
  return false;
}

int getans() {
  for (int i = 0; i < num; ++i) {
    tmp = s[i];
    if (tmp[0] == 'i') {
      l = 3;
      if (iexp()) {
        l += 7;
        return exp();
      }
    } else {
      l = 6;
      return exp();
    }
  }
  return -1;
}

int main() {
  char ch;
  scanf("%d", &ans);
  cin >> ch;
  while (ch != '{') cin >> ch;
  num = 0;
  while (true) {
    cin >> ch;
    if (ch == '}') break;
    if (ch == ';')
      ++num;
    else
      s[num] += ch;
  }

  int anst = -1;
  for (int i = 0; i <= 32767; ++i) {
    v = i;
    f[i] = getans();
    if (f[i] == ans) anst = i;
  }
  printf("%d\n", anst);
  return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
int n,i,j,k;
int main()
{
	int x,y;
	scanf("%d%d",&x,&y);
	if(x!=0&&y==1)
	{
		printf("No");
		return 0;
	}
	if(x<y-1||y==0)
	printf("No");
	else
	{
	if((x-y+1)%2==0)
	printf("Yes");
	else
	printf("No");}
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
	long long n, m;
	int a, b;
	scanf("%lld %lld %d %d", &n, &m, &a, &b);
	if (n % m == 0)
	{
		printf("0");
	}
	else if (n < m)
	{
		if (a * (m - n) <= b * n)
			printf("%lld", a * (m - n));
		else
			printf("%lld", b * n);
	}
	else
	{
		if (a * ((n / m + 1)* m - n) <= b * (n - n / m * m))
			printf("%lld", a * ((n / m + 1)* m - n));
		else
			printf("%lld", b * (n - n / m * m));
	}
}


#include<iostream>
using namespace std;
int main(){
long long int k,a,b;
cin>>k>>a>>b;
if((a/k)+(b/k)==0)
    cout<<"-1";
else
{
    if(a>=k&&b>=k)
        cout<<(a/k)+(b/k);
    else
    {
        if(a!=0&&b!=0)
        {
            if(a%k!=0&&b%k!=0)
                cout<<"-1";
            else cout<<(a/k)+(b/k);
        }
        else
        {
            if(a%k==0&&b%k==0)
                cout<<(a/k)+(b/k);
            else cout<<"-1";
        }
    }
}
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

long long n;
vector <long long> v;

void gen(long long a)
{
    if (a > 4444477777) return;
    long long x = a;
    int c4 = 0, c7 = 0;
    while(x)
    {
        long long l = x % 10;
        if (l == 4) ++c4;
        else if (l == 7) ++c7;
        x /= 10;
    }
    if (c4 == c7)
    {
        v.push_back(a);
    }
    gen(a * 10 + 4);
    gen(a * 10 + 7);
    return;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
    cin >> n;
    gen(0);
    sort(v.begin(), v.end());
    for (long long i : v)
    if (i >= n)
    {
        cout << i << endl;
        break;
    }
    return 0;
}


#include <iostream>

int main()
{
    int n; std::cin >> n;

    int res = 0;

    for (int i = 1; i < n; i++)
    {
        int x = i;
        bool b = true;
        for (int j = 0; j < n - 2; j++)
        {
            if (!((x - 1 + n) % n))
            {
                b = false;
                break;
            }
            x *= i;
            x %= n;
        }
        res += b && !((x - 1 + n) % n);
    }

    std::cout << res << std::endl;
}


#include<iostream>
using namespace std;
int isprime(int x)
{
    int y=2;
    if(x==1 || x==0)return 0;
    while(y<x)
    {
        if((x%y)==0)return 0;
        y++;
    }
    return 1;
}
int main()
{
    int n,m,w;
    cin>>n>>m;
    w=n+1;
    while(1)
    {
     if(isprime(w)==1)break;
     else w++;
    }
    if(w==m)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}


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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define hash dhsjakhd
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;

struct FT {
	vector<ll> s;
	FT(int n) : s(n) {}
	void update(int pos, ll dif) { // a[pos] += dif
		for (; pos < sz(s); pos |= pos + 1) s[pos] += dif;
	}
	ll query(int pos) { // sum of values in [0, pos)
		ll res = 0;
		for (; pos > 0; pos &= pos - 1) res += s[pos-1];
		return res;
	}
	int lower_bound(ll sum) {// min pos st sum of [0, pos] >= sum
		// Returns n if no sum is >= sum, or -1 if empty sum is.
		if (sum <= 0) return -1;
		int pos = 0;
		for (int pw = 1 << 25; pw; pw >>= 1) {
			if (pos + pw <= sz(s) && s[pos + pw-1] < sum)
				pos += pw, sum -= s[pos-1];
		}
		return pos;
	}
};

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 eng(seed);

uniform_int_distribution<> DD(0, (1ll << 31));

ll random2(){
    return (ll(DD(eng))*(1ll << 31) + ll(DD(eng))) & ((1ll << 62ll)-1);
}

ll n,m,T,k,q;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod =  998244353;

const ll MAXN = 500001;

vl x,X;
vl y,Y;
vl ind;

bool solve(set<pii> &sx, set<pii> &SX, set<pii> &sy, set<pii> &SY){
    if(sz(sx) <= 1)return 1;
    int N = sz(sx);
    auto itx = sx.begin();
    auto ITX = SX.begin();
    auto ity = sy.begin();
    auto ITY = SY.begin();
    ll mx = -big;
    ll MX = big;
    ll my = -big;
    ll MY = big;
    vi news;
    //cerr << N << "\n";
    rep(c1,0,N){

        pii px = *itx;
        if(itx != sx.begin() && px.first >= mx){
            auto it = sx.begin();
            while(it != itx){
                pii p = *it;
                news.push_back(p.second);
                it++;
            }
            break;
        }
        mx = max(mx, X[px.second]);
        itx++;

        pii py = *ity;
        if(ity != sy.begin() && py.first >= my){
            auto it = sy.begin();
            while(it != ity){
                pii p = *it;
                news.push_back(p.second);
                it++;
            }
            break;
        }
        my = max(my, Y[py.second]);
        ity++;

        pii PX = *ITX;
        if(ITX != SX.begin() && -PX.first <= MX){
            auto it = SX.begin();
            while(it != ITX){
                pii p = *it;
                news.push_back(p.second);
                it++;
            }
            break;
        }
        MX = min(MX, x[PX.second]);
        ITX++;

        pii PY = *ITY;
        if(ITY != SY.begin() && -PY.first <= MY){
            auto it = SY.begin();
            while(it != ITY){
                pii p = *it;
                news.push_back(p.second);
                it++;
            }
            break;
        }
        MY = min(MY, y[PY.second]);
        ITY++;

    }
    if(sz(news) == 0)return 0;
    set<pii> sx2,SX2,sy2,SY2;
    rep(c1,0,sz(news)){
        int a = news[c1];
        sx.erase({x[a],a});
        sx2.insert({x[a],a});

        SX.erase({-X[a],a});
        SX2.insert({-X[a],a});

        sy.erase({y[a],a});
        sy2.insert({y[a],a});

        SY.erase({-Y[a],a});
        SY2.insert({-Y[a],a});
    }
    return (solve(sx,SX,sy,SY) && solve(sx2,SX2,sy2,SY2));
}

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

    //freopen("input.txt","r",stdin);
   // freopen("autput.txt","w",stdout);

    ll a,b,c,d;
    cin >> n;
    set<pii> sx;
    set<pii> SX;
    set<pii> sy;
    set<pii> SY;

    rep(c1,0,n){
        cin >> a >> b >> c >> d;
        x.push_back(a);
        X.push_back(c);
        y.push_back(b);
        Y.push_back(d);
        ind.push_back(c1);
        sx.insert({a,c1});
        SX.insert({-c,c1});
        sy.insert({b,c1});
        SY.insert({-d,c1});
    }

    bool ans = solve(sx,SX,sy,SY);
    if(ans){
        cout << "YES\n";
    }
    else{
        cout << "NO\n";
    }

    return 0;
}


#include<iostream>
using namespace std;

int main(){
  int k,n,w, totalCost;
  cin>>k>>n>>w;
  totalCost = w*(w+1)/2*k;
  if(totalCost <= n) cout<<0;
  else cout<<totalCost - n;

}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define F(i,j,k) for(int i=(j);i<=(k);i++)
using namespace std;

namespace Base {
	inline char gc(void)
	{
		static char buf[100000], *p1 = buf, *p2 = buf;
		return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
	}

	template <class T> inline void read(T &x)
	{
		T flag = (T) 1; x = 0; static char ch = gc();
		for (; ch > '9' || ch < '0'; ch = gc())
			flag = ch == '-' ? -1 : 1;
		for (; ch >= '0' && ch <= '9'; ch = gc())
			x = (x << 1) + (x << 3) + (ch & 15);
		x *= flag; return;
	}

	int wbuf[25], _wl = 0;
	template <class T> inline void write(T x) {
		if (x == 0) { putchar(48); return; }
		if (x < 0) putchar('-'), x = -x; _wl = 0;
		while (x) wbuf[++_wl] = x % 10, x /= 10;
		for (int i = _wl; i >= 1; i--) putchar(wbuf[i] + 48);
	}

	template <class T> inline void writeln(T x) { write(x); puts(""); }
	template <class T> inline void writeln(T x, char c) { write(x); putchar(c); }
	template <class T> inline void writeln(char c, T x) { putchar(c); write(x); }
}

using namespace Base;

const int N=1e6+100;
int x[N],y[N],w[N],s[N],v[N];
bool in[N*2];
vector <pair<int,int> > e[N];
vector <int> res; int n, m;
int main(){
 	read(n), read(m);
 	F(i,1,m){
 		read(x[i]), read(y[i]); w[i]=1;s[x[i]]++;s[y[i]]++;
 		e[max(x[i],y[i])].push_back(make_pair(min(x[i],y[i]),i));
 	}
 	F(i,1,n){
 		for(auto it:e[i]){if(!v[it.first]){v[it.first]=1;w[it.second]=0;s[i]--;}in[s[it.first]]=1;}
 		for(auto it:e[i]){if(!in[s[i]])break;s[i]++;v[it.first]=0;w[it.second]++;}
 		for(auto it:e[i])in[s[it.first]]=0;
 	}
 	F(i,1,n)if(v[i])res.push_back(i);
 	writeln(int(res.size()));
 	for(auto it:res)writeln(it, ' ');puts("");
 	F(i,1,m) writeln(x[i], ' '), writeln(y[i], ' '), writeln(w[i]);
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define pob pop_back
#define ll long long
#define pll pair < ll, ll >
#define fi first
#define se second
#define sefi second.first
#define sese second.second

using namespace std;

ll n, s = 0, ans1 , r,  ans = 0, b, c, a;
int main(){
    cin >> n >> a >> b >> c;
    if(n >= b && a >= b - c){
        r = (n - b) / (b - c) + 1;
        ans += r;
        n -= r * (b - c);
    }
    ans += n / a;
    cout << ans;

}


#include <iostream>
//#include<bits/stdc++.h>
#include <algorithm>

using namespace std;

#pragma warning(disable:4996)

const int MAXN = 1e5 + 10;

long long n, m, ad[MAXN], ac[MAXN], bd[MAXN], bc[MAXN], indsa[MAXN], indsb[MAXN], pr[MAXN], d, prc[MAXN];

bool keya(int x, int y) {
	if (ac[x] == ac[y]) return ad[x] < ad[y];
	return ac[x] > ac[y];
}

bool keyb(int x, int y) {
	if (bc[x] == bc[y]) return bd[x] < bd[y];
	return bc[x] > bc[y];
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin >> n >> m >> d;
	for (int i = 0; i < n; i++) {
		cin >> ac[i] >> ad[i];
		indsa[i] = i;
	}
	for (int i = 0; i < m; i++) {
		cin >> bc[i] >> bd[i];
		indsb[i] = i;
	}
	sort(indsa, indsa + n, keya);
	sort(indsb, indsb + m, keyb);
	for (int i = 0; i < m; i++) {
		pr[i] = ((i > 0) ? pr[i - 1] : 0) + bd[indsb[i]];
		prc[i] = ((i > 0) ? prc[i - 1] : 0) + bc[indsb[i]];
	}
	long long kol = 0;
	long long ans = 0;
	long long koks = 0;
	for (int i = 0; i < n; i++) {
		kol += ad[indsa[i]];
		koks += ac[indsa[i]];
		int l = -1;
		int r = m;
		while (r - l > 1) {
			int mid = r + l >> 1;
			if (pr[mid] + kol <= d) {
				l = mid;
			}
			else {
				r = mid;
			}
		}
		if (l != -1) {
			ans = max(ans, koks + prc[l]);
			//cout << ans << " " << i << " " << l << "\n";
		}
	}
	cout << ans;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll M = 1e9+7;

ll pw(ll a, ll b){
	ll ret = 1;
	while(b){
		if(b&1){ ret = ret * a % M; }
		b>>=1;
		a = a * a % M;
	}
	return ret;
}

void solve(){
	ll n, m, k, r, c; cin >> n >> m >> k >> r >> c;
	ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
	if(x1 > x2)swap(x1,x2);
	if(y1 > y2)swap(y1,y2);
	ll ans = 1;
	ll sz1 = r*c, sz2 = sz1;
	if(x1+r-x2 > 0 && y1+c-y2 > 0) sz2-= (x1+r-x2)*(y1+c-y2);
	if(!sz2)ans*=(pw(k,sz1));
	else ans*=(pw(k,sz2));
	ans%=M;
	ans*=(pw(k,n*m-sz1-sz2));
	ans%=M;
	cout << ans << '\n';

}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}


#define _CRT_SECURE_NO_WARNINGS true

#include <stdio.h>
int main(){
	int n, s;
	scanf("%d%d", &n, &s);
	int max = 0;
	for (int i = 0; i < n; i++){
		int a;

		scanf("%d", &a);
		if (a>max)
			max = a;
		s -= a;
	}
	s += max;
	if (s < 0)
		printf("NO");
	else
		printf("YES");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int ans=0,num=0;
string a[50];
int main()
{
	int i,n,p;
	scanf("%d%d",&n,&p);
	p/=2;
	for(i=1;i<=n;i++)
	cin>>a[i];
	for(i=n;i>=1;i--)
	{
		num*=2;
		if(a[i]=="halfplus")
		num++;
		ans+=num;
	}
	ans*=p;
	printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,k;
	cin>>n>>k;
	long long minv=min(n,(long long)65000);
	for(long long i=minv;i>=0;i--)
	{
		if((i*i+i)/2-n+i==k)
		{
			cout<<n-i;
		}
	}
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{   string arrangement;
    int cnt = 0,i;
    cin >> arrangement;
    char value = arrangement[0];
    for(i = 0; i <arrangement.length(); i++)
    {
        if(value == arrangement[i])
        {
        cnt = cnt + 1;
        }
        else{   value = arrangement[i];
                cnt = 0;
                i--;
            }
        if(cnt == 7)
        {
            cout << "YES";
            return 0;
        }

    }
    cout << "NO";
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const double eps=1e-10;
const int SIZEN=110,SIZEM=5010;
void Gauss_Jordan(double A[SIZEN][SIZEN],double b[SIZEN],int N){
	//for(int i=1;i<=N;i++){for(int j=1;j<=N+1;j++) cout<<A[i][j]<<" ";cout<<endl;}cout<<endl;
	for(int j=1;j<=N;j++){
		int r=j;
		for(int i=j+1;i<=N;i++){
			if(fabs(A[i][j])>fabs(A[r][j])) r=i;
		}
		for(int k=1;k<=N+1;k++) swap(A[r][k],A[j][k]);
		if(fabs(A[j][j])<eps) continue;
		for(int i=1;i<=N;i++){
			if(i==j) continue;
			double t=A[i][j]/A[j][j];
			for(int k=1;k<=N+1;k++){
				A[i][k]-=A[j][k]*t;
			}
		}
		//for(int i=1;i<=N;i++){for(int j=1;j<=N+1;j++) cout<<A[i][j]<<" ";cout<<endl;}cout<<endl;
	}
	//for(int i=1;i<=N;i++){for(int j=1;j<=N+1;j++) cout<<A[i][j]<<" ";cout<<endl;}
	for(int i=1;i<=N;i++) b[i]=A[i][N+1]/A[i][i];
}
int N,M;
double A[SIZEN][SIZEN]={0};
int u[SIZEM],v[SIZEM],w[SIZEM];
double pot[SIZEN]={0};
double traffic[SIZEM];
int ufs[SIZEN]={0};
int grand(int x){
	return !ufs[x]?x:ufs[x]=grand(ufs[x]);
}
void merge(int x,int y){
	int gx=grand(x),gy=grand(y);
	if(gx!=gy) ufs[gx]=gy;
}
void work(void){
	A[1][1]=A[N][N]=1;
	A[1][N+1]=1;A[N][N+1]=0;
	for(int i=1;i<=M;i++){
		merge(u[i],v[i]);
		if(u[i]!=1&&u[i]!=N) A[u[i]][u[i]]++,A[u[i]][v[i]]--;
		if(v[i]!=1&&v[i]!=N) A[v[i]][u[i]]--,A[v[i]][v[i]]++;
	}
	if(grand(1)!=grand(N)){
		for(int i=1;i<=M+1;i++) printf("%.9lf\n",0);
		return;
	}
	Gauss_Jordan(A,pot,N);
	//for(int i=1;i<=N;i++) cout<<pot[i]<<" ";cout<<endl;
	double magnify=1e18;
	for(int i=1;i<=M;i++){
		magnify=min(magnify,fabs(w[i]/(pot[u[i]]-pot[v[i]])));
	}
	double ans=0;
	for(int i=1;i<=M;i++){
		traffic[i]=(pot[u[i]]-pot[v[i]])*magnify;
		if(u[i]==1) ans+=traffic[i];
		if(v[i]==1) ans-=traffic[i];
	}
	printf("%.9lf\n",ans);
	for(int i=1;i<=M;i++) printf("%.9lf\n",traffic[i]);
}
void read(void){
	scanf("%d%d",&N,&M);
	for(int i=1;i<=M;i++)
		scanf("%d%d%d",&u[i],&v[i],&w[i]);
}
int main(){
	//freopen("t1.in","r",stdin);
	read();
	work();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
using lint = long long;
using pi = pair<int, int>;

struct edg{
	int pos; lint cst;
	int idx;
};

int n, q; lint w;
vector<edg> gph[MAXN];
vector<int> updList[MAXN];
bool vis[MAXN];

struct segtree{
	vector<lint> tree, lazy;
	int lim;
	void init(int n){
		for(lim = 1; lim <= n; lim <<= 1);
		tree.resize(2 * lim);
		lazy.resize(2 * lim);
	}
	void lazydown(int p){
		lint cur = lazy[p]; lazy[p] = 0;
		for(int j=0; j<2; j++){
			lazy[2*p+j] += cur;
			tree[2*p+j] += cur;
		}
	}
	void add(int s, int e, int ps, int pe, int p, lint v){
		if(e < ps || pe < s) return;
		if(s <= ps && pe <= e){
			tree[p] += v;
			lazy[p] += v;
			return;
		}
		int pm = (ps+pe)/2;
		lazydown(p);
		add(s, e, ps, pm, 2*p, v);
		add(s, e, pm+1, pe, 2*p+1, v);
		tree[p] = max(tree[2*p], tree[2*p+1]);
	}
	lint query(int s, int e, int ps, int pe, int p){
		if(e < ps || pe < s) return -1e18;
		if(s <= ps && pe <= e) return tree[p];
		int pm = (ps + pe) / 2;
		lazydown(p);
		return max(query(s, e, ps, pm, 2*p), query(s, e, pm+1, pe, 2*p+1));
	}
};

struct dcmp{
	int idx;
	map<int, int> din, dout;
	int piv;
	vector<lint> ds;
	vector<pi> intv;
	vector<lint> assoc;
	multiset<lint> maxVal;
	lint ans;

	segtree DS;
}cd[MAXN];

priority_queue<pair<lint, int>> pq;

lint query(){
	while(!pq.empty() && cd[pq.top().second].ans != pq.top().first){
		pq.pop();
	}
	if(pq.empty()) return 0;
	return pq.top().first;
}

void update(int e, int x, int y, lint d){
	for(auto &i : updList[e]){
		if(cd[i].din[x] > cd[i].din[y]) swap(x, y);
		int l = cd[i].din[y];
		int r = cd[i].dout[y];

		cd[i].DS.add(l, r - 1, 0, cd[i].piv - 1, 1, d);

		int pos = lower_bound(cd[i].intv.begin(), cd[i].intv.end(), pi(l, 1e9))
		- cd[i].intv.begin() - 1;
		cd[i].maxVal.erase(cd[i].maxVal.find(cd[i].assoc[pos]));

		cd[i].assoc[pos] =
		cd[i].DS.query(cd[i].intv[pos].first, cd[i].intv[pos].second - 1,
		0, cd[i].piv - 1, 1);

		cd[i].maxVal.insert(cd[i].assoc[pos]);
		assert(cd[i].maxVal.size());
		if(cd[i].maxVal.size() == 1) cd[i].ans = *cd[i].maxVal.rbegin();
		else{
			auto cur = prev(cd[i].maxVal.end());
			cd[i].ans = *cur + *prev(cur);
		}
		pq.emplace(cd[i].ans, i);
	}
}

void dfs(int x, int p, dcmp &cd){
	cd.din[x] = cd.piv++;
	for(auto &i : gph[x]){
		if(i.pos != p && !vis[i.pos]){
			updList[i.idx].push_back(cd.idx);
			dfs(i.pos, x, cd);
		}
	}
	cd.dout[x] = cd.piv;
}

void sangsoo(int x, int p, dcmp &cd){
	for(auto &i : gph[x]){
		if(i.pos != p && !vis[i.pos]){
			cd.DS.add(cd.din[i.pos], cd.dout[i.pos] - 1, 0, cd.piv - 1, 1, i.cst);
			sangsoo(i.pos, x, cd);
		}
	}
}

void build(int v, dcmp &cd){
	cd.idx = v;
	dfs(v, -1, cd);
	cd.DS.init(cd.piv);
	sangsoo(v, -1, cd);
	for(auto &i : gph[v]){
		if(vis[i.pos]) continue;
		cd.intv.emplace_back(cd.din[i.pos], cd.dout[i.pos]);
	}
	sort(cd.intv.begin(), cd.intv.end());
	for(int i=0; i<cd.intv.size(); i++){
		int l = cd.intv[i].first, r = cd.intv[i].second;
		lint ans = cd.DS.query(l, r - 1, 0, cd.piv - 1, 1);
		cd.maxVal.insert(ans);
		cd.assoc.push_back(ans);
	}
	if(cd.maxVal.size() == 0) cd.ans = 0;
	else if(cd.maxVal.size() == 1) cd.ans = *cd.maxVal.rbegin();
	else{
		auto cur = prev(cd.maxVal.end());
		cd.ans = *cur + *prev(cur);
	}
	pq.emplace(cd.ans, v);
}

vector<int> dfn;
int sz[MAXN], msz[MAXN];

void dfsc(int x, int p){
	dfn.push_back(x);
	sz[x] = 1;
	msz[x] = 0;
	for(auto &i : gph[x]){
		if(!vis[i.pos] && i.pos != p){
			dfsc(i.pos, x);
			sz[x] += sz[i.pos];
			msz[x] = max(msz[x], sz[i.pos]);
		}
	}
}

int get_center(int x){
	dfsc(x, -1);
	pi ret(1e9, 1e9);
	for(auto &i : dfn){
		int mxv = max(msz[i], (int)dfn.size() - sz[i]);
		ret = min(ret, pi(mxv, i));
	}
	dfn.clear();
	return ret.second;
}

int main(){
	lint w;
	scanf("%d %d %lld",&n,&q,&w);
	vector<int> S(n - 1), E(n - 1);
	vector<lint> X(n - 1);
	for(int i=0; i<n-1; i++){
		int s, e; lint x;
		scanf("%d %d %lld",&s,&e,&x);
		gph[s].push_back({e, x, i});
		gph[e].push_back({s, x, i});
		tie(S[i], E[i], X[i]) = make_tuple(s, e, x);
	}
	queue<int> que;
	que.push(1);
	while(!que.empty()){
		int x = que.front(); que.pop();
		x = get_center(x);
		build(x, cd[x]);
		vis[x] = 1;
		for(auto &i : gph[x]){
			if(!vis[i.pos]){
				que.push(i.pos);
			}
		}
	}
	lint last = 0;
	while(q--){
		lint d, e; scanf("%lld %lld",&d,&e);
		d = (d + last) % (n - 1);
		e = (e + last) % w;
		update(d, S[d], E[d], e - X[d]);
		X[d] = e;
		last = query();
		printf("%lld\n", last);
	}
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gi()
{
	char c;int num=0,flg=1;
	while((c=getchar())<'0'||c>'9')if(c=='-')flg=-1;
	while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
	return num*flg;
}
#define N 300005
int fir[N],to[4*N],nxt[4*N],cnt;
void adde(int a,int b)
{
	to[++cnt]=b;nxt[cnt]=fir[a];fir[a]=cnt;
	to[++cnt]=a;nxt[cnt]=fir[b];fir[b]=cnt;
}

int bel[N];// !!!
namespace BBC{
	int dfn[N],low[N],bbcno[N],stk[N],top,dc,bbccnt;
	void dfs(int u,int ff){
		dfn[u]=low[u]=++dc;
		stk[top++]=u;
		for(int v,p=fir[u];p;p=nxt[p]){
			if((p^1)==ff)continue;
			v=to[p];
			if(!dfn[v]){dfs(v,p);low[u]=min(low[u],low[v]);}
			else low[u]=min(low[u],dfn[v]);
		}
		if(dfn[u]==low[u]){
			bbccnt++;
			while(top>0){
				bbcno[stk[--top]]=bbccnt;
				if(stk[top]==u)
					break;
			}
		}
	}
}//----BBC----

int root[N],nowrt,dep[N],fa[N],siz[N],son[N],top[N];
void dfs1(int u)
{
	root[u]=nowrt;
	dep[u]=dep[fa[u]]+1;siz[u]=1;
	for(int v,p=fir[u];p;p=nxt[p]){
		if((v=to[p])!=fa[u]){
			fa[v]=u;dfs1(v);
			siz[u]+=siz[v];
			if(siz[son[u]]<siz[v])
				son[u]=v;
		}
	}
}
int dfn[N],dc;
void dfs2(int u)
{
	dfn[u]=++dc;
	if(son[u])top[son[u]]=top[u],dfs2(son[u]);
	for(int v,p=fir[u];p;p=nxt[p])
		if((v=to[p])!=fa[u]&&v!=son[u])
			top[v]=v,dfs2(v);
}
int LCA(int x,int y)
{
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])swap(x,y);
		x=fa[top[x]];
	}
	return dep[x]<dep[y]?x:y;
}
int dl[N],dcnt,stk[N],tp;bool ont[N];
bool cmp(int x,int y){return dfn[x]<dfn[y];}
void build()
{
	tp=0;sort(dl+1,dl+dcnt+1,cmp);
	int i,u,lca,tmp=dcnt;
	//if(dl[1]!=1)dl[++dcnt]=stk[++tp]=1;
	for(i=1;i<=tmp;i++){
		u=dl[i];ont[u]=1;
		if(root[u]!=root[dl[i-1]]){
			while(tp>1)adde(stk[tp-1],stk[tp]),tp--;tp=0;
			if(u!=root[u])dl[++dcnt]=stk[++tp]=root[u];
		}
		if(i<=1||(lca=LCA(stk[tp],u))==stk[tp]){stk[++tp]=u;continue;}
		while(tp>1&&dep[stk[tp-1]]>=dep[lca]){adde(stk[tp-1],stk[tp]);tp--;}
		if(lca!=stk[tp]){adde(lca,stk[tp]);stk[tp]=lca;dl[++dcnt]=lca;}
		stk[++tp]=u;
	}
	while(tp>1)adde(stk[tp-1],stk[tp]),tp--;
}
void CL()
{
	for(int i=1,u;i<=dcnt;i++){
		fir[u=dl[i]]=0;
		BBC::dfn[u]=BBC::bbcno[u]=BBC::low[u]=0;
	}
	cnt=1;dcnt=0;BBC::dc=BBC::top=BBC::bbccnt=0;
}
struct enode{int u,v;}e[N],etmp[N];
bool pd[N];int ptmp[N];
int main()
{
	//freopen("3.in","r",stdin);
	cnt=1;
	int n,m,Q,i,u,v;
	n=gi();m=gi();Q=gi();
	for(i=1;i<=m;i++){
		u=gi();v=gi();adde(u,v);
		e[i].u=u;e[i].v=v;
	}
	for(i=1;i<=n;i++)if(!dfn[i])BBC::dfs(i,0);
	cnt=1;memset(fir,0,sizeof(fir));
	memcpy(bel,BBC::bbcno,sizeof(BBC::bbcno));
	//printf("bel:");for(i=1;i<=n;i++)printf("%d ",bel[i]);printf("\n");

	for(i=1;i<=m;i++){
		int p=bel[e[i].u],q=bel[e[i].v];
		if(p!=q)adde(p,q);//,printf("adde:%d %d\n",p,q);
	}
	for(i=1;i<=BBC::bbccnt;i++){
		if(root[i])continue;
		nowrt=i;dfs1(i);top[i]=i;dfs2(i);
	}
	//printf("root:");for(i=1;i<=BBC::bbccnt;i++)printf("%d ",root[i]);printf("\n");

	memset(BBC::dfn,0,sizeof(BBC::dfn));
	memset(BBC::low,0,sizeof(BBC::low));
	memset(BBC::bbcno,0,sizeof(BBC::bbcno));
	BBC::dc=BBC::top=BBC::bbccnt=0;
	cnt=1;memset(fir,0,sizeof(fir));
	int R=0,nn,mm,cas=0;
	while(Q--){
		cas++;
		nn=gi();mm=gi();
		for(i=1;i<=nn;i++){
			u=ptmp[i]=bel[(gi()+R-1)%n+1];
			if(!pd[u])dl[++dcnt]=u,pd[u]=1;
		}
		for(i=1;i<=mm;i++){
			etmp[i].u=u=bel[(gi()+R-1)%n+1];
			etmp[i].v=v=bel[(gi()+R-1)%n+1];
			if(!pd[u])dl[++dcnt]=u,pd[u]=1;
			if(!pd[v])dl[++dcnt]=v,pd[v]=1;
		}
		build();
		for(i=1;i<=mm;i++){
			int p=etmp[i].u,q=etmp[i].v;
			if(p!=q)adde(p,q);//,printf("adde:%d %d\n",p,q);
		}
		for(i=1;i<=dcnt;i++)
			if(!BBC::dfn[dl[i]])BBC::dfs(dl[i],0);
		//printf("bbcno:");for(i=1;i<=n;i++)printf("%d ",BBC::bbcno[i]);printf("\n");
		bool flg=0;
		for(i=1;i<=nn;i++)
			if(BBC::bbcno[ptmp[i]]!=BBC::bbcno[ptmp[1]]){flg=1;break;}
		if(!flg){printf("YES\n");R=(R+cas)%n;}
		else printf("NO\n");
		CL();
		for(i=1;i<=nn;i++)pd[ptmp[i]]=0;
		for(i=1;i<=mm;i++)pd[etmp[i].u]=pd[etmp[i].v]=0;
	}
}


#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
	int n,i,j,k,g=0;
	scanf("%d",&n);
	int a[n+1],c[n+1],h=0;
    	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		c[i]=0;
	}c[i+1]=0;
	a[i+1]=0;
	sort(a,a+n);
		for(j=0;j<n;j++)
	{	if(a[j]==a[j+1])
		{
			c[g]++;
		}
		else
		{
		c[g]++;
		g++;
		}
	}
sort(c,c+g);
printf("%d\n",c[g-1]);
return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,t0=0,t1=0;
	char a[101];
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(a[i]=='0')
			t0++;
		else
			t1++;
	}
	if(t0==t1)
	{
		cout<<2<<endl;
		cout<<a[0]<<' ';
		for(int i=1;i<n;i++)
			cout<<a[i];
	}
	else
	{
		cout<<1<<endl;
		for(int i=0;i<n;i++)
			cout<<a[i];
	}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int num,n=1,m=1;
	cin>>num;
	for(int i=1;;i++)
	{
		n=pow(2,2*i-1)-(pow(2,i-1));
		if(n<=num && num%n==0)
			m=n;
		else if(n>num)
			break;
	}
	cout<<m;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e8;

set<int> a;

void work(int l, int r) {
  int m = (l + r) / 2, d, d2;
  if (m == l || m == r)
    return;
  printf("0 %d %d\n", m, m);
  fflush(stdout);
  scanf("%d", &d);
  if (m - d > l) {
    printf("0 %d %d\n", m - d, m - d);
    fflush(stdout);
    scanf("%d", &d2);
    if (!d2) {
      a.insert(m - d);
      work(l, m - d);
      work(m - d, r);
      return;
    }
  }
  if (m + d < r) {
    printf("0 %d %d\n", m + d, m + d);
    fflush(stdout);
    scanf("%d", &d2);
    if (!d2) {
      a.insert(m + d);
      work(l, m + d);
      work(m + d, r);
      return;
    }
  }
}

int main() {
  int dx, dy;
  printf("0 %d %d\n0 %d %d\n", -N, -N, N, N);
  fflush(stdout);
  scanf("%d%d", &dx, &dy);
  dx = dx - N;
  dy = N - dy;
  a.insert(dx);
  a.insert(dy);
  work(dx, dy);

  int z = 0;
  while (a.count(z))
    ++z;
  vector<int> x, y;
  for (int i : a) {
    printf("0 %d %d\n0 %d %d\n", i, z, z, i);
    fflush(stdout);
    scanf("%d%d", &dx, &dy);
    if (!dx)
      x.push_back(i);
    if (!dy)
      y.push_back(i);
  }
  printf("1 %d %d\n", (int)x.size(), (int)y.size());
  for (int i : x)
    printf("%d ", i);
  puts("");
  for (int i : y)
    printf("%d ", i);
  puts("");
  return 0;
}


using namespace std;
#include<string.h>
#include <sstream>
typedef long long int ll;
typedef long double ld;
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <map>
//#define N 4
#include <queue>
#include <unordered_set>
#include <math.h>
#include <cstring>
#include <bitset>
#include <numeric>
#include <set>
#include <sstream>
#include <list>
#include <queue>
#define PI 3.14159265358979323846264338327950
#define mp make_pair
#define inf 1e9
#define M 1000000007
#define N 100005

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);

	int a, b, c;
	cin >> a >> b >> c;

	for (int x = 0; x <= 1e6; x++) {
		int y = b - x;
		int z = a - x;

		if (x >= 0 && y >= 0 && z >= 0 && z + y == c) {
			cout << x << " " << y << " " << z << endl;
			return 0;
		}
	}

	cout << "Impossible" << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define pb push_back
#define READ(FILE) freopen(FILE,"r",stdin)
#define WRITE(FILE) freopen(FILE,"w",stdout)
using namespace std;
void mego()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

}
ll calc(ll x)
{
    return ((x*(x+1))/2);
}
int main()
{

    mego();
    int n,m,k=0,x,y,cost=0;
    cin>>n;
vector<int> v;
for(int i=0;i<n;i++)
{
    cin>>x;
    v.pb(x);
}
sort(v.begin(),v.end());
for(int i=0;i<n-2;i++)
    if(v[i]+v[i+1] >v[i+2] && v[i]+v[i+2] >v[i+1])
return  cout<<"YES",0;
cout<<"NO";
  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <string>
#include <string.h>
#include <ctype.h>
#include <iomanip>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
#include <stack>
#include <cstdio>

using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef pair<string, int> psi;

#define pb push_back
#define mp make_pair
#define eps 1e-7
#define PI  3.14159265358979323846
#define X first
#define Y second
#define ACCEPTED return 0;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int usab[6][8] = {
		{ 3, 3, 0, 4, 4, 0, 3, 3 },
		{ 3, 3, 0, 4, 4, 0, 3, 3 },
		{ 2, 2, 0, 3, 3, 0, 2, 2 },
		{ 2, 2, 0, 3, 3, 0, 2, 2 },
		{ 1, 1, 0, 2, 2, 0, 1, 1 },
		{ 1, 1, 0, 2, 2, 0, 1, 1 }
	};
	string A[6];
	int max = 0, x, y;
	for (int i = 0; i < 6; i++) cin >> A[i];
	for (int i = 0; i < 6; i++)
		for (int j = 0; j < 8; j++)
			if (A[i][j] == '.' && usab[i][j] > max) {
				max = usab[i][j];
				x = i;
				y = j;
			}
	A[x][y] = 'P';
	for (int i = 0; i < 6; i++) cout << A[i] << endl;
	ACCEPTED
}


#include<bits/stdc++.h>

using namespace std ;

int main()
{
    string f, m , s ;
    cin>>f ;
    cin>>m;
    cin>>s ;

    if( (f=="rock"&& m=="scissors" && s=="scissors") ||
            (f=="paper" && m=="rock" && s=="rock")
             || (f=="scissors" && m=="paper" && s=="paper")  )
       {
           cout<<"F" <<endl ;
       }

       else if( (m=="rock" && f=="scissors" && s=="scissors") ||
             (m=="paper" && f=="rock" && s=="rock")
             || (m=="scissors" && f=="paper" && s=="paper")  )
                {
                    cout<<"M"<<endl ;
                }

      else if( (s=="rock" && f=="scissors" && m=="scissors") ||
            ( s=="paper" && f=="rock" && m=="rock")
             || (s=="scissors" && f=="paper" && m=="paper")  )
                {
                    cout<<"S"<<endl ;
                }
        else
        {
            cout<<"?" <<endl ;
        }
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 400000;
ll x[N + 1];

void solve(){
	ll n, k;
	int m;
	scanf("%lld%d%lld", &n, &m, &k);
	x[0] = 0;
	f(i, 1, m + 1)scanf("%lld", x + i);
	f(i, 0, m + 1)x[i] = (i == m ? n : x[i + 1]) - x[i];
	++m;
	k += m;

	auto sm1 = [&](ll n){
		ll an = 0;
		f(i, 0, m)an += x[i] / n;
		return an;
	};
	auto sm2 = [&](ll n){
		ll an = 0;
		f(i, 0, m)an += (x[i] + n - 1) / n;
		return an;
	};

	ll l = 1, r = *min_element(x, x + m);
	while (r > l){
		ll m = l + r + 1 >> 1;
		if (sm1(m) < k)r = m - 1;
		else l = m;
	}
	ll L = l;

	l = 1, r = n;
	while (r > l){
		ll m = l + r >> 1;
		if (sm2(m) > k)l = m + 1;
		else r = m;
	}
	ll R = l;

	multiset<ll> st;
	st.insert(L);
	st.insert(R);
	vector<pair<ll, ll> > v;
	f(i, 0, m)if (x[i] / L < (x[i] + R - 1) / R){
		ll a = (x[i] + R - 1) / R, b = x[i] / L;
		v.emplace_back(x[i] / a, (x[i] + b - 1) / b);
		st.insert(v.back().first);
	}
	ll an = *st.rbegin() - *st.begin();
	sort(v.begin(), v.end());
	for (auto &u: v){
		st.erase(st.find(u.first));
		st.insert(u.second);
		an = min(an, *st.rbegin() - *st.begin());
	}
	printf("%lld\n", an);
}

int main(){
	int t;
	scanf("%d", &t);
	while (t--)solve();
}


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

int main () {
    int n, val = 0;
    cin >> n;

    for(int i=3; i<=n; i++) {
        if (i == 3) {
            val = 1*2*3;
        }else {
            val = val + (i * (i-1));
        }
    }

    cout << val;

    return 0;
}


#define frp

#include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
#include <iomanip>

using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x7fffff;
const int maxn = 2e6;
const int MAXN = 100000 + 5;
const int MOD = 1e9 + 7;

ll n,num=1,tot;
void solve() {
    cin>>n;
    tot=n;
    for (int i = 2; i < n+1; ++i) {//通过枚举，得到最小分解底数
        if(tot%i==0){
            num*=i;
        }
        while(tot%i==0){
            tot/=i;
        }
    }
    int ans=0,tmp=num;
    while(num%n){
        num*=num;
        ans++;
    }
    cout<<tmp<<" "<<(num>n?ans+1:ans)<<endl;
    num=1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("D:\\coding\\c_coding\\in.txt", "r", stdin);
//    freopen("D:\\coding\\c_coding\\out.txt", "w", stdout);
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
string sss;
map<char,bool> p;
int main()
{
    ios::sync_with_stdio(false);
    p['A']=1;
    p['E']=1;
    p['F']=1;
    p['H']=1;
    p['I']=1;
    p['K']=1;
    p['L']=1;
    p['M']=1;
    p['N']=1;
    p['T']=1;
    p['V']=1;
    p['W']=1;
    p['X']=1;
    p['Y']=1;
    p['Z']=1;
    cin>>sss;
    int sum=0;
    for(int i=0;i<sss.size();i++) sum+=p[sss[i]];
    if(sum==sss.size() or sum==0) cout<<"YES";
    else cout<<"NO";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
int L[202020],R[202020];
int A[202020],B[202020],C[202020];
ll ret;
int idx,idy;

set<pair<int,int> > Ls,Rs;
vector<pair<int,int> > events;
vector<int> P;

template<class V,int NV> class SegTree_1 {
public:
	vector<V> val;
	static V const def=0;
	V comp(V l,V r){ return max(l,r);};

	SegTree_1(){val=vector<V>(NV*2,def);};
	V getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=v;
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]);
	}
};

SegTree_1<ll,1<<20> ST;
map<int,int> MM;

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N>>M;
	FOR(i,N) {
		cin>>L[i]>>R[i];
		if(L[i]<R[i]) {
			events.push_back({L[i],i+300000});
			events.push_back({R[i],i+300000});
		}
		P.push_back(L[i]);
		P.push_back(R[i]);
	}
	FOR(i,M) {
		cin>>A[i]>>B[i]>>C[i];
		if(A[i]<B[i]) {
			events.push_back({A[i],i});
			events.push_back({B[i],i});
		}
		P.push_back(A[i]);
		P.push_back(B[i]);
	}
	sort(ALL(events));
	sort(ALL(P));
	P.erase(unique(ALL(P)),P.end());

	FORR(e,events) {
		int pos=e.first;
		r = e.second;
		if(r<300000) {
			if(A[r]==pos) {
				if(Rs.size()) {
					ll len = min(B[r],Rs.rbegin()->first)-pos;
					if(ret<len*C[r]) {
						ret=len*C[r];
						idx=Rs.rbegin()->second;
						idy=r;
					}
				}
			}
			else if(B[r]==pos) {
				if(Ls.size()) {
					ll len = pos-max(A[r],Ls.begin()->first);
					if(ret<len*C[r]) {
						ret=len*C[r];
						idx=Ls.begin()->second;
						idy=r;
					}
				}
				x = lower_bound(ALL(P),A[r])-P.begin();
				y = lower_bound(ALL(P),B[r])-P.begin();
				ll len = ST.getval(x,y);
				if(ret<len*C[r]) {
					ret=len*C[r];
					idx=MM[len];
					idy=r;
				}
			}
		}
		else {
			r-=300000;
			if(L[r]==pos) {
				Ls.insert({L[r],r});
				Rs.insert({R[r],r});
			}
			else {
				l = lower_bound(ALL(P),L[r])-P.begin();
				int v=ST.getval(l,l+1);
				if(R[r]-L[r]>v) {
					v=R[r]-L[r];
					ST.update(l,v);
					MM[v]=r;
				}

				Ls.erase({L[r],r});
				Rs.erase({L[r],r});
			}
		}
	}

	if(ret) {
		cout<<ret<<endl;
		cout<<(idx+1)<<" "<<(idy+1)<<endl;
	}
	else {
		cout<<0<<endl;
	}

}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        if(n%2==0)
        {
            cout<<n/2<<" "<<n/2<<'\n';
        }
        else
        {
            ll div=1;
            for(ll i=3;i*i<=n;i+=2)
            {
                if(n%i==0)
                {
                    div=n/i;
                    break;
                }
            }
            if(div==1)
            {
                cout<<1<<" "<<n-1<<'\n';
            }
            else
            {
                cout<<div<<" "<<n-div<<'\n';
            }
        }
    }
}


#include <stdio.h>
#include <iostream>

using namespace std;

main(){
    // freopen("80b.txt","r",stdin);
    int ora,perc;
    scanf("%d:%d",&ora,&perc);
    if (ora>=12){
        ora=ora-12;
    }
    double sol1=1.0*((360/12)*ora+perc/2);
    cout.precision(10);
    if (perc%2==1){
        sol1+=0.5;
    }
    cout<<sol1<<" ";
    cout<<6*perc<<endl;
}


#pragma GCC target("avx,avx2")
#include<bits/stdc++.h>
#include<immintrin.h>
using namespace std;

template <typename T> void chmin(T&x,const T &y)
{
	if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
	if(x<y)x=y;
}
typedef int64_t s64;
typedef uint64_t u64;
typedef uint32_t u32;
typedef pair<int,int> pii;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
#define rep0(i,l,r) for(int i=l;i<r;++i)
#define gc (c=getchar())
int read()
{
	char c;
	while(gc<'-');
	if(c=='-')
	{
		int x=gc-'0';
		while(gc>='0')x=x*10+c-'0';
		return -x;
	}
	int x=c-'0';
	while(gc>='0')x=x*10+c-'0';
	return x;
}
#undef gc

const int N=1e5+5;
int n;
map<int,int> Map_A;
map<int,map<int,int>> Map_V;
namespace BIT
{
//kx+b
s64 s0[N],s1[N];//sum_b,sum_k
void A(int i,int k,int b)
{
	for(;i<=n;i+=i&-i)
	{
		s1[i]+=k;
		s0[i]+=b;
	}
}
void add(int l,int r,int k,int b)
{
	if(l>r)return ;
	A(l,k,b);
	A(r+1,-k,-b);
}
s64 query(int x)
{
	s64 k=0,b=0;
	for(int i=x;i;i-=i&-i){k+=s1[i];b+=s0[i];}
	return k*x+b;
}
};

void add_ij(int i,int j,int w)
{
	assert(j<i);
	int mn=n-i+1,mx=i-j;
	if(mx<mn)swap(mn,mx);
	BIT::add(mx,n-j,-1*w,(n-j+1)*w);
	BIT::add(mn,mx-1,0,mn*w);
	BIT::add(1,mn-1,1*w,0);
}
void add_lr(int l,int r,int w)
{
	++l;
	if(l>r)return ;
	BIT::add(1,n-r+1,0,(r-l+1)*w);
	BIT::add(n-r+2,n-l+1,-1*w,(n-l+1+1)*w);
}
void ins(map<int,int> &Map,int l,int r)
{
	auto it=Map.lower_bound(l);
	int r1=0,l1=0;
	if(it!=Map.end())
		add_ij(r1=it->first,r,1);
	if(it!=Map.begin())
		l1=(--it)->second;

	add_ij(l,l1,1);
	if(r1)add_ij(r1,l1,-1);

	add_lr(l,r,1);
	Map[l]=r;
}
void del(map<int,int> &Map,int l,int r)
{
	auto split=[&](int p)
	{
		auto it=Map.lower_bound(p);
		if(it==Map.begin())return ;
		--it;
		if(it->second>=p)
		{
			Map[p]=it->second;
			it->second=p-1;
		}
	};
	split(l);split(r+1);

	auto it=Map.lower_bound(l);
	if(it==Map.end())
	{
		cerr<<"it==Map.end() "<<l<<" "<<r<<endl;
		for(auto p:Map)cerr<<p.first<<" "<<p.second<<endl;
		assert(0);
	}
	if(it->second!=r)
	{
		cerr<<"it->second!=r "<<l<<" "<<it->second<<" "<<r<<endl;
		assert(0);
	}
	Map.erase(it);

	int l1=0,r1=0;
	it=Map.lower_bound(r+1);
	if(it!=Map.end())
		add_ij(r1=it->first,r,-1);
	if(it!=Map.begin())
		l1=(--it)->second;
	add_ij(l,l1,-1);
	if(r1)add_ij(r1,l1,1);

	add_lr(l,r,-1);
}
void split(int p)//split p-1 and p
{
	auto it=Map_A.lower_bound(p);
	if(it->first==p)return ;
	--it;
	Map_A[p]=it->second;
}

int main()
{
#ifdef kcz
	freopen("1.in","r",stdin);//freopen("1.out","w",stdout);
#endif
	n=read();int m=read();
	rep(i,1,n)
	{
		int x=read();
		ins(Map_V[x],i,i);
		Map_A[i]=x;
	}
	Map_A[n+1]=0;
	while(m--)
	if(read()==1)
	{
		int l=read(),r=read(),x=read();
		split(l);split(r+1);
		auto it=Map_A.lower_bound(l);
		while(it->first<=r)
		{
			auto it1=it;
			++it1;
			//cerr<<it->second<<endl;
			del(Map_V[it->second],it->first,it1->first-1);
			Map_A.erase(it);
			it=it1;
		}
		Map_A[l]=x;
		ins(Map_V[x],l,r);
	}
	else
	{
		printf("%lld\n",BIT::query(read()));
	}
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int mo = 1000000007;
int b[120], a[120], i, n, p, j, tot, lmax, rmax, l, r, f[1200][4], g[4], k;
int mel(int x, int y) {
  for (int i = 0; i <= 2; ++i)
    if (x != i && y != i) return i;
}
int calc(int x, int y) {
  int r = p - x, l = p - y;
  return (long long)(l + r) * (r - l + 1) / 2 % mo;
}
int main() {
  tot = 1;
  a[tot] = 2;
  b[tot] = 0;
  scanf("%d%d", &n, &p);
  l = r = 1;
  while (a[tot] < p) {
    lmax = (a[l] + 1) * 3 - 1;
    if (a[r] & 1)
      rmax = (a[r] - 1) / 2 * 3 + 1;
    else
      rmax = a[r] / 2 * 3;

    b[++tot] = mel(b[l], b[r]);
    if (b[tot] == b[tot - 1]) tot--;
    a[tot] = min(rmax, lmax);
    if (lmax == a[tot]) l++;
    if (rmax == a[tot]) r++;
  }
  for (i = 1; i <= tot; ++i) {
    if (i != tot)
      g[b[i]] = (g[b[i]] + calc(a[i - 1] + 1, a[i])) % mo;
    else
      g[b[i]] = (g[b[i]] + calc(a[i - 1] + 1, p)) % mo;
  }
  f[0][0] = 1;
  for (i = 1; i <= n; ++i) {
    for (j = 0; j <= 3; ++j)
      for (k = 0; k <= 2; ++k)
        f[i][j ^ k] = ((long long)f[i - 1][j] * g[k] + f[i][j ^ k]) % mo;
  }
  printf("%d\n", ((long long)f[n][1] + f[n][2] + f[n][3]) % mo);
}


#include <cstdio>

const int MAXN = 24 + 10;
int N;
int A[MAXN];
int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool visited;

int main() {
	register int i, j;
	scanf( "%d", &N );
	for( i = 0; i < N; ++i )
		scanf( "%d", A + i );
	for( i = 0; i < 12; ++i ) {
		visited = 0;
		for( j = 0; j < N; ++j ) {
			if( ( i + j ) % 12 == 1 && !visited && A[j] == 29 ) {
				visited = 1;
				continue;
			}
			if( A[j] != month[( i + j ) % 12] )
				break;
		}
		if( j == N )
			return 0 * printf( "YES" );
	}
	printf( "NO" );
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
		int n; cin>>n;
		set<int>s;
		for(int i=0;i<n;i++)
		{
			int x; cin>>x;
			s.insert(x);
		}
		vector<int>cp;
		for(auto x: s)
		{
			cp.push_back(x);
		}
		if(cp.size()==1){cout<<0<<endl;return 0;}
		if(cp.size()>3){cout<<-1<<endl;return 0;}
	//	if(n==2){cout<<cp[1]-cp[0]<<endl;return 0;}
		int dif=cp[1]-cp[0];
	//	cout<<dif;
		if(cp.size()==2){if(dif%2==0){dif/=2;cout<<dif<<endl;}else{cout<<dif<<endl;}return 0;}
		bool ok=true;
		for(int i=1;i<cp.size();i++)
		{
			//cout<<cp[i]-cp[i-1]<<" ";
			if((cp[i]-cp[i-1])!=dif){ok=false; break;}
		}

	//	if(dif%2==0){dif/=2;}

		cout<<(ok?dif:-1);

}


#include <algorithm>
#include <numeric>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <complex>
#include <cassert>
#include <bitset>
#include <cstring>
using namespace std;

#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define decl(v, c) typeof(c) v = c
#define forall(i, c) for(decl(i, c.begin()); i!=c.end(); ++i)
#define dforall(i, c) for(decl(i, c.rbegin()); i!=c.rend(); ++i)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define D(a) cerr << #a << "=" << a << endl;
#define pb push_back
#define mp make_pair

typedef long long int tint;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<tint> vt;
typedef vector<vt> vvt;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INF = 1<<29;

map<tint,int> memo;
int go(tint n) {
	if (n == 0) return 0;

	if (memo.count(n)) return memo[n];
	int &res = memo[n];

	tint ones = 1, d = n, k = 1;
	while (d >= 10) {
		d /= 10;
		ones = 10*ones + 1;
		k++;
	}

	res = d*k + go(abs(n-ones*d));
	if (d>1) res = min(res, int((d-1)*k) + go(abs(n-ones*(d-1))));
	res = min(res, int((11-d)*k) + 1 + go(abs(n-ones*d-1)));
	if (d > 1) res = min(res, int((12-d)*k) + 1 + go(abs(n-ones*(d-1)-1)));
	return res;
}

int main() {

	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
	    //freopen("out.txt","w",stdout);
	#endif

	tint n; cin >> n;
	cout << go(n) << endl;

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <deque>
using namespace std;
typedef long long ll;
int main()
{
    ll n,m,a;
    while(cin>>n>>m>>a)
    {
        ll x1=0,x2=0;
        x1=(ceil)(n/(double)a);
        x2=(ceil)(m/(double)a);
        cout<<x1*x2<<endl;
    }
    return 0;
}