#define _CRT_SECURE_NO_WARNINGS // scanf(), gets() (needed for Visual C++)

//#define NDEBUG
#include <cassert>

#include <iostream>
#include <cstdio>
#include <algorithm> // min(), max(), sort(), lower_bound()
#include <cstring> // memset(), memcpy()
#include <functional> // greater<>()
#include <vector>
#include <map>
#include <stack>
#include <queue>

using namespace std;

#define GETINT(x) scanf("%d", &x)
#define GETDBL(x) scanf("%lf", &x)
#define GETSTR(s) scanf("%s", s)
#define NEWINT(x) int x; scanf("%d", &x)
#define NEWDBL(x) double x; scanf("%lf", &x)
#define NEWSTR(s, ARRAYSIZE) char s[(ARRAYZIE)]; scanf("%s", s)
#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for(int i=0; i<(E); i++)
#define NEWLN putchar('\n')

typedef long long ll;

int a[100000];
int b[100000];
int n, d, x;

int getNextX() {
	x = ((ll)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){
		int j = getNextX() % (i + 1);
		swap(a[i], a[j]);
	}

	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 s = 30;
int inva[100000+8];
int bidx[100000+8];

int main() {
	cin >> n >> d >> x;
	initAB();

	for (int i=0; i<n; i++) inva[a[i]] = i;
	int ii = 0;
	for (int i=0; i<n; i++) if (b[i]) bidx[ii++] = i;
	bidx[ii] = 987654321;

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

		bool found = false;
		for (int val=n; val>=max(1, n-s); val--) {
			int j = inva[val];
			if (j<=i && b[i-j]) {
				//cout << val << endl;
				found = true;
				break;
			}
		}
		if (found) continue;

		int ans = 0;
		for (int ii=0; bidx[ii]<=i; ii++)
			ans = max(ans, a[i-bidx[ii]]);
		cout << ans << endl;
	}

	return 0;
}


/*....Abhilash's coding template....*/

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define mp make_pair
#define in insert
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define vll vector<ll>
#define vint vector<int>
#define sll set<ll>
#define sint set<int>
#define pll pair<ll,ll>
#define pint pair<int,int>
#define fl(i,a,b) for(ll i=a;i<=b;i++)
#define flr(i,a,b) for(ll i=a;i>=b;i--)
#define print(v) for(auto i=v.begin();i!=v.end();i++){cout<<*i<<" ";}cout<<"\n";
#define test() int te; cin >> te; while(te--)
#define PI 3.14159265
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int main()
{
            int n,k,temp,temp2;
    cin>>n>>k;
    temp=min(k,(n-k+1));
    temp2=min((7*temp-1),(temp-1+(3*n)));
    if(k==1||k==n)
        cout<<3*n<<endl;
    else
        cout<<temp2<<endl;
}


/* *											In the name of Allah
 *
 * codeforces 320	Magic Number
 * 92/4/2
 */

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

using namespace std;

bool MN (string n, int s)
{
	if (n[0] == '1')
	{
		for (int i = 1; i < s; i++)
		{
			n[i - 1] = n[i];
			s--;
			return MN (n, s);
		}
	}
	else
	{
		if (n[1] == '4')
		{
			if (n[2] == '4')
				return false;
			else
				for (int i = 2; i < s; i++)
				{
					n[i - 2] = n[i];
					s -= 2;
					return MN (n, s);
				}
		}
		else
		{
			for (int i = 1; i < s; i++)
			{
				n[i - 1] = n[i];
				s--;
				return MN (n, s);
			}
		}
	}
}

using namespace std;

