#include<iostream>
#include<stdio.h>
#include<conio.h>
const long maxN=55;
struct subject{long long a,b,c,cs;};
long long f[maxN][maxN][maxN*2],sl[maxN];
long n,m,k,d[maxN];
subject t[maxN];
int nhap();
int xuli();
int inkq();
int main()
{
    nhap();
    xuli();
    inkq();
}
int nhap()
{
    cin >> n >> m >> k;
    long i,j;
    subject tg;
    for (i=1;i<=m;i++)
    {
       cin >> t[i].a >>t[i].b >>t[i].c;
       t[i].cs=i;
    }
    for (i=m;i>=2;i--)
    for (j=1;j<=i-1;j++)
    if (t[j].c>t[j+1].c)
    {
         tg=t[j];t[j]=t[j+1];t[j+1]=tg;
    }
}
long long max(long long a,long long b)
{
     if (a>b) return a; else return b;
 }
int xuli()
{
    long i,j,p,q;
    long long m1,m2;
    for (i=1;i<=n;i++)
    for (p=i;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    {
        if (i==1)
        {
            f[i][p][j]=j+t[p].a;
        }
        else
        for (q=1;q<p;q++)
        {
            m1=j+t[p].a-k-t[q].a;
            if ((j+t[p].a)%k==0) m2=(j+t[p].a)/k-t[q].a;else m2=-1;
            if ((m1>=0)&&(m1<=t[q].b-t[q].a)&&(f[i-1][q][m1]>0)) f[i][p][j]=max(f[i][p][j],f[i-1][q][m1]+j+t[p].a);
            if ((m2>=0)&&(m2<=t[q].b-t[q].a)&&(f[i-1][q][m2]>0)) f[i][p][j]=max(f[i][p][j],f[i-1][q][m2]+j+t[p].a);
        }
    }
}
int landg(long n,long m,long long x,long long kq)
{
    if (n==0) return 0;
    long p,j;
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    {
        if ((j+t[p].a+k==x)||((j+t[p].a)*k==x))
        if (f[n][p][j]==kq)
        {
            d[n]=t[p].cs;
            sl[n]=j+t[p].a;
            landg(n-1,p-1,j+t[p].a,kq-j-t[p].a);
            return 0;
        }
    }
}
int inkq()
{
    long i,j,p;
    long long kq=0;
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    if (f[n][p][j]>kq) kq=f[n][p][j];
    if (kq==0)
    {
       printf("NO");
       return 0;
    }
    printf("YES\n");
    for (p=1;p<=m;p++)
    for (j=0;j<=t[p].b-t[p].a;j++)
    if (f[n][p][j]==kq)
    {
         d[n]=t[p].cs;
         sl[n]=j+t[p].a;
         landg(n-1,p-1,j+t[p].a,kq-j-t[p].a);
    }
    for (i=1;i<=n;i++)
    cout << d[i] << " "<< sl[i];
    //getch();
}


#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 105;

long long dp[maxn][maxn][maxn];
long long a[maxn], b[maxn], c[maxn], number[maxn];
long long best[maxn][maxn][maxn];
long long N, M, K;

int solve(int i, int j, int k) {
	long long many = 0;
	if (i != 1)
		if (best[i][j][k] > 0) {
			int newt = a[j] + k;
			newt -= K;
			newt -= a[best[i][j][k]];
			solve(i - 1, best[i][j][k], newt);
			many = dp[i][j][k] - dp[i - 1][best[i][j][k]][newt];
		}
		else {
			int newt = a[j] + k;
			newt /= K;
			newt -= a[-best[i][j][k]];
			solve(i - 1, -best[i][j][k], newt);
			many = dp[i][j][k] - dp[i - 1][-best[i][j][k]][newt];
		}
	else
		many = dp[i][j][k];
	cout << number[j] << " " << many << "\n";
}

int main() {

	cin >> N >> M >> K;
	for (int i = 1; i <= M; ++i)
		cin >> a[i] >> b[i] >> c[i], number[i] = i;

	for (int i = 1; i <= M; ++i)
		for (int j = i + 1; j <= M; ++j)
			if (c[i] > c[j])
				swap(a[i], a[j]), swap(b[i], b[j]), swap(c[i], c[j]), swap(number[i], number[j]);

	for (int i = 1; i <= M; ++i)
		for (int j = 0; j <= b[i] - a[i]; ++j)
			dp[1][i][j] = a[i] + j;

	for (int i = 2; i <= N; ++i)
		for (int j = i; j <= M; ++j)
			for (int l = 0; l <= b[j] - a[j]; ++l)
				for (int k = i - 1; k < j; ++k)
				 {
						int poz = a[j] + l - K;
						if (poz >= a[k] && poz <= b[k] && dp[i - 1][k][poz - a[k]] != 0) {
							poz -= a[k];
							if (dp[i - 1][k][poz] + a[j] + l > dp[i][j][l])
								dp[i][j][l] = dp[i - 1][k][poz] + a[j] + l,
								best[i][j][l] = k;
						}
						poz = a[j] + l;
						if (poz % K)
							continue;
						poz /= K;
						if (poz >= a[k] && poz <= b[k] && dp[i - 1][k][poz - a[k]] != 0) {
							poz -= a[k];
							if (dp[i - 1][k][poz] + a[j] + l > dp[i][j][l])
								dp[i][j][l] = dp[i - 1][k][poz] + a[j] + l,
								best[i][j][l] = -k;
						}
				}

	int pos = 0;
	int row = 1;
	for (int i = 1; i <= M; ++i)
		for (int l = 0; l <= b[i] - a[i]; ++l)
			if (dp[N][i][l] > dp[N][row][pos])
				row = i, pos = l;

	if (dp[N][row][pos] == 0) {
		cout << "NO";
		return 0;
	}
	cout << "YES\n";
	solve(N, row, pos);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    char a[9][20]={"one","two","three","four","five","six","seven","eight","nine"};
    char b[9][20]={"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
  cin>>i;
  if(i==0)
  {
      cout<<"zero"<<endl;
  }
       else if((i>=0)&&(i<=9))
        {
            cout<<a[i-1]<<endl;
        }
        else if((i%10)==0)
        {
            int p=i/10;
            cout<<b[p-1]<<endl;
        }
        else if(i==11)
        {
            cout<<"eleven"<<endl;
        }
        else if(i==12)
        {
            cout<<"twelve"<<endl;
        }
        else if(i==13)
        {
            cout<<"thirteen"<<endl;
        }
        else if(i==14)
        {
            cout<<"fourteen"<<endl;

        }
        else if(i==15)
        {
            cout<<"fifteen"<<endl;
        }
        else if((i>=16)&&(i<=19))
        {
            if(i==18)
            {
                cout<<"eighteen"<<endl;
            }
            else{
            int p=i-10;
            p--;
            cout<<a[p]<<"teen";
            }
        else{
            int p=i/10;
            int q=i%10;
            p--;
            q--;
            cout<<b[p]<<"-"<<a[q]<<endl;
        }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#pragma GCC optimize(3)

using namespace std;
signed main()
{
	ios::sync_with_stdio(false);
	if (a[i] == 0) cout << "zero";
	if (a[i] == 1) cout << "one";
	if (a[i] == 2) cout << "two";
	if (a[i] == 3) cout << "three";
	if (a[i] == 4) cout << "four";
	if (a[i] == 5) cout << "five";
	if (a[i] == 6) cout << "six";
	if (a[i] == 7) cout << "seven";
	if (a[i] == 8) cout << "eight";
	if (a[i] == 9) cout << "nine";
	if (a[i] == 10) cout << "ten";
	if (a[i] == 11) cout << "eleven";
	if (a[i] == 12) cout << "twelve";
	if (a[i] == 13) cout << "thirteen";
	if (a[i] == 14) cout << "fourteen";
	if (a[i] == 15) cout << "fifteen";
	if (a[i] == 16) cout << "sixteen";
	if (a[i] == 17) cout << "seventeen";
	if (a[i] == 18) cout << "eighteen";
	if (a[i] == 19) cout << "nineteen";
	if (a[i] == 20) cout << "twenty";
	if (a[i] == 21) cout << "twenty-one";
	if (a[i] == 22) cout << "twenty-two";
	if (a[i] == 23) cout << "twenty-three";
	if (a[i] == 24) cout << "twenty-four";
	if (a[i] == 25) cout << "twenty-five";
	if (a[i] == 26) cout << "twenty-six";
	if (a[i] == 27) cout << "twenty-seven";
	if (a[i] == 28) cout << "twenty-eight";
	if (a[i] == 29) cout << "twenty-nine";
	if (a[i] == 30) cout << "thirty";
	if (a[i] == 31) cout << "thirty-one";
	if (a[i] == 32) cout << "thirty-two";
	if (a[i] == 33) cout << "thirty-three";
	if (a[i] == 34) cout << "thirty-four";
	if (a[i] == 35) cout << "thirty-five";
	if (a[i] == 36) cout << "thirty-six";
	if (a[i] == 37) cout << "thirty-seven";
	if (a[i] == 38) cout << "thirty-eight";
	if (a[i] == 39) cout << "thirty-nine";
	if (a[i] == 40) cout << "forty";
	if (a[i] == 41) cout << "forty-one";
	if (a[i] == 42) cout << "forty-two";
	if (a[i] == 43) cout << "forty-three";
	if (a[i] == 44) cout << "forty-four";
	if (a[i] == 45) cout << "forty-five";
	if (a[i] == 46) cout << "forty-six";
	if (a[i] == 47) cout << "forty-seven";
	if (a[i] == 48) cout << "forty-eight";
	if (a[i] == 49) cout << "forty-nine";
	if (a[i] == 50) cout << "fifty";
	if (a[i] == 51) cout << "fifty-one";
	if (a[i] == 52) cout << "fifty-two";
	if (a[i] == 53) cout << "fifty-three";
	if (a[i] == 54) cout << "fifty-four";
	if (a[i] == 55) cout << "fifty-five";
	if (a[i] == 56) cout << "fifty-six";
	if (a[i] == 57) cout << "fifty-seven";
	if (a[i] == 58) cout << "fifty-eight";
	if (a[i] == 59) cout << "fifty-nine";
	if (a[i] == 60) cout << "sixty";
	if (a[i] == 61) cout << "sixty-one";
	if (a[i] == 62) cout << "sixty-two";
	if (a[i] == 63) cout << "sixty-three";
	if (a[i] == 64) cout << "sixty-four";
	if (a[i] == 65) cout << "sixty-five";
	if (a[i] == 66) cout << "sixty-six";
	if (a[i] == 67) cout << "sixty-seven";
	if (a[i] == 68) cout << "sixty-eight";
	if (a[i] == 69) cout << "sixty-nine";
	if (a[i] == 70) cout << "seventy";
	if (a[i] == 71) cout << "seventy-one";
	if (a[i] == 72) cout << "seventy-two";
	if (a[i] == 73) cout << "seventy-three";
	if (a[i] == 74) cout << "seventy-four";
	if (a[i] == 75) cout << "seventy-five";
	if (a[i] == 76) cout << "seventy-six";
	if (a[i] == 77) cout << "seventy-seven";
	if (a[i] == 78) cout << "seventy-eight";
	if (a[i] == 79) cout << "seventy-nine";
	if (a[i] == 80) cout << "eighty";
	if (a[i] == 81) cout << "eighty-one";
	if (a[i] == 82) cout << "eighty-two";
	if (a[i] == 83) cout << "eighty-three";
	if (a[i] == 84) cout << "eighty-four";
	if (a[i] == 85) cout << "eighty-five";
	if (a[i] == 86) cout << "eighty-six";
	if (a[i] == 87) cout << "eighty-seven";
	if (a[i] == 88) cout << "eighty-eight";
	if (a[i] == 89) cout << "eighty-nine";
	if (a[i] == 90) cout << "ninety";
	if (a[i] == 91) cout << "ninety-one";
	if (a[i] == 92) cout << "ninety-two";
	if (a[i] == 93) cout << "ninety-three";
	if (a[i] == 94) cout << "ninety-four";
	if (a[i] == 95) cout << "ninety-five";
	if (a[i] == 96) cout << "ninety-six";
	if (a[i] == 97) cout << "ninety-seven";
	if (a[i] == 98) cout << "ninety-eight";
	if (a[i] == 99) cout << "ninety-nine";
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
  char a[14][8]={{"zero"},{"one"},{"two"},{"three"},{"four"},{"five"},
   {"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},{"twelve"},{"thirteen"}},
   b[8][8]={{"twenty"},{"thirty"},{"forty"},{"fifty"},{"sixty"},{"seventy"},{"eighty"},{"ninety"}};
   int t,i,j;
  while(scanf("%d",&t)!=EOF)
  {
      if(t<=13)
       printf("%s\n",a[t]);
      else if(t<=19)
       {
           if(t==15)
            printf("fifteen\n");
           else  if(t==18)
           printf("eighteen\n");
           else
            printf("%steen\n",a[t-10]);
       }
       else
       {
           i=t%10;
           j=t/10;
           printf("%s-%s\n",b[j-2],a[i]);
           }
  }
}


#include <iostream>

using namespace std;

int main()
{
    int n,z=0,c=0;
    cin>>n;
    int h=n,u=n;
    string d,s;
    if(n==10)
    {
        cout<<"ten"<<endl;
        return 0;
    }
    else
        if(n==0)
    {
        cout<<"zero"<<endl;
        return 0;
    }
    if(n==1)
    {
        cout<<"one"<<endl;
        return 0;
    }
    if(n==11)
    {
        cout<<"eleven"<<endl;
        return 0;
    }
    if(n==15)
        {
            cout<<"fifteen"<<endl;
            return 0;
        }
        if(x==12)
        {
            cout<<"twelve"<<endl;
            return 0;
        }

while(h!=0)
{
h=h/10;
    z++;
}

        if(z==1)
        {
      int y=n%10;
        if(y==1)
        d="one";
            if(y==2);
            d="two";
            if(y==3)
            d="three";
            if(y==4)
            d="four";
           if(y==5)
            d="five";
           if(y==6)
            d="six";
           if(y==7)
            d="seven";
           if(y==8)
            d="eight";
           if(y==9)
            d="nine";
            cout<<d;
        }
       else
        {
            int y=n%10;
            n=n/10;
            int x=n%10;

        if(x==2)
        {
            s="twenty";
        }

        if(x==3)
        {
            s="thirty";
        }

        if(x==4)
        {
            s="forty";
        }

        if(x==5)
        {
            s="fif";
        }

        if(x==6)
        {
            s="sixty";
        }

        if(x==7)
        {
            s="seventy";
        }

        if(x==8)
        {
           s="eighty";
        }

        if(x==9)
            {
            s="ninety";
            }

       if(y==0)
       {
           cout<<s<<endl;
           return 0;
       }

           if(y==1)
                d="one";
            if(y==2);
            d="two";
            if(y==3)
            d="three";
            if(y==4)
            d="four";
           if(y==5)
            d="five";
           if(y==6)
            d="six";
           if(y==7)
            d="seven";
           if(y==8)
            d="eight";
           if(y==9)
            d="nine";
            cout<<s<<'-'<<d<<endl;

        }

 }


#include <cstdio>
#include <algorithm>

using namespace std;

main() {
	LL n, m;
	scanf("%I64d %I64d", &n, &m);
	if (__gcd(n, m) > 1) puts("Impossible");
	else {
		while (n + m > 2) {
			if (n > m) printf("%I64dA", (n - 1) / m), n %= m;
			else printf("%I64dB", (m - 1) / n), m %= n;
		}
	}
}


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

typedef long long ll;

string ans;

void gcd(ll a, ll b)
{
	char tmp[32];
	while (a && b)
	{
		if (a > b)
		{
			sprintf(tmp, "%lld", b!=1?a/b:(a-1));
			ans.append(tmp);
			ans.push_back('A');
			a %= b;
		} else
		{
			sprintf(tmp, "%lld", a!=1?b/a:(b-1));
			ans.append(tmp);
			ans.push_back('B');
			b %= a;
		}
	}
	if (a+b != 1)
	{
		printf("Impossible\n");
		exit(0);
	}
}

int main()
{
	ll a, b;
	cin >> a >> b;
	gcd(a, b);
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define vi vector<int>
#define pb push_back
#define sz size
#define rev reverse
#define all(x) (x).begin(),(x).end()
#define it iterator
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define REPX(i,a,b,c) for(int i=(a);i<(b);i+=(c))
#define DEP(i,a,b) for(int i=(a)-1;i>=(b);i--)
#define REX(i,x) for(__typeof((x).begin()) i=(x).begin(); i!=(x).end();i++)
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define st string
#define ffo find_first_of
#define su substr
#define vs vector<st>
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define li list<int>
#define em empty
#define ap append
#define ni next_int
#define gu getchar_unlocked
#define ts to_string
#define mems(x) memset(x,0,sizeof(x))
#define memc(x,y) memcpy(x,y,sizeof(y))
using namespace std;
st s;
LL gcd(LL a,LL b)
{
    if(a<b)
    {
        LL c=(b-1)/a;
        s.ap(ts(c));
        s.pb('B');
        return gcd(a,b-c*a);
    }
    else if(b<a)
    {
        LL c=(a-1)/b;
        s.ap(ts(c));
        s.pb('A');
        return gcd(a-c*b,b);
    }
    return a;
}
int main()
{
    LL a,b; cin>>a>>b;
    LL g=gcd(a,b);
    if(g!=1) cout<<"Impossible\n";
    else cout<<s<<"\n";
    return 0;
}


#include <bits/stdc++.h>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

using namespace std;

typedef long long ll;
typedef long double ld;

const ld eps = 1e-9, pi = acos(-1.0);

int main() {
  ll x, y;
  cin >> x >> y;
  if (__gcd(x, y) != 1LL) {
    cout << "Impossible" << endl;
    return 0;
  }
  string res;
  while (x > 1 || y > 1) {
    if (x == 1) {
      res += to_string(y - 1) + "B";
      y = 1;
    }
    else if (y == 1) {
      res += to_string(x - 1) + "A";
      x = 1;
    }
    else if (x > y) {
      res += to_string(x / y) + "A";
      x %= y;
    }
    else {
      res += to_string(y / x) + "B";
      y %= x;
    }
  }
  cout << res << endl;
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

#define MAXN 100005
__int64 x ,d ;
int n ;

int a[MAXN] , b[MAXN] , c[MAXN] ;
vector<int> v ;

set<pair<int,int>> s ;
set<pair<int,int>>::iterator it ;

//x is 64-bit variable;
__int64 getNextX() {
    x = (x * 37 + 10007) % 1000000007;
    return x;
}

void initAB() {
    int i ;
	for(i = 0; i < n; i = i + 1){
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1){
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i = i + 1){
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1){
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}

int main(){
	while( scanf( "%d%I64d%I64d" , &n, &d , &x ) != EOF ) {
		initAB() ;
		int cnt = 0 ;
		for( int i = 0 ; i < n;  i ++ )
			if( b[i] ) cnt ++ ;
		memset( c , 0 , sizeof(c) ) ;
		if( (__int64)cnt * cnt <= n ) {
			v.clear() ;
			for( int i = 0 ; i < n ; i ++ ) if( b[i] )v.push_back( i ) ;
			for( int i = 0 ; i < n ; i ++ ) {
				int size = v.size () ;
				for( int j = 0 ; j < size ; j ++ ) {
					if( v[j] > i ) break;
					c[i] = max( c[i] , a[i-v[j]] ) ;
				}
			}
		}else{
			s.clear() ;
			for( int i = 0 ; i < n ; i ++ ) {
				s.insert( pair<int,int>( a[i] , i ) ) ;
				it = s.end() ; it -- ;
				do{
					if( b[i-it->second] ) {
						c[i] = it->first ;
						break;
					}
					if( it == s.begin () ) break;
					it --;
				}while( true ) ;
			}
		}
		for( int i = 0 ; i < n ; i ++ )
			printf( "%d\n" , c[i] ) ;
	}
	return 0 ;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d\n",a)
using namespace std;
const int nm=100005;
const int mm=100005;
int k,m,t;
int a[nm],b[nm],c[nm],e[nm];
ll x,n,d;
ll getNextX() {
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
void initAB() {
    int i;
    for(i = 0; i < n; i = i + 1){
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1){
        swap(a[i], a[int(getNextX() % (i + 1))]);
    }
    for(i = 0; i < n; i = i + 1){
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1){
        swap(b[i], b[int(getNextX() % (i + 1))]);
    }
}
struct node{
    int v;
    node* next;
    node(){
        next=NULL;
    }
    node(int r){
        v=r;
        next=NULL;
    }
};
struct llist{
    node* head, *tail;
    llist(){head=tail=NULL;
    }
    void add( int r){
        node*p = new node(r);
        if(head==NULL) head=tail=p;
        else {
            tail->next=p;
            tail=p;
        }
    }
};
llist L;
bool cmp(int u, int v){
    return a[u]>a[v];
}
int vt[nm];
main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,y,z,w;

    INL3(n,d,x);
    initAB();
    int dem=0;
    FOR2(i,0,n) vt[i]=i;
    sort(vt,vt+n,cmp);
    node *p,*q;
    FOR2(i,0,n) L.add(i);
    FOR2(i,0,n){
        if(L.head==NULL) break;
        p=L.head;
        while(p!=NULL){
            if(p->v >=vt[i] && b[p->v-vt[i]]){
                c[p->v]=a[vt[i]];
                if(p==L.head){
                    L.head=p->next;
                    p=p->next;
                }else{
                    q->next=p->next;
                    p=p->next;
                }
                continue;
            }
            q=p;
            p=p->next;
        }
    }
    FOR2(i,0,n) OUTD(c[i]);
}


#include <stdio.h>
#include <algorithm>
using namespace std;
int a[100005],b[100005],c[100005],pos[100005],map[100005];
long long x,n,d;
long long getNextX()
{
    x=(x*37+10007)%1000000007;
    return x;
}
void initAB()
{
    for(int i=0;i<n;i++)
        a[i]=i+1;
    for(int i=0;i<n;i++)
        swap(a[i],a[getNextX()%(i+1)]);
    for(int i=0;i<n;i++)
    {
        if(i<d)
            b[i]=1;
        else
            b[i]=0;
    }
    for(int i=0;i<n;i++)
        swap(b[i],b[getNextX()%(i+1)]);
}
int main()
{
    scanf("%I64d%I64d%I64d",&n,&d,&x);
    initAB();
    int one=0;
    for(int i=0;i<n;i++)
        pos[a[i]]=i;
    for(int i=0;i<n;i++)
        if(b[i])
            map[++one]=i;
    for(int i=1;i<=n&&cnt<=10000000;i++)
        for(int j=1;j<=one;j++,cnt++)
        {
            if(pos[i]+map[j]<n)
            {
                c[pos[i]+map[j]]=max(c[pos[i]+map[j]],i);
            }
        }
    for(int i=0;i<n;i++)
    {
        if(!c[i])
        {
            for(int j=1;j<=one;j++)
            {
                if(i<map[j])
                    break;
                else
                    c[i]=max(c[i],a[i-map[j]]);
            }
        }
    }
    for(int i=0;i<n;i++){
        printf("%d\n",c[i]);
    }
    return 0;
}


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

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define pii pair < int, int >
#define ull unsigned long long
#define pll pair < ll, ll >
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)
#define all(s) s.begin(), s.end()
#define sz(a) (int)a.size()

const int inf = (1ll << 25) - 1;
const int maxn = (int)2e5+100;
const int mod = (int) 1e9 + 7;
using namespace std;
ll x;
int n;
int d;
int a[maxn];
int b[maxn];
int getNextX() {
    x = (x * 37 + 10007) % 1000000007;
    return x;
}
void initAB() {
	int i;
    for(i = 0; i < n; i = i + 1){
        a[i] = i + 1;
    }
    for(i = 0; i < n; i = i + 1){
        swap(a[i], a[getNextX() % (i + 1)]);
    }
    for(i = 0; i < n; i = i + 1){
        if (i < d)
            b[i] = 1;
        else
            b[i] = 0;
    }
    for(i = 0; i < n; i = i + 1){
        swap(b[i], b[getNextX() % (i + 1)]);
    }
}
const int C = 30;
ll A[maxn/C];
ll B[maxn/C];
int ans[maxn];
int pos[maxn];
void upd(int j, int T, ll val) {
	for(int z = 0; z < C; z++) {
		if(val & (1ll<<z)) {
			A[(z+j)/C] ^=1ll<<((z+j) % C);
			ans[z+j] = T;
		}

	}
}
void solve(){
	cin >> n >> d >> x;
	initAB();
	for(int i = 0; i < n; i++) {
		pos[a[i]] = i;
		int z = i % C;
		A[i/C] |= 1ll<<z;
	}
	for(int i = 0; i < n; i++) {
		if(b[i]) {
			int z = i % C;
			B[i/C] |= 1ll<<z;
		}
	}
	for(int i = n; i >= 1; i--) {
		int j = pos[i];
		int t = j % C;
		int o = 0;
		ll sec = (1ll<<t)-1;
		ll fir = ((1ll<<C)-1)^sec;
		while(j < n + C) {
			ll h = (A[j/C] | (A[j/C + 1] << C)) >> t;
			h &= B[o];
			if(h) {
				upd(j, i, h);
			}
			j += C;
			o++;
		}
	}
	for(int i = 0; i < n; i++) {
		cout << ans[i] << "\n";
	}
}
int main () {
    int t=1;
   	//scanf("%d", &t);
    for(int i=1; i <= t; i++){
      //printf("Case #%d\n", i);
      solve();
    }

    return 0;
}


#include <cstdio>

int solve(int m, int n, int s)
{
	if (m < n) {
		int t = m;
		m = n;
		n = t;
	}

	if (s > m) return m * n;

	int a = (m-1) % s;
	int b = (m-1) / s;
	int x = (n-1) % s;
	int y = (n-1) / s;
	return (b+1)*(y+1)*(a+1)*(x+1);
}

int main()
{
	int m = 0;
	int n = 0;
	int s = 0;
	while (scanf_s("%d %d %d", &m, &n, &s))
	{
		int ret = solve(m, n, s);
		printf("%d\n", ret);
	}
}


#include <iostream>
using namespace std;
int main()
{
    long long n,m,s,i,j,k,l;
    cin>>n>>m>>s;
    if (s>=n&&s>=m)
    {
        k=n*m;
        cout<<k<<endl;
    } else
    {
        if (s>=n||s>=m)
        {
            if (s>=n) {k=(m+s-1)/s; k=k+k*((m-1)%s); k=k*(n%s); cout<<k<<endl;}
            if (s>=m) {k=(n+s-1)/s; k=k+k*((n-1)%s); k=k*(m%s); cout<<k<<endl;}
        } else
        {
            k=(m+s-1)/s;
            k=k+k*((m-1)%s);
            i=k;
            k=(n+s-1)/s;
            k=k+k*((n-1)%s);
            k=i*k;
            cout<<k<<endl;
        }
    }
    system("pause");
    return 0;
}


#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
#define ll long long int
#define FOR(i,a,b) for(int  i= a;i<b;i++)
#define rl(x) scanf("%lld ",&x);
#define rll(x,y) scanf("%lld %lld",&x,&y);
#define rs(x) scanf("%s",x);
int R,C,s;

int main() {
    scanf("%d %d %d",&R,&C,&s);
    ll mi =0,ll =0;
    ll lx =0;
    FOR(i,0,C)
        lx = max(lx,(C-i-1)/s + i/s +1);
    FOR(i,0,C)
        if((C-i-1)/s + i/s +1 == lx) mi++;
    ll ly = 0;
    FOR(i,0,R)
        ly = max(ly,(R-i-1)/s + i/s +1);
    FOR(i,0,R)
        if((R-i-1)/s + i/s +1 == ly) mj++;
    ll ans = mi*mj;
    printf("%lld",ans);
}


#include<iostream>
#include<vector>

using namespace std;

#typedef long long llint;

inline int min_edge(int pos, int length, int step)
{
    return length - ((length-1) % step);
}
long solution(llint n, llint m, llint s)
{
    llint minw = min_edge(1, n, s);
    llint minh = min_edge(1, m, s);
    llint max_points =  (1+(minw-1)/s)*(1+(minh-1)/s);
    return (n-minw+1)*(m-minh+1)*max_points;
    /*long long max = in_rectangle(n, m, 1, 1, s);
    if(max == 1)
        return n*m;
    long long result = -max;
    for(int i=1;i<=s;i++)
    {
        if(in_rectangle(n, m, 1, i, s) == max)
            result+=max;
        if(in_rectangle(n, m, i, 1, s) == max)
            result+=max;
    }
    return result;*/
}
int main()
{
    int n, m , s;
    cin >> n >> m >> s;
    cout << solution(n, m, s) << endl;
}


#include <bits/stdc++.h>
using namespace std;
long long n,m,apple,money,i;
string s[51];
int main()
{
    cin>>n>>m;
    m/=2;
    for(i=1;i<=n;i++) cin>>s[i];
    for(i=n;i>=1;i--)

        apple*=2;
        if(s[i]=="halfplus") apple+=1;
        money+=apple;
    }
    money*=m;
    cout<<money;
    return 0;
}


#include <iosstream>
using namespace std;

long long p, ans, num;
int n;
string s[50];

int main()
{
    scanf("%d%lld", &n, &p);
    p /= 2;
    for (int i = 1;i <= n;i ++)
    {
        cin >> s[i];
    }
    for (int i = n;i >= 1;i --)
    {
        num *= 2;
        if(s[i] == "halfplus") num ++;
        ans += num;
    }
    printf("%lld", ans*p);
}


#include<iostream>
#include<cstdio>
#include<string>

using namespace std;
int n,m,p,ans;
string s[1010];
int main()
{
    cin>>n>>p;
    for(int i=1;i<=n;i++)
        cin>>s[i];
    for(int i=n;i>=1;i--)
    {
        ans*=2;
        if(s[i]=="halfplus") ans+=1;
    }
    cout<<ans*m/2
}


#include<bits/stdc++.h>
using namespace std;
long long n,m,apple,money;
string a[51];
int main()
{
    cin>>n>>m;
    m/=2;
    for(int i=1;i<=n;i++)
	  cin>>s[i];
    for(int i=n;i>=1;i--)
	{
        apple*=2;
        if(strcmp(a[i],"halfplus")==0)
		  apple++;
        money+=apple;
    }
    money*=m;
    cout<<money;
    return 0;
}


/*************************************************************************
Author: ziki
Created Time: 2012/6/25 1:12:13
File Name: E.cpp
Description:
************************************************************************/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <string.h>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double pi=acos(-1.0);
const double eps=1e-11;
const int inf=0x7FFFFFFF;
template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define zero(a) memset(a, 0, sizeof(a))
#define out(x) (cout<<#x<<": "<<x<<endl)
template<class T>void show(T a, int n){for(int i=0; i<n; ++i) cout<<a[i]<<' '; cout<<endl;}
template<class T>void show(T a, int r, int l){for(int i=0; i<r; ++i)show(a[i],l);cout<<endl;}

long long extgcd(long long a, long long b, long long &x, long long &y)
{
    long long d, t;
    if (b == 0) { x = 1; y = 0; return a; }
    d = extgcd(b, a % b, x, y);
    t = x - a/b*y; x = y; y = t;
    return d;
}

int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		int a[3] = {0};
		for(int i=0; i<n; i++)
		{
			int tmp;
			cin>>tmp;
			a[tmp-3]++;
		}
		int64 ans = inf;//(uint64)(-1)>>1;
		int64 a1,b1,x,y, gv;
		a1 = a[0], b1 = a[2];
		gv = extgcd(a1,b1,x,y);
		int64 gbs = a1 / gv * a2;

		for(int j=1; a[1]*j<m; j++)
		{
			int aa,cc,bb = a[i]*j, ret = m - bb;
			if(ret%gv != 0)continue;
			int64 bs = ret/gv, tx = x*bs, ty = y*bs;
			aa = tx*a1, cc = ty*b1;
			if(aa>cc)swap(aa,cc);
			int64 delb = abs(aa-cc)/gbs/2*gbs;
			aa -= delb;
			cc += delb;
			if(aa>0 && cc>0){
				checkmin(ans,(int64)abs(aa-bb)+abs(bb-cc));
			}
			if(aa>cc)swap(aa,cc);
			aa+=gbs;
			cc-=gbs;
			if(aa>0 && cc>0){
				checkmin(ans,(int64)abs(aa-bb)+abs(bb-cc));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#define INF (1<<29)
#define M 300000
#define LL long long
using namespace std;
int c3,c4,c5,k3,k4,k5,ans3,ans4,ans5,n,s,f,Gcd,rest;
int gcd(int a,int b,int& x,int& y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	int d,tp;
	d=gcd(b,a%b,x,y);
	tp=x;
	x=y;
	y=tp-(a/b)*y;
	return d;
}
int abs(int i)
{
	return i>=0?i:-i;
}
int calF(int k)
{
	int i,j,tp;
	i=k4+(c5/Gcd)*k;
	j=k5-(c4/Gcd)*k;
	if(k3<=i&&i<=j)
	{
		tp=abs(k3*c3-k4*c4)+abs(k4*c4-k5*c5);
		if(tp<f)
		{
			f=tp;
			ans3=k3;ans4=i;ans5=j;
		}
		return tp;
	}
	else if(i<k3)
		return INF-k;
	return INF+k;
}
void solve()
{
	int i,j,k,x,y,low,high,tp;
	ans3=ans4=ans5=-1;
	f=INF;
	Gcd=gcd(c4,c5,x,y);
	for(k3=0;k3<=s/c3;k3++)
	{
		rest=s-k3*c3;
		if(rest%Gcd)
			continue;
		k4=x*(rest/Gcd);k5=y*(rest/Gcd);
		low=-M,high=M;
		while(low<high-2)
		{
			i=low+(high-low)/3;
			j=high-(high-low)/3;
			if(calF(i)<calF(j))
				high=j;
			else
				low=i;
		}
		for(k=low;k<=high;k++)
		{
			i=k4+(rest/Gcd)*k;
			j=k5-(rest/Gcd)*k;
			if(i>=k3&&i<=j)
			{
				tp=abs(c3*k3-c4*k4)+abs(c4*k4-c5*k5);
				if(tp<f)
				{
					f=tp;
					ans3=k3;ans4=i;ans5=j;
				}
			}
		}
	}
	if(ans3==-1)
		cout<<-1<<endl;
	else
		cout<<ans3<<' '<<ans4<<' '<<ans5<<endl;

}
int main()
{
	cin>>n>>s;
	for(int i=0;i<n;i++)
	{
		int j;
		cin>>j;
		if(j==3)
			c3++;
		else if(j==4)
			c4++;
		else if(j==5)
			c5++;
	}
	solve();
	return 0;
}


/*
 Anton Gulikov
*/
#pragma comment(linker,"/STACK:300000000")
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <list>
#include <math.h>
#include <string>
#include <stdlib.h>

#define gcd(a,b) __gcd((a),(b))
#define sqr(a) ((a)*(a))
#define odd(a) ((a)&1)
#define foru(i,n) for (int i=0;i<(n);i++)
#define ford(i,n) for (int i=(n)-1;i>=0;i--)
#define forab(i,l,r) for (int i=(l);i<=(r);i++)
#define forabd(i,r,l) for (int i=(r);i>=(l);i--)
#define fillchar(a,b) memset((a),(b),sizeof((a)))
#define pb push_back
#define F first
#define S second
#define all(x) x.begin,x.end
#define pw2(x) (1ull<<(x))

const long double eps=1e-20;
const long double pi=acos(-1.0);
const long long inf=1000*1000*1000*1000*1000*1000;
const long long base=1000*1000*1000+7;

using namespace std;

int gcd_(int a,int b,int & x,int & y){
	if (a==0){
		x=0; y=1;
		return b;
	}
	int x1,y1;
	int d=gcd_(b%a,a,x1,y1);
	x=y1-(b/a)*x1;
	y=x1;
	return d;
}

int main(){
	int n,s;
	int a[4];
	scanf("%d%d",&n,&s);
	foru(i,4) a[i]=0;
	foru(i,n){
		int x;
		scanf("%d",&x);
		a[x-2]++;
	}
	int ans=1000000000,a1,a2,a3;
	foru(i,(s+1)/a[2]+1){
		int x=i*a[2];
		int tmps=s-x;
		if (tmps%gcd(a[1],a[3])!=0) continue;
		int x1=min(s-i*a[3],i*a[1]);
		if (x>x1){
			for(int j=x1;j>=0;j--)if (j%a[1]==0 && (tmps-j)%a[3]==0){
				int k2=i,k1=s1/a[1],k3=s3/a[3];
				if (k1<0 || k1>k2 || k2>k3 || k1>k3) continue;
				if (k1*a[1]+k2*a[2]+k3*a[3]==s && ans>abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3])){
					ans=abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3]);
					a1=k1; a2=k2; a3=k3;
					break;
				}
			}
		} else{
			for(int j=x;j<=x1;j++)if (j%a[1]==0 && (tmps-j)%a[3]==0){
				int k2=i,k1=s1/a[1],k3=s3/a[3];
				if (k1<0 || k1>k2 || k2>k3 || k1>k3) continue;
				if (k1*a[1]+k2*a[2]+k3*a[3]==s && ans>abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3])){
					ans=abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3]);
					a1=k1; a2=k2; a3=k3;
					break;
				}
			}
			for(int j=x;j>=0;j--) if (j%a[1]==0 && (tmps-j)%a[3]==0){
				int k2=i,k1=s1/a[1],k3=s3/a[3];
				if (k1<0 || k1>k2 || k2>k3 || k1>k3) continue;
				if (k1*a[1]+k2*a[2]+k3*a[3]==s && ans>abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3])){
					ans=abs(k1*a[1]-k2*a[2])+abs(k2*a[2]-k3*a[3]);
					a1=k1; a2=k2; a3=k3;
					break;
				}
			}
		}
	}
	if (ans==1000000000) printf("-1"); else printf("%d %d %d",a1,a2,a3);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    char n[11];
    while(gets(n) && n[0]!=NULL)
    {
        int i,j,len=strlen(n),s=0;
        for(i=len-1,j=0;i>=0;i--,j++)
            s+=(n[i]-48)*(int)pow(2,j);
        printf("%d\n",s);
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <string>
#define int64 long long

using namespace std;

int kol_zifr(int t) {
	int x = 0;
	while (t > 0) {
		t /= 10;
		++x;
	}
	return x;
}

int next (int t) {
	int k = kol_zifr(t);
	char s[k];
	char* r = itoa(t, s, 10);
	bool z = false;
	for (int i = k - 1; i >= 0; --i) {
		if (s[i] > '1') {
			z = true;
			s[i] = '1';
		}
	}
	if (!z) {
		bool b = false;
		for (int i = k - 1; i >= 1; --i) {
			if (s[i] == '1') {
				s[i] = '0';
				for (int j = i + 1; j < k; ++j) s[j] = '1';
				b = true;
				break;
			}
		}
		if (!b) {
			s[0] = '0';
			for (int i = k - 1; i > 0; --i) s[i] = '1';
		}
	}
	int res = 0, ku = 1;
	for (int i = k - 1; i >= 0; --i) {
		res += ku * (s[i] - '0');
		ku *= 10;
	}
	return res;
}

int is_n_good(int t) {
	int b = 0;
	while (t > 0) {
		int zz = t % 10;
		if (zz != 0 && zz != 1) {
			b = 1;
			break;
		}
		t /= 10;
	}
	return b;
}

int main () {
	int n, h = 0;
	cin >> n;
	int na = is_n_good(n);
	while (n > 0) {
		n = next(n);
		++h;
	}
	cout << h - na;
}


#include <iostream>
#include<stdio.h>
using namespace std;

int main(){
    long long a,b,order[600],n;
    int t[64],i=512,j,k;
    scanf("%I64d",&n);
    for(i=1;i<=512;i++)
    {

        k=i;
        memset(t,0,sizeof(t));
        for(j=0;k>0;j++)
        {
            t[j]=k%2;
            k=k/2;
        }
        a=0;
        for(j=0;j<64;j++)
        b=1;
        for(k=0;k<=10;k++)
        {

            a=a+t[k]*b;
            b=b*10;
        }
        order[i]=a;

    }
    for(i=1;n>=order[i];i++)
    {
       ;
    }
    printf("\n%d",i-1);

}


include<iostream>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
long P(long x)
{
	long flag=1;
	if (x==111111111||x==1000000000)
	{
		return 1;
	}
	while(x!=0)
	{
		long a=x%10;
		if (a!=0&&a!=1)
		{
			flag=0;
			break;
		}
		x/=10;
	}
	return flag;
}
int main()
{
	long n,s=0;
	cin>>n;
	for (long i=1;i<=n;i++)
	{
		if (i>111111111&&i<1000000000)
		{
			continue;
		}
		if (P(i)==1)
		{
			s++;
		}
	}
	cout<<s;
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 35
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~i))
#define rf(i,a,b) for(register int i=(a);i>=(b);i=~(-i))
int dp[N][N][2],sum[N][2],n,m,mod=1e9+7,fac[N],ifac[N],lim[N],pow2[N],ans;
int spow(int m,int n)
{
	int b=1;
	for(;n;n>>=1,m=1ll*m*m%mod)if(n&1)b=1ll*b*m%mod;
	return b;
}
int C(int n,int m){return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;}
int ADD(int x,int y){return (x+=y)>=mod?x-mod:x;}
void init()
{
	fac[0]=ifac[0]=1,pow2[0]=1;
	f(i,1,32)fac[i]=1ll*fac[i-1]*i%mod,pow2[i]=2ll*pow2[i-1]%mod;
	ifac[32]=spow(fac[32],mod-2);
	rf(i,31,1)ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
	f(i,1,32)
	 f(j,0,i)
	  sum[i][j&1]=ADD(sum[i][j&1],C(i,j));
	while(n)lim[++m]=n&1,n>>=1;
	std::reverse(lim+1,lim+m+1);
}
int main()
{
	scanf("%d",&n);
	init();
	dp[0][0][1]=1;
	f(i,1,m)dp[i][0][0]=1;//如果该位填第一个向量，那也算一种方案
	f(i,1,m)
		f(j,1,i)//create a new unlimited state stands iff this is 1 and we make it 0
		{
			dp[i][j][0]=ADD(1ll*dp[i-1][j][0]*pow2[j]%mod,dp[i-1][j-1][0]);//no limit, or fill and last unlimited
			if(lim[i])
			{
				dp[i][j][0]=ADD(dp[i][j][0],1ll*dp[i-1][j][1]*sum[j][0]%mod);//make it!
				dp[i][j][1]=ADD(dp[i-1][j-1][1],1ll*dp[i-1][j][1]*sum[j][1]%mod);//limited, fill/unfill a vector
			}
			else dp[i][j][1]=1ll*dp[i-1][j][1]*sum[j][0]%mod;//cannot fill a vector
			/*
			   i j 0 <- (i-1 j 1 & make it 0) | (i-1 j 0 & forall) | (i-1 j-1 0)
			   i j 1 <- (i-1 j 1 & make it fit the num) | (i-1 j-1 1)
			*/
		}
	f(i,0,m)ans=ADD(ans,ADD(dp[m][i][0],dp[m][i][1]));
	return printf("%d\n",ADD(ans,mod)),0;


#include <cstdio>
#include <cstring>

typedef long long ll;
const int mod = 1000000007;

ll pw2[99];

ll f_rlen[99][99];

inline void upda(ll &a, ll b) {
    a = (a + b) % mod;
}

inline void init_f_rlen() {
    pw2[0] = 1;
    for(int i = 1; i <= 91; ++i) pw2[i] = pw2[i - 1] * 2 % mod;
    f_rlen[0][0] = 1;
    for(int rlen = 1; rlen <= 90; ++rlen) for(int rlen1 = 0; rlen1 <= rlen; ++rlen1) {
            if(rlen1) f_rlen[rlen][rlen1] = f_rlen[rlen - 1][rlen1 - 1] * pw2[rlen - rlen1]) % mod;
            if(rlen1 < rlen) f_rlen[rlen][rlen1] = (f_rlen[rlen][rlen1] + f_rlen[rlen - 1][rlen1]) % mod;
        }
}

ll g_pf[44][44];

inline ll calc_prefix(int pf, int rlen) {
    ll ans = 0;
    for(int rlen1 = 0; rlen1 <= rlen; ++rlen1) {
        ll t1ans = 0;
        memset(g_pf, 0, sizeof(g_pf));
        g_pf[0][0] = 1;
        for(int pfmv = 0; (pf >> pfmv); ++pfmv) for(int pfmv1 = 0; pfmv1 <= pfmv; ++pfmv1) {
                if((pf >> pfmv) == 1) t1ans = (t1ans + g_pf[pfmv][pfmv1]) % mod;
                else {
                    upda(g_pf[pfmv + 1][pfmv1], g_pf[pfmv][pfmv1]);
                    if(pf >> pfmv & 1) upda(g_pf[pfmv + 1][pfmv1 + 1], g_pf[pfmv][pfmv1] * pw2[pfmv - pfmv1 + rlen - rlen1]);
                }
            }
        ans = (ans + t1ans * pw2[rlen - rlen1] % mod * f_rlen[rlen][rlen1]) % mod;
    }
    return ans;
}

inline ll calc_ans(int n) {
    if(n == 0) return 1;
    ll ans = 1; // max = 0;
    int len0;
    for(len0 = 0; (1 << (len0 + 1)) <= n; ++len0) ans = (ans + calc_prefix(1, len0)) % mod;
    int t_pf = 1;
    for(int i = len0 - 1; i >= 0; --i) {
        int a = n >> i & 1;
        if(!a) t_pf <<= 1;
        else {
            ans = (ans + calc_prefix(t_pf << 1, i)) % mod;
            t_pf = t_pf << 1 | 1;
        }
    }
    ans = (ans + calc_prefix(n, 0)) % mod;
    return ans;
}

int main() {
    init_f_rlen();
#ifdef TEST
    printf("[DEBUG] calc_ans(3) = %I64d (expected: 5)\n", calc_ans(3));
#else
    int k; scanf("%d", &k);
    printf("%I64d\n", calc_ans(k));
#endif
    return 0;
}


// https://pp.vk.me/c540102/v540102394/7cd5/J2NXxBbvBrM.jpg
#pragma GCC optimize("O500")
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <ctime>
#include <queue>
#include <cmath>
#include <deque>
#include <list>
#include <sstream>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
//#include <unordered_set>
//#include <unordered_map>
#include <cassert>
#include <iomanip>
#define mp make_pair
#define FOR(i,a,b) for(int i=(a),_b(b);i<_b;++i)
#define FORD(i,a,b) for(int i=(a),_b(b);i>=_b;--i)
#define REP(i,n) FOR(i,0,n)
#define CL(x) memset(x, 0, sizeof(x))
#define SINF(x) memset(x, 0x3F, sizeof(x))
#define SVAL(x, y) memset(x, y, sizeof(x))
#define CP(t, f) memcpy(t, f, sizeof(f));
#define CPS(t, f, s) memcpy(t, f, s*sizeof(f[0]))
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define FIN freopen("in.txt", "r", stdin);
#define FINOUT freopen("in.txt", "w", stdout);
#define FOUT freopen("out.txt", "w", stdout);
#define y1 ____y1
using namespace std;
typedef const int CI;
typedef long long ll;
typedef unsigned long long ull;
//typedef __float128 ld;
//typedef long double ld;
typedef double ld;
typedef vector<int> VINT;
typedef vector<ld> VLD;
typedef vector<ll> VLL;
typedef vector<vector<int> > VVINT;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<PII, PII> PIIII;
typedef pair<ll, ll> PLL;
typedef pair<ld, ld> PLD;
typedef pair<ld, ld> ptp;
typedef complex<ld> Cn;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PIII> VPIII;
typedef unsigned int uint;
const ld eps=1e-12;
//const int INF = 1000000001;
const int oo=0x3F3F3F3F;
const ll ooLL=0x3F3F3F3F3F3F3F3FLL;
const int MOD=1000000007;
template<typename T> T sqr(T x) { return x*x; }
template<typename T> void debpr(const T &);
template<typename T1, typename T2> void debpr(const pair<T1, T2> &);
template<typename T1, typename T2> void debpr(const vector<T1, T2> &);
template<typename T> void debpr(const set<T> &);
template<typename T1, typename T2> void debpr(const map<T1, T2> &);
template<typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft=0; while (be!=en) { if (ft) debpr(mi); ft=1; debpr(*be); ++be; } debpr(fi); }
template<typename T> void debpr(const T &a) {
#ifdef EgQrauETGh
    cerr<<a;
#endif
}
template<typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); }
template<typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); }
template<typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); }
template<typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); }
void deb() {};
template<typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); }
template<typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); }
template<typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); }
template<typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); }
const ld PI=acos(-1.);
ll Round(double x) { return x<0?x-.5:x+.5; }
template <typename T> void ass(bool v, const T &x, string m="Fail") { if (!v) { deb(m); deb(x); throw; } }
int main()
{
#ifdef EgQrauETGh
    FIN; ld tm=clock();
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    void run(); run();
#ifdef EgQrauETGh
    cerr<<setprecision(9)<<fixed;
    deb((clock()-tm)/CLOCKS_PER_SEC);
#endif
    return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int add(int a, int b)
{
    a+=b; if (a>=MOD) a-=MOD; return a;
}
int mul(int a, ll b)
{
    return (a*b)%MOD;
}
int pw2(int p)
{
    return p<0?1:1<<p;
}
#include <array>
map<array<int, 3>, int> m;
int go(int n, int p, int l, int f)
{
    if (p==-1) return 1;
    int &rs=m[{p, l, f}];
    if (rs) return rs;
    if (f)
    {
        return rs=add(go(n, p-1, l+1, f), mul(go(n, p-1, l, f), pw2(l)));
    }
    else
    {
        int bi=(n>>p)&1;
        if (!bi) return mul(go(n, p-1, l, 0), pw2(l-1));
        if (l)
        {
            rs=add(rs, mul(go(n, p-1, l, 1), pw2(l-1)));
            rs=add(rs, mul(go(n, p-1, l, 0), pw2(l-1)));
        }
        else rs=add(rs, go(n, p-1, l, 1));
        rs=add(rs, go(n, p-1, l+1, 0));
        return rs;
    }
    throw;
}
void run()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", go(n, 30, 0, 0));
}


