// Author :
// Date :
// Problem:
// Solution:
// Comment:

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

#define pp push
#define pb push_back
#define mp make_pair
#define xx first
#define yy second

using namespace std;

void play(int n,int mm){
    int ans[105] = {0};
    int cur = -1;
    int day = 0;
    int q;

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

        cin >> m;
        for(int i = 0 ; i < m ; i++){
            cin >> q;
            ans[q]++;
        }
    }

    for(int i = 0 ; i < 103 ; i++){
        if(ans[i] > cur && ans[i] >=mm){
            cur = ans[i];
            day = i;
        }
    }

    cout << day << endl;
}

main(){
    int nn,mm;

    while(cin >> nn >> mm){
        if(nn == 0) break;
        play(nn,mm);
    }
    #ifdef GEREL
        for(;;);
    #endif

    return 0;
}

//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1144&lang=jp

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

using namespace std;

int W,H;
const int NONE=0, BLOCK=1, START=2, GOAL=3;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int ans;
vector<vector<int>> field;

bool out_range(int x, int y){
	return x < 0 || y < 0 || x >= W || y >= H;
}

void dfs(int x, int y, int depth)
{
	if(depth + 1 >= ans){ return; }

	REP( d, 0, 4 )
	{
		int nx = x + dx[d];
		int ny = y + dy[d];
		//次に移動する場所がBLOCKではなく、範囲内である。
		if(!out_range(nx,ny) && field[ny][nx] != BLOCK)
		{
			//エリア外に出るまで移動する
			while(!out_range(nx,ny))
			{
				//GOALならば、答えを更新する
				if(field[ny][nx] == GOAL)
				{
					ans = min(ans,depth+1);
					return;
				}

				//BLOCKならば、次の移動を行う
				if(field[ny][nx] == BLOCK)
				{
					field[ny][nx] = NONE;
					dfs(nx-dx[d], ny-dy[d], depth+1);
					field[ny][nx] = BLOCK;
					break;
				}

				nx = nx + dx[d];
				ny = ny + dy[d];
			}
		}
	}

	return;
}