int main()
{
	int s;
	bool b;
	string n;
	cin >> n;
	s = n.size();
	if (n[0] == '4')
	{
		cout << "NO\n";
		return 0;
	}
	for (int i = 0; i < s; i++)
		if (n[i] != '1' || n[i] != '0')
		{
			cout << "NO|N";
			return 0;
		}
	b = MN (n, s);
	if (b)
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;

const int N = 1e2 + 7;
int ans[N][3], sum;
set<int> sx, sy;

void boosted()
{
    ios_base::sync_with_stdio(false);
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
}

void files()
{
    freopen("input.txt", "r", stdin);
}

bool comp(pair<int, int> l, pair<int, int> r)
{
    return l > r;
}

int main()
{
    boosted();
    //files();
    string jabber_id;
    cin>>jabber_id;
    int kolvo = 0;
    for(int i = 0; i < jabber_id.size(); ++i){
        if(jabber_id[i] >= 'A' && jabber_id[i] <= 'Z'){
            jabber_id[i] += ' ';
        }
        if(jabber_id[i] == '/'){
            kolvo++;
            if(kolvo > 1){
                cout<<"NO";
                return 0;
            }
        }
    }
    if(    !(jabber_id[0] >= 'a' && jabber_id[0] <= 'z') &&
           !(jabber_id[0] >= '0' && jabber_id[0] <= '9') &&
           jabber_id[0] != '_'){
            cout<<"NO";
            return 0;
           }
    int i = 0;
    while(jabber_id[i] != '@'){
        if((!(jabber_id[i] >= 'a' && jabber_id[i] <= 'z') &&
           !(jabber_id[i] >= '0' && jabber_id[i] <= '9') &&
           jabber_id[i] != '_') || (i >= 16)){
            cout<<"NO";
            return 0;
           }
        i++;
    }
    i++;
    if(jabber_id[i] == '.'){
        cout<<"NO";
        return 0;
    }
    int kol = 0, kolvsl = 0;
    while(jabber_id[i] != '/' && i < jabber_id.size()){
        if(( !(jabber_id[i] >= 'a' && jabber_id[i] <= 'z') &&
           !(jabber_id[i] >= '0' && jabber_id[i] <= '9') &&
           jabber_id[i] != '_' && jabber_id[i] != '.') || (kol > 32) || (kolvsl > 16)) {
            cout<<"NO ";
            //cout<<kol<<' '<<kolvsl<<endl;
            return 0;
           }
        i++;
        kol++;
        if(jabber_id[i] == '.' && jabber_id[i - 1] == '.'){
            cout<<"NO";
            return 0;
        }
        if(jabber_id[i - 1] == '.'){
            kolvsl = 0;
        } else{
            kolvsl++;
        }
    }
    if(kolvsl == 0){
        cout<<"NO";
        return 0;
    }
    if(i >= jabber_id.size()){
        cout<<"YES";
        return 0;
    }
    kol = 0;
    if(jabber_id[i] == '/'){
        while(i < jabber_id.size()){
            if(( (jabber_id[i] >= 'A' && jabber_id[i] <= 'Z') &&
                !(jabber_id[i] >= 'a' && jabber_id[i] <= 'z') &&
                !(jabber_id[i] >= '0' && jabber_id[i] <= '9') &&
                jabber_id[i] != '_') || (kol > 16)){
                    cout<<"NO";
                    return 0;
                }
            i++;
        }
    }
    cout<<"YES";

    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define  _CRT_SECURE_NO_WARNINGS
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
//#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_set>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#  define __builtin_popcountll __popcnt64
inline int getchar_unlocked() { return _getchar_nolock(); }
inline int putchar_unlocked(int c) { return _putchar_nolock(c); }
#endif

using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T& x) { x = 0; bool _ = 0; T c = gc; _ = c == 45; c = _ ? gc : c; while (c < 48 || c>57)c = gc; for (; c < 48 || c>57; c = gc); for (; c > 47 && c < 58; c = gc)x = (x << 3) + (x << 1) + (c & 15); x = _ ? -x : x; }
template<typename T> void printn(T n) { bool _ = 0; _ = n < 0; n = _ ? -n : n; char snum[65]; int i = 0; do { snum[i++] = n % 10 + 48; n /= 10; } while (n); --i; if (_)pc(45); while (i >= 0)pc(snum[i--]); }
template<typename First, typename ... Ints> void scan(First& arg, Ints&... rest) { scan(arg); scan(rest...); }
template<typename T> void print(T n) { printn(n); pc(10); }
template<typename First, typename ... Ints> void print(First arg, Ints... rest) { printn(arg); pc(32); print(rest...); }
#define db(x)       { cerr << #x << " = " << x << endl; }
template <typename T> void _dbarr(T* a, size_t sz) { for (int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz - 1]; }
template <typename T> void _dbarr(vector<T> a, size_t sz) { for (int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz - 1]; }
#define dbarr(x, n) {cerr << #x << ": "; _dbarr((x),(n));}
#define pb push_back
#define eb emplace_back
#define mp make_pair
const int MM = 105;
int n, k, l, d[MM];

bool vis[MM][MM][MM];
bool solve(int pos, int tide, bool inc)
{
	if (pos >= n + 1) return true;
	if (vis[pos][tide][inc]) return false;
	vis[pos][tide][inc] = true;
	int ntide = tide + (inc ? 1 : -1);
	bool ninc = inc;
	if (ntide == 0) ninc = true;
	else if (ntide == k) ninc = false;

	for(int i = 0; i < 2; i++)
	{
		if (d[pos + i] + ntide <= l)
			if (solve(pos + i, ntide, ninc)) return true;
	}
	return false;
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);
	int t;
	cin >> t;
	while(t--)
	{
		memset(vis, 0, sizeof vis);
		memset(d, 0, sizeof d);

		cin >> n >> k >> l;
		for(int i = 1; i <= n; i++)
		{
			cin >> d[i];
		}

		cout << (solve(0, 0, true) ? "Yes" : "No") << "\n";
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstring>
#include <utility>
#include <unordered_set>
using namespace std;

int n1,k1,n2,k2;

int main(){
	cin >> n1 >> k1 >> n2 >> k2;
	if(n1 > n2) cout << "First" << endl;
	else cout << "Second" << endl;
	return 0;
}


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

#define ll              long long
#define pb              push_back
#define ppb             pop_back
#define endl            '\n'
#define mii             map<ll,ll>
#define pii             pair<ll,ll>
#define vi              vector<ll>
#define vs              vector<string>
#define all(a)          (a).begin(),(a).end()
#define F               first
#define S               second
#define sz(x)           (ll)x.size()
#define hell            1000000007
#define inf             (ll)1e18 + 5
#define PI              3.14159265358979323844
#define rep(i,a,b)      for (ll i=a;i<b;i++)
#define repr(i,a,b)     for (ll i=a-1;i>=b;i--)
#define lbnd            lower_bound
#define ubnd            upper_bound
#define mp              make_pair
#define ios             ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ordered_set     tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(...)      __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

using namespace std;
using namespace __gnu_pbds;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//primes for hashing 937,991,1013,1409,1741

void solve() {
    ll x,y,n,maxi,mini,meow;
    cin>>x>>y>>n;
    set <pii> s;
    rep(i,1,n+1){
    	mini=((x*i)/y);
    	maxi=ceil((x*i*1.0)/(y*1.0));
    	if ((mini+maxi)*y>2*x*i){
    		meow=__gcd(i,mini);
    		s.insert({i/meow,mini/meow});
    	}
    	else {
    		meow=__gcd(i,maxi);
    		s.insert({i/meow,maxi/meow});
    	}
    }
    ll q=(*s.begin()).F;
    ll p=(*s.begin()).S;
    for (auto i:s){
    	if ((i.S)*y==x*(i.F)){
    		p=i.S;
    		q=i.F;
    	}
    	else if (((i.S)*y>(i.F)*x) && (p*y>x*q)){
    		if ((i.S)*q<(p*(i.F))){
    			p=i.S;
    			q=i.F;
    		}
    	}
    	else if (((i.S)*y<(i.F)*x) && (p*y>x*q)){
    		if (2*x*q*(i.F)<y*((p*(i.F)) + (q*(i.S)))){
    			p=i.S;
    			q=i.F;
    		}
    	}
    	else if (((i.S)*y>(i.F)*x) && (p*y<x*q)){
    		if (2*x*q*(i.F)>y*((p*(i.F)) + (q*(i.S)))){
    			p=i.S;
    			q=i.F;
    		}
    	}
    	else if (((i.S)*y<(i.F)*x) && (p*y<x*q)) {
    		if ((i.S)*q>(p*(i.F))){
    			p=i.S;
    			q=i.F;
    		}
    	}
    }
    meow=__gcd(p,q);
    cout<<p/meow<<'/'<<q/meow<<endl;
}

signed main() {
    ios
    ll test=1;
    //cin>>test;
    while (test--) solve();
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int ans=6;
	while(n--)
	{
		int k;
		cin>>k;
		if(ans==k)
		{
			cout<<"NO";
			return 0;
		}
		ans=6-ans-k;
	}
	cout<<"YES";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool a[120];
int n,x,js;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>x;
		a[x]=1;
	}
	for(int i=1;i<=620;i++)
		if(a[i]==1)
		js+=1;
	cout<<js-12;
    return 0;
}


#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<map>

#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define MS0(x) memset(x,0,sizeof(x))

typedef long long LL;
using namespace std;
int n,x;

int gcd(int a,int b){
  if(b==0)return a;
  return gcd(b,a%b);
}

LL pw(int i,int z){
  if(z==1)return i;
  LL t=pw(i,z/2);
  if(z&1)return(t*t%n*i%n);
  return(t*t%n);
}

int main(){
  scanf("%d%d",&n,&x);
  n++;
  FORD(i,x-1,2){
    if((pw(i,n-1)==1)&&((i-1)%n!=0)){
      printf("%d",i);
      exit(0);
    }
  }

  printf("-1");
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int n,x,a[1000000],tot;
long long modpow(long long x,long long y,int p){
	long long res=1;
	for(;y;y>>=1,(x*=x)%=p)
		if(y&1)(res*=x)%=p;
	return res;
}
bool check(int b,int p){
	for(int i=1;i*i<p;i++)
		if((p-1)%i==0){
			a[tot++]=i;
			if(i*i!=p-1)
				a[tot++]=(p-1)/i;
		}
	sort(a,a+tot);
	for(int i=0;i<tot;i++)
		if(a[i]!=1&&modpow(b,(p-1)/a[i],p)==1)return 0;
	return 1;
}
int main(){
	cin>>n>>x;
	n++;
	for(int i=2;i<n;i++)
		if(n%i==0)return puts("-1"),0;
	for(int i=x-1;i>=1;i--)
		if(check(i%n,n))
			return printf("%d\n",i),0;
	puts("-1");
}


#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n, d;
    int mas[110] = {0};
    cin >> n >> d;
    for ( int i = 0 ; i < n; i++)
	{
		cin >> mas[i];
	}

    if (n == 1 )
	{
		cout << "0"<< endl;
		return 0;
	}

	sort(&mas[0], &mas[n-1]);

	if(n == 2)
	{
		if (mas[1] - mas[0] > d)
			cout << "1" << endl;
		else
			cout << "0"<< endl;
		return 0;
	}

	int x;
	int rez =0;
	int maxrez = 0;

	for (int i = 0; i < n-1; i++)
	{
		for(int j = i; j < n; j++)
		{
			if (abs(mas[i] - mas[j] )<=d)
			{
				rez++;
			}
			else
			{
				maxrez = max(maxrez,rez);
				rez = 0;
				break;
			}
		}
		maxrez = max(maxrez,rez);
		rez = 0;
	}

	cout  << n - maxrez << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>

std::vector<long long> ans;

int main()
{
	long long n, k, i;
	std::cin >> n >> k;

	for (i = 1; i <= sqrt(n); i++)
		if (!(n % i))
			ans.push_back(i);

	while (--i > 0)
		if (!(n % i))
			ans.push_back(n / i);

	if ((n == 1 && k > 1) || (k > ans.size()))
		std::cout << -1;
	else
		std::cout << ans[k - 1];

	return 0;
}


//wise words from the departing:
//eat your greens, especially broccoli
//remember to say thank you for all the things
//you haven't had
#include <ctime>
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <string>
#include <bitset>
#include <string.h>
#include <stdio.h>
#include <map>
#include <sstream>
#include <set>
#include <stdlib.h>
#include <memory.h>

using namespace std;

#define		FOR(i,a,b) for(int i=int(a);i<int(b);++i)
#define		X first
#define		Y second
typedef pair <int,int> pii;
#define MP(a,b) make_pair(a,b);
typedef vector <int> vi;
typedef set <int> si;

#define EPS 1e-7

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

  cin >> n;

  int a,b, cnt = 0;
  int c;

  for(a = 1; a<=n; a++) {
    for(b = a+1; b<=n; b++) {
      c = (int)max(a,b)*sqrt(1.0+min(a,b)*min(a,b)*1.0/(max(a,b)*max(a,b)));
      if(c <=n && a<=c && b<=c && c*c == a*a+b*b) {
        cnt++;
      }

    }
  }
  printf("%d\n", cnt);
  return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#define int long long

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fs first
#define sc second
#define MP make_pair
#define pb push_back
#define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define sz(x) (int)x.size()
#define sqr(x) ((x) * (x))

const int INF = 1e18;
signed main() {
    fastio;

    int x, y;
    cin >> x >> y;
    vector<int> a;
    for (int i = 1; i * i <= x; i++) {
        if (x % i == 0) {
            a.pb(i);
            if (i * i != x)
                a.pb(x / i);
        }
    }

    sort(all(a));
   /* for (auto x:a)
        cout << x << ' ';*/
    int ans = 0;
    while (y) {

        int g = __gcd(x, y);
        int best = INF;
        for (int i = 0; i < sz(a); i++) {
            if (a[i] > y) break;
            if ((y - a[i]) % g || a[i] <= g) continue;
            int d = (y - a[i]) / g;
            best = min(best, d);
        }
        if (best == INF) {
            ans += y / g;
            break;
        }
        ans += best;
        y -= best * g;
    }
    cout << ans << '\n';
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define QWQ cout<<"QwQ"<<endl;
#define ll long long
#include <vector>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int N=101010;
const int qwq=303030;
const int inf=0x3f3f3f3f;

int n,m;
char s[N];
int a[N],sum[N];
int num[N];
int b[N];
int ans = -1;
priority_queue <int, vector<int>, greater<int> > q;

inline int read() {
	int sum1 = 0, f = 1; char c = getchar();
	while(c<'0' || c>'9') { if(c=='-') f = -1; c = getchar(); }
	while(c>='0'&&c<='9') { sum1 = sum1 * 10 + c - '0'; c = getchar(); }
	return sum1 * f;
}

void DFS(int h,int res) {
	if(h==n) {
		ans = max(ans,res);
		if(res==5184) {
			for(int i=1;i<=n;i++)
				cout<<(char)(b[i]+'a');
			cout<<endl;
		}
		return ;
	}
	h++;
	for(int i=0;i<26;i++) {
		if(!num[i]) continue;
		if(h>(n>>1) && b[n-h+1]==i) continue;
		num[i]--;
		b[h] = i;
		if((s[h]-'a')==i) DFS(h,res+a[h]);
		else DFS(h,res);
		num[i]++;
	}
}

void work1() {
	DFS(0,0);
}

void work2() {
	if(num[0]!=num[1]) return ;
	ans = 0;
	for(int i=1;i<=n;i++) ans += a[i];
	for(int i=1;i<=n/2;i++)
		if(s[i]==s[n-i+1])
			ans -= min(a[i],a[n-i+1]);
}

void work3() {
	for(int i=0;i<16;i++) if(num[i]>(n/2)) return ;
	memset(num,0,sizeof(num)); ans = 0;
	for(int i=1;i<=n;i++) ans += a[i];
	int tot = 0,mann = 0, cl;
	for(int i=1;i<=n/2;i++) {
		if(s[i]==s[n-i+1]) {
			num[s[i]-'a']++; tot++;
			ans -= min(a[i],a[n-i+1]);
		}
	}
	for(int i=0;i<26;i++) if(num[i]>mann) mann = num[i], cl = i;
	if(tot>=(mann<<1)) return ;
	tot = 2*mann - tot;
	for(int i=1;i<=n/2;i++) {
		if(s[i]==s[n-i+1]) continue;
		if(s[i]==cl+'a') q.push(a[n-i+1]);
		else if(s[n-i+1]==cl+'a') q.push(a[i]);
		else q.push(min(a[n-i+1],a[i]));
	}
	while(tot--) {
		ans -= q.top(); q.pop();
	}
}

int main() {
	n = read();
	if(n&1) { cout<<-1; return 0; }
	scanf("%s",s+1);
	for(int i=1;i<=n;i++) num[s[i]-'a']++;
	for(int i=1;i<=n;i++) a[i] = read();
	// if(n<=12) work1();
	// else if(num[0]+num[1]==n) work2();
	work3();
	cout<<ans;
	return 0;
}

/*

10
qqobivboqt
429 635 612 360 711 268 933 281 352 871

4
abba
1 1 2 2

12
aababbbbabcc
2 2 2 1 1 1 1 1 1 1 2 2

*/


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

ll modulo = 1e9 + 7;

int main()
{
  string s;
  ll a,b,c,d,e,f;
  ld ar1,ar2,ar3,ar4,an1,an2,h1,h2;
  cin>>a>>b>>c>>d>>e>>f;
  ar1=a*f*pow(3,0.5)/4;
  ar2=c*d*pow(3,0.5)/4;
  h1=pow(a*a+f*f+a*f,0.5);
  h2=pow(c*c+d*d+c*d,0.5);
  an1=asin(pow(3,0.5)*f/2/h1);
  an1=(2*M_PI/3)-an1;
  ar3=0.5*b*h1*sin(an1);

  an1=asin(pow(3,0.5)*c/2/h2);
  an1=(2*M_PI/3)-an1;
  ar4=0.5*e*h2*sin(an1);
  ar1=ar1+ar2+ar3+ar4;
  ar2=pow(3,0.5)/4;
  ar1=round(ar1/ar2);
  cout<<ar1<<endl;

	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <utility>
#include <array>
#include <map>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define pb push_back
typedef long long ll;
using namespace std;

inline ll calc(ll n) {
  return (n*(n-1))/2;
}

int main() {
  FAST_IO;
  ll n,m; cin>>n>>m;
  ll min=0,max;
  if(n%m==0) min=m*calc(n/m);
  else min=(n%m)*calc((n/m)+1);
  max=calc(n-m+1);
  cout<<min<<' '<<max;
  return 0;
}


#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main()
{
    int a;
    cin>>a;
    int t=0,t1=0,T=0;
    for(int i=0;i<a;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x%2!=0){t1++;}
        if(y%2!=0){t++;}
        else if(x%2!=0&&y%2==0||y%2!=0&&x%2==0){T++;}
    }
    if(t1%2==0&&t%2==0){cout<<0<<endl; }
    else if(t1%2!=0&&t%2!=0&&T>0){cout<<1<<endl; }
    else cout<<-1<<endl;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<ctime>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
#define mm(a,b) memset(a,b,sizeof(a))
#define MAXN 100010
#define inf 0x3f3f3f3f
int gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll l, a, b;
int main()
{
	ll ans = 0;
	cin >> l >> a >> b;
	ll m = a*b / gcd(a, b);
	ans += (l / m)* min(a, b);
	l %= m;
	ans += min(l, min(a - 1, b - 1));
	cout << ans;
}


#include <bits/stdc++.h>

using namespace std;
int main(){
	int b,g,n;
	cin >> b;
	cin >> g;
	cin >> n;
	int ans = b;
	if (b >= g)
		ans = g;
		if (g >= n)
			ans = n;
	if (b >= n)
		ans = n;

	return ans+1;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,x,y,_count;
const ll N=1e7+9;
ll dp[N];
ll dfs(ll s)
{
	// _count++;
	// cout<<s<<" "<<_count<<"\n";
	if(dp[s]!=-1)	return dp[s];
	ll ans=1e18;
	ans=min(ans,y+(s&1)*x+dfs((s+1)/2ll));
	return dp[s]=ans;
}
int main()
{
	memset(dp,-1,sizeof(dp));
	dp[0]=0;dp[1]=x;
	cin>>n>>x>>y;
	cout<<dfs(n);
	// for(int i=2;i<=n;i++){
	// 	dp[i]=x+dp[i-1];
	// 	dp[i]=min(dp[i],((i%2)*x)+y+dp[(i+1)/2]);
	// }
	// cout<<dp[n];
	//check for int vs ll
	return 0;
}


#include <iostream>

using namespace std;

int main() {
int n;
int count=0;
cin>>n;
for(int i=0;i<n;i++){
    if((n-i)%i==0){count++;}
}
cout<<count;
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int iIn1,iIn2,iIn3,iIn4;
int main(){
	scanf("%d%d%d%d",&iIn1,&iIn2,&iIn3,&iIn4);
	printf("%d",(iIn1^iIn2)&(iIn3|iIn4)^(iIn2&iIn3)|(iIn1^iIn4));
}


#include<iostream>//
#include<fstream>
#include<cmath>
using namespace std;
#define LL __int64
typedef long long ll;

int main()
{
    ifstream in("input.txt");
    ll i=0;
    ll x,y,m;
    cin>>x>>y>>m;

    if(x==-1000000000000000000LL)
    {
        cout<<"1000000000000000087";
        return 0;
    }
    if( m>x && m>y && x<=0 && y<=0 )
    {
        cout<<"-1";
        return 0;
    }
    else{
        if(x>=m || y>=m)
            i=0;
        else
        {
            ll a;
            if(x<=y)
            {
                a=(-x/y) +1;
                x+=a*y;
            }
            else
            {
                a= (-y/x) +1;
                y+=a*x;
            }
            i+=a;
            while(x<m || y< m)
            {
                x<=y? x+=y : y+=x;
                i++;
            }
            i--;
        }
    }
    cout<<i<<endl;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"

using namespace std;
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
#include <assert.h>
typedef long long ll;
typedef double ld;
#define PB push_back
#define MP make_pair
const int MOD=1000000007;
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<29;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;
const ll SLIM=sqrt((ld)1e11)+4;
/*
(n+k)*r+t+q=A // k>=q
(n+k)*r+t+q=A+1 // k>=q>=1 if gap exists, k>=1 o/w

(n+k)*r+q = A-t
k*r+q = A-t-n*r

k*r+q = RHS
0<=q<=t

k = RHS/r to (RHS-t)/r

Find highest k st k>=q

(w+q)*r+q = A-t-n*r
Find highest w+q st 0<=w<=n-t, 0<=q<=t
This means q must be minimized.

Suppose q is free. Then w can be binsearched?
If k is too high, then w is too high
*/

ll solve(ll n, ll t, ll A, bool qone) {
  if (A<t) return -1;
  if (n<=SLIM) {
    //printf("AA");
    for (ll k=n;k>=0;k--) {
      ll q=(A-t)%(n+k);
      if (qone && q==0) continue;
      if (k>=q && k-q <= n-t && q<=t) {
        assert(0<=q&&q<=t&&q<=k&&0<=k-q&&k-q<=n-t);
        return k;
      }
    }
    return -1;
  }
  else {
    //printf("BB");
    ll rlim=2+((A-t)/n);
    ll final=-1;
    for (ll r=1;r<=rlim;r++) {
      ll rhs=(A-t-n*r);
      if (rhs<0) break;
      ll kmin=((rhs+1+r)/(r+1)); ll kmax=min(n,rhs/r)+1;
      while(kmin<kmax) {
        ll k=(kmin+kmax)/2;
        ll q=rhs-r*k;
        bool toolow= (k-q<=n-t) || (qone&&q==0);
        if (toolow) kmin=k+1;
        else kmax=k;
      }
      ll k=kmin-1;
      ll q=rhs%k;
      //if (r==1) printf("k:%lld q:%lld t:%lld rhs:%lld A:%lld\n",k,q,t,rhs,A);
      if (r*k+q==rhs&&q<=t&&k-q<=n-t) {
        assert(0<=q&&q<=t&&q<=k&&0<=k-q&&k-q<=n-t);
        assert((n+k)*r+q == A-t);
        chkmax(final,k);
      }
    }
    {
      ll q=A-t;
      ll w=n-t;
      if (!qone || q>=1) {
        if (q<=t && t+q==A) {
          chkmax(final,q+w);
        }
      }
    }
    return final;
  }
}
//  XXX
//01234
//01
int main()
{
  ios::sync_with_stdio(false); cin.tie(0);
  ll n,l,r,A;
  cin>>n>>l>>r>>A;
  if (l>r) r+=n;
  ll t=(r-l+1)%n;
  //printf("t:%lld\n",t);
  ll ans0=solve(n,t,A, false);
  ll ans1=solve(n,t,A+1, t!=0);
  ll final=max(ans0,ans1);
  printf("%lld\n",final);
}


#include<iostream>
#include<math.h>
using namespace std;
int main()
{unsigned long long  A,B,C;
unsigned long long  q;
q=sqrt(A*B*C);
cout << 4*((q/A)+(q/B)+(q/C));
}


  /** 	Author: Apoorva Mittal  **/
  /**   Published: 19/11/2020 21:20:22 **/
#include <bits/stdc++.h>
using namespace std;
#define int             long long
//#warning int long long MLE
#define endl            "\n"
#define vi              vector<int>
#define vvi             vector<vector<int>>
#define vii             vector<pair<int, int>>
#define rep(i,a,b)      for(int i=a;i<b;i++)
#define repd(i,b,a)     for(int i=b;i>=a;i--)
#define init(i, k)      memset(i, k, sizeof i)
#define pb              push_back
#define pii             pair<int, int>
#define ff              first
#define ss              second
#define all(a)          a.begin(), a.end()
#define maxe            *max_element
#define mine            *min_element
#define pow2(n)          (1LL << (n))
#define sz(a)            (int)a.size()
#define unq(x)          x.erase(unique(x.begin(), x.end()), x.end())
#define LOCAL
//--------------------------------------------debugging showoff---------------------------------------------------------
#define out(a)                               \
    cout << #a << ": "<<endl;                      \
    rep(i, 0, a.size()) cout << a[i] << " "; \
    cout << endl;
#define out2d(a,w)                \
    cout << #a << ": " << endl; \
    rep(i, 0, a.size())         \
    {                           \
        rep(j, 0, a[0].size())  \
                cout            \
           <<setw(w)<< a[i][j] << " ";  \
        cout << endl;           \
    }
#define sim template < class c
#define ris return * this
#define dor > deb & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, deb&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb {
#ifdef LOCAL
~deb() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define show(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL
//--------------------------------------------useless stuff---------------------------------------------------------------------

#define speed                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define flush                fflush(stdout);
#define lb                   lower_bound
#define ub                   upper_bound
#define precise(x)           cout << fixed << showpoint << setprecision(x);
#define total1(x)            __builtin_popcountll(x)
#define left1(x)            __builtin_clzll(x) // "63-right1(x)" to find 0 based index of first set bit from left
#define right1(x)             __builtin_ctzll(x) // to find 0 based index of first set bit from right
#define gcd(a, b)            __gcd(a, b)
#define n2b(n,b)               bitset<b>(n).to_string()

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
//------------------------------------------------------------------------------------------------------------------------------
const int mod = 998244353;  //1000000007
// ====== Useful functions begin ======
inline int add(int a, int b) { return ((a % mod) + (b % mod)) % mod; }
inline int sub(int a, int b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline int mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; }
// ====== Useful functions end ======
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
//--------------------------------------------------------------------------------------------------------------------------------
const double PI = acos(-1.0);
const long long inf = 1e18;  //1000000000000000000
const int maxn = 1e3 + 5;
//--------------------------------------------------------------------------------------------------------------------------------
vi dp;
int ways(int idx, int n)
{

    if(idx==n+1) return 1;
    // if(idx>n+1) return 0;
    int &ans=dp[idx];
    if(ans!=-1) return ans;
    // deb()<<show(idx);
    ans=0;
    for(int pos=idx;;pos++)
    {
        int d =pos-idx+1;
        if(pos+d-1>n) break;
        ans+=ways(pos+d,n);
    }
    return ans;
}
template <int mod>
struct ModInt
{
    using lint = long long;
    static int get_mod() { return mod; }
    static int get_primitive_root() {
        static int primitive_root = 0;
        if (!primitive_root) {
            primitive_root = [&](){
                std::set<int> fac;
                int v = mod - 1;
                for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < mod; g++) {
                    bool ok = true;
                    for (auto i : fac) if (ModInt(g).power((mod - 1) / i) == 1) { ok = false; break; }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root;
    }
    int val;
    constexpr ModInt() : val(0) {}
    constexpr ModInt &_setval(lint v) { val = (v >= mod ? v - mod : v); return *this; }
    constexpr ModInt(lint v) { _setval(v % mod + mod); }
    explicit operator bool() const { return val != 0; }
    constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }
    constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + mod); }
    constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % mod); }
    constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % mod); }
    constexpr ModInt operator-() const { return ModInt()._setval(mod - val); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt()._setval(a % mod + x.val); }
    friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt()._setval(a % mod - x.val + mod); }
    friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.val % mod); }
    friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.inv() % mod); }
    constexpr bool operator==(const ModInt &x) const { return val == x.val; }
    constexpr bool operator!=(const ModInt &x) const { return val != x.val; }
    bool operator<(const ModInt &x) const { return val < x.val; }  // To use std::map<ModInt, T>
    friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; is >> t; x = ModInt(t); return is; }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { os << x.val;  return os; }
    constexpr lint power(lint n) const {
        lint ans = 1, tmp = this->val;
        while (n) {
            if (n & 1) ans = ans * tmp % mod;
            tmp = tmp * tmp % mod;
            n /= 2;
        }
        return ans;
    }
    constexpr lint inv() const { return this->power(mod - 2); }
    constexpr ModInt operator^(lint n) const { return ModInt(this->power(n)); }
    constexpr ModInt &operator^=(lint n) { return *this = *this ^ n; }

    inline ModInt fac() const {
        static std::vector<ModInt> facs;
        int l0 = facs.size();
        if (l0 > this->val) return facs[this->val];

        facs.resize(this->val + 1);
        for (int i = l0; i <= this->val; i++) facs[i] = (i == 0 ? ModInt(1) : facs[i - 1] * ModInt(i));
        return facs[this->val];
    }

    ModInt doublefac() const {
        lint k = (this->val + 1) / 2;
        if (this->val & 1) return ModInt(k * 2).fac() / ModInt(2).power(k) / ModInt(k).fac();
        else return ModInt(k).fac() * ModInt(2).power(k);
    }

    ModInt nCr(const ModInt &r) const {
        if (this->val < r.val) return ModInt(0);
        return this->fac() / ((*this - r).fac() * r.fac());
    }

    ModInt sqrt() const {
        if (val == 0) return 0;
        if (mod == 2) return val;
        if (power((mod - 1) / 2) != 1) return 0;
        ModInt b = 1;
        while (b.power((mod - 1) / 2) == 1) b += 1;
        int e = 0, m = mod - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModInt x = power((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModInt z = b.power(m);
        while (y != 1) {
            int j = 0;
            ModInt t = y;
            while (t != 1) j++, t *= t;
            z = z.power(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModInt(std::min(x.val, mod - x.val));
    }
};
using mint = ModInt<mod>; //don't forget to check mod
void solve()
{

    int n;
    cin>>n;
    // dp=vi(n+1,-1);
    // int num = ways(1,n);
    vi dp(n+1);
    // vvi pdp(2,vi(n+1));

    dp[n]=1;
    dp[n+1]=1;
    // pdp[n&1][n]=1;
    vi pdp(n+2);
    pdp[n]=1;
    pdp[n+1]=1;
    for(int i=n-1;i>=1;i--)
    {
        // int d = i+(n-i)/2;
        dp[i] = pdp[i+1];
        pdp[i]=dp[i];
        pdp[i]=add(pdp[i+2],pdp[i]);
    }
    deb()<<show(dp);
    deb()<<show(pdp);

    deb()<<show(dp[1]);
    mint ans=dp[1];
    for(int i=1;i<=n;i++)
    {
        ans = ans/mint(2);
    }
    cout<<ans<<endl;
    // deb()<<show(num);
}
signed main()
{
     #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
     // freopen("output.txt", "w", stderr);
     #endif // ONLINE_JUDGE

    speed;
    int t = 1;
    //cin >> t;

    for (int tt = 1; tt <= t; tt++)
    {
        // cout<<"Case #"<<tt<<": ";
        solve();
    }

//CERTAIN STUFF to REMEMBER::
//1.Check integer overflow
//2.Check edge case n=1 probable <----------  don't take it lightly, if this will be the reason you'll collapse
//3.If conditions can be checked without bits, then never check with bits, never try to be oversmart!
//4.WA can come bcz of very silly things ->  base case, if with bits,bla bla
//5. MLE? -> #define int long long
//6. When it's getting too complex till C, consider starting afresh, you might be going in a wrong direction
//7.long long!!

}


#include <iostream>
using namespace std;
long long a[100][100];
int main(){
	int n,m,sum=0;;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if((i+j)%5==0) {
				a[i][j]=1;
				sum++;
				j=j+4;
			}
		}
	}
	cout<<sum;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int pow2 [45];
int main(){
    pow2[0] = 1;
    for (int g=1; g<45; g++) pow2[g] = (pow2[g-1] * 2) % mod;
    int n; cin >> n;
    if (n==1){
        cout << 1;
    }
    else if (n==2){
        cout << 3;
    }
    else if (n==3){
        cout << 15;
    }
    else if(n==4){
        cout << 63;
    }
    else if(n==5){
        cout << 4095;
    }
    else if (n==6){
        cout << 65535;
    }
    else if (n==7){
        cout << 262143;
    }
    else if (n==8){
        assert(false);
        cout << 4194303;
    }
    else{
        cout << (1LL * 67108863 * pow2[n-9])%mod;
        return 0;
    }
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long int
#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i, n) for(int i=n-1;i>=0;i--)
int mod=1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

//to find nth fibonacci number.
//This code returns Fn and Fn+1 as a pair.
pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}

int fact(int n)
{
    int res=1;
    for(int i=n;i>=1;i--)
        (res*=i)%=mod;
    return (res%mod);

}

int power(int a, int b, int m)
{
	int ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;

    return true;
}

//THIS IS THE CODE TO FIND ALL PRIME NUMBERS IN THE RANGE "L" to "R";

vector<long long > segmentedSieve(long long L, long long R) {
    // generate all primes up to sqrt(R)
    long long lim = sqrt(R);
    vector<bool> mark(lim + 1, false);
    vector<long long> primes;
    for (long long i = 2; i <= lim; ++i) {
        if (!mark[i]) {
            primes.emplace_back(i);
            for (long long j = i * i; j <= lim; j += i)
                mark[j] = true;
        }
    }

    vector<bool> isPrime(R - L + 1, true);
    for (long long i : primes)
        for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
            isPrime[j - L] = false;
    if (L == 1)
        isPrime[0] = false;
    vector<long long >realprime;
   for(int i=0;i<=R-L;i++){
      if(isPrime[i]){
         realprime.emplace_back(i+L);

      }
   }
    return realprime;

}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int ternarySearch(int l, int r, int key, int ar[])

{
    while (r >= l) {

        // Find the mid1 and mid2
        int mid1 = l + (r - l) / 3;
        int mid2 = r - (r - l) / 3;

        // Check if key is present at any mid
        if (ar[mid1] == key) {
            return mid1;
        }
        if (ar[mid2] == key) {
            return mid2;
        }

        // Since key is not present at mid,
        // check in which region it is present
        // then repeat the Search operation
        // in that region

        if (key < ar[mid1]) {

            // The key lies in between l and mid1
            r = mid1 - 1;
        }
        else if (key > ar[mid2]) {

            // The key lies in between mid2 and r
            l = mid2 + 1;
        }
        else {

            // The key lies in between mid1 and mid2
            l = mid1 + 1;
            r = mid2 - 1;
        }
    }

    // Key not found
    return -1;
}

  int binarySearch(int arr[], int l, int r, int x)
{
    while (l <= r) {
        int m = l + (r - l) / 2;

        // Check if x is present at mid
        if (arr[m] == x)
            return m;

        // If x greater, ignore left half
        if (arr[m] < x)
            l = m + 1;

        // If x is smaller, ignore right half
        else
            r = m - 1;
    }

    // if we reach here, then element was
    // not present
    return -1;
}

//"KADANE'S ALGORITHM"
//iski time complexity o(n) hn i.e the smallest possible
long long maxsubarraysum(long long *array,long long n){
   long long best = 0, sum = 0;
for (long long  k = 0; k < n; k++) {
sum = max(array[k],sum+array[k]);
best = max(best,sum);
}
return  best ;

}
//to add two very large numbers

string findSum(string str1, string str2)
{
	if (str1.length() > str2.length())
		swap(str1, str2);
	string str = "";
	int n1 = str1.length(), n2 = str2.length();

	reverse(str1.begin(), str1.end());
	reverse(str2.begin(), str2.end());

	int carry = 0;
	for (int i=0; i<n1; i++)
	{

		int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
		str.push_back(sum%10 + '0');

		carry = sum/10;
	}
	for (int i=n1; i<n2; i++)
	{
		int sum = ((str2[i]-'0')+carry);
		str.push_back(sum%10 + '0');
		carry = sum/10;
	}
	if (carry)
		str.push_back(carry+'0');
	reverse(str.begin(), str.end());
	return str;
}

string smin(string a,string b)
{

if(a.size()<b.size())return a;
if(a.size()>b.size())return b;
if(a<=b)return a;
else return b;
}

  bool so(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}

int setBitNumber(int n)
{
    // Below steps set bits after
    // MSB (including MSB)

    // Suppose n is 273 (binary
    // is 100010001). It does following
    // 100010001 | 010001000 = 110011001
    n |= n >> 1;

    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set. It does following
    // 110011001 | 001100110 = 111111111
    n |= n >> 2;

    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;

    // Increment n by 1 so that
    // there is only one set bit
    // which is just before original
    // MSB. n now becomes 1000000000
    n = n + 1;

    // Return original MSB after shifting.
    // n now becomes 100000000
    return (n >> 1);
}

bool CompareVector(vector<string>  first, vector<string>  second){
    if (first[0] < second[0])
       return true;
    if (first[1] < second[1])
       return true;
    if (first[2] < second[2])
       return true;
    return false;

}

unsigned countBits(unsigned int number)
{
      // log function in base 2
      // take only integer part
      return (int)log2(number)+1;
}

signed main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int a1,a2,k1,k2;
int n;
cin>>a1>>a2>>k1>>k2>>n;
cout<<n-a1*(k1-1)-a2*(k2-1)<<" ";
int x=a1*k1+a2*k2-n;

int ans=max(k1/x,k2/x);

cout<<a1+a2-ans<<endl;
return 0;
}


//CodeForces 520A
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int n; cin >> n;
	string a; cin >> a;
	if (n < 26) cout << "NO" << endl;
	else
	{
		transform(a.begin(), a.end(), a.begin(), ::tolower);
		sort(a.begin(), a.end());
		unique(a.begin(), a.end());
		if (a[25] == 'z' && a[0]=='a') cout << "YES" << endl;
		else cout << "NO" << endl;
	}
//	system("pause");
	return 0;
}