// https://pp.vk.me/c540102/v540102394/7cd5/J2NXxBbvBrM.jpg
#pragma GCC optimize("O500")
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <ctime>
#include <queue>
#include <cmath>
#include <deque>
#include <list>
#include <sstream>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
//#include <unordered_set>
//#include <unordered_map>
#include <cassert>
#include <iomanip>
#define mp make_pair
#define FOR(i,a,b) for(int i=(a),_b(b);i<_b;++i)
#define FORD(i,a,b) for(int i=(a),_b(b);i>=_b;--i)
#define REP(i,n) FOR(i,0,n)
#define CL(x) memset(x, 0, sizeof(x))
#define SINF(x) memset(x, 0x3F, sizeof(x))
#define SVAL(x, y) memset(x, y, sizeof(x))
#define CP(t, f) memcpy(t, f, sizeof(f));
#define CPS(t, f, s) memcpy(t, f, s*sizeof(f[0]))
#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define FIN freopen("in.txt", "r", stdin);
#define FINOUT freopen("in.txt", "w", stdout);
#define FOUT freopen("out.txt", "w", stdout);
#define y1 ____y1
using namespace std;
typedef const int CI;
typedef long long ll;
typedef unsigned long long ull;
//typedef __float128 ld;
//typedef long double ld;
typedef double ld;
typedef vector<int> VINT;
typedef vector<ld> VLD;
typedef vector<ll> VLL;
typedef vector<vector<int> > VVINT;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<PII, PII> PIIII;
typedef pair<ll, ll> PLL;
typedef pair<ld, ld> PLD;
typedef pair<ld, ld> ptp;
typedef complex<ld> Cn;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PIII> VPIII;
typedef unsigned int uint;
const ld eps=1e-12;
//const int INF = 1000000001;
const int oo=0x3F3F3F3F;
const ll ooLL=0x3F3F3F3F3F3F3F3FLL;
const int MOD=1000000007;
template<typename T> T sqr(T x) { return x*x; }
template<typename T> void debpr(const T &);
template<typename T1, typename T2> void debpr(const pair<T1, T2> &);
template<typename T1, typename T2> void debpr(const vector<T1, T2> &);
template<typename T> void debpr(const set<T> &);
template<typename T1, typename T2> void debpr(const map<T1, T2> &);
template<typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft=0; while (be!=en) { if (ft) debpr(mi); ft=1; debpr(*be); ++be; } debpr(fi); }
template<typename T> void debpr(const T &a) {
#ifdef EgQrauETGh
    cerr<<a;
#endif
}
template<typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); }
template<typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); }
template<typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); }
template<typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); }
void deb() {};
template<typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); }
template<typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); }
template<typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); }
template<typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); }
const ld PI=acos(-1.);
ll Round(double x) { return x<0?x-.5:x+.5; }
template <typename T> void ass(bool v, const T &x, string m="Fail") { if (!v) { deb(m); deb(x); throw; } }
int main()
{
#ifdef EgQrauETGh
    FIN; ld tm=clock();
#else
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    void run(); run();
#ifdef EgQrauETGh
    cerr<<setprecision(9)<<fixed;
    deb((clock()-tm)/CLOCKS_PER_SEC);
#endif
    return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int add(int a, int b)
{
    a+=b; if (a>=MOD) a-=MOD; return a;
}
int mul(int a, ll b)
{
    return (a*b)%MOD;
}
int pw2(int p)
{
    return p<0?1:1<<p;
}
map<array<int, 3>, int> m;
int go(int n, int p, int l, int f)
{
    if (p==-1) return 1;
    int &rs=m[{p, l, f}];
    if (rs) return rs;
    if (f)
    {
        return rs=add(go(n, p-1, l+1, f), mul(go(n, p-1, l, f), pw2(l)));
    }
    else
    {
        int bi=(n>>p)&1;
        if (!bi) return mul(go(n, p-1, l, 0), pw2(l-1));
        if (l)
        {
            rs=add(rs, mul(go(n, p-1, l, 1), pw2(l-1)));
            rs=add(rs, mul(go(n, p-1, l, 0), pw2(l-1)));
        }
        else rs=add(rs, go(n, p-1, l, 1));
        rs=add(rs, go(n, p-1, l+1, 0));
        return rs;
    }
    throw;
}
void run()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", go(n, 30, 0, 0));
}