int main()
{
	while(cin >> W >> H && W)
	{
		int sx,sy;
		field.assign(H,vector<int>(W));
		rep(y,H){
			rep(x,W){
				cin >> field[y][x];
				if(field[y][x] == START){
					sx = x;
					sy = y;
					field[y][x] = NONE;
				}
			}
		}
		ans = 11;
		dfs(sx,sy,0);
		cout << ((ans <= 10) ? ans : -1) << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define mod (long)(1e9 + 7)
using namespace std;

long dp[10000][2][100];

long solve(int curr, int tight, int sum, string &s, int d) {
	if(curr == s.size()) {
		return (sum == 0);
	}
	if(dp[curr][tight][sum] != -1) return dp[curr][tight][sum];
	if(tight) {
		long res = 0;
		for(int i = 0; i <= (s[curr] - '0'); ++i) {
			res = (res + solve(curr + 1, tight & (i == (s[curr]-'0')), (sum + i) % d, s, d)) % mod;
		}
		return dp[curr][tight][sum] = res;
	}
	long res = 0;
	for(int i = 0; i < 10; ++i) {
		res = (res + solve(curr + 1, tight, (sum + i) % d, s, d)) % mod;
	}
	return dp[curr][tight][sum] = res;
}

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

	string k;
	int d;
	cin >> k >> d;
	memset(dp, -1, sizeof dp);
	cout << (solve(0, 1, 0, k, d) - 1 + mod) % mod;

	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
using llong = long long;

llong h, w;
llong a[100][100];
llong b[100][100];
llong v[100][100];
llong u[100][100];
bool dp[100][100][26000];
llong offset = 13000;

int main() {
    cin >> h >> w;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cin >> b[i][j];
        }
    }

    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            v[i][j] = a[i][j] - b[i][j];
            u[i][j] = b[i][j] - a[i][j];
        }
    }

    dp[0][0][offset] = true;
    dp[0][1][offset] = true;
    dp[1][0][offset] = true;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            for (int k = 0; k < 26000; k++) {
                if (dp[i - 1][j][k] == true) {
                    dp[i][j][k + v[i][j]] = true;
                    dp[i][j][k + u[i][j]] = true;
                }
                if (dp[i][j - 1][k] == true) {
                    dp[i][j][k + v[i][j]] = true;
                    dp[i][j][k + u[i][j]] = true;
                }
            }
        }
    }

    llong ans = 1ll << 60ll;
    for (int k = 0; k < 26000; k++) {
        if (dp[h][w][k] == true) {
            ans = min(ans, llabs(k - offset));
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> void re(T& x) { cin >> x; }
template<typename T>
void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "/home/wjex/cp/pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

void solve(){
	int k ;
	string s ;
	re(k,s);
	if(k>=s.length()){
		cout << s << endl;
	}
	else if (s.length()>k){
		rep(i,0,k) cout << s[i];
		cout << "...";
	}
}

signed main() {
	unsyncIO();
	int tt =1 ;
	//cin >> tt;
	rep(i,0,tt) solve();

#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

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

long long get_rev(long long x) {
	long long nx = 0;
	while (x) nx = nx * 10 + x % 10, x /= 10;
	return nx;
}

long long get_rev(long long x, int len) {
	long long nx = 0;
	while (len--) nx = nx * 10 + x % 10, x /= 10;
	return nx;
}

int cnt;

long long solve(long long d, long long len) {
	long long tmp = len >> 1, pw = 1;
	while (tmp--) pw *= 10;
	long long ans = 0;
	for (int i = 0; i < pw; i++) {
		long long tmp = (d + i) % pw;
		tmp = get_rev(tmp, len >> 1);
		if (len & 1) tmp = tmp * 10;
		long long n = tmp * pw + i;
		if ((cnt ? get_rev(n, len) : get_rev(n)) == n + d) ans++;
	}
	if (len & 1) ans *= 10;
	return ans;
}

int main () {
	int d; cin >> d;
	cnt = 0;
	while (d % 10 == 0) d /= 10, cnt++;
	int len = 0, D = d;
	while (D) len++, D /= 10;
	long long ans = solve(d, len) + solve(d, len + 1) + solve(d, len + 2) + solve(d, len + 3) + solve(d, len + 4) + solve(d, len + 5) + solve(d, len + 6);
	if (cnt) cnt--, ans *= 9;
	while (cnt--) ans *= 10;
	cout << ans << endl;
	return 0;
}


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

int main() {
  int N,K;
  cin >> N >> K;
  int x;
  x = 1;

  for(int i = 0; i < N; i++){
    if(2 * x > x + K)
    x = x + K;
    if(2 * x <= x + K)
    x = 2 * x;
  }
  cout << x << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll         = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
const long long INF = numeric_limits<long long>::max();
template <class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main() {
    ll N, K;
    cin >> N >> K;
    ll value = 1;
    for (ll i = 0; i < N; ++i) {
        if (value < K) {
            value *= 2;
        } else {
            value += K * (N - i);
            break;
        }
    }
    cout << value << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
static const int64_t mod=1000000007;
int64_t f(int64_t x,int64_t y){
if(y==0)
  return 1;
  else if(y%2==0)
    return (f(x,y/2)*f(x,y/2))%mod;
  else
    return (f(x,y/2)*((x*f(x,y/2))%mod))%mod;
}
int64_t comb(int64_t x,int64_t y){
int64_t ans=1;
  for(int64_t i=y;1<=i;i--){
  ans=(f(i,mod-2)*((ans*(x-y+i))%mod))%mod;
  }return ans;

}
int main(){
int64_t N,M;
  cin>>N>>M;
  int64_t m=M;
  vector<int64_t>A={};
  for(int64_t i=2;i*i<=m;i++){
  if(M%i==0){
    int64_t a=0;
    while(M%i==0){
    M/=i; a++;
    }A.push_back(a);

  }
  }if(M!=1)
    A.push_back(1);
  int L=A.size();
  int64_t ans=1;
  for(int i=0;i<L;i++){
    int64_t a=A.at(i);
    ans=(ans*comb(N-1+a,a))%mod;
  }cout<<ans<<endl;
     return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=(1e5)+10;
const int mod=(1e9)+7;
const int M=(1e7)+9;
int n,m,tot,d[maxn],iv[maxn],ivjc[maxn],jc[maxn];
int dp[40][maxn],ans;
vector<int> g[maxn];
struct Haxi {
	int to[maxn],tot,head[M],nxt[maxn],w[maxn];
	void insert(int x,int y) {
		tot++; nxt[tot]=head[x%M];
		head[x%M]=tot; to[tot]=x; w[tot]=y;
	}
	int find(int x) {
		for (int i=head[x%M];i;i=nxt[i])
			if (to[i]==x) return w[i];
		return 0;
	}
} Map;
int C(int x,int y) {
	if (x<y||y<0) return 0;
	return (ll)jc[x]*ivjc[y]%mod*ivjc[x-y]%mod;
}
void update(int &x,int y) {
	x+=y; if (x>=mod) x-=mod;
}
int main() {

	read(n); read(m);
	iv[0]=jc[0]=ivjc[0]=iv[1]=jc[1]=ivjc[1]=1;
	for (int i=2;i<maxn;i++) {
		jc[i]=(ll)jc[i-1]*i%mod;
		iv[i]=(ll)(mod-mod/i)*iv[mod%i]%mod;
		ivjc[i]=(ll)ivjc[i-1]*iv[i]%mod;
	}
	for (int i=1;(ll)i*i<=m;i++)
		if (m%i==0) {
			d[++tot]=i;
			if (m/i!=i) d[++tot]=m/i;
		}
	sort(d+1,d+tot+1);
	int x;
	for (int i=1;i<=tot;i++) {
		Map.insert(d[i],i);
		x=d[i];
		for (int j=1;(ll)j*j<=d[i];j++)
			if (x%j==0) {
				g[Map.find(j)].push_back(i);
				if (x/j!=j) g[Map.find(x/j)].push_back(i);
			}
	}
	int mx=min(n,35);
	dp[0][1]=1;
	for (int i=0;i<mx;i++)
	for (int j=1;j<=tot;j++) if (dp[i][j]) {
		for (int k=0;k<g[j].size();k++) if (g[j][k]!=j) {
			update(dp[i+1][g[j][k]],dp[i][j]);
		}
	}
	for (int i=0;i<=mx;i++) {
		update(ans,(ll)dp[i][tot]*C(n,i)%mod);
	}
	printf("%d\n",ans);
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/


#include <bits/stdc++.h>

using namespace std;

// [l, r)
int cnt_w(vector<int> &csum, int l, int r) { return csum[r] - csum[l]; }
int cnt_e(vector<int> &csum, int l, int r) {
  return (r - l) - (csum[r] - csum[l]);
}

int main(int argc, const char *argv[]) {
  int n;
  string s;
  cin >> n >> s;

  vector<int> csum(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    csum[i] = csum[i - 1];
    if (s[i - 1] == 'W') {
      csum[i]++;
    }
  }

  int ans;
  for (int i = 0; i < n; ++i) {
    int cnt = cnt_w(csum, 0, i) + cnt_e(csum, i + 1, n);
    if (i == 0 || ans > cnt) {
      ans = cnt;
    }
  }

  cout << ans << '\n';

  return 0;
}

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

int main(){
int n,mn=1000000;
  string s;
  cin>>n>>s;
  vector<int> v(n+2);
  vector<int> u(n+2);
  v[0]=0;u[0]=0;
  for(int i=0;i<n;i++){
  if(s[i]=='W'){v[i+1]=v[i]+1;}
  else v[i+1]=v[i];
  }
  reverse(s.begin(),s.end());
  for(int j=0;j<n;j++){
  if(s[j]=='E'){u[j+1]=u[j]+1;}
  else u[j+1]=u[j];
  }
  reverse(u.begin(),u.end());
  u.push_back(0);
  for(int k=0;k<=n;k++){
  mn=min(v[k]+u[k+1],mn);
  }
  cout<<mn;
  return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
      int tmp;
      int dp[100];
      cin>> tmp;
      do{
            fill(dp,dp+100,0);
            dp[0] = 1;
            for (int i = 0; i<tmp;i++)
            {
                  for(int j = 1;j < 4;j++)
                        dp[i+j] += dp[i];
            }
            cout << dp[tmp]/3650 + 1<<endl;
            cin >> tmp;
      }while(tmp);

}

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define SZ(X) ((int)(X).size())
#define endl "\n";
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int a[3],k;
	cin>>a[0]>>a[1]>>a[2]>>k;
	sort(a,a+3);
for (int i = 0; i < k; ++i)
{
	a[2]*=2;
}
	cout<<a[0]+a[1]+a[2];

   return 0;
}


#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int mod = 998244353;

int main() {
	// freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	ll ans = 1;
	vector<int> d(n);
	for (int& i : d) {
		cin >> i;
	}
	sort(d.begin() + 1, d.end());
	vector<int> cnt(n + 1);
	for (int i = 1; i < n; i++) {
		cnt[d[i - 1]]++;
		if (d[i] == 0) {
			ans = 0;
			break;
		}
		ans = ans * cnt[d[i] - 1] % mod;
	}
	cout << ans << endl;
	return 0;
}

#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <tuple>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

using ll=long long;

typedef long long ll;
typedef pair<int,int> ii;
typedef pair<string,int> si;
typedef pair<int,ii> iii;
typedef vector <si> vsi;
typedef vector <ii> vii;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <vll> vl2d;
typedef vector <vi> vi2d;
typedef vector <char> vc;
typedef vector <bool> vb;
typedef vector <string> vs;
typedef map <string,vs> msvs;
typedef map <string,int> msi;
typedef map <char,int> mci;
typedef map <int,int> mii;
typedef map <long,long> ml;
typedef map <string,string> mss;
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define foreach(x,arr) for(auto& x:arr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define endl '\n' // Normal `cout << endl` flushes the output every time wich hit performance badly
#define INF 1000000000LL
#define MOD 1000000007ll
#define deb(x) cout<<#x<<": "<<x<<endl;
#define printArray(arr) for(auto x: arr) { cout<<x<<"\t"; }
#define printMatrix(mat) for(auto x: mat) { cout<<" "; printArray(x); cout<<endl; }
#define printMap(mmap) for(auto p: mmap) { cout<< p.first<<": "<<p.second<<endl; }
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define forch(ch,str) for(char& c : str)
#define FILL(a,b) memset(a,b,sizeof(a))
//Swift related stuff
#define append push_back
#define nil nullptr
#define guard(_condition) if (bool(_condition)){}
#define count size()
#define let const auto
#define var auto
#define startsWith(str) rfind(str, 0)
#define prq priority_queue
#define tostr(argument) to_string(argument)
#define alphabet "abcdefghijklmnopqrstuvwxyz"

ll modulo(ll a, ll b, ll n){
    long long x=1, y=a;
    while (b > 0) {
        if (b%2 == 1) {
            x = (x*y) % n; // multiplying with base
        }
        y = (y*y) % n; // squaring the base
        b /= 2;
    }
    return x % n;
}
int main() {
    ll n;
    cin>>n;
    vll arr(n);
    bool wrong = false;
    map<ll, ll> dic;
    ll mx = 0;
    rep(i,0,n) {
        ll num;
        cin>>num;
        if(i==0 && num != 0) {
            wrong = true;
        }
        if(i != 0 && num == 0) {
            wrong = true;
        }
        dic[num] += 1;
        mx = MAX(mx, num);
    }

    ll res = 1;
    rep(i,1,mx+1) {
        guard(dic[i-1] != 0 && dic[i] != 0) else {
            wrong = true;
            break;
        }
        ll p = modulo(dic[i-1], dic[i],998244353);
        res = (res * p)%998244353;
    }
    if(wrong) {
        cout<<0<<endl;
        return 0;
    }
    cout<<res<<endl;
    return 0;
}


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

int main() {
	string a, b, c;
	cin >> a >> b >> c;
	if (a.at(a.length() - 1) == b.at(0) && b.at(b.length() - 1) == c.at(0))cout << "YES" << endl;
	else cout << "NO" << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 1e12

#define PB push_back
#define PF push_front
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpi vector<pii>
#define vll vector<ll>
#define vpl vector<pll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pr_d(x) cout << fixed << setprecision(15) << x << endl
#define ud(c, x) distance(c.begin(), upper_bound(all(c), x))
#define ld(c, x) distance(c.begin(), lower_bound(all(c), x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep3(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep4(i, a, b) for (int i = (a); i > (b); --i)
#define pb push_back
#define out(x) cout << x << "\n"
bool odd(int i) { return i % 2; }

#define all(v) v.begin(), v.end()
#define size(x) int(x.size())

int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void Yes_No(bool f) {
  if (f)
    printf("YES\n");
  else
    printf("NO\n");
}

void deb_p(pii p) { printf("pair = %d %d\n", p.fi, p.se); }
void deb_i(int i) { printf("i = %d\n", i); }
void deb_v(vector<int> v) {
  printf("v = ");
  for (auto i : v) printf("%d ", i);
  printf("\n");
}
template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//-------------------ここから回答する-----------------------

void solve(void) {
  string a, b, c;
  cin >> a >> b >> c;
  Yes_No(a[size(a)-1]==b[0]&&b[size(b)-1]==c[0]);
}
int main(void) { solve(); }


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

class Dice_c{

private:
  int me[4];
  int topbot[2]; // top,bottom

public:
  void Input();
  void Roll(char c);
  void Output();

};

int main(){

  Dice_c dice;
  char com;

  dice.Input();

  while(1){
    scanf("%c",&com);

    if(com=='\n')
      break;

    dice.Roll(com);

  }

  dice.Output();

  return 0;
}

///////////////////////////////////////

void Dice_c::Input(){

  cin >> me[1] >> topbot[1] >> me[2] >> me[0] >> topbot[0] >> me[3];
  getchar();

}

void Dice_c::Roll(char c){

  int tmp;

  if(c == 'W'){
    tmp = me[0];
    me[0]=me[1] , me[1]=me[2] , me[2]=me[3] , me[3]=tmp;
  }
  else if(c == 'E'){
    tmp = me[3];
    me[3]=me[2] , me[2]=me[1] , me[1]=me[0] , me[0]=tmp;
  }
  else if(c == 'N'){
    tmp = topbot[1];
    topbot[1]=me[3] , me[3]=topbot[0] , topbot[0]=me[1] , me[1]=tmp;
  }
  else{
    tmp = topbot[0];
    topbot[0]=me[3] , me[3]=topbot[1] , topbot[1]=me[1] , me[1]=tmp;
  }

  /*
  cout << "  " << topbot[0] << endl;
  for(int i=0; i<4; i++)
    cout << me[i] << ' ';
  cout << endl << "  " << topbot[1] << endl;
  */

}

void Dice_c::Output(){

  cout << me[1] << endl;

}

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

int main() {
  string s;
  cin >> s;
  vector<bool> app(26);
  rep(i, s.size()) app[s[i] - 'a'] = true;
  rep(i, 26) if(!app[i]) {
    cout << string(1, i + 'a') << "\n";
    return 0;
  }

  cout << "None" << "\n";
  return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <complex>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
const double PI = 3.141592653589793238462643383279502884L;
const int LARGE_PRIME = 1000000007;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define rtr(c, i) for(auto i = (c).rbegin(); i != (c).rend(); i++)
#define repfn(i,a,cont,next) \
  for(auto i = (a); [=](){return(cont);}(); i = [=](){return(next);}())
#define repby(i,a,b,s) repfn(i,a,i<b,i+s)
#define repab(i,a,b) repby(i,a,b,1)
#define rep(i,b) repab(i,0,b)
#define pb push_back
#define sz(c) int((c).size())

int main(int argc, char **argv)
{
  for (;;) {
    int n;
    vi v;

    cin >> n;
    if (!n)
      break;

    while (n--) {
      int tmp;
      cin >> tmp;
      v.pb(tmp);
    }

    vi w(v);
    for (int i = 1; i < sz(v); i++) {
      v[i] = max(v[i-1] + v[i], v[i]);
    }
    cout << *max_element(all(v)) << endl;
  }
  return 0;
}

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <deque>
#include <array>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define range(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
#define all(x) x.begin(), x.end()
#define PI 3.1415926536

using namespace std;

const int MAX_N = 5000;
array<int, MAX_N + 1> bit;
int sets, n;

int sum(int i){
  int ret = 0;
  while(i > 0){
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x){
  while(i <= n){
    bit[i] += x;
    i += i & -i;
  }
}

void solve(){
  cin >> n;
  if(n == 0) exit(0);
  bit.fill(0);
  for (int i = 1; i <= n; ++i) {
    int t; cin >> t;
    add(i, t);
  }

  int ans = -1e9;
  for (int i = 0; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      ans = max(ans, sum(j) - sum(i));
    }
  }
  cout << ans << endl;
}

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

  for(;;) solve();

  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

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

    int ans = n*n*n;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
typedef long long int ll;
typedef pair<int,int> P;
const int INF = 1e9+7;

int main(){
    int r;
    cin >> r;
    if(r<1200){
        cout << "ABC";
    }else if(1200<=r&&r<2800){
        cout << "ARC";
    }else{
        cout << "AGC";
    }
}


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

int N, M;

int main() {
	cin >> N >> M;
	int cx = M + 1, cnts = 0;
	for (int i = M; i >= 1; i--) {
		if (N % 2 == 0 && (cx - i) * 2 >= N && cnts == 0) { cx++; cnts = 1; }
		cout << i << " " << cx << endl;
		cx++;
	}
	return 0;
}

#pragma target("avx")
#pragma optimize("O3")
#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using PP = std::pair<ll,P>;
using V = std::vector<ll>;
template<typename T> using pq = std::priority_queue<T>;
template<typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i,b,e) for(ll i=b; i<e; i++)
#define ALL(vec) vec.begin(),vec.end()
#define PRINT(vec) printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");
#define SCAN(vec) for(auto &i:vec)scanf("%ld",&i)
#define fi first
#define se second

const int MOD = 1e9+7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(){
	int n, m;
	scanf("%d %d", &n, &m);
	if((n-2*m)%2){
	    REP(i, 0, m) printf("%ld %ld\n", i+1, n-i);
	} else {
	    REP(i, 0, (m+1)/2){
		printf("%ld %ld\n", i+1, n-i);
	    }
	    REP(i, (m+1)/2, m){
		printf("%ld %ld\n", i+2, n-i);
	    }
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353;

int main() {
    int N;
    cin>>N;
    int zero;
    cin>>zero;
    if(zero!=0){cout<<0<<endl;return 0;}
    vector<int> D(N-1);
    rep(i,N-1){cin>>D.at(i);}
    sort(D.begin(),D.end());
    int num=1,before=1,now=0;
    ll ans=1;
    rep(i,N-1){
        if(D.at(i)==num){
            now++;
            ans*=before;
            ans%=MOD2;
        }
        else{
            num++;
            before=now;
            ans*=before;
            ans%=MOD2;
            now=0;
            if(D.at(i)==num){
                now++;
            }
            else{cout<<0<<endl;return 0;}
        }
        //cout<<ans<<" "<<endl;
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=998244353;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
ll pow_mod(ll n,ll k,ll m){
  ll res=1;
  for(;k>0;k>>=1){
    if(k&1){
      res=(res*n)%m;
    }
    n=(n*n)%m;
  }
  return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin >> n;
    vector<ll> d(n),c(n);
    REP(i,n){
        cin >> d[i];
        c[d[i]]++;
    }
    if(d[0]!=0||c[0]!=1){
        cout << 0 << endl;
        return 0;
    }
    ll ans=1;
    FOR(i,1,n){
        if(!c[i]) continue;
        (ans*=pow_mod(c[i-1],c[i],mod))%=mod;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);++i)
#define sayyes cout<<"Yes"<<endl;
#define sayno cout<<"No"<<endl;
using namespace std;
typedef long long ll;
const double PI=acos(-1);
typedef pair<int,int>P;
const int mod =1e9+7;

int main()
{
    int a,b;
    cin>>a>>b;
    cout<<max(max(a+b,a-b),a*b);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main(){
  ll A,B;
  cin >> A >> B;

  if(max(A+B,A*B) > A-B) cout << max(A+B,A*B) << endl;
  else cout << A-B << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

int main() {
	int n;
	cin >> n;
	cout << 0 << endl;
	string s;
	cin >> s;
	if (s == "Vacant") return 0;
	vector<string> v(n);
	v[0] = s;
	int l = 0;
	int r = n;
	int p = (l+r)/2;
	cout << p << endl;
	while (cin >> s, s != "Vacant") {
		v[p] = s;
		int x = p - l - 1;
		int y = r - p - 1;
		if ((v[l] == s && x % 2 == 0) || (v[l] != s && x % 2 == 1)) {
			r = p;
			p = (l+r)/2;
		} else {
			l = p;
			p = (l+r)/2;
		}
		cout << p << endl;
	}
}

#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<cassert>
#include<cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define sz size()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;

ll dp[110][110];

string query(int n){
    cout << n << endl;
    fflush(stdout);
    string res;
    cin >> res;
    if(res == "Vacant")exit(0);
    return res;
}

int main(){
    int n;
    cin >> n;
    int l = 0, r = n - 1;
    string x = query(l);
    string y = query(r);
    while(1){
        int mid = (l + r) / 2;
        string z = query(mid);
        if((z == x) != (l % 2 == mid % 2)){
            r = mid;
            y = z;
        }
        else {
            l = mid;
            x = z;
        }
    }
}


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

int main(){
int n;
cin >> n;
int ara[n];
for(int i=0;i<n;i++) cin >> ara[i];
for(int i=n-1;i>=1;i--) cout << ara[i] << " ";
cout << ara[0] << endl;
return 0;
}

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

using namespace std;

// cl /EHsc xxx.cpp
int main(int argc, char *argv[])
{
	int n;
	scanf("%d", &n);

	int *ary = new int[n];

	int v;
	int t = n;
	while (t)
	{
		scanf("%d", &v);
		ary[--t] = v;
	}

	for (int j = 0; j < n; j++)
	{
		printf((j == n-1) ? "%d\n" : "%d ", ary[j]);
	}

	delete[] ary;

	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <fstream>
#include <cmath>
#include <random>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

template<class T> inline void chmin(T& a, T b) {
    if (a > b) {
        a = b;
    }
}
template<class T> inline void chmax(T& a, T b) {
    if (a < b) {
        a = b;
    }
}

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

    ll N, M;
    cin >> N >> M;
    mat A(N, vec(M));
    Rep (i, N) Rep (j, M) {
        cin >> A[i][j];
        A[i][j]--;
    }

    vector<bool> sports(M, true);
    ll ans = 310;
    Rep (i, M) {
        vec cnt(M, 0);
        Rep (j, N) {
            Rep (k, M) {
                if (sports[A[j][k]]) {
                    cnt[A[j][k]]++;
                    break;
                }
            }
        }

        ll sub = 0;
        ll popular = -1;
        Rep (j, M) {
            if (sub < cnt[j]) popular = j;
            chmax(sub, cnt[j]);
        }
        chmin(ans, sub);
        sports[popular] = false;
    }

    cout << ans << endl;
}

#include <bits/stdc++.h>

#define PI 3.14159265358979323846264338327950288419

using namespace std;

int n, m;
int pref[303][303];

int ptr[303];
int freq[303];

queue<int> Q;

bool die[303];

void incre(int j, int c){
//	printf(" ++%d\n", j);
	freq[j]++;
	if(freq[j] == c + 1){
//		printf("c = %d, die %d\n", c, j);
		die[j] = true;
		Q.push(j);
	}
}

bool possible(int c){
	// can choose with <= c ppl per event?
	memset(freq, 0, sizeof(freq));

	while(!Q.empty()){
		Q.pop();
	}

	for(int j = 1; j <= m; j++){
		die[j] = false;
	}

	for(int i = 1; i <= n; i++){
		ptr[i] = 1;
		incre(pref[i][1], c);
	}

	while(!Q.empty()){
		int del = Q.front();
		Q.pop();
		for(int i = 1; i <= n; i++){
			while(ptr[i] <= m && die[pref[i][ptr[i]]]){
				ptr[i]++;
				incre(pref[i][ptr[i]], c);
			}
		}
	}

	for(int j = 1; j <= m; j++){
		if(!die[j]){
			return true;
		}
	}
	return false;
}

int main(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			scanf("%d", &pref[i][j]);
		}
	}
	int l = 0, r = n;
	while(r - l > 1){
		int mid = (l + r) >> 1;
		if(possible(mid)){
			r = mid;
		}else{
			l = mid;
		}
	}
	printf("%d\n", r);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rep2(i,s,t) for(int i = s; i <= t; ++i)
#define rev(i,n) for(int i = n-1; i >= 0; --i)
#define rev1(i,n) for(int i = n; i > 0; --i)
#define rev2(i,s,t) for(int i = s; i >= t; --i)
#define F first
#define S second
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
using ll = long long;
using pi = pair<int,int>;
using vi = vector<ll>;
using vii = vector<vi>;
int main()
{
  int n; cin >> n;
  vi a(n);
  rep(i,n) cin >> a[i];

  ll inf = (1LL << 60);
  vii dp(n+1, vi(2, -inf));
  dp[0][0] = 0;
  rep1(i,n) {
    chmax(dp[i][0], dp[i-1][0] + a[i-1]);
    chmax(dp[i][1], dp[i-1][0] - a[i-1]);
    chmax(dp[i][0], dp[i-1][1] - a[i-1]);
    chmax(dp[i][1], dp[i-1][1] + a[i-1]);
  }
  // rep(i,n+1) {
  //   cout << dp[i][0] << ":" << dp[i][1] << "\n";
  // }
  cout << dp[n][0] << "\n";
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  int cnt = 0, cnt_z = 0;
  rep(i,0,n) {
    cin >> a[i];
    if (a[i] < 0) cnt++;
    else if (a[i] == 0) cnt_z++;
  }
  ll ans = 0;
  rep(i,0,n) ans += abs(a[i]);
  if (cnt_z == 0 && cnt % 2 == 1) {
    ll b = 100000000000;
    rep(i,0,n) {
      b = min(b,abs(a[i]));
    }
    ans -= 2*b;
  }
  cout << ans << endl;
  return 0;
}


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

int main(){
	int n, a;
	while(cin >> n, n){
		n++;
		vector<int> t(n);
		t[0] = 0;
		for(int i = 1;i < n;i++){
			cin >> a;
			t[i] = max(t[i-1]+a, a);
		}
		cout << *max_element(t.begin()+1, t.end()) << endl;
	}
	return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
#define chmax(x, v)                                                            \
  do {                                                                         \
    x = max(x, v);                                                             \
  } while (0)
#define chmin(x, v)                                                            \
  do {                                                                         \
    x = min(x, v);                                                             \
  } while (0)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

// {{{ (k)MP: Search pattern from text
// MP<std::string> mp("a");
// auto res = mp.findAll("aabb"); // res = {0, 1}
template<typename T>
class MP {
  private:
    ll n;
    T t;
    std::vector<ll> a;
  public:
    explicit constexpr MP(const T &pattern) : t(pattern) {
      n = pattern.size();
      a = std::vector<ll>(n+1);
      a[0] = -1;
      int j = -1;
      for (int i = 0; i < n; ++i) {
        while (j != -1 && pattern[j] != pattern[i])
          j = a[j];
        j++;
        a[i+1] = j;
      }
    }
    ll operator[](ll i) {
      return a[i];
    }
    std::vector<ll> findAll(const T &text) {
      std::vector<ll> res;
      int j = 0;
      for (int i = 0; i < text.size(); ++i) {
        while (j != -1 && t[j] != text[i])
          j = a[j];
        j++;
        if (j == n) {
          res.push_back(i-j+1);
          j = a[j];
        }
      }
      return res;
    }
};
// }}}

ll N;
vector<ull> a, b;

vector<ull> diff(vector<ull> &v) {
  vector<ull> res(v.size());
  rep(i, N) {
    res[i] = v[i] ^ v[(i+1)%N];
  }
  return res;
}

signed main() {
  cin >> N;
  a.resize(N);
  b.resize(N);
  rep(i, N) { cin >> a[i]; }
  rep(i, N) { cin >> b[i]; }
  auto ad = diff(a);
  auto bd = diff(b);

  bd.insert(bd.end(), bd.begin(), bd.end()); // bd += bd;

  // cout << "diff: " << endl;
  // for (auto x : ad) { cout << x << " "; } cout << endl;
  // for (auto x : bd) { cout << x << " "; } cout << endl;

  MP<vector<ull>> mp(ad);
  auto matches = mp.findAll(bd);
  reverse(begin(matches), end(matches));
  for (auto i : matches) {
    ll k = N-i;
    if (k >= N)
      continue;
    ull x = a[k] ^ b[0];
    cout << k << " " << x << endl;
  }
  return 0;
}


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

// MY TOOLS
#define let long long int
#define async ios_base::sync_with_stdio(false);cin.tie(NULL);

//

int dp[10001][101][2] ;
int mod = 1e9 + 7 ;
void self_add( int &a, int b ){
	a += b ;
	if( a >= mod ) a -= mod ;
}
int SIZE ;
int generate_top_down( string num, int idx, int sum, int D, int bound ){
	if( idx == SIZE ) return ( sum % D == 0 ) ;
	int &check = dp[idx][sum][bound] ;
	if( check != -1 ) return check ;
	int N = ( num[idx]-'0' ) ;
	int up_to = ( bound == 1 ) ? N : 9 ;
	int payload = 0, new_bound ;
	for( int digit = 0 ; digit <= up_to ; digit++ ){
		new_bound = ( digit == N ) ? bound : 0 ;
		self_add( payload, generate_top_down( num, idx+1, (sum+digit)%D, D, new_bound ) ) ;
	}
	return check = payload ;
}

// main
// write your code here
int main(){
	async ;
	memset( dp, -1, sizeof(dp) ) ;
	string K ; int D ;
	cin >> K >> D ;
	SIZE = K.size() ;
	int ans = generate_top_down( K, 0, 0, D, 1 ) - 1 ;
	if( ans == -1 ) {
		cout << mod - 1 ;
	}
	else{
		cout << ans ;
	}
    return 0 ;
}

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int mod = 1e+9 + 7;
/* global variables */
/* funciton */
/* main */
int main() {
    string S;
    int D;
    cin >> S >> D;
    vector<int> a(S.size());
    for (int i = 0; i < S.size(); i++) a[i] = S[i] - '0';

    // dp[i][j][0] i桁目まで使って, mod D = j, 現時点でSよりも数字が小さいことが確定している
    // dp[i][j][1] i桁目まで使って, mod D = j, 現時点でSと同じ数字 -> これから同じ or 小さい
    // transition)
    // dp[i+1][j+(num = a[i]) mod D][1] += dp[i][j][1]
    // dp[i+1][j+(num < a[i]) mod D][0] += dp[i][j][1]
    // dp[i+1][j+(0<= num <= 9) mod D][0] += dp[i][j][0]
    // init
    vector<vector<vector<ll>>> dp(a.size() + 1, vector<vector<ll>>(D, vector<ll>(2)));
    dp[0][0][1] = 1;
    // calc
    for (int i = 0; i < a.size(); i++) {
        for (int j = 0; j < D; j++) {
            for (int num = 0; num <= 9; num++) {
                int neJ = (j + num) % D;
                if (num == a[i]) {
                    dp[i+1][neJ][1] += dp[i][j][1];
                    dp[i+1][neJ][1] %= mod;
                }
                if (num < a[i]) {
                    dp[i+1][neJ][0] += dp[i][j][1];
                    dp[i+1][neJ][1] %= mod;
                }
                dp[i+1][neJ][0] += dp[i][j][0];
                dp[i+1][neJ][0] %= mod;
            }
        }
    }
    // ans = (mod D == j)の個数
    // 答えに0が含まれるので-1する
    ll ans = (dp[a.size()][0][1] + dp[a.size()][0][0] - 1 + mod) % mod;
    cout << ans << '\n';
}


#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)
vector<complex<double>>  DFT(vector<complex<double>> a) {
	int n = a.size();
	if (n == 1) return a;
	vector<complex<double>> a0(n / 2), a1(n / 2);
	REP(i, n) {
		if (i % 2 == 0) {
			a0[i / 2] = a[i];
		}
		else {
			a1[i / 2] = a[i];
		}
	}
	vector<complex<double>> inversed_a0 = DFT(a0), inversed_a1 = DFT(a1);
	vector<complex<double>> inversed_a(n);
	for (int i = 0; i < n; ++i) {
		complex<double> zeta = complex<double>(cos(2 * Ma_PI * i / n), sin(2 * Ma_PI * i / n));
		inversed_a[i] = inversed_a0[i % (n / 2)] + zeta * inversed_a1[i % (n / 2)];
	}
	return inversed_a;
}
vector<complex<double>> IDFT(vector<complex<double>> inversed_a) {
	int n = inversed_a.size();
	vector<complex<double>> now = DFT(inversed_a);
	reverse(now.begin(), now.end());
	for (int q = now.size() - 1; q >= 1; --q) {
		swap(now[q], now[q - 1]);
	}
	REP(i, n) {
		now[i] /= complex<double>(n, 0);
	}
	return now;
}
vector<complex<double>> conv(vector<complex<double>> a, vector<complex<double>> b) {
	int deg = a.size() + b.size() - 1;
	int n = 1;
	while (n < deg) n <<= 1;
	a.resize(n);
	b.resize(n);
	vector<complex<double>> inversed_a = DFT(a), inversed_b = DFT(b);
	vector<complex<double>> inversed_c(n);
	REP(i, n) {
		inversed_c[i] = inversed_a[i] * inversed_b[i];
	}
	return IDFT(inversed_c);
}
long long inv(long long now) {
	long long hoge = MOD - 2LL;
	long long ans = 1;
	while (hoge != 0) {
		if (hoge % 2 == 1) {
			ans *= now;
			ans %= MOD;
		}
		hoge /= 2;
		now *= now;
		now %= MOD;
	}
	return ans;
}
long long jun[2100000] = {};
long long gyaku[2100000] = {};
long long combination(long long a, long long b) {
	long long ans = jun[a];
	ans *= gyaku[a - b];
	ans %= MOD;
	ans *= gyaku[b];
	ans %= MOD;
	return ans;
}
long long no_limit(long long a, long long b) {
	return combination(a + b - 1, a);
}

int main() {
	jun[0] = 1;
	gyaku[0] = 1;
	for (long long i = 1; i < 2100000; ++i) {
		jun[i] = jun[i - 1] * i;
		jun[i] %= MOD;
		gyaku[i] = inv(jun[i]);
	}
	long long n, m;
	cin >> n >> m;
	long long ans = 0;
	long long tmp = 0;
	for (long long odd = m;; ++odd) {
		long long even = m - (odd - m) * 2LL;
		if (even < 0) break;
		tmp -= (n * no_limit(odd - m, n - 1)) % MOD;
		tmp += 2LL * MOD;
		tmp %= MOD;
		if (even > n) continue;
		long long geko = no_limit(odd, n);
		geko += tmp;
		geko += MOD;
		geko %= MOD;
		ans += geko * combination(n, even);
		ans %= MOD;
		if (n - 1 >= even) {
			ans += ((n * no_limit(odd - m, n - 1)) % MOD * combination(n - 1, even)) % MOD;
			ans %= MOD;
		}
	}
	cout << ans << endl;
	return 0;
}

#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
#define maxn 2010
#define maxv 4000010
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define pii pair<int, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define IL inline
#define ss system
using namespace std;

int n, k;
ll f[maxn][maxn], fac[maxv], ifac[maxv];
const ll p = 1e9 + 7;
template <class T> void read (T &x) {
  char ch = x = 0;
  bool fl = false;
  while (!isdigit(ch))
    fl |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = fl ? -x : x;
}
ll pw(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % p;
    a = a * a % p, b >>= 1;
  }
  return ret;
}
ll C(int a, int b) {
  return a < b || b < 0 ? 0 : fac[a] * ifac[b] % p * ifac[a - b] % p;
}
void init() {
  fac[0] = 1;
  for (int i = 1; i < maxv; i++) {
    fac[i] = fac[i - 1] * i % p;
  }
  ifac[maxv - 1] = pw(fac[maxv - 1], p - 2);
  for (int i = maxv - 2; ~i; i--) {
    ifac[i] = ifac[i + 1] * (i + 1) % p;
  }
}
int main() {
  read(n), read(k), init(), f[0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = !i; j <= i; j++) {
      if (i)
        f[i][j] = f[i - 1][j];
      f[i][j] = (f[i][j] + f[i][j - 1] * (n - j + 1) % p * C(n - i + (n - j + 1) * (k - 1) - 1, k - 2)) % p;
    }
  }
  printf("%d\n", f[n][n * (k > 1)]);
  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vector<ll>>

using namespace std;

const ll mod=1000000007;
const ll inf=LONG_LONG_MAX;
ll dx4[4]={1,0,-1,0};
ll dy4[4]={0,-1,0,1};
ll dx8[8]={1,0,-1,1,-1,1,0,-1};
ll dy8[8]={1,1,1,0,0,-1,-1,-1};

int main(){

  ll n;
  cin >> n;
  ll a=0;
  while(3*a<=n){
    a++;
  }
  cout << a-1 << endl;

}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N;
  cin >> N;
  cout << N/3 << endl;
  return 0;
}


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

int main()
{
	int month[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	int m,day,sumday;
    while(1){
		cin >> m; // 月
		if (m==0){ break;}
		cin >> day;  //日付
		for(int i=0;i<m;i++){
			day+=month[i];
		}
		switch(day%=7){
			case 1:
				cout<<"Thursday"<<endl;
				break;
			case 2:
				cout<<"Friday"<<endl;
				break;
			case 3:
				cout<<"Saturday"<<endl;
				break;
			case 4:
				cout<<"Sunday"<<endl;
				break;
			case 5:
				cout<<"Monday"<<endl;
				break;
			case 6:
				cout<<"Tuesday"<<endl;
				break;
			default:
				cout<<"Wednesday"<<endl;
				break;
		}

	}
	return 0;
}

#include <iostream>
#include <cstdio>
using namespace std;
string name[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int dayOfWeek(int y, int m, int d) {
    if(m == 1 || m == 2) {
        y--;
        m += 12;
    }
    return(y + y / 4 - y / 100 + y / 400 + (13*m + 8) / 5 + d) % 7;
}

int main(void) {
    int y = 2004, m, d;
    while(scanf("%d %d", &m, &d), m + d != 0) {
        cout << name[dayOfWeek(y, m, d)] << endl;
    }
    return(0);
}


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

int main() {
  int N;
  cin >> N;

  cout << (N + 110) / 111 * 111 << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int main( ) {
    //freopen("B.01.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vi a={111,222,333,444,555,666,777,888,999};
    int n;
    cin >> n;
    rep(i, 0, 9){
        if(n<=a[i]){
            cout << a[i] << endl;
            break;
        }
    }
    return 0;
}


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

class Cube{
public:
  int f[6];
  Cube(){}
  void roll_z(){roll(1,2,4,3);}
  void roll_y(){roll(0,2,5,3);}
  void roll_x(){roll(0,1,5,4);}
  void roll(int i,int j,int k, int l){
      int t=f[i];f[i]=f[j];f[j]=f[k];f[k]=f[l];f[l]=t;
  }
  void move(char ch){
      if(ch=='E')for(int i=0;i<3;i++)roll_y();
      if(ch=='W')roll_y();
      if(ch=='N')roll_x();
      if(ch=='S')for(int i=0;i<3;i++)roll_x();
  }
};

int main(){
    Cube c;
    string com;
    for(int i=0;i<6;i++){
        cin>>c.f[i];
    }
    cin>>com;
    for(int i=0;i<com.size();i++)c.move(com[i]);
    cout<<c.f[0]<<endl;
    return 0;
}

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

/* Articulation Points (Critical Routers) */
int timer = 0;
void getAP(unordered_map<int,vector<int>*>& adjacencyList, unordered_map<int,int>& d, unordered_map<int,int>& l, unordered_map<int,int>& p, unordered_map<int,bool>& visited, int* ap, int sv){
    visited[sv] = true;
    l[sv] = d[sv] = ++timer;
    int children = 0;
    for(int i=0;i<adjacencyList[sv]->size();i++){
        int n = adjacencyList[sv]->at(i);
        if(visited.count(n)!=0 && p[sv]!=n){
            l[sv] = min(l[sv],d[n]);
        }else if(visited.count(n)==0){
            p[n] = sv;
            children += 1;
            getAP(adjacencyList, d, l, p, visited, ap, n);
            l[sv] = min(l[sv],l[n]);
            if(p[sv]!=-1 && d[sv] <= l[n]){
                ap[sv]=1;
            }
        }
    }
    if(p[sv]==-1 && children>1){
        ap[sv] = 1;
    }
}

void getAP(unordered_map<int,vector<int>*>& adjacencyList){
    int n = adjacencyList.size();
    unordered_map<int,int> discoveryTime;
    unordered_map<int,int> lowestTime;
    unordered_map<int,int> parent;
    int* ap = new int[n];
    unordered_map<int,bool> visited;
    timer = 0;
    for(int i=0;i<n;i++){
        ap[i] = 0;
    }
    parent[0] = -1;
    getAP(adjacencyList,discoveryTime,lowestTime,parent,visited,ap,0);
    for(int i=0;i<n;i++){
        if(ap[i]==1){
            cout << i << endl;
        }
    }
}

int main() {
    int n, e;
    cin >> n >> e;
    unordered_map<int,vector<int>*> adjacencyList;
    for(int i=0;i<n;i++){
        vector<int>* v = new vector<int>;
        adjacencyList[i] = v;
    }
    for(int i=0;i<e;i++){
        int x,y;
        cin >> x >> y;
        adjacencyList[x]->push_back(y);
        adjacencyList[y]->push_back(x);
    }
    getAP(adjacencyList);
}


#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  if (A + B < C + D) {
    cout << "Right" << endl;
  } else if (A + B > C + D) {
    cout << "Left" << endl;
  } else {
    cout << "Balanced" << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a = a + b - c - d;
    if (a > 0) cout << "Left";
    if (a < 0) cout << "Right";
    if (a == 0) cout << "Balanced";
}


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

int main(){
  int N;
  int p[100], order[100];
  int a,b,c,d;
  vector<int> v;

  cin >> N;
  for( int i=0;i<N;i++ ) p[i]=0;
  for( int i=0,len=N*(N-1)/2;i<len;i++ ){
    cin >>a >> b >> c >> d;
    //    cout << a<<b<<c<<d<<endl;
    a--; b--;
    if( c==d ){ // テつ暗δクテつづつォテつ陛つェテつづつッ
      p[a]++; p[b]++;
    }else if( c>d )
      p[a]+=3;
    else
      p[b]+=3;
  }
  /*
  for( int i=0;i<N;i++ )
    cout << "p "<<p[i] << endl;
  */
  for( int i=1;i<=N; ){ // テつ湘つつ暗δ甘つεつ凝つーテつプ
    //    cout << "No."<<i<<endl;
    v.clear();
    int max = -1;
    for( int j=0;j<N;j++ ){
      if( p[j]>max ){
	max=p[j];
	//	cout << " max="<<j<<endl;
	v.clear();
	v.push_back( j );
      } else if( p[j]==max ){
	v.push_back( j );
      }
    }
    for( int j=0;j<v.size();j++ ){
      order[v[j]] = i; p[v[j]] = -10;
    }
    i+=v.size();
  }
  for( int i=0;i<N;i++ )
    cout << order[i] << endl;

  return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<ll> VLL;
typedef vector<PLL> VP;
const static int INF = 1000000000;
const static int MOD = 1000000007;
const int dx[4]={1, 0, -1, 0};
const int dy[4]={0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i,n) for (ll i=0; i<(ll)(n); i++)
#define repd(i,n) for (ll i=n-1; i>=0; i--)
#define rept(i,m,n) for(ll i=m; i<n; i++)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define PF push_front
#define PB push_back
#define SORT(V) sort((V).begin(), (V).end())
#define RVERSE(V) reverse((V).begin(), (V).end())
#define paired make_pair
#define PRINT(V) for(auto v : (V)) cout << v << " "
//charを整数に
int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; }
// 累積和 for (int i = 0; i < N; ++i) s[i+1] = s[i] + a[i];
void cum_sum(int N,vector<double> a, vector<double> &s){ for(int i=0; i<N; i++){ s[i+1]=s[i]+a[i];}}
//ユークリッドの控除法
ll gcd(ll a,ll b){
    if(b == 0) return a;
    return gcd(b,a%b);
}
//最小公倍数
ll lcm(ll a,ll b){
    ll g = gcd(a,b);
    return a / g * b; // Be careful not to overflow
}
//素数判定
bool is_prime(long long n) {
    if (n <= 1) return false;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p == 0) return false;
    }
    return true;
}
int getdigit(ll num){
    unsigned digit=0;
    while(num!=0){
        num /= 10;
        digit++;
    }
    return digit;
}
//空白文字も入力 getline(cin, S);
//桁数指定 setprecision

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll N, A, B;
    cin >> N >> A >> B;
    VLL X(N);
    rep(i,N){
        cin >> X[i];
    }
    VLL dif(N);
    rep(i,N-1){
        dif[i]=X[i+1]-X[i];
        dif[i]*=A;
    }
    ll ans=0;
    rep(i,N-1){
        if(dif[i]>=B){
            ans+=B;
        }
        else{
            ans+=dif[i];
        }
    }
    cout << ans << endl;
}

#include <iostream>

using namespace std;

int main() {
    int n;
    long long a, b;
    long long xi, x;
    long long ans=0;
    cin >> n >> a >> b;
    cin >> x;
    for (int i =0; i < n-1; ++i) {
        cin >> xi;
        if (a*(xi - x) > b)
            ans += b;
        else
            ans += a*(xi-x);
        x = xi;
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = (1 << 17) + 10;
const int mod = 1e9 + 7;

int n, x, y, z, ans;
int lim, Haiku, dp[45][N];

ll PowMod(ll a, ll b) {
	ll res = 1 % mod;
	while(b) {
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}

int main() {
	cin >> n >> x >> y >> z;
	dp[0][0] = 1; lim = (1 << (x + y + z)) - 1;
	Haiku = (1 << (x - 1)) + (1 << (x + y - 1)) + (1 << (x + y + z - 1));
	// cout << n << " " << lim << endl;
	for(int i = 1;i <= n; ++i) {
		for(int S = 0;S <= lim; ++S) {
			if(!dp[i - 1][S]) continue;
			for(int j = 1;j <= 10; ++j) {
				int T = (((S << j) | (1 << (j - 1)))) & lim;
				if((T & Haiku) == Haiku) continue;
				dp[i][T] += dp[i - 1][S];
				dp[i][T] %= mod;
			}
		}
	}
	for(int i = 0;i <= lim; ++i)
		ans = (ans + dp[n][i]) % mod;
	ans = (PowMod(10, n) - ans + mod) % mod;
	cout << ans << endl;
	return 0;
}

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % MOD;
    (a *= a) %= MOD;
    n >>= 1;
  }
  return res;
}

const ll maxf = 10000000;
ll fact[maxf+5];
ll ifact[maxf+5];

void fact_init(){
  fact[0] = 1;
  for(ll i=1; i<=maxf; i++) fact[i] = (fact[i-1] * i) % MOD;
  ifact[maxf]=modpow(fact[maxf],MOD-2);
  for(ll i=maxf; i>=1; i--) ifact[i-1] = (ifact[i] * i) % MOD;
}

ll comb(ll n, ll r){
  if(n<r || n<0 || r<0) return 0;
  if(fact[0] == 0) fact_init();
  return ((fact[n] * ifact[n-r]) % MOD * ifact[r]) % MOD;
}

ll naive_comb(ll n, ll r){
  ll res = 1;
  for (ll i = n; i > n - r; i--) res = res * i % MOD;
  for (ll i = 1; i <= r; i++) res = (res * modpow(i, MOD-2)) % MOD;
  return res;
}

// 参考: https://drken1215.hatenablog.com/entry/2019/06/15/111500

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll n,m,k; cin>>n>>m>>k;
  ll sum=0;
  // y軸方向の取りうる距離
  for(ll i=0; i<=n-1; i++){
    // x軸方向の取りうる距離
    for(ll j=0; j<=m-1; j++){
      // y軸方向の距離がiのとき、選べるマスの組数はn-i通り (x軸も同様に考える)
      // (配置する駒同士は区別されない)
      ll tmp=(n-i)*(m-j)%MOD*(i+j)%MOD;
      // i,jがともに0でないならば、そのような駒の配置の仕方は2パターン存在する
      // 例えば (x1,y1), (x2,y2) が i,j を成すなら、(x1,y2), (x2,y1) も同様に i,j を成す
      // 互いに区別される配置で同じ i,j の組合せがもう一組作れるので、i,j がともに0でない場合は2倍する
      if(i!=0 && j!=0) (tmp*=2)%=MOD;
      (sum+=tmp)%=MOD;
    }
  }
  ll ans=(sum*comb(n*m-2,k-2))%MOD;
  cout<<ans<<endl;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <cstdio>
#include <new>
#include <vector>
#include <cstdlib>
#include <string>
#include <set>
#include <tuple>
#include<map>

using namespace std;
int main() {
	string A, B, C;
	cin >> A >> B >> C;

	bool ans = true;

	if (A.at(A.size() - 1) != B.at(0)) ans = false;
	if (B.at(B.size() - 1) != C.at(0)) ans = false;

	if (ans) cout << "YES" << endl;
	else cout << "NO" << endl;

}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int n,k;

int f(int x,vector<int>v){
  int ans=0;
  for(int i=min(x-1,n-1);i>=0;i--){
    int res=0;
    int p=x-1-i;
    vector<int> t(i+1);
    rep(j,i+1) t[j]=v[j];
    sort(t.begin(),t.end());
    rep(j,i+1){
      if(p>0&&t[j]<0){
        p--;
      }else res+=t[j];
    }
    ans=max(ans,res);
  }
  return ans;
}

int main(){
  cin>>n>>k;
  vector<int> v(n);
  rep(i,n) cin>>v[i];

  vector<int> u(n);
  rep(i,n) u[i]=v[i];
  reverse(u.begin(),u.end());

  int ans=0;
  if(k>=n){
    ans=f(k,u);
  }else{
    rep(i,k){
   	  ans=max(f(i,u)+f(k-i,v),ans);
    }
  }
  cout<<ans<<endl;
}

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

int main(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    int cnt=0;
    for(int i=1; i<n; i++){
        if(s.at(i) == 'E') cnt++;
    }
    vector<int> change(n);
    change.at(0) = cnt;
    for(int i=1; i<n; i++){
        change.at(i) = change.at(i-1);
        if(s.at(i-1) == 'W') change.at(i)++;
        if(s.at(i) == 'E') change.at(i)--;
    }
    int ans=change.at(0);
    for(int i=0; i<n; i++){
        ans = min(ans, change.at(i));
    }
    cout << ans << endl;
}


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

using namespace std;

int sum, n;
int a[101], odd, evn, pos[101];
int b[101], cnt;

void work()
{
	if(n == 1)
	{
		if(a[1] == 1) b[++cnt] = 1;
		else b[++cnt] = 1, b[++cnt] = a[1] - 1;
		return;
	}
	if(a[1] & 1) b[++cnt] = a[1] + 1;
	else b[++cnt] = 1, b[++cnt] = a[1];
	for(int i=2; i<n; i++) b[++cnt] = a[i];
	if(a[n]==1) return;
	else b[++cnt] = a[n]-1;
}

int main()
{
	scanf("%d%d", &sum, &n);
	for(int i=1; i<=n; i++) scanf("%d", &a[i]);
	for(int i=1; i<=n; i++)
		if(a[i] & 1) pos[++odd] = i;
		else evn++;
	if(odd > 2) printf("Impossible\n"), exit(0);
	if(odd == 0) work();
	else if(odd == 1) swap(a[pos[1]], a[1]), work();
	else swap(a[pos[1]], a[1]), swap(a[pos[2]], a[n]), work();
	for(int i=1; i<=n; i++) printf("%d ", a[i]); putchar('\n');
	printf("%d\n", cnt);
	for(int i=1; i<=cnt; i++) printf("%d ", b[i]); putchar('\n');
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using T = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout<<(v)<<"\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;

//dp[i][j]はそれぞれiグラム、jグラム買うときの最小価格
vector<vector<ll>> dp(500, vector<ll>(500, INF));

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll A;
    cin >> A;
    ll B;
    cin >> B;
    vector<ll> X(N,0);
    for(ll i=0;i<N;++i)cin>>X[i];
    ll ans=0;
    for(ll i=1;i<N;++i){
        ans+=min(A*(X[i]-X[i-1]),B);
    }
    prt(ans)

    return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp>
//#include <functional> // for less
#define PI acos(-1.0)
#define eps 1e-9
#define ll long long
#define ull unsigned long long
#define nl printf("\n")
#define sf scanf
#define pf printf
#define INF (ll)1e15
#define mod 1000000007

///#define isValid(n,m,nx,ny)nx>=0&&nx<n&&ny>=0&&ny<m
///int kx[]={1, 1, -1, -1, 2, 2, -2, -2}; //knight moves
///int ky[]={2,-2,  2, -2, 1,-1,  1, -1}; //knight moves
///int dx[]={1,-1,0,0,1,-1,1,-1};//eight direction
///int dy[]={0,0,1,-1,1,-1,-1,1};//eight direction

///using namespace __gnu_pbds;
using namespace std;

///inline double toRad(double deg){return deg*PI/180;}
///inline double toDeg(double rad){return rad*180/PI;}
///typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;

const int S=123456;
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
    #endif
    ll n,a,b;
    while(sf("%lld %lld %lld",&n,&a,&b)==3)
    {
        ll ara[n+5];
        for(int i=0;i<n;i++)sf("%lld",&ara[i]);

        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ll dis=ara[i+1]-ara[i];
            if(dis*a<=b)ans+=(dis*a);
            else ans+=b;
        }
        pf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int a[maxn],b[maxn];
int main(){
    int n;
    vector<int>p;
    cin>>n;
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    for(int i=1;i<=n;++i)scanf("%d",&b[i]);
    reverse(b+1,b+n+1);
    int h1=n,q1=1,mx;
    for(int i=1;i<=n;++i){
        if(a[i]==b[i]){
            mx=a[i];
            h1=i;
            while(a[i]==b[i]){
                i++;
            }
            q1=i-1;
            break;
        }
    }
    for(int i=1;i<=n;++i){
        if(a[i]!=mx&&b[i]!=mx&&h1<=q1){
            swap(b[i],b[h1]);
            h1++;
        }
    }
    if(h1<=q1){ puts("No");return 0;}
    puts("Yes");
    for(int i=1;i<n;++i)
        printf("%d ",b[i]);
    printf("%d\n",b[n]);
}

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

typedef long long lint;
typedef long double louble;

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;

	char buf[bufl],*s=buf,*t=buf;

	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}

	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
}
using ae86::ty;

const int _ = 100007;
const char *YES = "Yes" , *NO = "No";

inline void failure(){puts(NO);exit(0);}

struct tla
{
#define lb(a) ((a)&(-(a)))
	int t[_];
	void cls(){memset(t,0,sizeof(t));}
	inline void add(int x,int d){while(x)t[x]+=d,x-=lb(x);}
	inline int sum(int x){int d=0;while(x<_)d+=t[x],x+=lb(x);return d;}
#undef lb
}t;

int n,val[_][3],tar[_],ic[2]={0},fc[2]={0};

int main()
{
	n=ty();
	for(int i=0;i<3;i++)for(int j=1;j<=n;j++)val[j][i]=ty();

	for(int i=1;i<=n;i++)
	{
		int loc=(val[i][0]-1)/3+1;
		if((loc-i)&1)failure();
		tar[loc]=i;
		int a=loc*3-2,b=a+1,c=b+1;
		if(val[i][0]==a && val[i][1]==b && val[i][2]==c)continue;
		if(val[i][0]==c && val[i][1]==b && val[i][2]==a)ic[i&1]^=1;
		else failure();
	}

	t.cls();
	for(int i=1,x;i<=n;i+=2)x=tar[i]+t.sum(tar[i])*2,fc[1]^=((x-i)/2)&1,t.add(tar[i],1);
	t.cls();
	for(int i=2,x;i<=n;i+=2)x=tar[i]+t.sum(tar[i])*2,fc[0]^=((x-i)/2)&1,t.add(tar[i],1);

	puts((ic[1]==fc[0] && ic[0]==fc[1])?YES:NO);

	return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

bool check(int n){
  double rn=sqrt(n);
  if(rn==(int)rn)return true;
  else return false;
}

int main(){
  int n;
  cin >> n;
  rep(i,n){
    if(check(n-i)){
      cout << n-i << endl;
      return 0;
    }
  }
  cout << 1 << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
using P = pair <int, int>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
ll gcd(ll a,ll b) {return  b ? gcd(b,a%b) : a;}
const long long INF = 1LL << 60;
const int mod = 1000000007;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int main() {
     string x;
     cin >> x;
     int n = x.size();
     stack<char> st;
     rep(i,n){
          if (st.empty()) st.push(x[i]);
          else if(st.top()=='S'){
               if(x[i]=='T'){
                    st.pop();
               }
               else st.push(x[i]);
          }
          else st.push(x[i]);
     }
     cout << st.size() <<endl;
}


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

int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    mp[x] += 1;
  }
  vector<int> ans;
  for (auto p : mp) {
    if (p.second >= 4) {
      ans.push_back(p.first);
      ans.push_back(p.first);
    } else if (p.second >= 2) {
      ans.push_back(p.first);
    }
  }
  sort(ans.begin(), ans.end(), greater<int>());
  if (ans.size() < 2) {
    cout << 0 << endl;
  } else {
    cout << 1LL * ans[0] * ans[1] << endl;
  }
  return 0;
}

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

#include<bits/stdc++.h>

using namespace std;

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

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

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int mod = 1e9 + 7;
const char nl = '\n';

void solve() {
    int n;
    cin >> n;
    int ara[n];
    for(int i = 0; i < n; ++i){
        cin >> ara[i];
    }
    sort(ara, ara + n);
    ll a(0), b(0);
    ll len = n - 1;
    char c(1);
    while(len){
        if(ara[len] == ara[len - 1]){
            if(c){
                a = ara[len];
                c = 0;
            }
            else{
                b = ara[len];
                break;
            }
            len--;
        }
        len--;
    }
    cout << a * b << nl;
}

int main() {
#ifndef ONLINE_JUDGE
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}


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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.141592653589793238462643383279;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

ll dp[1<<16], score[1<<16];
ll a[16][16];

ll rec(int S){
    if(dp[S] != -1) return dp[S];
    if(S == 0) return dp[S] = 0;

    ll ret = score[S];
    for(int T=S; T>0; T=T-1&S){
        if(S==T) continue;
        chmax(ret, rec(T) + rec(S^T));
    }

    return dp[S] = ret;
}

int main(void){
    int N; cin >> N; REP(i,1<<N) dp[i] = -1;
    REP(i,N)REP(j,N) cin >> a[i][j];

    REP(S,1<<N){
        REP(i,N)FOR(j,i+1,N) if(S>>i & S>>j & 1) score[S] += a[i][j];
    }

    ll ans = rec((1<<N)-1);
    cout << ans << en;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 100005;
ll val[1<<16];
ll dp[1<<16];
ll solve(int mask) {
	if (~dp[mask]) {
		return dp[mask];
	}
	ll mx = -1e18;
	int low = mask & (mask);
	for (int s = mask ; s; s = (s - 1) & mask) {
		if (!(s & low))continue;
		mx = max(mx, solve(mask ^ s) + val[s]);
	}
	return dp[mask] = mx;
}
void go() {
	int n;
	cin >> n;
	f(n) {
		fr(j,0,n) {
			int x;
			cin >> x;
			if (i <= j)continue;
			for (int k = 0 ; k < (1<<n) ; k++) {
				if ((k & (1<< i)) && (k & (1<<j))) {
					val[k] += x;
				}
			}
		}
	}
	memset(dp, -1, sizeof(dp));
	dp[0] = 0;
	cout << solve((1 << n) - 1) << '\n';
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        go();
    }

}


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

ll powll(ll b, int e) {
    ll result = 1;
    while (e) {
        if (e & 1) {
            result *= b;
        }
        b *= b;
        e >>= 1;
    }
    return result;
}

bool is_palindrome(const string &s) {
    int N = s.size();
    int i = 0;
    int j = N - 1;
    while (i < j) {
        if (s[i++] != s[j--]) {
            return false;
        }
    }
    return true;
}

ll count_palindromes(string s, bool is_inner) {

    int L = s.size();

    if (L == 0) {
        return 1;
    }

    if (L == 1) {
        int n = stoi(s);
        if (is_inner) {
            return n + 1;
        } else {
            return n;
        }
    }

    string t = s.substr(1, L - 2);
    int a = stoi(s.substr(0, 1));
    int b = stoi(s.substr(L - 1, 1));

    ll result = 0;

    if (!is_inner) {
        for (int k = 1; k < L; ++k) {
            result += 9 * powll(10, (k - 1) / 2);
        }
    }

    if (is_inner) {
        result += a * powll(10, (L - 1) / 2);
    } else {
        result += (a - 1) * powll(10, (L - 1) / 2);
    }

    if (is_inner || a > 0) {
        result += count_palindromes(t, true);
        if (a > b) {
             if (is_palindrome(t)) {
                --result;
            }
        }
    }

    return result;
}

ll count_palindromes(ll n) {
    if (n) {
        return count_palindromes(to_string(n), false);
    } else {
        return 0;
    }
}

int main() {

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

    int A, B;
    cin >> A >> B;

    int result = count_palindromes(B) - count_palindromes(A - 1);
    cout << result << endl;

    return 0;
}

//Author- Vaibhav Singh

#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
*/
using namespace std;
#define ll long long int
#define YES cout<<"YES"<<"\n";
#define NO cout<<"NO"<<"\n";
#define ld     long double
#define yes cout<<"yes"<<"\n";
#define no cout<<"no"<<"\n";
#define No cout<<"No"<<"\n";
#define Yes cout<<"Yes"<<"\n";
#define f(i,a) for(i=0;i<a;i++)
#define fo(i,a) for(i=1;i<=a;i++)
#define fa(i,a) for(auto i:a)
#define r(i,a) for(auto i=a.rbegin();i!=a.rend();i++)
#define en cout<<"\n";
#define ull unsigned long long int
#define o(x) cout<<x<<"\n";
#define o1(x) cout<<x<<" ";
#define pb push_back
#define F first
#define in insert
#define mp make_pair
#define S second
#define pre(n) cout<<fixed<<setprecision(n);
#define      gcd(a,b)    __gcd(a,b)
#define bs            binary_search
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t;while(t-->0)
const ll Mod = 998244353;
#define mod 1000000007
#define pi 3.14159265358979323846
#define all(x) x.begin(),x.end()
#define re return 0;
// *#######################################################################################*

/*
bool isp(ll n)
{
    ll i;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            return false;
        }

    }
}
  */

  ll powerm(ll x, unsigned ll y, ll p)
{
    ll res = 1;

    x = x % p;

    if (x == 0) return 0;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
  long long lcm(long long a, long long b){//?????????
   return a * b / gcd(a, b);
}

/*
ll po(ll z,ll z1)
{
    ll rer=0;
    rer=     ((z*(z-1))/2+(z1*(z1-1))/2);
    return rer;

}
*/
/*
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
*/
/*
bool is_prime(ll n){
    for(ll i = 2;i*i<=n;i++){
        if(n%i == 0) return false;
    }
    return true;
}
*/
int inv(int a){
    return powerm(a,mod-2,mod);
}

ll const N=1e5;
ll fact[N], invfact[N];

void precompute(){
    fact[0] = 1;
    for(int i=1;i<N;i++) fact[i] = (fact[i-1]*i)%mod;
    invfact[N-1] = inv(fact[N-1])%mod;
    for(int i=N-2;i>=0;i--) invfact[i] = (invfact[i+1]*(i+1))%mod;
}

ll ncr(ll n, ll r){
    if(n<r) return 0;
    return (((fact[n]*invfact[r])%mod)*invfact[n-r])%mod;
}
ll di(ll n)
{ ll c=0;
    string s=to_string(n);
    if(s[0]==s[s.size()-1]&&s[1]==s[s.size()-2])
    {
        c=1;
    }
    return c;
}

//*88888*********5555555555555555******88888888$$$$$$$$$$******!!!!!!!!!!

       int main()
       {
           //????????
    fast

         ll a,b,c=0,i;
         cin>>a>>b;
         string s;
         for(i=a;i<=b;i++)
         {
             c+=di(i);
         }
         o(c)

//?????????
re}


#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

vector<string> dp(10001," ");
vector<pair<int,char>> a(10);

string string_max(int i,int j){
	string s = dp[i];
	string t = dp[i + a[j].fs];
	char c = a[j].sc;
	if(s == "0"){
		s = c;
	}
	else{
		auto index = lower_bound(RALL(s),c);
		s.insert(index.base(),c);
	}
	string ans;
	// cout << "s" << s << " t " << t << endl;
	// cout << (s > t) << endl;
	if (t == " "){
		ans =  s;
	}
	else{
		if(s.size() > t.size()){
			ans = s;
		}
		else if(s > t){
			ans =  s;
		}
		else{
			ans =  t;
		}
	}
	return ans;
}

int main(){
	int n,m;cin>>n>>m;
	a.resize(m);
	int match[9] = {2,5,5,4,5,6,3,7,6};
	for(int i = 0; i < m; i++) {
		char x;cin>>x;
		a[i] = mp(match[x - '0' - 1],x);
	}
	sort(ALL(a));
	// for(int i = 0; i < m; i++) {
	// 	cout << a[i].fs << endl;
	// }

	dp[0] = "0";
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(dp[i] != " " && i + a[j].fs <= n){
				dp[i + a[j].fs] = string_max(i,j);
				// cout << "i " << i << " j " << j << endl;

			}
			// for(int i = 0; i <= n; i++) {
			// 	cout << dp[i] << " ";
			// }
			// cout << endl;
		}
	}
	// for(int i = 0; i <= n; i++) {
	// 	cout << dp[n] << endl;
	// }
	cout << dp[n] << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define MAX 2000000
#define MOD 1000000007

int main(){
  int N,M;
  cin>>N>>M;
  vector<int> A(M);
  for(int i=0;i<M;i++){
    cin>>A.at(i);
  }
  sort(A.begin(),A.end());
  reverse(A.begin(),A.end());
  int match[9]={2,5,5,4,5,6,3,7,6};
  vector<int> dp(N+1,-10000000);
  dp.at(0)=0;
  for(int i=1;i<=N;i++){
    for(int j=0;j<M;j++){
      if(i-match[A.at(j)-1]<0){
        continue;
      }
      dp.at(i)=max(dp.at(i),dp.at(i-match[A.at(j)-1])+1);
    }
  }
  int i=N;
  while(i>0){
    for(int j=0;j<M;j++){
      if(i-match[A.at(j)-1]<0){
        continue;
      }
      if(dp.at(i-match[A.at(j)-1])==dp.at(i)-1){
        cout<<A.at(j);
        i=i-match[A.at(j)-1];
        break;
      }
    }
  }
  cout<<endl;
}


#include<iostream>
using namespace std;

int main(){
  int x, y, z;
  cin >> x >> y >> z;
  swap(x, y);
  swap(x, z);
  cout << x << " " << y << " " << z;
  return 0;
}

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

const int N = 220000;

vector<int> con[N];

int root[N];
inline int find_root(int u) {
	if (u == root[u]) return root[u];
	return root[u] = find_root(root[u]);
}

int vis[N];
vector<int> cur;
void dfs(int u) {
	vis[u] = 1;
	cur.push_back(u);
	for (int v : con[u]) if (!vis[v]) dfs(v);
}

int cnt[N];
int ans[N];

int main() {

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

	int n, m, k; cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) root[i] = i;
	while (m--) {
		int u, v; cin >> u >> v;
		int a = find_root(u), b = find_root(v);
		if (a == b) continue;
		root[a] = b;
	}
	while (k--) {
		int u, v; cin >> u >> v;
		con[u].push_back(v), con[v].push_back(u);
	}
	for (int i = 1; i <= n; i++) if (!vis[i]) {
		cur.clear();
		dfs(i);
		for (auto u : cur) cnt[find_root(u)] = 0;
		for (auto u : cur) cnt[find_root(u)]++;
		for (auto u : cur) ans[u] = cnt[find_root(u)];
	}
	for (int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n];

	return 0;
}


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

int main()
{
	string s1,s2,s3;
	cin>>s1>>s2>>s3;
	ll a = 0,b = 0,c = 0;
	string ch = "a";
	while(1)
	{
		if(ch == "a")
		{
			if(s1.size()>a)
			{
				ch = s1[a];
				a++;
			}
			else
			{
				cout<<"A"<<endl;
				return 0;
			}
		}
				if(ch == "b")
		{
			if(s2.size()>b)
			{
				ch = s2[b];
				b++;
			}
			else
			{
				cout<<"B"<<endl;
				return 0;
			}
		}
				if(ch == "c")
		{
			if(s3.size()>c)
			{
				ch = s3[c];
				c++;
			}
			else
			{
				cout<<"C"<<endl;
				return 0;
			}
		}
	}
}

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

//#include"atcoder/all"

#ifdef _MSC_VER
#include <intrin.h>

#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;

void solve() {
	int A, B, C, D; cin >> A >> B >> C >> D;
	int K; cin >> K;
	int t1 = A * 60 + B;
	int t2 = C * 60 + D;
	int d = t2 - t1;
	print(d - K);
}

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

	//int q;
	//cin >> q;
	//while (q--)
	solve();

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
const ll mod = 1000000007;

vector<int> to[500000];
int tab[1005][1005];

int toID(int i, int j) {
    if (i>j) swap(i, j);
    return tab[i][j];
}

int dp[500000];
bool used[500000];
bool calculated[500000];

int dfs(int x) {
    if (used[x]) {
        if (!calculated[x]) return -1;
        return dp[x];
    }
    used[x] = true;
    int now = 1;
    for (int e: to[x]) {
        int res = dfs(e);
        if (res<0) return -1;
        now = max(res+1, now);
    }
    dp[x] = now;
    calculated[x] = true;
    return dp[x];
}

int main(){
    int n;
    cin >> n;
    vector<vector<int>> a(n, vector<int>(n-1));
    rep(i, n) rep(j, n-1) {
        cin >> a[i][j];
        a[i][j]--;
    }
    int id = 0;
    rep(i, n) rep(j, n) {
        if (i<j) tab[i][j] = id++;
    }
    rep(i, n) {
        rep(j, n-1) {
            a[i][j] = toID(i, a[i][j]);
        }
        rep(j, n-2) {
            to[a[i][j]].push_back(a[i][j+1]);
        }
    }

    int ans = 0;
    rep(i, id) {
        int res = dfs(i);
        ans = max(ans, res);
        if (res==-1) {
            cout << "-1" << endl;
            return 0;
        }
    }

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

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
    int n; cin >> n;
    vector<vector<int>> a(n,vector<int>(n));
    vector<int> now(n,0);
    rep(i,n) rep(j,n-1) cin >> a[i][j], a[i][j]--;
    int total = n * (n-1) / 2;
    int ans = 0;
    queue<int> q;
    set<int> se;
    rep(i,n){
        if(a[a[i][0]][0] == i){
            if(se.find(i) == se.end() && se.find(a[i][0]) == se.end()){
                q.push(i);
                se.insert(i); se.insert(a[i][0]);
            }
        }
    }
    while(total > 0){
        se.clear();
        int m = q.size();
        total -= m;
        vector<int> wait;
        rep(j,m){
            int u = q.front(); q.pop();
            int v = a[u][now[u]];
            wait.push_back(u);
            wait.push_back(v);
            now[u]++;
            now[v]++;
        }
        for(auto u : wait){
            if(se.find(u) != se.end()) continue;
            if(now[u] == n-1) continue;
            int v = a[u][now[u]];
            if(se.find(v) != se.end()) continue;
            if(now[v] == n-1) continue;
            if(a[v][now[v]] != u) continue;
            q.push(u);
            se.insert(u);
            se.insert(v);
        }
        if(!m){
            puts("-1");
            return 0;
        }
        ans++;
    }
    cout << ans << "\n";
}


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

using namespace std;

int main() {

	int n;
	int kaisu;
	int team[100][3] = {0};
	int teamn[100];
	int a,b,c,d;
	int kari;
	int juni = 1;
	int counter = 1;

	cin >> n;
	kaisu = n * (n - 1) / 2;

	for (int i = 0; i < n; i++) {
		team[i][0] = i;
	}

	for (int i = 0; i < kaisu; i++) {
		cin >> a >> b >> c >> d;
		if (c > d) {
			team[a-1][1] += 3;
		} else if ( c < d) {
			team[b-1][1] += 3;
		} else {
			team[a-1][1] += 1;
			team[b-1][1] += 1;
		}
	}

	for (int i = n-1; i > 0; i--) {
		for (int j = 0; j < i; j++) {
			if (team[j][1] < team[j+1][1]) {
				for (int k = 0; k < 2; k++) {
					kari = team[j][k];
					team[j][k] = team[j+1][k];
					team[j+1][k] = kari;
				}
			}
		}
	}

	for (int i = 0; i < n; i++) {
		team[i][2] = juni;
		if (team[i][1] > team[i+1][1]) {
			juni += counter;
			counter = 1;
		} else {
			counter++;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (team[j][0] == i) {
				cout << team[j][2] << endl;
				break;
			}
		}
	}
	return 0;
}

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

typedef pair<int,int> P;
const int MAX = 120;

int maze[MAX][MAX];
bool come[MAX][MAX];
P s,g;
int W,H,c;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};

void init(){
  memset(come,false,sizeof(come));
  memset(maze,0,sizeof(maze));
}

void input_B(){
  int n;
  cin >> n;
  while(n--){
    int c,d,x,y;
    cin >> c >> d >> x >> y;
    if(d){
      for(int i = y; i < y+4; i++)
	for(int j = x; j < x+2; j++)
	  maze[i][j] = c;
    }else{
      for(int i = y; i < y+2; i++)
	for(int j = x; j < x+4; j++)
	  maze[i][j] = c;
    }
  }
}

void solve(P p){

  if(come[p.second][p.first] == false){
    come[p.second][p.first] = true;
    for(int i = 0; i < 4; i++)
      if(0 < p.first+dx[i] && p.first+dx[i] <= W &&
	 0 < p.second+dy[i] && p.second+dy[i] <= H &&
	 maze[p.second+dy[i]][p.first+dx[i]] == c) solve(P(p.first+dx[i],p.second+dy[i]));
  }
}

void print(){
  for(int i = 1; i < H; i++){
    for(int j = 1; j < W; j++)// cout << come[i][j];
      cout << maze[i][j];
    cout << endl;
  }

}

int main(){
  while(cin >> W >> H && W+H){
    init();
    cin >> s.first >> s.second;
    cin >> g.first >> g.second;
    input_B();
    c = maze[s.second][s.first];
    solve(s);
    //print();
    if(come[g.second][g.first] && c) cout << "OK" << endl;
    else cout << "NG" << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i,n) for(int i=n; i>-1; --i)
#define ALL(a) (a).begin(),(a).end()
#define FILL(a,n,x); REP(i,(n)){ (a)[i]=(x); }
#define CINA(a,n); REP(i,(n)){ cin >> (a)[i]; }
#define FILL2(a,n,m,x); REP(i,(n)){ REP(j,(m)){(a)[i][j]=(x);} }
#define CINA2(a,n,m); REP(i,(n)){ REP(j,(m)){cin >> (a)[i][j];} }
#define Liny "Yes\n"
#define Linn "No\n"
#define LINY "YES\n"
#define LINN "NO\n"

//cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x){ if(x<10){return 1;} else{return keta(x/10) + 1;}}
int keta_wa(ll x){ if(x<10){return x;} else{return keta_wa(x/10) + x%10;} }
int ctoi(char c){ return ( (c>='0' && c<='9')? c - '0': 0 );}
int __stoi(string s){ return atoi(s.c_str()); }
ll sum(ll a[],ll N){ return accumulate(a,a+N,0LL);}
ll gcd(ll a,ll b){if(a<b)swap(a,b); return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){if(a<b){swap(a,b);} return a/gcd(a,b)*b;}
template<class T> void chmax(T& a, T b){ if(a<b){a=b;} }
template<class T> void chmin(T& a, T b){ if(a>b){a=b;} }
template<class T> bool isIn(T a,vector<T> v){ for(T x:v){ if(a==x){return true;}} return false;}
string strReplace(string s,string target, string replacement){
    if (!target.empty()) {
      std::string::size_type pos = 0;
      while ((pos = s.find(target, pos)) != std::string::npos) {
        s.replace(pos, target.length(), replacement);
        pos += replacement.length();
      }
    }
    return s;
}
const ll MOD = 1e9+7;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair

bool DEBUG = 0;

template<class T> void dprint(T s){
  if(DEBUG){ cout << s << "\n"; }
}

int main(int argc,char* argv[]){
  if(argc>1 && argv[1][0]=='D'){ DEBUG=1;}
  dprint("start");

  ll N; cin>>N;
  ll a[N]; CINA(a,N); reverse(a,a+N);
  vector<ll> v;
  v.push_back(a[0]);
  for(int i=1;i<N;++i){
    int m = v.size();
    if(a[i]>=v[m-1]){
      v.push_back(a[i]);
    }else{
      auto x = upper_bound(ALL(v),a[i]);
      while(*x==a[i]){
        ++x;
      }
      *x = a[i];
    }
  }
  if(DEBUG){ for(auto x:v){ cout << x << " ";} cout << "\n"; }
  cout << v.size() << "\n";
}

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

//PointBlank's code (⌐■_■)

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

ll power(ll x, ll y);
#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitcount(n) __builtin_popcountll(n)
#define in_edges(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_edges2(M) repp(I, 1, M){int A, B; cin >> A >> B; v[A].pb(B);}
#define in_edges3(M) repp(I, 1, M){int A, B, C; cin >> A >> B >> C; v[A].pb(B, C), v[B].pb(A, C);}
#define endl "\n"

const ll MOD = 1000000007;

int main() //PointBlank's code ¯\_(ツ)_/¯
{
    int n;
    cin >> n;

    multiset<int> st;
    int res = 0;
    int inp;

    rep(i, 0, n){
        cin >> inp;
        auto it = st.UB(-inp);
        if(it == st.end()){
            res++;
            st.insert(-inp);
        }else{
            st.erase(it);
            st.insert(-inp);
        }
    }

    cout << res;
}

ll power(ll x, ll y)
{
    ll res = 1; x %= MOD;
    while(y > 0){if(y & 1) res = (res*x) % MOD; y = y>>1, x = (x*x) % MOD;}
    return res;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector<int> to[MAXN];
vector<int> d1(MAXN, -1), d2(MAXN, -1);

void dfs(int u, int path, vector<int> &d)
{
  if (d[u] != -1)
  {
    return;
  }
  d[u] = path;
  for (auto v : to[u])
  {
    dfs(v, path + 1, d);
  }
}

int main()
{
  int N, u, v, a, b;
  cin >> N >> u >> v;
  u--, v--;
  for (int i = 0; i < N - 1; i++)
  {
    cin >> a >> b;
    a--, b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(u, 0, d1);
  dfs(v, 0, d2);
  int ans = 0, same = (1 << 30);
  for (int i = 0; i < N; i++)
  {
    if (d2[i] < d1[i])
      continue;
    if (d2[i] == d1[i])
    {
      same = min(d2[i], same);
    }
    else
    {
      ans = max(ans, d2[i] - 1);
    }
  }
  if (ans == 0 && same != (1 << 30))
  {
    cout << same << endl;
  }
  else
  {
    cout << ans << endl;
  }
}

#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
vector<int> G[100010];

int color[100010];

// 頂点を1と-1で塗っていく
bool dfs(int v, int c) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); i++) {
        // 隣接している頂点が同じ色ならfalse
        if (color[G[v][i]] == c) return false;
        // 隣接している頂点がまだ塗られていないなら-cで塗る
        if (color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
    }

    // すべての頂点を塗れたらtrue
    return true;

}
signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    rep(i, M) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    if(dfs(0, 1)) {
        int k1 = 0, k2 = 0;
        rep(i, 100010) {
            if (color[i] == 1) k1++;
            else if (color[i] == -1) k2++;
        }
        cout << k1 * k2 - M << endl;
    } else {
        cout << N * (N - 1) / 2 - M << endl;
    }

    return 0;
}


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

int main(){
    int a,b,c,d,e,k;
    cin>>a>>b>>c>>d>>e>>k;
    if(e-a<=k)cout<<"Yay!"<<endl;
    else cout<<":("<<endl;
     return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll N,M,X,Y;
    cin >>N>>M>>X>>Y;
    ll XZ=X;
    ll YZ=Y;
    rep(i,N){
        ll x;
        cin>>x;
        chmax(XZ,x);
    }

    rep(i,M){
        ll y;
        cin>>y;
        chmin(YZ,y);
    }

    if(XZ<YZ){
        cout<<"No War"<<endl;
    }else{
        cout<<"War"<<endl;
    }

    return 0;

}