//
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD=998244353;
int n,m,k;
int jc(int x)
{
	if(!x) return 1;
	return jc(x-1)*x;
}
int Comb(int n,int k)
{
	if(!k) return 1;
	return jc(n)/(jc(k)*jc(n-k));
}
int pow_mod(int a,int i)
{
	int s=1;
	while(i)
	{
		if(i&1) s=1LL*s*a%MOD;
		a=1LL*a*a%MOD;
		i>>=1;
	}
	return s;
}
signed main()
{
//	freopen("","r",stdin);
//	freopen("","w",stdout);
	ios::sync_with_stdio(0);   //syn加速
	cin>>n>>m>>k;
	if(k==0)
	{
		cout<<m<<endl;
		return 0;
	}
	int ans=(Comb(n-1,k)*m)%MOD;
	ans=ans*pow_mod(m-1,k)%MOD;
	cout<<ans%MOD<<endl;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    unsigned long long int a,b,ans=1,i;
    cin>>a>>b;
    if(a==0)
        a=1;
    if(b==0)
        b=1;
    a%=10;
    b%=10;
    for(i=1; i<=a; i++)
        ans*=i;
    if(ans>1)
    {
        a=ans;
        ans=1;
    }
    for(i=1; i<=b; i++)
        ans*=i;
    if(ans>1)
        b=ans;
    cout<<(b/a)%10<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define M_PI    3.14159265358979323846  /* pi */
#define ri(i) int i;cin>>i;
#define rii(i,j) int i,j;cin>>i>>j;
#define riii(i,j,k) int i,j,k;cin>>i>>j>>k;
#define rc(i) char i;cin>>i;
#define rll(i) long long i;cin>>i;
#define ff(i,s,n) for(int i=(s);i<(n);i++)
#define fr(i,s,n) for(int i=(n-1);i>=(s);i--)
#define FF(i,s,n) for(int i=(s);i<=(n);i++)
#define Fr(i,s,n) for(int i=(n);i>=(s);i--)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()

using namespace std;
typedef long long ll;typedef vector<int> vi;typedef vector<ll> vll;typedef vector<vll > vvll;typedef pair<vi, vi> pvi;typedef vector<vi > vvi;typedef pair<int, int> pii;
typedef pair<ll, ll> pll;typedef vector<pair<string, string> > vpss;typedef vector<pii > vpii;typedef vector<pll > vpll;typedef vector<vpii > vvpii;
typedef map<int, int> mii;typedef unordered_map<int, int> imap;typedef unordered_map<ll, ll> lmap;

template<typename T,typename U>inline void swap(T&a,U&b){a=a^b;b=a^b;a=a^b;}inline void swap(int&a,int&b){a=a^b;b=a^b;a=a^b;}inline void swap(ll&a,ll&b){a=a^b;b=a^b;a=a^b;}
template<typename T,typename U>inline void smax(T&a,U b){if(b>a)a=b;}template<typename T,typename U>inline void smin(T&a,U b){if(b<a)a=b;}
inline int gcd(int a, int b){return __gcd(a,b);}inline ll gcd(ll a, ll b){return __gcd(a,b);}
ll nchose(int a,int b){ll ans=1;smax(a,b-a);int p=2;FF(i,b-a+1,b){ans*=(ll)i;while(p<=a&&ans%p==0)ans/=p++;}return ans;}static ll MOD = 1000000007;

string s, a[]={"74","7744","777444","77774444"},b[]={"47","4477","444777","44447777"};

int main(){
#ifdef localhost
    //freopen("xx/2.txt","r",stdin);
#endif
    cin>>s;int n=s.length();
    if(n%2==1){
        ff(i,-1,n/2)cout<<4;
        ff(i,-1,n/2)cout<<7;
    }
    else if(s>a[n/2-1]){
        ff(i,-1,n/2)cout<<4;
        ff(i,-1,n/2)cout<<7;
    }else{
        string t=b[n/2-1];
        int p=0,p2=n/2;
        while(t<s){
            if(t[p]<s[p]){
                t[p]='7';
                t[p2++]='4';
            }
            p++;
        }
        cout<<t;
    }
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<bitset>
#include<iomanip>
#include<iostream>
#include<cmath>
using namespace std;
#define rep(i,x,y) for(i=x;i<=y;i++)
#define _rep(i,x,y) for(i=x;i>=y;i--)
#define REP(i,x,y) for(int i=(x);i<=(y);i++)
#define _REP(i,x,y) for(int i=(x);i>=(y);i--)
#define CL(S,x) memset(S,x,sizeof(S))
#define CP(S1,S2) memcpy(S1,S2,sizeof(S2))
#define ALL(x,S) for(__typeof((S).end()) x=S.begin();x!=S.end();x++)
#define pb push_back
#define IN insert
#define ER erase
#define BE begin()
#define ED end()
#define LB lower_bound
#define UB upper_bound
#define mp make_pair
#define fi first
#define se second
#define upmin(x,y) x=min(x,y)
#define upmax(x,y) x=max(x,y)
#define COUT(S,x) cout<<fixed<<setprecision(x)<<S<<endl
template<class T> inline void read(T&x){bool fu=0;char c;for(c=getchar();c<=32;c=getchar());if(c=='-')fu=1,c=getchar();for(x=0;c>32;c=getchar())x=x*10+c-'0';if(fu)x=-x;};
template<class T> inline void read(T&x,T&y){read(x);read(y);}
template<class T> inline void read(T&x,T&y,T&z){read(x);read(y);read(z);}
template<class T> inline void read(T&x,T&y,T&z,T&q){read(x);read(y);read(z);read(q);}

inline char getc(){char c;for(c=getchar();c<=32;c=getchar());return c;}

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int mod=1000000007 ;
int n,i,j,k,K,l,p;ll m;
ll pow(ll a,ll b,ll c){ll ans=1;for(a%=c;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
ll f[110][110],C[110][110],c1[110][110],c2[110][110];

int main()
{
    //freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	read(n);cin>>m;read(K);

	rep(i,0,100){C[i][0]=1;rep(j,1,i)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;}
	rep(i,0,100)rep(j,0,i)
	{
		c1[i][j]=pow(C[i][j],m/n,mod);
		c2[i][j]=pow(C[i][j],m/n+1,mod);
	}

	f[0][0]=1;
	rep(i,0,n-1)
	rep(j,0,K)
	{
		#define up(x,y) x=((x)+(y))%mod
		rep(k,0,min(K-j,n))
		if(i<(m%n))up(f[i+1][j+k],f[i][j]*c2[n][k]);
		else up(f[i+1][j+k],f[i][j]*c1[n][k]);
	}
	cout<<f[n][K]<<endl;

    return 0;
}


#include<iostream>
#include<deque>
using namespace std;
int main(){
    deque<int> q;
    int n;
    cin>>n;
    q.push_back(1);
    q.push_back(2);
    q.push_back(3);
    q.push_back(4);
    q.push_back(5);
    int x;
    for(int i=0;i<n;i++){
        if(i==n-1)
        x=q.front();
        q.push_back(q.front());
        q.push_back(q.front());
        q.pop_front();
    }
    switch(x){
        case 1:cout<<"Sheldon";
        break;
        case 2:cout<<"Leonard";
        break;
        case 3:cout<<"Penny";
        break;
        case 4:cout<<"Rajesh";
        break;
        case 5:cout<<"Howard";
        break;
        default : break;
    }
    return 0;
}


#include <iostream>
using namespace std;
int a[10000*10000+10];
int main()
{
    long long n,k,bomb=0,b=0;
    cin>>n>>k;
    for(int i=n/2;i<=n;i++)
    {
       b+=1;
       for(int j=1;j<=n;j*=k)
       {
           a[b]+=i/j;
           if(a[b]>=n)
           {
              cout<<i<<endl;
              bomb=1;
              break;
           }
       }
       if(bomb==1)
          break;
    }
    return 0;
}


#include<bits/stdc++.h>
#define forn(i, n) for(int i = 1; i <= n; i++)
#define forab(a, b) for(int i = a; i <= b; i++)

using namespace std;

const int INF = 1e7;
const long double EPS = 1e-8;
const long double PI = acos(-1.0L);
const int MAXN = 50;
const int MAXC = 1e9;

long long a[10000], n, spl, i, m, k1, t,  ans, sum, k, mid, p;

int main() {
    //freopen("merge.in", "r", stdin);
    //freopen("merge.out", "w", stdout);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n >> k >> t;
    forn(i, n + k) {
        if (i <= n - k) {
            a[i] = i;
        }
        if (n - k <= i && i <= n ) {
            a[i] = k;
        }
        if (n <= i && i <= n + k)
           a[i] = n + k - i;
    }
    cout << a[t];
    return 0;
}


#include <iostream>
#include <iomanip>
#include <ios>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>

//#include <limits.h>

typedef int long long bint;
typedef unsigned int long long buint;

using namespace std;
int min(int a, int b){
	if (a < b)
		return a;
	else
		return b;
}
int max(int a, int b){
	if (a > b)
		return a;
	else
		return b;
}
int recursion(vector<int> a, int sum){
	if (a.size() == 3)
		return sum + min(a[0], a[2]);
	else if (a.size() < 3)
		return sum;
	else{

		int maxi = -1;
		int price = 0;
		for (int i = 0; i < a.size(); i++){

			if (i != a.size() - 1 && i != 0)
				price = min(a[i - 1], a[i + 1]);
			else
				price = 0;
			int temp = a[i];
			a.erase(a.begin() + i);
			int tmp1 = recursion(a, sum + price);
			a.insert(a.begin() + i,temp);
			if (maxi < tmp1)
				maxi = tmp1;
		}
		return  maxi;
	}

}

int main(){
	int n;
	cin >> n;
	if (n == 2){
		cout << 0;
		return 0;
	}

	int max = 0;
	int maxindex;
	vector<int> a(n);
	for (int i = 0; i < n; i++){
		cin >> a[i];
	}
	int sum = 0;
	cout << recursion(a, sum);
 	return 0;
}


#include<bits/stdc++.h>
#define fio  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ll long long int
#define l long int
#define Max INT_MAX
#define Min INT_MIN

using namespace std;

ll gcd(ll x, ll y)
{
        if(x==y)
                return x;
        if(x > y)
                return gcd(x-y,y);
        return gcd(x,y-x);
}

int main()
{
        fio
        ll a,b,x,y;
        cin >> a >> b >> x >> y;
        ll k = gcd(x,y);
        x = x/k;
        y = y/k;
        cout << min(a/x,b/y);
        return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() cin.tie(0), ios::sync_with_stdio(false)

// xをb進法にする
template <typename T>
vector<T> convert_base(T x, T b)
{
    vector<T> res;
    // t : digit
    T t = 1, k = abs(b);
    while (x != 0)
    {
        res.emplace_back((x * t) % k);
        if (res.back() < 0)
            res.back() += k;
        x -= res.back() * t;
        x /= k;
        t *= b / k;
    }
    if (res.empty())
        res.emplace_back(0);
    return res;
}

ll int_pow(ll x, ll n)
{
    ll res = 1;
    while (n > 0)
    {
        if (n & 1)
            res *= x;
        x *= x;
        n >>= 1;
    }
    return res;
}

ll w, M;

int main()
{
    cin >> w >> M;
    vector<ll> dig = convert_base(M, w);
    for (int i = 0; i < dig.size(); i++)
    {
        if (1 < dig[i] && dig[i] < w - 1)
        {
            cout << "NO" << endl;
            return 0;
        }
        else if (dig[i] == w - 1)
        {
            M += int_pow(w, i);
            dig = convert_base(M, w);
        }
    }
    cout << "YES" << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;

ll BinExp(ll bas, ll exp){
   ll ans=1, temp=bas;
    while(exp){
        if(exp&1) ans*=temp;
        temp*=temp;
        exp>>=1;
    }
    return ans;
}

ll BinModExp(ll bas, ll exp, ll mod){
   ll ans=1, temp=bas%mod;
    while(exp){
        if(exp&1) ans=(ans*temp)%mod;
        temp=(temp*temp)%mod;
        exp>>=1;
    }
    return ans;
}

vvi Multiply_Matrix(vvi A, vvi B, ll mod){
    if(A[0].size()!=B.size()) return vvi();
    vvi C(A.size(), vi(B[0].size(), 0));
    for(int i=0; i<C.size(); i++)
        for(int j=0; j<C[i].size(); j++)
            for(int k=0; k<B.size(); k++)
                C[i][j]=(C[i][j]+(A[i][k]*B[k][j])%mod)%mod;
    return C;
}

vvi Expon_Matrix(vvi A, int exp, ll mod){
    if(exp==1) return A;
    if(exp&1) return Multiply_Matrix(Expon_Matrix(Multiply_Matrix(A, A, mod), exp>>1, mod), A, mod);
    return Expon_Matrix(Multiply_Matrix(A, A, mod), exp>>1, mod);
}

ll Nfibo(ll n, ll mod){
    vvi A;
    A.assign(2,vi(2));
    A[0][0]=1;
    A[0][1]=1;
    A[1][0]=1;
    A[1][1]=0;
    A=Expon_Matrix(A, n, mod);
    return (A[0][0]+A[0][1])%mod;
}
int main(){
    ll n, k, l, m;
    while(scanf("%I64d %I64d %I64d %I64d", &n, &k, &l, &m)!=EOF){
        if(BinExp(2, l)<k){
            printf("0\n");
        }else{
            ll A=BinModExp(2, n, m);
            ll B=Nfibo(n, m);
            ll ans=1;
//          cout<<A<<" "<<B<<endl;
            for(int i=0; i<l;i++){
                if(k&(1LL<<i)){
                    ans*=A-B+m;
                }else{
                    ans*=B;
                }
                ans%=m;
            }
            printf("%I64d\n", ans);
        }
    }
    return 0;
}


#include<bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define mod 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define sz(a) ((int)a.size())

#define eps 1e-8

#define x first

#define y second

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) ((x) ? (__builtin_clzll(x)) : 0)

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

#define shit(a) (1 << (a))

#define rev(a) (power(a, mod-2, mod))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef pair<int, int> pii;

typedef pair<pii, int> ppi;

typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, U b) {if(a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if(a<b) a=b;}

template<class T>inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T>inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template <class T> inline void println(T x) {print(x); putchar('\n');}

template <class T> inline void printsp(T x) {print(x); putchar(' ');}

template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}

template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}

template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}

template <class T> inline void eprint(T x) {cerr << "--------> " <<x << endl;}

template <class T1, class T2> inline void eprint(T1 x, T2 y) { cerr << "--------> " << x << ' ' << y << endl; }

template <class T1, class T2, class T3> inline void eprint(T1 x, T2 y, T3 z) { cerr << " -------> " << x << ' ' << y << ' ' << z << endl;}

template <class T1, class T2, class T3, class T4> inline void eprint(T1 x, T2 y, T3 z, T4 w) { cerr << " -------> " << x << ' ' << y << ' ' << z << ' ' << w << endl;}

int power(int a, int b, int m, int ans=1) {

	for(; b; b>>=1, a=1LL*a*a%m) if(b&1) ans=1LL*ans*a%m;

	return ans;

}

int dp[111];

int n, K;

int get(int u) {

	int ans=0;

	while(u) {

		ans += u%K;

		u /= K;

	}

	return ans % K;

}

int main() {

#ifndef	ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	cin >> n >> K;

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

		int u=get(i);

//		printsp(u);

//		if(i==n-1) eprint(u);

		for(int j=0; j<K; j++) if(j!=u) (dp[u]+=dp[j])%=mod;

		dp[u]++;

	}

	INT ans=1;

	for(int i=0; i<K; i++) ans += dp[i];

	println(ans % mod);

	return 0;

}


#include <iostream>
#include "string.h"
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;

struct ac{
    double x, y;
};
ac a[4], b[4];
double check(ac a, ac b) {
    double d = sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
    //cout << d << endl;
    return d;
}

int main(int argc, char const *argv[]) {
    ios::sync_with_stdio(false);
    for (int i = 0; i < 8; i ++)
        cin >> a[i].x >> a[i].y;
    ac z, z1;
    z1.x = (a[0].x + a[1].x) / 2.;
    z1.y = (a[0].y + a[1].y) / 2.;
    if (a[4].x == a[6].x) {
        z.x = a[4].x; z.y = a[5].y;
    }else {
        z.x = a[5].x; z.y = a[4].y;
    }
    int vis = 0;
    //cout << z.x << " " << z.y << endl;
    // if (z.x <= r.x && z.x >= l.x && z.y <= r.y && z.y <= r.y){
    //     vis = 1;
    // }else{
        double d = check(a[4], a[6]) / 2.;
        //cout << d << endl;
        for (int i = 0; i < 4; i ++) {
            if (check(a[i], z) <= d)
                vis = 1;
        }
        //cout << endl;
        double d1 = check(a[0], a[2]) / 2.;
        //cout << d1 << endl;
        for (int i = 4; i < 8; i ++) {
            if (check(a[i], z1) <= d1)
                vis = 1;
        }
    // }
    cout << ((vis == 1) ? "YES" : "NO") << endl;
    return 0;
}


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

int main()
{
    int n,ans=0;
    cin>>n;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='-'&&ans>0)ans--;
        if(s[i]=='+') ans++;
    }
    cout<<ans;
    return 0;
}


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