/*
 * parasol
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
#define ll long long
const ll maxn = 1e6+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;
ll l1, r1, t1;
ll l2, r2, t2;

ll gcd(ll a, ll b){
    return b==0?a:gcd(b, a%b);
}

void solve() {

    if (gcd(t1, t2) == 1) {
        printf("%lld\n", min(len1, len2));
        return;
    }
    if (len1 > len2) {
        swap(l1, l2);
        swap(r1, r2);
        swap(t1, t2);
    }
    ll len1 = r1-l1+1;
    ll len2 = r2-l2+1;

    //if (l1 > l2) {
            //for(ll i = 1; i <= 10020; i++){
                //l2 += 100000*t2;
                //if (l2 >= l1) break;
            //}
            //l2 += t2;
        //}
        ll ans = 0;
        ll g = gcd(t1, t2);
        ll p1 =((l2-l1)%g+g)%g;
        if (p1 <= len1) {
            ll lenth1 = len1-p1;
            ans = max(ans, min(lenth1, len2));
        }
        ll p2 = (t1 - p1);
        ans = max(ans, min(len2-p2, len1));
        printf("%lld\n",ans);
}

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

    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;

    solve();
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
inline long long read () {
    char c = getchar();
    long long t = 0, f = 1;
    while (!isdigit(c)) f = (c == '-') ? -1 : 1, c = getchar();
    while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
    return t * f;
}
int a[N];
int b[N];
int getrand(int x, int w) {
    while(1) {
        int d = rand() % w;
        //cout << d << " " << x << endl;
        if(x < d)
            return d;
    }
}
main()
{
    int n, m, i, j, t, d, l1, r1, t1, l2, t2, r2;
    cin >> l1 >> r1 >> t1;
    cin >> l2 >> r2 >> t2;

    int ans = 0;
    if(g == 1)  {
        cout << min(r1 - l1 + 1, r2 - l2 + 1);
        return 0;
    }
    else {
        int e = l2 - l1;
        t2 = max(t2, t1);
        if(e < 0) {
            e += t2;
        }
        e %= g;
        ans = max(ans, min(r1 - l1 + 1 - e, r2 - l2 + 1));
        e = l1 - l2;
        if(e < 0) {
            e += t2;
        }
        e %= g;
        ans = max(ans, min(r2 - l2 + 1 - e, r1 - l1 + 1));
    }
    cout << ans;
}


#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;
#define tll(x) static_cast<ll>(x)

int la, ra, ta;
int lb, rb, tb;

int gcd(int a, int b) {
    if(b > a) swap(a, b);
    if(b == 0) return a;
    return gcd(b, a%b);
}
int gcd2(int a, int b, int &x, int &y)      //from geeksforgeeks
{
    if (a == 0) {
        x = 0;
        y = 1;
        return b;
    }

    int x1, y1;
    int gcd = gcd2(b%a, a, x1, y1);

    x = y1 - (b/a) * x1;
    y = x1;

    return gcd;
}
ll divF(ll a, ll b) {
    assert(b != 0);
    if(b < 0) a*=-1, b*=-1;
    if(a < 0) return -((-a + b - 1)/b); else return a/b;
}

int ans;
int v1, v2;
void test(ll t) {
    ll a1 = t * ta * v1 + la, a2 = t * ta * v1 + ra + 1;
    ll b1 = t * tb * (-v2) + lb, b2 = t * tb * (-v2) + rb + 1;
    //cerr << a1 << " " << a2 << " " << b1 << " " << b2 << "\n";
    ans = (max(ans, static_cast<int>(min(a2, b2) - max(a1, b1))));
}

int main() {
    scanf("%d%d%d%d%d%d", &la, &ra, &ta, &lb, &rb, &tb);

    int v = gcd(ta, tb);

    gcd2(ta, tb, v1, v2);
    //cerr << (tll(v1) * ta + tll(v2) * tb) << "\n";

    if(tll(v1) * ta + tll(v2) * tb == 1) {printf("%d\n", 1 + min(ra-la, rb-lb));return 0;}

    ans = 0;
    ll t = divF(lb - la, tll(v1) * ta + tll(v2) * tb);
    test(t - 1);
    test(t);
    test(t + 1);

    printf("%d\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
typedef pair<pii,ll> piii;
typedef pair<ll,pii> ipii;
#define MOD 1000000007
#define mod(a) ((a)%MOD)
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define dbg(x) cout<<#x<<" = "<<x<<"\n"
#define dbg2(x,y) cout<<#x<<" = "<<x<<" , "<<#y<<" = "<<y<<"\n"
#define dbg3(x,y,z) cout<<#x<<" = "<<x<<" , "<<#y<<" = "<<y<<" , "<<#z<<" = "<<z<<"\n"
#define INF (1LL<<60)
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;*/
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(v) v.begin(),v.end()
#define endl '\n'
ll gcd(ll a,ll b){if(a==0) return b; return gcd(b%a,a);}
ll powm(ll base,ll exp,ll mod=MOD) {base%=mod;ll ans=1;while(exp){if(exp&1LL) ans=(ans*base)%mod;exp>>=1LL,base=(base*base)%mod;}return ans;}

int main(){
     sync;
     ll l[2],r[2],t[2];
     cin>>l[0]>>r[0]>>t[0];
     cin>>l[1]>>r[1]>>t[1];
     if(l[0]<l[1]){
           swap(l[0],l[1]);
           swap(r[0],r[1]);
           swap(t[0],t[1]);
     }

     ll g=gcd(t[0],t[1]);
     ll ans=0;
     ll x=(l[0]-l[1])%g;
     ll k1=x/g*g;
     ll k2=(x+g)/g;
     ans=1+min(r[1],l[1]+x+(r[0]-l[0]))-max(l[1],l[1]+x);
     x-=g;
     ans=max(ans,1+min(r[1],l[1]+x+(r[0]-l[0]))-max(l[1],l[1]+x));
     cout<<max(0,ans)<<endl;

}


// God & me
// Life is ruined and love is lost :'(
// @Yazd
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
using namespace std;
template <class T, class L> bool smax(T &x,L y){  return x < y ? ( x = y, 1) : 0;  }
template <class T, class L> bool smin(T &x,L y){  return y < x ? ( x = y, 1) : 0;  }
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 17;
int n, m, p[maxn], d[maxn];
pii e[maxn];
set<pii> cur;
bool bad(int v, int u){
    return v == u || d[v] == 2 || d[u] == 2 || binary_search(e, e + m, pii(minmax(v, u))) || cur.count(minmax(v, u));
}
main(){
    ios::sync_with_stdio(0), cin.tie(0);
    srandom(time(0));
    cin >> n >> m;
    for(int i = 0; i < m; i++){
	cin >> e[i].X >> e[i].Y;
	e[i] = minmax(e[i].X, e[i].Y);
    }
    sort(e, e + m);
    if(n < 7){
	iota(p, p + n, 1);
	do{
	    bool ok = 1;
	    for(int i = 0; ok && i < m; i++)
		if(binary_search(e, e + m, pii(minmax(p[i], p[(i + 1) % n]))))
		    ok = 0;
	    if(ok){
		for(int i = 0; i < m; i++)
		    cout << p[i] << ' ' << p[(i + 1) % n] << '\n';
		return 0;
	    }
	}  while(next_permutation(p, p + n));
	return cout << "-1\n", 0;
    }
    for(int i = 0; i < m; i++){
	int v = random() % n + 1, u = random() % n + 1;
	while(bad(v, u))
	    v = random() % n + 1, u = random() % n + 1;
	cout << v << ' ' << u << '\n';
	d[v]++, d[u] ++;
	cur.insert(minmax(v, u));
    }
    return 0;
}