#define speed ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define ub(a,x) upper_bound(a.begin(),a.end(),x)-a.begin()
#define lb(a,x) lower_bound(a.begin(),a.end(),x)-a.begin()
#define ll long long
#define endl "\n";
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define pb push_back
#define PI 3.1415926535897932384626

int n,k;
string s;
set<string> st;

main()
{
    speed;

    cin>>n>>k;
    cin>>s;
    queue< string > q;
    q.push(s);
    while(q.size())
    {
    	if(st.size()==k) break;
    	string temp = q.front();q.pop();
    	st.insert(temp);
    	int skip = 0;
    	for(int i=0;i<temp.length();i++)
    	{
    		string tt = "";
    		for(int j=0;j<temp.length();j++)
    		{
    			if(j!=skip) tt += temp[j];
    		}
    		q.push(tt);
    		skip++;
    	}
    }
    if(st.size()<k) cout<<-1;
    else
    {
    	int sum = 0;
    	for(set<string>::iterator it = st.begin();it!=st.end();it++)
    	{
    		sum += (*it).length();
    		//cout<<*it<<"\n";
    	}
    	cout<<n*k-sum;
    }
}


#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n,m,vm,cpt,r;
    cin>>n>>m;
    vector<int>t1(n);
    vector<int>t2(n);
    for(int i(0);i<m;i++)
    {
        cin>>t1[i];
        cin>>t2[i];
    }
    cpt=0;
    r=0;
    while(cpt<n)
    {
        vm=0;
        for(int i(1);i<m;i++)
            if(t2[i]>t2[vm])
                vm=i;
        if(cpt+t1[vm]<=n)
        {
            r+=t1[vm]*t2[vm];
            cpt+=t1[vm];
        }
        else
        {
            r+=t1[vm]*t2[vm];
            r-=(cpt-n)*t2[vm];
            cout<<r;
            return 0;
        }

        t1[vm]=0;
        t2[vm]=0;

    }
    cout<<r;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sc scanf
#define pf printf
#define fo(i,n) for (int i=0;i<=(n-1);i++)
#define s second
#define f first
#define int ll

using namespace std;
int n, m;
string s1, s2;
//int a[100001];
//int b[100001];
   main() {
   	int l, r ,pos;
	int ans=0;
	cin>>n>>pos>>l>>r;
	int a=1;
	int b=n;
	while (a!=l || b!=r) {
		if (pos<l && a<pos) {
			ans++;
			a=pos;
		}
		if (pos>r && b>pos) {
			ans++;
			b=pos;
		}
		if (pos>=l && pos<=r && a<l && b>r) {
			int t=min(pos-l, r-pos);
			ans+=t+r-l+2;
			if (t==pos-l) pos=l; else pos =r;
			break;
		}
		if (pos>r && b==pos) {
			ans+=pos-r;
			pos=r;
		}
		if (pos<l && a==pos) {
			ans+=l-pos;
			pos=l;
		}

	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
bool a[1000000010];
long long t,s,x;
int main()
{
	cin>>t>>s>>x;
	//a[t]=0;
	//a[t+1]=0;
	for(int i=t;i<=x;i++)
	{
		if((i-t)%s==0||(i-t-1)%s==0)
		a[i]=1;
		//cout<<a[i]<<" ";
	}
	a[t+1]=0;
	if(a[x]==0)
	cout<<"NO";
	else
	cout<<"YES";
}


#include <bits/stdc++.h>
#define maxn 2020
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define mod 998244353
#define debug(args...) printf(args)
using namespace std;

int n,k;
int w(int n){
    if(k == 1) return n;
    return (n*n+n)/2;
}

int dp[maxn][maxn];

int get(int m,int s){
    if(m == 0) return 1;
    int &r = dp[m][s];
    if(r+1) return r;
    r = get(m-1,s);
    if(w(m) <= s) r += get(m,s-w(m));
    r %= mod;
    return r;
}

int v[maxn];

int check(int sz){
    vector<int> a(sz);
    for(int i=0;i<sz;i++) a[i] = v[i];
    for(int i=0;i<k-1;i++){
        vector<int> b;
        sort(a.begin(), a.end());
        reverse(a.begin(), a.end());
        for(int j=0;j<a.size();j++)
            for(int u=0;u<a[j];u++) b.pb(j+1);
        a = b;
    }
    int s = 0;
    for(int i=0;i<a.size();i++) s += a[i];
    return s <= n;
}

int ans;

void go(int pos){

    v[pos] = 1;

    while(check(pos+1)){
        ans++;
        go(pos+1);
        v[pos]++;
        if(pos && v[pos] > v[pos-1]) break;
    }

}

main(){
    scanf("%d%d",&n,&k);
    if(k <= 2){
        memset(dp,-1,sizeof(dp));
        printf("%d\n",(get(n,n)-1+mod)%mod);
        return 0;
    }
    go(0);
    printf("%d\n",ans);
}


#include <iostream>

using namespace std;

typedef long long LL;
const LL MAX = 1E9 + 7;
LL x, k;
LL out;

//return x^(2^k) mod c
LL twoPowModulo(LL x, LL k, LL c)
{
    LL out = x%c;
    if (k==0) return out;
    else
    {
        for (LL i=1; i<=k; i++)
            out = ((out %c)*(out%c)%c);
        //LL out_t = twoPowModulo(out,k-1,c);
        //return (out_t*out_t%c);
    }
    return out;
}
//return a^b mod c
LL fastModulo(LL a, LL b, LL c)
{
    if (b==0) return 1;
    LL out = 1;
    int i = 0;
    LL b2 = b;
    while (b2>0)
    {
        if (b2 & 1) out = (out * twoPowModulo(a,i,c)) % c;
        i++;
        b2 >>= 1;
    }
    return out;
}
//return 2^p
LL twoPow(LL p)
{
    if (p>MAX)
        p %= (MAX -1);
    LL re=1;
    for (LL i=1; i<=p; i++)
    {
        re<<=1;
        if (re>MAX) (re %= MAX);
    }
    return re;
}
void input()
{
    cin>>x>>k;
}

void process()
{
    if (x<=0) out =0;
    else
    {
        if (x>MAX) x %= MAX;
        //LL t = twoPow(k);
        LL t = fastModulo(2,k,MAX);
        x %= MAX;
        out = (t*((2*x - 1) % MAX)+1)%MAX;
    }

}

void output()
{
    cout<<out<<endl;
}
int main()
{

    input();
    process();
    output();

    return 0;
}


#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 5e2 + 11;
const int X = 1e6;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

int n, k, dp[N][N][N];

void solve(){

    cin >> n >> k;

    string s, t;

    cin >> s >> t;

    //s = '.' + s + '.';

    char a = t[0], b = t[1];

    if(a == b){
        int x = 0;
        for(int i = 1; i <= n; i++)x += (s[i] == a);
        x = min(n, x + k);
        cout << x * (x - 1) / 2 << '\n';
        return;
    }

    memset(dp, -1, sizeof(dp));

    dp[0][0][0] = 0;

    int ans = 0;

    for(int i = 0; i < n; i++){
        for(int j = 0; j <= k; j++){
            for(int cnt = 0; cnt <= i; cnt++){
                if(dp[i][j][cnt] == -1)continue;
                dp[i + 1][j + 1][cnt + 1] = max(dp[i + 1][j + 1][cnt + 1], dp[i][j][cnt]);
                dp[i + 1][j + 1][cnt] = max(dp[i + 1][j + 1][cnt], dp[i][j][cnt] + cnt);
                dp[i + 1][j][cnt + (s[i] == a)] = max(dp[i + 1][j][cnt + (s[i] == a)], dp[i][j][cnt] + (s[i] == b)*cnt);
            }
        }
    }

    for(int j = 0; j <= k; j++){
        for(int cnt = 0; cnt <= n; cnt++){
            ans = max(ans, dp[n][j][cnt]);
        }
    }

    cout << ans << '\n';

}

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

    //cout << setprecision(6) << fixed;

    int T = 1;
    //cin >> T;
    while(T--){
        solve();
    }
}


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

int main()
{
            string s;
            cin>>s;
                  int a = 0;
                  int oa = 0;
                  for(int i = 0; i<s.length();i++)
                  {
                      if(s[i]=='a')a++;
                      else oa++;
                  }
                  if(a>=oa)cout<<s.length()/oa<<endl;
                  else cout<<s.length()/a<<endl;

     return 0;
}


                                  /* This Template is property of Pranav Khiste aka desmond_kaneki*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi(x,n) for(ll i=x;i<n;i++)
#define fj(x,n) for(ll j=x;j<n;j++)
#define fit(a,b) for(auto it=a;it!=b;it++)
#define vll vector<ll>
#define pb push_back
#define qll queue<ll>
#define p push
#define mp make_pair
#define fst first
#define snd second
#define pll pair<ll,ll>
#define vpll vector<pll>
#define debug(x) cout<<#x<<' '<<x<<endl;
#define endl '\n'
#define SUP  1000000000
#define INF -1000000000
#define MAX_N 100005
#define test() ll var;  cin>>var; while(var--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fileio freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout);  // use cin cout only!!
ll dp[5005][5005],sum[5005][5005],mod=1e9+7;
main()
{
    ll n,a,b,k;
    cin>>n>>a>>b>>k;
    fi(0,5005)
        fj(0,5005)
        {
            dp[i][j]=-1;
            sum[i][j]=0;
        }
    if(a<b)
    {
        for(int j=0;j<=k;j++)
        {
            for(int i=b-1;i>=0;i--)
            {
                if(j==0)
                    dp[i][j]=1;
                else if(i==b-1)
                    dp[i][j]=0;
                else
                {
                    ll l=i-(b-i-1),r=i+(b-i-1),z;
                    l=max(1ll,l);
                    r=min(n,r);
                    z=(sum[r][j-1]-sum[l-1][j-1]+mod)%mod;
                    dp[i][j]=(z-dp[i][j-1]+mod)%mod;
                }
            }
            fi(1,b)
                sum[i][j]=(sum[i-1][j]+dp[i][j])%mod;
        }
    }
    else
    {
        for(int j=0;j<=k;j++)
        {
            for(int i=b+1;i<=n;i++)
            {
                if(j==0)
                    dp[i][j]=1;
                else if(i==b+1)
                    dp[i][j]=0;
                else
                {
                    ll l=i-(i-b-1),r=i+(i-b-1),z;
                    l=max(l,1ll);
                    r=min(r,n);
                    z=(sum[r][j-1]-sum[l-1][j-1]+mod)%mod;
                    dp[i][j]=(z-dp[i][j-1]+mod)%mod;
                }
            }
            fi(b+1,n+1)
                sum[i][j]=(sum[i-1][j]+dp[i][j])%mod;
        }

    }
    /*fi(1,n+1)
    {
        fj(0,k+1)
            cout<<dp[i][j]<<' ';
        cout<<endl;
    }*/
    cout<<dp[a][k]<<endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long int n;
    cin>>n;
    if(n<=3)
    {
        if(n==1)
        {
            cout<<"1"<<endl;
        }
        else if(n==2)
        {
            cout<<"2"<<endl;
        }
        else
        {
            cout<<"6"<<endl;
        }
        return 0;
    }
    if(n%2==1)
    {
        cout<<n*(n-1)*(n-2)<<endl;
        return 0;
    }
    long long int ans=(n-1)*(n-2)*(n-3);
    for(long long int i=n;i>=max(0LL,n-50);i--)
    {
        for(long long int j=i-1;j>=max(0LL,n-50);j--)
        {
            for(long long int k=j-1;k>=max(0LL,n-50);k--)
            {
                long long int lcm=(i*j*k)/(__gcd(i,j)*__gcd(__gcd(i,j),k));
                ans=max(ans,lcm);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:268435456")

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#include <memory.h>
#include <ctime>
#include <cmath>
#include <complex>

//#include <unordered_set>
//#include <unordered_map>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define for1(i, n) for(int i = 1; i < int(n); ++i)
#define fork(i, k, n) for(int i = int(k); i <= int(n); ++i)
#define forft(i, from, to) for(int i = int(from); i < int(to); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define pb push_back
#define mp make_pair
#define mnp(a, b) make_pair((a) < (b) ? (a) : (b), (a) < (b) ? (b) : (a))
#define sz(d) int(d.size())
#define all(a) a.begin(), a.end()
#define ms(a, v) memset(a, v, sizeof(a))
#define X first
#define Y second
#define correct(x, y, xmax, ymax) ((x) >= 0 && (x) < (xmax) && (y) >= 0 && (y) < (ymax))

template<typename T> T sqr(const T &x) {
	return x * x;
}

template<typename T> T my_abs(const T &x) {
	return x < 0 ? -x : x;
}

typedef long long li;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long long uli;
typedef long double ld;
typedef pair<int,int> pt;
typedef pair<li,li> pli;
typedef pair<ld,ld> pd;

const int INF = (int)1e9;
const li LINF = (li)6e18;
const li INF64 = LINF;
const li INFLL = LINF;
const ld EPS = 1e-15;
const ld PI = 3.1415926535897932384626433832795;
const ld ME = 1e-5;
const li MOD = (int)1e9 + 7;
const li MOD2 = (int)1e9 + 21;
const int SQN = 320;
const int LOGN = 17;

const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};

const int N = (int)310;

bool used[N][N];

int n;
int t[40];

bool z[30][N][N][8];

void solve() {
	cin >> n;
	forn(i, n) {
		cin >> t[i];
	}
	z[0][N / 2][N / 2][8] = true;
	forn(i, n) {
		forn(x, N) {
			forn(y, N) {
				forn(dir, 8) {
					if (z[i][x][y][dir]) {
						int cx = x;
						int cy = y;
						forn(j, t[i]) {
							cx += dx[dir];
							cy += dy[dir];
							used[cx][cy] = true;
						}
						int ldir = dir + 1;
						if (ldir >= 8) {
							ldir -= 8;
						}
						int rdir = dir - 1;
						if (rdir < 0) {
							rdir += 8;
						}
						z[i + 1][cx][cy][ldir] = true;
						z[i + 1][cx][cy][rdir] = true;
					}
				}
			}
		}
	}
	int ans = 0;
	forn(i, N) {
		forn(j, N) {
			ans += used[i][j];
		}
	}
	cout << ans << endl;
}

int main() {
	ios_base::sync_with_stdio(false);
#ifdef SU2
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
#endif

	srand((unsigned int)time(NULL));

	cout << setprecision(15) << fixed;

	solve();

#ifdef SU2
		cerr << "time: " << clock() << " ms" << endl;
#endif

	return 0;
}


#include <iostream>
using namespace std;
main(){
       int k,n,s,p;
       cin>>k>>n>>s>>p;
       int x;
       x=n/s;
       while(n%s!=0)
       x++;

       int y;
       while(k*x%p!=0)
       y++;
       cout<<y;
       }


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

int N, V, E;
int mat[301][301];
int A[301], B[301];
vector<pair<pair<int, int>, int> > R;

void flow2(vector<int>& P, int si, int ti, int d) {
	int i;
	if (si == ti) return;
	if (A[P[si + 1]] + d <= V) {
		A[P[si]] -= d;
		A[P[si + 1]] += d;
		R.push_back(make_pair(make_pair(P[si], P[si + 1]), d));
		flow2(P, si + 1, ti, d);
	}
	else {
		int d2 = V - A[P[si + 1]];
		A[P[si]] -= d2;
		A[P[si + 1]] += d2;
		if (d2 > 0) R.push_back(make_pair(make_pair(P[si], P[si + 1]), d2));
		flow2(P, si + 1, ti, d);
		A[P[si]] -= d - d2;
		A[P[si + 1]] += d - d2;
		R.push_back(make_pair(make_pair(P[si], P[si + 1]), d - d2));
	}
}

#define FOR(y, N) for (int y = 0; y < N; ++y)

int flow(int s, int t, int d) {
	int i, j, y;
	vector<int> P;
	P.push_back(s);
	for (i = s; i != t;) {
		FOR(y, N) if (mat[i][y] == 1 && mat[y][t] == mat[i][t] - 1) break;
		P.push_back(i = y);
	}

	flow2(P, 0, P.size() - 1, d);
}

#define _P printf

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

	cin >> N >> V >> E;
	FOR(i, N) cin >> A[i];
	FOR(i, N) cin >> B[i];

	FOR(x, N) FOR(y, N) mat[x][y] = 9999;
	FOR(x, N) mat[x][x] = 0;
	FOR(i, E) {
		cin >> x >> y;
		mat[x - 1][y - 1] = mat[y - 1][x - 1] = 1;
	}
	FOR(i, N) FOR(j, N) FOR(k, N) mat[j][k] = min(mat[j][k], mat[j][i] + mat[i][k]);
	FOR(x, N) FOR(y, N) {
		if (mat[x][y] > N) continue;
		j = min(A[x] - B[x], B[y] - A[y]);
		if (j <= 0) continue;
		flow(x, y, j);
	}

	FOR(x, N) if (A[x] != B[x]) return (void)_P("NO\n");
	_P("%d\n", R.size());
	FOR(i, R.size()) _P("%d %d %d\n", R[i].first.first + 1, R[i].first.second + 1, R[i].second);
}

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


/*
.....   .   .   .   .
  .    . .   . .   . .
  .   .....   .   .....
  .  .     .  .  .     .
*/
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <random>
#include <iomanip>
#include <bitset>

using namespace std;

template<typename T> void uin(T &a, T b) {
    if (b < a) {
        a = b;
    }
}

template<typename T> void uax(T &a, T b) {
    if (b > a) {
        a = b;
    }
}

#define left left228
#define right right228

const int MOD = 1e9 + 7;

vector< vector<int> > mult(vector< vector<int> > a, vector< vector<int> > b) {
    int n = (int)a.size() - 1;
    int m = (int)b[1].size() - 1;
    vector< vector<int> > c(n + 1, vector<int> (m + 1, 0));
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            for (int k = 1; k < (int)a[1].size(); ++k) {
                c[i][j] += ((long long)a[i][k] * (long long)b[k][j]) % MOD;
                if (c[i][j] >= MOD) c[i][j] -= MOD;
            }
        }
    }
    return c;
}