/*.
.
.
.
*///       _________        ________           ________         ________
          /////\\\\\\      /////\\\\\          ||||||||         ||||||||
         /////  \\\\\\    /////  \\\\\         ||||||||_________||||||||
        /////    \\\\\\  /////    \\\\\        ||||||||---------||||||||
       /////      \\\\\\/////      \\\\\  ...  ||||||||         ||||||||
//    `````        `````````        ````       ````````         ````````

///                            Plz Don't Copy
///                                  ^__^

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
#include <fstream>

using namespace std;

#define pb push_back
#define pp pop_back
#define mp make_pair
#define fr first
#define sc second
#define fo front()
#define FIX(x) cout.precision(x)
#define sz(x) ((int)(x).size())
#define smax(x,y) ((x)=max((x),(y)))
#define smin(x,y) ((x)=min((x),(y)))
#define all(x) (x).begin(),(x).end()
#define mem(x) memset(x,0,sizeof(x))
#define connect(z,x,y) z[x].pb(y),z[y].pb(x)
#define _ ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const ll N = 1e6+85;
const ll M = 4e3+85;
const ll mod = 1e9+7;

int p[N];
set<pii> st;

int main()
{_
	int n, m; scanf("%d %d",&n,&m);
	for(int i=1;n>=i;i++) p[i]=i;
	for(int i=1; i<=m; i++){
		int x, y; scanf("%d %d",&x,&y);
		st.insert(mp(min(x,y),max(x,y)));
	}
	int cnt=50;
	while(cnt--){
		random_shuffle(p+1,p+n+1);
		int can=0;  p[n+1]= p[1];
		for(int i=1; i<=n; i++)
			if (st.find(mp(min(p[i],p[i+1]),max(p[i],p[i+1])))==st.end()) can++;
		}
		if(can>=m)
			for (int i=1; i<=n; i++)
				if (st.find(mp(min(p[i],p[i+1]),max(p[i],p[i+1])))==st.end()){
					printf("%d%d\n",p[i],p[i+1]);
					m--;
					if(!m) return 0;
				}
	}
	return cout<<-1,0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
using namespace std;

#define DB(x) cout<<#x<<"="<<x<<" "
#define DBN(x) cout<<#x<<"="<<x<<"\n"

typedef long long ll;
typedef unsigned long long ull;

#define INF 111111111111111LL
#define EPS (double)1e-9
#define MOD 1000000007
#define PI 3.14159265358979328462

#define MAX 100005
int n, m;

int edge[MAX][3];
int newedge[MAX][3];
int ansx[MAX], ansy[MAX];
int ansi;
int sh[MAX];

void init()
{
    srand(time(0));
    memset(edge, 0, sizeof(edge));
    scanf("%d%d", &n, &m);
    int u, v;
    for (int i = 0; i < m; ++i)
    {
	scanf("%d%d", &u, &v);
	edge[u][++edge[u][0]] = v;
	edge[v][++edge[v][0]] = u;
    }

}

void work()
{
    int test = 100;
    int succ = false;
    while(test--) {
	int nm = m;
	for (int i = 1; i <= n; ++i)
	    sh[i] = 2;
	int c = 0;
	memset(newedge, 0, sizeof(newedge));
	while(nm!=0 && c < m * 100)
	{
	    int x = rand() % n + 1;
	    int y = rand() % n + 1;
	    if (x != y && sh[x] > 0 && sh[y] > 0 && x != edge[y][1] && x != edge[y][2] && x != newedge[y][1] && x != newedge[y][2]) {
		ansx[ansi] = x;
		ansy[ansi++] = y;
		newedge[x][sh[x]] = y;
		newedge[y][sh[y]] = x;
		sh[x]--; sh[y]--;
		nm--;
	    }
	    c++;
	}

	if (nm == 0)
	{
	    succ = true;
	    break;
	}
    }
    if (succ) {
	for (int i = 0; i < m; ++i)
	    cout << ansx[i] << " " << ansy[i] << endl;
    }
    else {
	cout << -1 << endl;
    }

}

int main(int argc, char *argv[])
{
    init();
    work();
    return 0;
}


// God & me
// Life is ruined and love is lost :'(
// @Yazd
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define random rand
using namespace std;
template <class T, class L> bool smax(T &x,L y){  return x < y ? ( x = y, 1) : 0;  }
template <class T, class L> bool smin(T &x,L y){  return y < x ? ( x = y, 1) : 0;  }
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 1e5 + 17;
int n, m, p[maxn], d[maxn];
pii e[maxn];
set<pii> cur;
bool bad(int v, int u){
    return v == u || d[v] == 2 || d[u] == 2 || binary_search(e, e + m, pii(minmax(v, u))) || cur.count(minmax(v, u));
}
main(){
    ios::sync_with_stdio(0), cin.tie(0);
    srandom(time(0));
    cin >> n >> m;
    for(int i = 0; i < m; i++){
	cin >> e[i].X >> e[i].Y;
	e[i] = minmax(e[i].X, e[i].Y);
    }
    sort(e, e + m);
    if(n < 7){
	iota(p, p + n, 1);
	do{
	    bool ok = 1;
	    for(int i = 0; ok && i < m; i++)
		if(binary_search(e, e + m, pii(minmax(p[i], p[(i + 1) % n]))))
		    ok = 0;
	    if(ok){
		for(int i = 0; i < m; i++)
		    cout << p[i] << ' ' << p[(i + 1) % n] << '\n';
		return 0;
	    }
	}  while(next_permutation(p, p + n));
	return cout << "-1\n", 0;
    }
    for(int i = 0; i < m; i++){
	int v = random() % n + 1, u = random() % n + 1;
	while(bad(v, u))
	    v = random() % n + 1, u = random() % n + 1;
	cout << v << ' ' << u << '\n';
	d[v]++, d[u] ++;
	cur.insert(minmax(v, u));
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include <cstdlib>

using namespace std;

int n, totalSum, size;
int res[20], a[20], g[20];
bool mark[20];
int sum;
int n2;

inline void check()
{
    for (int i = 0; i < n2; i++)
    {
        cout << res[i] << " ";
        if (i % n == n - 1)
            cout << endl;
    }
    exit(0);
}

void backTrack(int x)
{
    if(x == 3 && res[2] > res[1])
        return;
    if(x == 3)
    {
        sum = res[0] + res[1] + res[2];
        bool flag = false;
        int h;
        for(int i = 0; i < 16; i++)
            if(!mark[i] && totalSum == sum + a[i])
            {
                mark[i] = true;
                h = i;
                res[3] = a[i];
                flag = true;
                break;
            }
        if(!flag)
            return;
        else
        {
            backTrack(x + 2);
            mark[h] = false;
            return;
        }
    }
    else if(x == 4 && res[n - 1] > res[0])
        return;
    else if(x == 7)
    {
        sum = res[4] + res[5] + res[6];
        bool flag = false;
        int h;
        for(int i = 0; i < 16; i++)
            if(!mark[i] && totalSum == sum + a[i])
            {
                mark[i] = true;
                h = i;
                res[7] = a[i];
                flag = true;
                break;
            }
        if(!flag)
            return;
        else
        {
            backTrack(x + 2);
            mark[h] = false;
            return;
        }
    }
    else if(x > 0 && x % 4 == 0)
    {
        sum = 0;
        for (int i = 0; i < 4; i++)
            sum += res[x - 4 + i];
        if (sum != totalSum)
            return;
    }
    if(x == 12)
    {
        int in = 0;
        for(int j = 0; j < 4; j++)
        {
            sum = res[j] + res[j + 4] + res[j + 8];
            bool flag = false;
            for(int i = 0; i < 16; i++)
                if(!mark[i] && totalSum == sum + a[i])
                {
                    mark[i] = true;
                    g[in++] = i;
                    res[j + 12] = a[i];
                    flag = true;
                    break;
                }
            if(!flag)
            {
                for(int i = 0; i < in; i++)
                    mark[g[i]] = false;
                return;
            }
        }
        sum = 0;
        for (int i = 3; i < 15; i += 3)
            sum += res[i];
        if (sum != totalSum)
        {
            for(int i = 0; i < in; i++)
                mark[g[i]] = false;
            return;
        }
        sum = 0;
        for (int i = 0; i < n2; i += n + 1)
            sum += res[i];
        if (sum != totalSum)
        {
            for(int i = 0; i < in; i++)
                mark[g[i]] = false;
            return;
        }
        check();
    }
    for (int i = 0; i < 16; i++)
        if (!mark[i])
        {
            mark[i] = true;
            res[x] = a[i];
            backTrack(x + 1);
            mark[i] = false;
        }
}


#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>

#define mp make_pair
#define x first
#define y second

using namespace std ;

typedef long long LL ;

int n, s ;
int q[20], path[4][4], pow2[1 << 20] ;

int lowbit (int x)
{
    return x & (-x) ;
}

bool check (int x, int y)
{
    if (y == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[x][i] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][y] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1 && y == 0)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][n - i - 1] ;
        if (ss != s) return 0 ;
    }
    if (x == n - 1 && y == n - 1)
    {
        int ss = 0 ;
        for (int i = 0; i < n; i ++ ) ss += path[i][i] ;
        if (ss != s) return 0 ;
    }
    return 1 ;
}

bool dfs (int x, int y, int state)
{
    if (y == n) x ++, y = 0 ;
    if (x == n) return 1 ;
    for (int i = state, t; i; i -= lowbit (i))
    {
        t = lowbit (i) ;
        path[x][y] = q[pow2 (t)] ;
        if (check (x, y) && dfs (x, y + 1, state - t)) return 1 ;
    }
    return 0 ;
}