vector< vector<int> > power(vector< vector<int> > a, int b) {
    vector< vector<int> > res = a;
    --b;
    while (b) {
        if (b & 1) {
            res = mult(res, a);
        }
        a = mult(a, a);
        b >>= 1LL;
    }
    return res;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector< vector<int> > dp(2, vector<int> (m + 1, 0));
    for (int i = 1; i <= m; ++i) {
        dp[1][i] = 1;
        if (i == m) dp[1][i] = 2;
    }
    if (n == m) {
        cout << 2 << endl;
        return 0;
    }
    vector< vector<int> > p(m + 1, vector<int> (m + 1, 0));
    p[1][m] = 1;
    p[m][m] = 1;
    for (int i = m - 1; i > 0; --i) {
        p[i + 1][i] = 1;
    }
    p = power(p, n - m);
    vector< vector<int> > ans = mult(dp, p);
    cout << ans[1][m] << endl;
    return 0;
}

/*
2 0 3

3 1 2
1 2

*/


#include <bits/stdc++.h>

using namespace std;

char t[15][15];
int dx[] = {1,1,1,-1,-1,-1,0,0};
int dy[] = {-1,0,1,-1,0,1,-1,1};
int mx;

bool isValid(int row, int col) {
  if (row >= 0 && row < 10 && col >= 0 && col < 10) {
    return true;
  } else {
    return false;
  }
}