int main ()
{
    for (int i = 0; i < 18; i ++ ) pow2[1 << i] = i ;
    scanf ("%d", &n) ;
    for (int i = 0; i < n * n; i ++ ) scanf ("%d", &q[i]), s += q[i] ;
    s /= n ;
    dfs (0, 0, (1 << n * n) - 1) ;
    printf ("%d\n", s) ;
    for (int i = 0; i < n; i ++ )
    {
        for (int j = 0; j < n; j ++ )
            printf ("%d ", path[i][j]) ;
        puts ("") ;
    }
    return 0 ;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#include<deque>
#include<math.h>
#include<cstring>
#include <bitset>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>

#define all(v) v.begin(),v.end()
#define mpair make_pair

using namespace std;
typedef long double ld;
const ld epsilon = 1e-9;
typedef long long ll;

int mat[16];
void solveSmall(int n)
{
	int sz = n * n;
	sort(mat, mat + sz );
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	s /= n;
	vector<int> rows(n);
	vector<int> cols(n);
	do {
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += mat[i];
			cols[i % n] += mat[i];
		}
		bool does = true;
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += mat[i * n + i];
			diag2 += mat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << mat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(next_permutation(mat, mat + sz));
}

int cnt[8];
bool used[16];
int te;
bool getNextCnt()
{
	int lim = 15;
	for (int i = 7; i >= 0; i--)
	{
		used[cnt[i]] = false;
		for (int j = cnt[i] + 1; j < 16; j++)
		{
			if (!used[j])
			{
				cnt[i] = j;
				te = 0;
				used[j] = true;
				for (int k = i + 1; k < 8; k++)
				{
					for (; te < 16; te++)
					{
						if(!used[te])
						{
							used[te] = true;
							cnt[k] = te;
							break;
						}
					}
				}
				for (int i = 0; i < 8; i++)
					cout << cnt[i] << " ";
				cout << endl;
				return true;
			}
		}

	}
	return false;
}
void solve4()
{

	for (int i = 0; i < 8; i++)
		cnt[i] = i;
	int n = 4;
	int sz = n * n;
	int s = 0;
	for (int i = 0; i < sz; i++)
		s += mat[i];
	for (int i = 0; i <sz; i++)
		used[i] = i < 8;
	sort(mat, mat + sz);
	s /= n;
	int realMat[16];
	int a, x, y, c, z, t, q, b;
	vector<int> rows(n);
	vector<int> cols(n);
	int  tmpMat[16];
	do {
		a = mat[cnt[0]];
		x = mat[cnt[1]];
		y = mat[cnt[2]];
		c = mat[cnt[3]];
		z = mat[cnt[4]];
		t = mat[cnt[5]];
		q = mat[cnt[6]];
		b = mat[cnt[7]];
		realMat[2] = a;
		realMat[5] = x;
		realMat[6] = y;
		realMat[7] = c;
		realMat[9] = z;
		realMat[10] = t;
		realMat[12] = q;
		realMat[15] = b;

		realMat[3] = s - z - y - q;
		realMat[4] = s - x - y - c;
		realMat[14] = s - a - y- t;
		realMat[13] = s - q - b - realMat[14];
		realMat[1] = s - x - z - realMat[13];
		realMat[0] = s - realMat[1] - realMat[2] - realMat[3];
		realMat[8] = s - realMat[0] - realMat[4] - realMat[12];
		realMat[11] = s - realMat[10] - realMat[9] - realMat[8];

		memcpy(tmpMat, realMat, sizeof(realMat));
		bool does = true;
		sort(tmpMat, tmpMat + sz);
		for (int i = 0; i < sz; i++)
		{
			if(tmpMat[i] != mat[i])
			{
				does = false;
				break;
			}
		}
		if (!does)
			continue;
		for (int i = 0; i < n; i++)
		{
			rows[i] = cols[i] = 0;
		}
		for (int i = 0; i < sz; i++)
		{
			rows[i / n] += realMat[i];
			cols[i % n] += realMat[i];
		}
		bool does = true;
		for (int i = 0; i < n; i++)
		{
			if (rows[i] != s || cols[i] != s)
			{
				does = false;
				break;
			}

		}
		if (!does)
			continue;
		int diag = 0, diag2 = 0;
		for (int i = 0; i < n; i++)
		{
			diag += realMat[i * n + i];
			diag2 += realMat[n - i - 1 + i * n];
		}
		if (diag != s || diag2 != s)
			continue;
		cout << s << endl;
		for (int i = 0; i < sz; i++)
		{
			cout << realMat[i];
			if (i % n != n - 1)
				cout << " ";
			else
				cout << endl;
		}
		return ;
	} while(getNextCnt());

}

int main()
{
	//freopen("bobi.in", "r", stdin);
	//freopen("bobi2.in", "w", stdout);
	int n;
	cin >> n;
	for (int i = 0; i < n * n; i++)
	{
		cin >> mat[i];
	}
	if (n <= 3)
	{
		solveSmall(n);
	}
	else
		solve4();
	return 0;
}


#include<iostream>
//#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define int long long
#define mp make_pair
//setprecision
int inf=1000*1000*1000+10;
int const MAX=1000*100+10;
int const MAX2=1000*1000+10;
int const MAX3=5000+10;
map<int,int>t;
vector<int>in;
int lim;
int c=1,r=1;
int sum[10][10];
int sq[10][10];
map<int,int>t2;
map<int,int>t3;
int a3[MAX];
bool mark3[MAX];
int s3;
int ind3[10][10];
vector<int>ans3;
int sum13[10];
int sum23[10];
int sumd3;
int sumd23;
int r3;
void output()
{
    cout<<sum13[1]<<endl;
    for(int i=1;i<=s3;i++)
    {
        for(int j=1;j<=s3;j++)
        {
            cout<<ind3[i][j]<<" ";
        }
        cout<<endl;
    }
    exit(0);
}
void check2(vector<int>a)
{
    int x=1;
    int y=1;
    bool tmp=true;
    for(int i=0;i<ans3.size();i++)
    {
        ind3[x][y]=ans3[i];
        sum13[x]+=ind3[x][y];
        sum23[y]+=ind3[x][y];
        if(x==y)
            sumd3+=ind3[x][y];
        if(x+y==s3+1)
            sumd23+=ind3[x][y];
        y++;
        if(y>r3)
            y=1,x++;
    }
    int n=s3;
    for(int i=1;i<=n;i++)
    {
        if(sum13[i]!=sum13[1] || sum23[i]!=sum13[1])
            tmp=false;
    }
    if(sum13[1]!=sumd3 || sum13[1]!=sumd23)
        tmp=false;
    if(tmp==true)
    {
        output();
    }
    for(int i=1;i<=s3;i++)
        sum13[i]=0,sum23[i]=0;
    sumd3=0;
    sumd23=0;
}
void jay(int n)
{
    //cerr<<n<<" ";3
    if(n==0)
    {
        s3=sqrt(s3);
        check2(ans3);
        s3*=s3;
    }
    for(int i=1;i<=s3;i++)
    {
        int t4=1;
        if(!mark3[i])
            mark3[i]=1,ans3.pb(a3[i]),t4=0,jay(n-1);
        if(t4==0)
            mark3[i]=0,ans3.pop_back();
    }
}
void check()
{
    for(int i=1;i<=3;i++)
    {
        sq[i][4]=lim-sum[1][i];
        sq[4][i]=lim-sum[2][i];
    }
    sq[4][4]=lim-sq[4][1]-sq[4][2]-sq[4][3];
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            t2[sq[i][j]]++;
            if(t2[sq[i][j]]>t3[sq[i][j]])
                return;
        }
    }
    int a=sq[1][4]+sq[2][4]+sq[3][4]+sq[4][4];
    int d=sq[4][1]+sq[4][2]+sq[4][3]+sq[4][4];
    int b=0;
    for(int i=1;i<=4;i++)
        b+=sq[i][i];
    c=sq[1][4]+sq[2][3]+sq[3][2]+sq[4][1];
    if(c!=lim || b!=lim || a!=lim || d!=lim)
        return;
    cout<<lim<<endl;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            cout<<sq[i][j]<<" ";
        }
        cout<<endl;
    }
    exit(0);
}
void pick(int lvl)
{
    if(lvl==10)
    {
        check();
        for(int i=0;i<in.size();i++)
            t2[in[i]]=0;
        return;
    }
    for(int i=0;i<in.size();i++)
    {
        int tmp=in[i];
        if(t[tmp]!=0){
            sum[1][r]+=tmp;
            sum[2][c]+=tmp;
            t[tmp]--,sq[r][c]=tmp;
            //change(&x,&y);
            int lr=r;
            int lc=c;
            r++;
            if(r==4)
                c++,r=1;
            pick(lvl+1);
            c=lc,r=lr;
            t[tmp]++;
            sum[1][r]-=tmp;
            sum[2][c]-=tmp;
        }
    }
}
main(){
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n;
	cin>>n;
    lim=0;
    if(n!=4)
    {
        for(int i=1;i<=n*n;i++)
        {
            cin>>a3[i];
        }
        r3=n;
        s3=n;
        s3*=n;
        jay(s3);
        return 0;
    }
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;
            lim+=x;
            if(t[x]==0)
                in.pb(x);
            t[x]++;
            t3[x]++;
        }
    }
    lim/=n;
    pick(1);
    cout<<-1;
}


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

using namespace std;

int abs(int x)
{
	return (x>0)?(x):(-x);
}
int max(int x,int y)
{
	return (x>y)?(x):(y);
}

int main()
{
	int r1,c1,r2,c2,L,S,K;
	cin>>r1>>c1>>r2>>c2;
	if(r1==r2 || c1==c2)
		L=1;
	else
		L=2;
	if((r1+c1)%2 != (r2+c2)%2)
		S=0;
	else if((r2-r1)==(c2-c1) || (r1-r2)==(c2-c1))
		S=1;
	else
		S=2;
	K = max(abs(r2-r1),abs(c2-c1));
	cout<<L<<' '<<S<<' '<<K;
	return 0;
}


#include <iostream>
#include <cmath>

using namespace std;

int main(int argc, char** argv[])
{
    int x1, x2, y1, y2;

    cin >> x1 >> y1 >> x2 >> y2;

    int difX = abs(x2 - x1);
    int difY = abs(y2 - y1);

    if ((x1 == x2) || (y1 == y2)) cout << "1 ";
    else print("2 ");

    if (((x1 + y1) % 2) == ((x2 + y2) % 2)){
        if (difX == difY) print("1 ");
        else print("2 ");
    }
    else print("0 ");

    cout << max(difX, difY) << endl;
    return 0;
}

int print(char str[]){
    cout << str;
}

int println(char str[]){
    cout << str << endl;
}

int max(int x1, int x2){
    return (x1 > x2 ? x1 : x2);
}


#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main() {
	int a, b, c, d;

    cin >> a >> b >> c >> d;
    if (a == c || b == d) cout << 1; else cout << 2;
    cout << " ";
    if ((a + b) % 2 != (c + d) % 2) cout << 0; else {
        else   if (a + b == c + d || a - d == c - b)
          cout << 2;
              else cout << 1;
    }
    cout << " ";
    cout << max(abs(a - c), abs(b - d)) << endl;
    return 0;}


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

#define addMode(a, b)\
		{\
			a += (b);\
			if (a >= M)\
				a -= M;\
		}

using namespace std;

const int N = 500;
const int M = 1000000009;

bool term[N];
int trans[N][4];
int height[N];
int suflink[N];
int cnt;

int dp[1010][410][12];

void insert(char* seq, int n)
{
	int cur = 0;
	for (int i = 0; i < n; i++)
	{
		if (trans[cur][seq[i]] == 0)
		{
			height[++cnt] = i + 1;
			trans[cur][seq[i]] = cnt;
		}
		cur = trans[cur][seq[i]];
	}
	term[cur] = true;
}

void find_suflink()
{
	queue<int> q;

	suflink[0] = -1;
	q.push(0);
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();

		for (int i = 0; i < 4; i++)
		{
			if (trans[cur][i])
			{
				int next = trans[cur][i];
				int suf = suflink[cur];
				while (suf > 0 && !trans[suf][i])
					suf = suflink[suf];
				suflink[next] = suflink < 0 ? 0 : trans[suf][i];
				q.push(next);
			}
		}
	}
}

void find_fail()
{
	queue<int> q;
	q.push(0);
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();

		for (int i = 0; i < 4; i++)
		{
			if (!trans[cur][i])
			{
				int cand = cur;
				while (cand > 0 && !trans[cand][i])
					cand = suflink[cand];
				trans[cur][i] = trans[cand][i];
			}
			else q.push(trans[cur][i]);
		}
	}
}