void dfsCheck(int r,int c,int len,int rPar,int cPar) {
  mx = max(mx, len);
  for (int i = 0; i < 8; i++) {
    int nextRow = r + dx[i];
    int nextCol = c + dy[i];
    if (isValid(nextRow, nextCol)) {
      if (t[nextRow][nextCol] == 'X' && (nextRow != rPar || nextCol != cPar)) {
        dfsCheck(nextRow, nextCol, len + 1, r, c);
      }
    }
  }
}

void testCell(int row,int col) {
  bool changed = 0;
  if (t[row][col] == '.') {
    t[row][col] = 'X';
    changed = 1;
  }
  dfsCheck(row, col, 1, row,col);
  if (changed) {
    t[row][col] = '.';
  }
}

int main() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> t[i][j];
    }
  }
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (t[i][j] != 'O') {
        testCell(i, j);
      }
    }
  }
  puts(mx >= 5 ? "YES" : "NO");
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b;

    cin>>a>>b;
    int i;
    for(i= 1; i<=a;i++){
        b += i*5;
        if (b>240) { return i;}
    }

}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

pii Func(int a, int b) {
	int GCD = __gcd(a, b);
	return pii((a / GCD), (b / GCD));
}

int Abs(int x) {return ((x < 0)?(-x):(x));}

int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;

	int mint = 2000000000, mintx = 1, minty = 1;

	int X, Y;

	for (int i = 1; i <= 1000; ++i) {
		for (int j = 1; j <= 1000; ++j) {
			X = (a * j) * (b * j);
			Y = (c * i) * (d * i);
			if (((Y - X) < mint) && ((Y - X) >= 0)) {
				mint = Y - X;
				mintx = j;
				minty = i;
				//cout << i << ' ' << j << '\n';
			}
		}
	}
	X = (a * mintx) * (b * mintx);
	Y = (c * minty) * (d * minty);
	pii res = Func((Y - X), Y);
	cout << res.first << '/' << res.second << '\n';

	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long long int x,y,i=1,j;
    cin>>x>>y;

    if(x<=1&&y==0&&x>=0)
    cout<<"0";
    else if(x==1&&y>0&&y<=1)
    cout<<"1";
    else
    {
        while(1)
        {
            if(y==i&&x<i&&x>=-i)
            {
                cout<<4*i-2;
                break;
            }
             if(x==-i&&y<i&&y>=-i)
            {
                cout<<4*i-1;
                break;
            }
             if(y==-i&&x<=i&&x>-i)
            {
                cout<<4*i;
                break;
            }
            if(x==2*i&&y>-i&&y<=i)
            {
                cout<<4*i+1;
                break;
            }
            i++;

        }
    }
    return 0;

}


#include <iostream>
#include <vector>
#include <set>
#include <math.h>
#include <algorithm>

using namespace std;
int main() {

  int n,k; cin>>n>>k;
  vector<int> vc;
    for (int i = 0; i < n; ++i) {
        int x;cin>>x;
        vc.emplace_back(x);
    }

    int sum=0;int c=k;
    for (int j = 0; j < n; ++j) {

       while (k>= vc[j] && j<n)
       {
           k-=vc[j];
           j++;
       }
       sum++;
       j--;
    }

cout<<sum;

return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long kMOD = 1e9 + 7;

struct Mat {
    vector<vector<long long>> a;
    int n, m;

    Mat (const vector<vector<long long>> &a): a(a), n(a.size()), m(a[0].size()) {}

    Mat (int n, int m, int val = 0): n(n), m(m) {
        a.resize(n);
        for (int i = 0; i < n; ++i) {
            a[i].resize(m);
            if (i < m) a[i][i] = val;
        }
    }

    const vector<long long>& operator [] (int i) const {
        return a[i];
    }

    vector<long long>& operator [] (int i) {
        return a[i];
    }

    Mat operator * (const Mat &B) const {
        assert(m == B.n);
        Mat ans = Mat(n, B.m);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < B.m; ++j) {
                for (int k = 0; k < m; ++k) {
                    ans[i][j] += (a[i][k] * B[k][j]) % kMOD;
                }
                ans[i][j] %= kMOD;
            }
        }
        return ans;
    }

    void print () const {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cout << a[i][j] << " ";
            }
            cout << "\n";
        }
    }
};