int main()
{
	int n, m;
	char str[20];
	int maxlen = 0;
	cin>>n>>m;
	while (m--)
	{
		scanf("%s", str);
		int len = strlen(str);
		if (maxlen < len) maxlen = len;
		for (int i = 0; i < len; i++)
		{
			switch (str[i])
			{
				case 'A':
						str[i] = 0;
						break;
				case 'C':
						str[i] = 1;
						break;
				case 'G':
						str[i] = 2;
						break;
				case 'T':
						str[i] = 3;
						break;
			}
		}
		insert(str, len);
	}

	find_suflink();
	find_fail();

	/*printf("cnt = %d\n", cnt);
	for (int i = 0; i <= cnt; i++)
	{
		printf("suflink[%d] = %d\n", i, suflink[i]);
		for (int j = 0; j < 4; j++)
			printf("trans[%d][%d] = %d, ", i, j, trans[i][j]);
		printf("\n");
	}*/

	for (int j = 0; j <= cnt; j++)
		dp[n][j][0] = 1;
	for (int i = n - 1; i >= 0; i--)
		for (int j = 0; j <= cnt; j++)
			for (int k = 0; k < maxlen; k++)
			{
				dp[i][j][k] = 0;
				if (height[j] < k) continue;
				for (int d = 0; d < 4; d++)
				{
					int nj = trans[j][d];
					int nk = k + 1;
					if (term[nj])
						nk = 0;
					dp[i][j][k] += dp[i + 1][nj][nk];
					if (dp[i][j][k] >= M)
						dp[i][j][k] -= M;
				}
			}
	printf("%d\n", dp[0][0][0]);
	return 0;
}


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

const int modm = 1000000009;
const int MaXN = 128;
const int maxk = 4;

struct trieNode
{
       int next[maxk], fail;
       int danger;
       void init()
       {
              fail = -1;
              danger = 0;
              for (int i = 0; i < maxk; ++i) next[i] = -1;
       }
}t[MaXN];
int root, top;

void init()
{
       root = 0, top = 1;
       t[root].init();
}

int getid(char ch)
{
       switch(ch)
       {
              case 'A': return 0;
              case 'G': return 1;
              case 'C': return 2;
              case 'T': return 3;
       }
}

void trieins(char *s)
{
       int i = 0;
       int p = root;
       while(s[i])
       {
             int idx = getid(s[i]);
             if (t[p].next[idx] == -1)
             {
                    t[top].init();
                    t[p].next[idx] = top++;
             }
             p = t[p].next[idx];
             i++;
       }
       t[p].danger = strlen(s);
}

void build_ac_automation()
{
    int cur, tmp;
    queue<int> q;
    t[root].fail = root;
    t[root].danger = 0;
    q.push(root);
    while (q.empty() == 0)
    {
        cur = q.front(); q.pop();
        for (int i = 0; i < maxk; i+2+)
        {
            if (t[cur].next[i] != -1)
            {
                tmp = t[cur].next[i];
                if (cur == root) t[tmp].fail = root;
                else
                {
                    t[tmp].fail = t[t[cur].fail].next[i];
                    t[tmp].danger = max(t[tmp].danger,t[t[tmp].fail].danger);
                }
                q.push(tmp);
            }
            else
            {
                if (cur == root) t[cur].next[i] = root;
                else t[cur].next[i] = t[t[cur].fail].next[i];
            }
        }
    }
}

long long dp[1010][128][11];
int n, m;
int main()
{
       memset(dp,0,sizeof(dp));
       cin >> n >> m;
       init();
       for (int i = 0; i < m; i++)
       {
              char str[20];
              cin >> str;
              trieins(str);
       }
       build_ac_automation();
       dp[0][0][0] = 1;
       for (int i = 0; i < n; i++)
       {
              for (int j = 0; j < top; j++)
              {
                     for (int k = 0; k <= 10; k++)
                     {
                            if (dp[i][j][k] == 0) continue;
                            for (int tt = 0; tt < maxk; tt++)
                            {
                                   int aa = t[j].next[tt];
                                   if (t[aa].danger > k)
                                   {
                                          dp[i + 1][aa][0] += dp1[i][j][k];
                                          dp[i + 1][aa][0] %= modm;
                                   }
                                   else if (k + 1 <= 10)
                                   {
                                          dp[i + 1][aa][k+1] += dp[i][j][k];
                                          dp[i + 1][aa][k+1] %= modm;
                                   }
                            }
                     }
              }
       }
       long long ans = 0;
       for (int i = 0; i < top; i++)
       {
              ans = ans + dp[n][i][0];
              ans %= modm;
       }
       cout << ans % modm << endl;
//       system("pause");
       return 0;
}


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

using namespace std;

class SS {
private:
	static const int offset = 26;
public:
	int w = 0;

	SS(const string s) {
		for (const char &c : s) {
			w <<= 2;
			switch (c) {
			case 'A': w |= 0; break;
			case 'C': w |= 1; break;
			case 'T': w |= 2; break;
			case 'G': w |= 3; break;
			}
		}
		w |= s.length() << offset;
	}

	SS(const int w) : w(w) {}

	int len() const {
		return w >> offset;
	}

	int containsIdx(const SS &o) const {
		const int limit = (len() - o.len()) << 1;
		const int mask = (1 << (o.len() << 1)) - 1;
		for (int i = 0; i <= limit; i += 2) {
			const int xor = (w >> i) ^ o.w;
			if (!(xor & mask))
				return i >> 1;
		}
		return -1;
	}

	bool isPrefixOf(const SS &o) const {
		if (len() > o.len())
			return false;
		const int mask = (1 << (len() << 1)) - 1;
		const int diff = (o.len() - len()) << 1;
		const int xor = (o.w >> diff) ^ w;
		return !(xor & mask);
	}

	operator int() const {
		return w;
	}

	SS substr1() const {
		const int l = len() - 1; // 1
		const int mask = ~(3 << (l << 1)) & ~(-1 << offset);
		return (l << offset) | (w & mask);
	}

	SS operator+(const char c) const {
		const int l = len() + 1;
		const int mask = ~(-1 << offset);
		return (l << offset) | ((w << 2 | c) & mask);
	}

	friend ostream &operator<<(ostream &out, const SS &ss) {
		for (int i = (ss.len() - 1) << 1; i >= 0; i -= 2) {
			switch ((ss.w >> i) & 3) {
			case 0: out << 'A'; break;
			case 1: out << 'C'; break;
			case 2: out << 'T'; break;
			case 3: out << 'G'; break;
			}
		}
		return out;
	}
};

struct Dict {
	vector<SS> words;
} dict;

const int mod = 1000000009;

map<int, int> dp[1001][11];

int f(const int n, const int i, const SS ss) {

	auto it = dp[n][i].find(ss);
	if (it != dp[n][i].end())
		return it->second;

	//cout << n << ' ' << i << ' ' << ss << '\n';
	//static int noCalls = 0;
	//if (++noCalls > 50)
		//exit(0);

	if (n == 0 && i == ss.len())
		return dp[n][i][ss] = 1;

	for (auto &&w : dict.words)
		if (w.isPrefixOf(ss) && w.len() > i)
			return dp[n][i][ss] = f(n, i + 1, ss);

	if (n == 0)
		return dp[n][i][ss] = i ? f(n, i - 1, ss.substr1()) : 0;

	int ans = 0;
	if (ss.len() == 10) {
		if (i == 0)
			return dp[n][i][ss] = 0;
		SS ss1 = ss.substr1();
		for (char c = 0; c < 4; ++c)
			ans = (ans + f(n - 1, i - 1, ss1 + c)) % mod;
	} else
		for (char c = 0; c < 4; ++c)
			ans = (ans + f(n - 1, i, ss + c)) % mod;

	return dp[n][i][ss] = ans;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);

	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; ++i) {
		string s;
		cin >> s;
		dict.words.emplace_back(s);
	}

	cout << f(n, 0, 0);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ int r,y,b;
 cin>>y>>b>>r;
int x=min(y,b);
x=min(x,r);
cout<<x<<endl;
 if(x==b)cout<<3b;
 else if(x==r)cout<<3r-3;
 else if(x==y){x=min(b,r);
 if(x==b)cout<<3b;
 if(x==r)cout<<3r-3;}

    return 0;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<map>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9+7)

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

set<int> ans;
map<int,int> mp;
vector<int> ss;
int n, t, size;

// int fv(int i){
	 // int xx = (i)%n;
	 // int funv = xx+1;
	// while(xx){
		// xx = (xx+i)%n;
		// funv += xx+1;
	// }
	//// cout<<i<<" "<<funv<<" "<<__gcd(i,n)<<endl;
	// return funv;
// }

// void solve(int dep, int hoge){//cout<<dep<<" "<<hoge<<endl;
	// if(dep == size){
		// ans.insert(fv(hoge));
		// return;
	// }
	// int ma = mp[ss[dep]];
	// for(int i = 0, xxx = 1; i <= ma; i++, xxx *= ss[dep]){
		// solve(dep+1,hoge*xxx);
	// }
// }

signed main(){
	// cout<<fixed<<setprecision(7);

	cin>>n;

	t = n;

	for(int i = 2; i*i <= t; i++){
		if(t%i==0)ss.push_back(i);
		while(t%i==0){
			mp[i]++;
			t /= i;
		}
	}

	// if(t != 1) mp[t]++,ss.push_back(t);

	size = mp.size();

	// solve((int)0,(int)1);

	// for(int y : ans){
		// cout<<(y==1?"":" ")<<y;
	// }
	// auto it = ans.begin();
	 set<int>::iterator it = ans.begin();
	for(int i = 0; i < ans.size(); i++){
		if(i)cout<<" ";
		cout<<*it;
		it++;
	}
	cout<<endl;
	// cout<<size<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll y,b,r,count=0,l=999;
	cin>>y>>b>>r;
	if(y<l)
	{
	 l=y;
	}
    if(b<l)
    {
	 l=b;
    }
    if(r<l)
	{
	 l=r;
	}
	if(y<b && b<r)
	{
		count=(3*y)+3;
	}
	else 13 3 6
	if(r==y && y==b)
	{
		count=y+y-1+y-2;
	//or 3y-3
	}

	else
	  {
		  count=l+(l-1)+(l+1);
		  } 		//or 3l

	cout<<count<<endl;
	return 0;
}


#include <cstdio>
using namespace std;
int a[100];
int main()
{
    int n;
    scanf("%d",&n);
    if(n==0)
    {
        printf("0\n");
        break;
    }
    int i,k=0;
    while(n>0)
    {
        k++;
        for(i=1;i<=n;i++)
        {
            if((i+1)*i/2==n)
            {
                n=0;
                i++;
                break;
            }
            else if((i+1)*i/2>n)
            {
                n-=(i)*(i-1)/2;
                break;
            }
        }
        int j;
        for(j=1;j<=i;j++)
        {
            printf("%c",'a'+k-1);
        }
    }
    return 0;
}


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

const int MAX = 1e6 + 6;

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

	int K; cin >> K;

	for(int i=0; i<26; ++i){

		if(K == 0) break;
		int low = 0, high = K, c = 0;
		while(low < high){
			int mid = (low + high) >> 1;
			int frq = (mid * (mid - 1)) >> 1;
			if(frq > K) high = mid;
			else if(frq < K) low = mid + 1;
		}
		if(low * (low - 1) > 2 * K) --low;
		// for(int j=low - 50; j<=low + 50; ++j){
		// 	if(j <= 0) continue;
		// 	int frq = (j * (j - 1)) >> 1;
		// 	if(frq > K){
		// 		c = j - 1;
		// 		break;
		// 	}
		// }
		for(int j=0; j<low; ++j){
			char x = 'A' + i;
			cout << x;
		}
		// cout << i << ' ' << low << endl;
		K = K - ((low * (low - 1)) >> 1);
	}
	cout << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)
#define mod 1000000007
#define ma 1000005
vector<int> d;
int main()
{
	int n;
	cin>>n;
	if(n==0)
	{
		cout<<a<<endl;
		return 0;
	}
	d.push_back(1);
	int i=0;
	while(d[i]<n)
	{
		//cout<<d[i]<<endl;
		i++;
		d.push_back(d[i-1]+i+1);
	}
	//cout<<d[0]<<endl;
	char s='a';
	int k=0;
	if(d[i]>n)
	k=i-1;
	else
	k=i;

	while(n>0)
	{
		//cout<<n<<" "<<d[k]<<" "<<k+2<<"   "<<s<<endl;
		f(i,k+2)
		cout<<s;
		n=n-d[k];
		s++;
		if(n<=0)
		return 0;
		//cout<<d[13]<<endl;
		int l=0;int r=i;
		while(l<=r)
		{
			int mid=(l+r)/2;
		//	if(n==19)
		//	cout<<"   "<<l<<" "<<mid<<"  "<<d[mid]<<" "<<r<<endl;
			if(d[mid]>n)
			{
				//k=mid;
				r=mid-1;
			}
			else
			{
				k=mid;
				l=mid+1;
			}
		}

	}
	return 0;
}