Mat b_pow (Mat A, int deg) {
    if (deg == 0) return Mat(A.n, A.m, 1);
    if (deg == 1) return A;
    if (deg == 2) return A * A;
    return deg % 2 ? b_pow(A, deg / 2) * b_pow(A, deg / 2 + 1) :
                     b_pow(b_pow(A, deg / 2), 2);
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    long long n, k;
    cin >> n >> k;
    Mat A(2 * k + 3, 2 * k + 3);
    vector<long long> c(k + 1, 0);
    c[0] = 1;
    for (int i = 1; i < k + 1; ++i) {
        long long b = 0;
        for (int j = 0; j <= i; ++j) {
            long long t = b;
            b = c[j] % kMOD;
            c[j] += t;
            c[j] %= kMOD;
        }
    }
    for (int i = 0; i < k + 1; ++i) {
        A[i][i + k + 1] = 1;
        for (int j = 0; j <= i; ++j) {
            A[i + k + 1][j + k + 1] = (A[i + k][j + k] + A[i + k][j + k + 1]) % kMOD;
            A[k + 1][k + 1] = 1;
            A[i + k + 1][j] = (A[i + k + 1][j + k + 1] * ((1 << (i - j)) % kMOD)) % kMOD;
        }
        A[2 * k + 2][i] = (c[i] * ((1 << (k - i)) % kMOD)) % kMOD;
        A[2 * k + 2][i + k + 1] = c[i];
    }
    A[2 * k + 2][2 * k + 2] = 1;
    Mat B(2 * k + 3, 1);
    for (int i = 0; i < 2 * k + 3; ++i) {
        B[i][0] = 1;
    }
    for (int i = 1; i < k + 1; ++i) {
        B[i][0] = 0;
    }
    cout << (b_pow(A, n - 1) * B)[2 * k + 2][0] << "\n";
#ifdef _DEBUG
    A.print();
    cout << "\n";
    (b_pow(A, 0) * B).print();
    cout << "\n";
    (A * B).print();
    cout << "\n";
    (b_pow(A, 2) * B).print();
    cout << "\n";
    (b_pow(A, n - 1) * B).print();
#endif
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 1,M = 1e9 + 7;
mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
int n,k;
bool bad[N];
vector<int>divi[N],need[N];
void add(int &a,int b){
	a+=b;
	if(a >= M)a-=M;
	if(a < 0)a+=M;
}
int power(int base,int to){
	int ret = 1;
	while(to){
		if(to&1)ret = ret*1LL*base%M;
		to>>=1;
		base = base*1LL*base%M;
	}
	return ret;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i = 2;i < N;i++)
		if(divi[i].empty())
			for(int j = i;j < N;j+=i){
				if(!(j%(i*1LL*i)))bad[j] = 1;
				divi[j].push_back(i);
			}
	for(int i = 2;i < N;i++)
		for(int j = i;j < N;j+=i)
			need[j].push_back(i);
	int ans = 0,sum = 0,temp = 0;
	for(int i = 1;i <= k;i++){
		sum = power(i,n);
		for(auto j : need[i]){
			if(bad[j])continue;
			add(temp,-power((i - 1)/j,n)*(divi[j].size()%2 ? 1 : -1));
			add(temp,power(i/j,n)*(divi[j].size()%2 ? 1 : -1));
		}
		add(sum,-temp);
		add(ans,sum^i);
	}
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
#include <vector>
typedef long long ll;

using namespace std;

int main()
{
 ll n,i,j,d;
 vector<ll> v;
 cin >> n;
 for(i=1;i<n;i++)
 {
   if(n%i!=0)
        v.push_back(n-i);
 }
 ll l = v.size();
 sort(v.begin(),v.end());
 if(l>0)
 cout << v[0];
 else
 cout << n;

}


#include <iostream>

using namespace std;

int main()
{
	unsigned long long a, b, ans = 1; cin >> a >> b;
	if(a == 1)
	{
		cout << b; return 0;
	}
	if(b == 1)
	{
		cout << a; return 0;
	}
	while(true)
	{
		if(a == 1)
		{
			ans += b;
			break;
		}
		if(b == 1)
		{
			ans += a;
			break;
		}
		if(a > b)
		{
			a = a - b;
			ans++;
		}
		if(a < b)
		{
			b = b - a;
			ans++;
		}
		if(a == b)
		{
			break;
		}
	}cout << ans;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define mid(l, r) ((l)+(r))/2
#define len(a) (a).length()
#define sz(a) (a).size()
#define xx first
#define yy second
#define inf int(2e9)
#define ff(i, a, b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i, a, b) for(int (i) = (a); (i) >= (b); --(i))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

template<class T>
void print(const T niz[], const int siz)
{
    for(int i=0;i<siz;i++)
        cout << niz[i] << " ";
    cout << endl;
}

int a,b,c,n;
int main()
{
    cin>>a>>b>>c>>n;
    if(a>n || b>n || n==0 || c>a || c>b || c>a+b)
        return -1;
    if(a+b-c>=n)
        return -1;
    cout<<n-a-b+c;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>
using namespace std;
const double Pi = 3.1415926536;
vector <int> v;
int res = 0;
int n;
void BFS()
{
	queue<pair<pair<int, int>, int>> q;
	int min = 1000;
	while (v[res] == 0)
	{
		res++;
	}
	if (v[res] == 1)
	{
		q.push(make_pair(make_pair(res+1, res), 1));
	}
	if (v[res] == 2)
	{
		q.push(make_pair(make_pair(res+1, res), 2));
	}
	if (v[res] == 3)
	{
		q.push(make_pair(make_pair(res+1, res), 2));
		q.push(make_pair(make_pair(res+1, res), 1));
	}
	q.push(make_pair(make_pair(res+1, res + 1), 0));
	while (!q.empty())
	{
		int a = q.front().first.first;
		int b = q.front().first.second;
		int c = q.front().second;
		q.pop();
		if (a == n)
		{
			if (min > b)
			{
				min = b;
			}
			continue;
		}
		if (c == 1)
		{
			if (v[a] == 2)
			{
				q.push(make_pair(make_pair(a + 1, b), 2));
			}

			if (v[a] == 3)
			{
				q.push(make_pair(make_pair(a + 1, b), 2));
			}
			q.push(make_pair(make_pair(a + 1, b + 1), 0));
		}
		if (c == 2)
		{
			if (v[a] == 1)
			{
				q.push(make_pair(make_pair(a + 1, b), 1));
			}

			if (v[a] == 3)
			{
				q.push(make_pair(make_pair(a + 1, b), 1));
			}
			q.push(make_pair(make_pair(a + 1, b + 1), 0));
		}
		if (c == 0)
		{
			if (v[a] == 3)
			{
				q.push(make_pair(make_pair(a + 1, b), 1));
				q.push(make_pair(make_pair(a + 1, b), 2));
			}
			if (v[a] == 2)
			{
				q.push(make_pair(make_pair(a + 1, b), 2));
			}
			if (v[a] == 1)
			{
				q.push(make_pair(make_pair(a + 1, b), 1));
			}
			q.push(make_pair(make_pair(a + 1, b + 1), 0));
		}
	}
	res = min;
}
int main()
{
#ifdef _KOCHERGIN
	freopen("abc.txt", "r", stdin);
#else
	//freopen("sample.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#endif
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; ++i)
	{
		cin >> v[i];
	}
	if (n == 1 && v[0] == 0)
	{
		cout << 1;
		return 0;
	}
	BFS();
	cout << res << endl;
	return 0;
}


#include <stdio.h>
#include <string.h>
int pan(char c)
{
    if(c=='a'||c=='o'||c=='i'||c=='u'||c=='e')
    {
        return 1;

    }
    else if(c=='n')
    {
        return 2;
    }
    else
    {
        return 0;
    }
}
int pan_fu(char c)
{
    if(pan(c)==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{   char a[101],*p;
    int n,ji;
    while(1)
    {
        gets(a);
        ji=0;
        n=strlen(a);
        p=a;
        while(*p!='\0')
        {
            if(pan(*p)==0)
            {
                p++;
                if(pan_fu(*p)==1)
                {   p++;
                    continue;
                }
                else
                {
                    ji=1;
                    break;
                }

            }
            else if(pan(*p)==2)
            {
                p++;
                if(*p=='\0')
                {
                    break;
                }
                else if(pan(*p)==1)
                {   p++;
                    continue;
                }
                else
                {
                    continue;
                }

            }
            else
            {
                continue;
            }
        }
        if(ji==1)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define N 20
using namespace std;
string s,ss;
int main(){
    cin>>s; ss=s;
    FOR(i,0,ss.size()) ss+=ss[i];
    FOR(i,1,5){
        string st;
        cin>>st;
        if (st[0]==s[0]||st[1]==s[1]) {cout<<"YES"; exit(0);}
    }
    cout<<"NO";
}


#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{long long k=0, x;

cin>>x;

while(x){
         if(x%4==0 || x%7==0)
                   {k++; x/=10;}
}

/*for (int i=0;i<x.size();i++)
	if(x[i]-'0'==4 || x[i]-'0'==7) k++;
*/

if(k>0){
        while(k){
                 if(k%4!=0 && k%7!=0) { cout<<"NO"; break; }
                 if(k<10) cout<<"YES";
                 else k/=10;
        }

}
else cout<<"NO";

cout<<k;

    system("PAUSE");
    return EXIT_SUCCESS;
}


#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define bit(x,j) ((x>>j)&1)
using namespace std;
// bitset<8>set1(p);
// __builtin_popcountll(x);
// bitset<100> b("1010");
ll int mod=998244353;
int mul(ll int x, ll int y)
{
    return (x * 1ll * y) % mod;
}

int add(int x, int y)
{
    x += y;
    while(x >= mod) x -= mod;
    while(x < 0) x += mod;
    return x;
}

long long power(long long a, long long b,ll m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = mul(res,a);
        a = mul(a,a);
        b >>= 1;
    }
    return res%m;
    }
  ll int binomial(ll int n, ll int k,ll m)
{
    ll int C[k+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;  // nC0 is 1

    for (ll int i = 1; i <= n; i++)
    {
        // Compute next row of pascal triangle using
        // the previous row
        for (int j = min(i, k); j > 0; j--)
            C[j] = add(C[j],C[j-1]);
    }
    return C[k]%m;
}
  vector<int> pr;
  bool prime[10000000];
void Sieve(ll int n)
{
    memset(prime, true, sizeof(prime));
    prime[1]=false;

    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            pr.push_back(p);
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
}

//It reurns Fn,Fn+1
pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}
int sum(int n)
{
    if (n == 0)
    return 0;
    return (n % 10 + sum(n / 10));
}
ll int factorial(ll int n,ll int m)
{
    ll int fact = 1;
    for(int i=2;i<=n;i++) {
        fact = (fact * i)%m;
    }
    return fact%m;
}

void reverse(string& str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
int main() {
  ll int n,m;
  cin>>n>>m;
  ll int a[n];
  for(int i=0;i<n;i++)
  {
      cin>>a[i];
  }
  ll int b[m];
  ll int k=0;
   map<int,int>mp;
  for(int i=0;i<m;i++)
  {
      cin>>b[i];
      mp[i+1]=b[i];
      k+=b[i];
  }
  ll int l=k;
  //cout<<mp[79]<<endl;
  map<int,int>ma[100005];
  int o=0;
  int y[m+1]={0};
  int r[m+1]={0};
  for(int i=0;i<n;i++)
  {
     if(mp[a[i]]>0)
     {
        mp[a[i]]--;
        ma[a[i]][y[a[i]]]=i;
        y[a[i]-1]++;
        l--;
     }
     else
     {
         for(int j=0;j<m;j++)
         {
             mp[j+1]=b[j];
         }
         l=k;
         if(mp[a[i]]>0)
         {
         i=ma[a[i]][r[a[i]-1]];
         r[a[i]-1]++;
         }
     }
    // cout<<l<<" "<<i<<endl;
     if(l==0)
     {
         cout<<"YES"<<endl;
         return 0;
     }
  }
  cout<<"NO"<<endl;
}


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <bits/stdc++.h>

using namespace std;
#define OO ll(1e18)
typedef unsigned long long ull;
typedef long long ll;

int bfs(int x, int y) {
	queue<pair<int, pair<int, int>>>q;
	q.push({ x,{y,0} });
	while (q.size()) {
		x = q.front().first;
		y = q.front().second.first;
		int c = q.front().second.second;
		q.pop();
		if (!x && !y)return c;
		if (x - 1 >= 0)
			q.push({ x - 1,{ y,c + 1 } });
		if (y - 1 >= 0)
			q.push({ x ,{ y - 1,c + 1 } });
		if (y - 1 >= 0 && x - 1 >= 0)
			q.push({ x - 1 ,{ y - 1,c + 1 } });
	}
}

int main() {
	int xp, yp, xv, yv;
	cin >> xp >> yp >> xv >> yv;
	int d = bfs(xv, yv);
	if ((xp + yp) - d <= 1)cout << "Polycarp\n";
	else cout << "Vasiliy\n";
}


#include<bits/stdc++.h>
using namespace std;
int main ()
{
    int n, m, r, a, b, k;
    cin >> n >> a >> b;
    map < long long, int > mp;
    mp[0] = 1;
    n--;
    while (n){
        long long cost = mp.begin() -> first;
        int cnt = mp.begin() -> second;
        if (cnt <= n)
            mp.erase(mp.begin());
        else
            mp[cost] -= n;
        int erased = min (n, cnt);
        mp[a+cost]+= erased;
        mp[b+cost]+= erased;
        n -= erased;
    }
    long long ans = 0;
    for(auto it : mp)
        ans += it.first * it.second;
    cout << ans ;
    return 0;
}