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

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int  MOD=1000000007;
const int  INF= int(1e9);
const int  MAX =1007;
string a,b;
int n,m;
int dp[MAX][MAX];

int solve ( int n,int m) {
    if(dp[n][m] != -1) {
        return dp[n][m];
    }
    else if(m == 0) {
        return dp[n][m]=n;
    }
    else if(n == 0) {
        return dp[n][m]=m;
    }
    if (a[n-1] == b[m-1] ) {
        return dp[n][m]=solve(n-1,m-1);
    }
    dp[n][m] = 1 + min(  solve(n-1,m-1) , min ( solve(n-1,m) ,solve(n,m-1) ) );
    return dp[n][m];
}

int main()
{
    ios_base::sync_with_stdio(false);
    for(int i=0;i<MAX;i++) {
        for(int j=0;j<MAX;j++) {
            dp[i][j]=-1;
        }
    }
    cin>>a;
    cin>>b;
    n=a.size(),m=b.size();
    solve(n,m);
    cout<<dp[n][m]<<"\n";

    return 0;

}

#include <iostream>
#include <string>
#define int long long
//#define _DEBUG
using namespace std;
string sa, sb;
int T[1009][1009];
int solve(){
    int sal = sa.length(), sbl = sb.length();
    for(int a=0; a<=sal; a++){
        for(int b=0; b<=sbl; b++){
            if(a==0){
                T[a][b] = b;
            }else if(b==0){
                T[a][b] = a;
            }else{
                T[a][b] =
                    min(min(T[a][b-1]+1, T[a-1][b]+1),
                    T[a-1][b-1]+((sa[a-1]==sb[b-1])?0:1));
            }
        }
    }
    return T[sal][sbl];
}
signed main(){
    while(cin >> sa >> sb){
        cout << solve() << endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 1e9+7;

ll add(ll a,ll b)
{
	a%=MOD; b%=MOD;
	a+=b;a%=MOD;
	if(a<0) a+=MOD;
	return a;
}
ll mult(ll a, ll b)
{
	a%=MOD; b%=MOD;
	ll ans=(a*b)%MOD;
	if(ans<0) ans+=MOD;
	return ans;
}
ll pw(ll a, ll b)
{
	ll r=1;
	while(b){
		if(b&1) r=mult(r,a);
		a=mult(a,a);
		b>>=1;
	}
	return r;
}
ll inverse(ll a)
{
	return pw(a,MOD-2);
}
const int MAXN = 100005;

void NO(){ cout<<0<<'\n'; exit(0); }

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

	int n; cin>>n;
	int a[n];
	forn(i,0,n) cin>>a[i];

	int c[3]{};
	ll ans=1;

	forn(i,0,n){
		int cnt=0;
		forn(j,0,3){
			if(a[i]==c[j]) cnt++;
		}
		ans=mult(ans,cnt);
		forn(j,0,3){
			if(a[i]==c[j]){ c[j]++; break; }
		}
	}

	cout<<ans<<'\n';

	return 0;
}

/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {//236
	double n;
	cin>>n;
	double mi = LONG_MAX;
	REP(i,1,5){
		double val;
		cin>>val;
		mi=min(mi,val);
	}
	if(n<=mi)cout<<5;
	else printf("%lld",(ll)ceil(n/mi)+4);
}

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}


#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
const int N = 200010;
int n, a[N], b[N], cnt, tot;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[n - i + 1];
    int l = 1, r = n, st = 0, ed = -1;
    for (int i = 1; i <= n; i++)
        if (a[i] == b[i]) {
            if (!st) st = i;
            ed = i;
        }
    for (int j = st; j <= ed; j++) {
        if (a[l] ^ b[j] && b[l] ^ b[j]) swap(b[j], b[l++]);
        else if (a[r] ^ b[j] && b[r] ^ b[j]) swap(b[j], b[r--]);
        else puts("No"), exit(0);
    }
    puts("Yes");
    for (int i = 1; i <= n; i++) printf("%d%c", b[i], " \n"[i == n]);
    return 0;
}

#include <iostream>
#include <string.h>
#include <sstream>
#include <ctype.h>
using namespace std;

string com[3] = {"the", "this", "that"};

string incAlpha (string str, int k) {
    for (int i = 0; i < str.size(); i++) {
        if(isalpha(str[i])) str[i] = ((str[i] + k - 'a') % 26) + 'a';
    }
    return str;
}

bool strsrch(string str1, string str2) {
    for (int i = 0; i < str1.size(); i++) {
        if (str1[i] == str2[0]) {
            bool flag = true;
            if (i + str2.size() > str1.size()) flag = false;
            for (int j = 0; j < str2.size() && flag; j++) {
                if (str1[i + j] != str2[j]) flag = false;
            }
            if (flag) return true;
        }
    }

    return false;
}

bool check (string str) {
    for (int i = 0; i < 3; i++) {
        if (strsrch(str, com[i])) return true;
    }
    return false;
}

int main ()
{
    string str;
    while (getline(cin, str)) {
        for (int i = 0; i <= 26; i++) {
            if (check((incAlpha(str, i)))) {
                cout << (incAlpha(str, i)) << endl;
                break;
            }
        }
    }

    return 0;
}


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

int main(){
    ll n; cin >> n;
    map<ll,ll> mp; rep(i,n){
        ll tmp;
        cin >> tmp;
        mp[tmp]++;
    }

    ll m1 = 0, m2 = 0;
    for(auto x:mp){
        if(x.second > 1 && (x.first > m1 || x.first > m2)){
            if(m1 < m2){
                m1 = x.first;
            }else{
                m2 = x.first;
            }
        }
        if(x.second > 3){
            if(x.first > m1){
                m1 = x.first;
            }
            if(x.first > m2){
                m2 = x.first;
            }
        }
    }

    put(m1 * m2);
}

#include <iostream>

int main(void){
    int N, tmp=200001, ans=0;
    std::cin >> N;
    for (int i=0; i<N; ++i){
        int a;
        std::cin >> a;
        if (a < tmp){
            ++ans;
            tmp = a;
        }
    }

    std::cout << ans << std::endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <numeric>
#include <math.h>

using namespace std;
void func()
{
	int N;
	cin >> N;

	int64_t sum = 0;
	int64_t a;
	cin >> a;
	for (int i = 1; i < N; ++i)
	{
		int64_t tmp;
		cin >> tmp;
		if (tmp < a)
		{
			sum += (a - tmp);
			tmp += (a - tmp);
		}
		a = tmp;
	}

	cout << sum << endl;

}

int main() {
//	while(1)
	func();
	return 0;
}

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

#define print cout <<
#define printl(s) cout << s << endl;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;

const ll modseed = pow(10,9) + 7;

template <typename T>
vector<T> getValues(int num){
    vector<T> values(num);
    for(int i=0;i<num;i++){
        cin >> values.at(i);
    }
    return values;
}

ll calculateMod(ll n, ll expo, ll modSeed){
  ll ret = 1;
  rep(i, expo){
    ret *= n;
    ret = ret%modSeed;
  }
  return ret;
}

int main(){
    ll N;
    cin >> N;
    auto inputs = getValues<ll>(N);
    vector<ll> steps(N,0);
    ll ans = 0;
    rep(i, N){
      if(i==0){
        continue;
      }
      ll maenohito = steps.at(i-1) + inputs.at(i-1);
      ll sub = inputs.at(i) - maenohito;
      if(sub < 0){
        steps.at(i) = -sub;
        ans += steps.at(i);
      }
    }
    printl(ans);
}


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

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int N, X, T;
    cin >> N >> X >> T;
    int q = (N / X) + (N % X != 0);
    cout << q*T;

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void Fast()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cerr.tie(0);
}
inline int D()
{
    int t;
    scanf("%d",&t);
    return t;
}
inline ll llD()
{
    ll t;
    scanf("%lld",&t);
    return t;
}
const int Max=1e5+8;
const int OO=-1e6;
int xt[4]= {1,0,0,-1};
int yt[4]= {0,1,-1,0};
ll n,m,f,l;
bool valid(int x,int y)
{
    return x>-1 && y>-1 && x<n && y<m;
}

int mem[209][209][209],k;
        string st,st2;

void init()
{
    memset(mem,-1,sizeof mem);
}
/////////////////////////////////////////////////////////////////////
int solve (int i=0,int t=0,int j=n )
{

}

int main()
{

    int T=1;
///T=D();
    while(T-->0)
    {
       n=D();
      int x=D();
      int t= D();
    int ans=  (n/x)*t;
    if(n%x)ans+=t;
    cout<<ans;

    }

}


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

typedef long long ll;

int n, k;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    int cur = 1;
    while (n) {
        cur = min(cur * 2, cur + k);
        --n;
    }
    cout << cur << endl;

    return 0;
}

#include<iostream>
using namespace std;

int main() {
    int N, K; cin >> N >> K;
    int ans = 1000;
    for (int i = 0; i <= N; i++) {
        int num = 1;
        for (int k = 0; k < i; k++) {
            num *= 2;
        }
        for (int k = 0; k < N - i; k++) {
            num += K;
        }
        ans = min(ans, num);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
#define rep(i,n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
using ll=long long;
using P=pair<ll,ll>;
using PP=tuple<ll,ll,ll>;
using S=multiset<ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
const ll inf=1001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
void decimal(int x){cout<<fixed<<setprecision(x);}
ll GCD(ll a,ll b){if(b==0) return a; return GCD(b,a%b);}
ll LCM(ll a,ll b){return(a/GCD(a,b))*(b/GCD(a,b))*GCD(a,b);}
ll POW(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*POW(a,b-1)%mod;ll k=POW(a,b/2);return k*k%mod;}
vi calc(ll x){vi res;while(x>0){res.eb(x%10);x/=10;}reverse(all(res));return res;}

void solve(){
  ll n; cin>>n;
  vi a(n),b(n);
  rep(i,n) cin>>a[i];
  rep(i,n) cin>>b[i];
  //Yes or No
  vi c(200001,0),d(200001,0);
  rep(i,n) c[a[i]]++;
  rep(i,n) d[b[i]]++;
  bool f=true;
  rep(i,200001) if(c[i]+d[i]>n) f=false;
  YesNo(f);
  ll p=0;
  vi ans(n);
  //show example
  if(f){
    REP(i,1,200001){
      c[i]+=c[i-1];
      d[i]+=d[i-1];
    }
    REP(i,1,200001) chmax(p,c[i]-d[i-1]);
    rep(i,n) ans[(i+p)%n]=b[i];
    outv(ans);
  }
}

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

#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<climits>
#include<map>
#include<string>
#include<functional>
#include<iomanip>
#include<deque>
#include<random>
#include<set>
#include<bitset>
#include<cassert>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define chmax(x,y) x = max(x,y)
ll gcd(ll a, ll b) { if (a % b == 0) { return b; } else  return gcd(b, a % b); }
ll lcm(ll a, ll b) { if (a == 0) { return b; }return a / gcd(a, b) * b; }
template<class T>ll LBI(vector<T>& ar, T in) { return lower_bound(ar.begin(), ar.end(), in) - ar.begin(); }
template<class T>ll UBI(vector<T>& ar, T in) { return upper_bound(ar.begin(), ar.end(), in) - ar.begin(); }

ll n, a[200005] = {}, b[200005] = {}, j = 0, bef = -1;

int main() {
	cin >> n;
	rep(i, n)cin >> a[i];
	rep(i, n)cin >> b[i];
	rep(i, n) {
		if (bef != a[i])j = 0;
		if (a[i] == b[i]) {
			while ((a[i] == a[j] || a[i] == b[j]) && j < n)j++;
			if (j < n) {
				swap(b[i], b[j]);
			}
			else {
				cout << "No" << endl;
				return 0;
			}
		}
		bef = a[i];
	}
	cout << "Yes" << endl;
	rep(i, n)cout << b[i] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    ll n,m,sum=0,ans=0,a;
    cin>>n>>m;
    map<ll,ll>mp;
    mp[0]=1;
    while(n--)
    {
        cin>>a;
        sum+=a;
        sum%=m;
        ans+=mp[sum]++;
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)

#define pb push_back
#define all(in) in.begin(),in.end()

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main(){
    int h, w; cin >> h >> w;
    int data[h][w]; rep(i,h) rep(j,w) cin >> data[i][j];

    vector<vector<int>> ans(4,vector<int>());
    rep(i,h) rep(j,w){
        if(j == w-1){
            if(i == h-1) continue;
            if(data[i][j]%2 == 1){
                data[i][j]--;
                data[i+1][j]++;
                ans[0].push_back(i+1);
                ans[1].push_back(j+1);
                ans[2].push_back(i+2);
                ans[3].push_back(j+1);
            }
        }
        else if(data[i][j]%2 == 1){
            data[i][j]--;
            data[i][j+1]++;
            ans[0].push_back(i+1);
            ans[1].push_back(j+1);
            ans[2].push_back(i+1);
            ans[3].push_back(j+2);
        }
    }
    cout << ans[0].size() << endl;
    rep(i,ans[0].size()) cout << ans[0][i] << " " << ans[1][i] << " " << ans[2][i] << " " << ans[3][i] <<endl;
}


#include <iostream>
using namespace std;
int main(void){
    int a,b,c,d;
    int ans=0;
    cin >> a >> b >> c >> d;
    if(a>b) ans +=b;
    else ans += a;
    if(c>d) ans +=d;
    else ans += c;
    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

ifstream fi("JJOOII.INP");
ofstream fo("JJOOII.OUT");

#define fi cin
#define fo cout

string S;
int demJ,demO,demI,res;

void xuli()
{
    int i=0;
    while (1)
    {
        while (S[i]=='J') ++i,++demJ;
        while (S[i]=='O') ++i,++demO;
        while (S[i]=='I') ++i,++demI;
        if (demJ>=demO && demI>=demO) res=max(res,demO);
        demJ=demO=demI=0;
        if (S[i]=='@') return;
    }
}

int main()
{
    fi>>S;
    S+='@';
    xuli();
    fo<<res<<'\n';
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char h[3]={" \n"};
int main()
{
int w[3],x;
for(int j=0;j<2;j++){
 memset(w,0,sizeof(w));
 for(int i=0;i<10;i++){
 scanf("%d",&x);
 if(x>w[0]){w[2]=w[1]; w[1]=w[0]; w[0]=x;}
 else if(x>w[1]) {w[2]=w[1]; w[1]=x;}
 else if(x>w[2]) w[2]=x;
 }
 printf("%d%c",w[0]+w[1]+w[2],h[j]);
}
}

#include<stdio.h>
#define INF 100000
int min(int a,int b){return a<b?a:b;}
int getk(int a){
	int res=0;
	while(a>0){
		res++;
		a/=10;
	}
	return res;
}
int pow(int a,int b){
	int l;
	int rt=1;
	for(l=0;l<b;l++)rt*=10;
	return rt;
}
int card[10];
int pos[10000];
int cpos;
int main(){
	int i,j;
	int n,k;
	while(1){
	scanf("%d",&n);
	scanf("%d",&k);
	if(n==0&&k==0)return 0;
	for(i=0;i<n;i++)scanf("%d",&card[i]);
	cpos=0;
	int max=1;
	int ket;
	int next;
	int ni;
	int num;
	int ans=0;
	bool used[10];
	for(i=0;i<k;i++)max*=n;
	for(i=0;i<max;i++){
cont:
		for(j=0;j<n;j++)used[j]=false;
		ket=1;
		ni=i;
		num=0;
		for(j=0;j<k;j++){
			next=ni%n;
			ni/=n;
			if(used[next]){
				i++;
				if(i==max)goto end;
				goto cont;
			}
			else used[next]=true;
			num+=card[next]*ket;
			ket*=pow(10,getk(card[next]));
		}
		for(j=0;j<cpos;j++){
			if(num==pos[j])break;
		}
		if(j==cpos){
			pos[cpos++]=num;
			ans++;
		}
	}
end:
	printf("%d\n",ans);
	}
}


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

using namespace std;

int main() {
    string str;
    cin >> str;
    set<char> st;
    for(int i=0; i<str.size(); i++) {
        st.insert(str[i]);
    }

    if(st.size()<26) {
        cout << str;
        for(int i=0; i<26; i++) {
            if(st.find((char)('a'+i))==st.end()) {
                cout << (char)('a'+i) << endl;
                break;
            }
        }
    } else {
        set<char> vec;
        vec.insert(str[25]);
        int i;
        for(i=25; i>=1; i--) {
            if(str[i-1]<str[i]) {
                break;
            }
            vec.insert(str[i-1]);
        }
        if(vec.size()==26) {
            cout << -1 << endl;
        } else if(vec.upper_bound(str[i-1])!=vec.end()) {
            for(int j=0; j<i-1; j++) {
                cout << str[j];
            }
            cout << (*vec.upper_bound(str[i-1])) << endl;
        }
    }

    return 0;

}

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

int main(){
	string S;
  cin >> S;
  bool p = S.at(0) == 'R';
  bool q = S.at(1) == 'R';
  bool r = S.at(2) == 'R';

  if(p && q && r) cout << 3 << endl;
  else if((p && q) || (q && r)) cout << 2 << endl;
  else if(p || q || r) cout << 1 << endl;
  else cout << 0 << endl;
}


#include <cstring>
#include <iostream>

using namespace std;

#define MAX_N 512
#define MAX_M 10240

int N, M, A[MAX_M], B[MAX_M]; bool LIST[2][MAX_N];

int main()
{
	while (true)
	{
		memset(LIST, false, sizeof(LIST));

		cin >> N >> M;

		if (N == 0 && M == 0) { break; }

		for (int i = 1; i <= M; i++)
		{
			cin >> A[i] >> B[i];

			if (A[i] == 1)
			{
				LIST[0][B[i]] = true;
				LIST[1][B[i]] = true;
			}
		}

		for (int i = 1; i <= M; i++)
		{
			if (LIST[0][A[i]] == true)
			{
				LIST[1][B[i]] = true;
			}

			if (LIST[0][B[i]] == true)
			{
				LIST[1][A[i]] = true;
			}
		}

		int count = 0;

		for (int i = 2; i <= N; i++)
		{
			if (LIST[1][i] == true)
			{
				count++;
			}
		}

		cout << count << endl;
	}

	return 0;
}

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

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

    int n,k;

    while(cin >> n >> k){
        if(!n && !k) break;

        int card[n];
        rep(i,n) cin >> card[i];

        sort( card, card + n );

        int ans = 0;
        map<int,int> memo;
        do{
            int temp = 0;
            rep(i,k){
                temp *= 10;
                if(card[i]>9) temp *= 10;
                temp += card[i];
            }
            if(memo[temp] == 0) ans++;
            memo[temp] = 1;
        }while(next_permutation( card, card + n ));

        cout << ans << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }
using P = pair<int,int>;
//Write From this Line

bool isPrime(int x) {
	for(int i = 2; i * i <= x; i++){
		if(x % i == 0) {
			return false;
		}
	}
	return true;
}
int main()
{
	int n;
	cin >> n;
	for(int i = 2; i <= 55555; i++){
		if(i%5 != 1) continue;
		if(isPrime(i)){
			cout << i << endl;
			n--;
		}
		if(n == 0) break;
	}
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))

//3111111111111111111111111111111

	bool isprime(int x){
	for(int i = 2;i*i<=x;i++){
		if(x%i == 0)return false;
	}
	return x!=1;
}

	void solve(){

		ini(n);
		vi morty;
		int i = 1;
		while(morty.size()!=55){
			i++;
			int tmp = i*5+1;
			while(!isprime(tmp)){
				i++;
				tmp = i*5+1;
			}
			morty.push_back(tmp);
		}
		FOR(i,0,n){
			cout<<morty[i]<<" ";
		}
		out();

	}

int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall

	solve();

	return 0;
}


#include <iostream>
#include <math.h>
using namespace std;
int main(){
	int x, M;
	cin >> x;
	M = pow(x, 3.0);
	cout << M << endl;
	return 0;
}

#include <iostream>

int main()
{
    int x;
    std::cin >> x;
    std::cout << x * x * x << "\n";
}


#define NODE 0
#define WEIGHT 1
#define COST_IDX 0
#define TIME_IDX 1
#define P_IDX 0
#define Q_IDX 1
#define R_IDX 2
#include <iostream>
#include <cstring>
using namespace std;

int n, m, k;
int weights[101][101][2];
int min_w[101][101][2];
int requests[200][3];

struct heap_queue {
  int arr[101];
  int *cmp_val;
  int lookup[101];
  int size=0;
};

void heap_push(int elm, heap_queue &q) {
  q.arr[q.size] = elm;
  int above, i = q.size++;
  while (i && q.cmp_val[q.arr[(above=(i-1)>>1)]] > q.cmp_val[elm]) {
    q.arr[i] = q.arr[above];
    q.lookup[q.arr[i]] = i;
    i = above;
  }
  q.arr[i] = elm;
  q.lookup[elm] = i;
}

int heap_pop(heap_queue &q) {
  int popped = q.arr[0];
  int elm = q.arr[--q.size];
  int below, i = 0;
  while ((below=(i<<1)+1) < q.size) {
    if (below+1 < q.size && q.cmp_val[q.arr[below+1]] < q.cmp_val[q.arr[below]])
      below++;
    if (q.cmp_val[q.arr[below]] >= q.cmp_val[elm])
      break;
    q.arr[i] = q.arr[below];
    q.lookup[q.arr[i]] = i;
    i = below;
  }
  q.arr[i] = elm;
  q.lookup[elm] = i;
  return popped;
}

void change_val(int elm, heap_queue &q) {
  int above, i = q.lookup[elm];
  while (i && q.cmp_val[q.arr[(above=(i-1)>>1)]] > q.cmp_val[elm]) {
    q.arr[i] = q.arr[above];
    q.lookup[q.arr[i]] = i;
    i = above;
  }
  q.arr[i] = elm;
  q.lookup[elm] = i;
}

bool read_input() {
  cin >> n >> m;
  if (n*m == 0)
    return false;
  memset(weights, 0, sizeof(int)*101*101*2);
  memset(min_w, 0, sizeof(int)*101*101*2);
  for (int i = 0; i < n; i++) {
    int a, b, c, t;
    cin >> a >> b >> c >> t;
    weights[a][b][COST_IDX] = weights[b][a][COST_IDX] = c;
    weights[a][b][TIME_IDX] = weights[b][a][TIME_IDX] = t;
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    cin >> requests[i][P_IDX] >> requests[i][Q_IDX] >> requests[i][R_IDX];
  }
  return true;
}

int resolve_request(int i_th) {
  int p, q, r;
  p = requests[i_th][P_IDX];
  q = requests[i_th][Q_IDX];
  r = requests[i_th][R_IDX];

  if (min_w[p][q][r])
    return min_w[p][q][r];

  int weight_sum_map[101];
  memset(weight_sum_map, -1, sizeof(int)*101);

  /* starting node has weight_sum of 0 */
  weight_sum_map[p] = 0;

  int in_queue[101] = {};
  heap_queue heap_q;
  heap_q.cmp_val = weight_sum_map;
  heap_push(p, heap_q);
  in_queue[p] = 1;

  while (heap_q.size) {
    int current_node = heap_pop(heap_q);
    in_queue[current_node] = 0;
    int weight_sum = weight_sum_map[current_node];
    min_w[p][current_node][r] = weight_sum;

    if (current_node == q)
      break;
    for (int i = 1; i < 101; i++) {
      if (!weights[current_node][i][r])
	continue;
      int adj_node = i;
      int w = weights[current_node][adj_node][r];
      int new_weight_sum =  weight_sum + w;
      if (weight_sum_map[adj_node] == -1 || new_weight_sum < weight_sum_map[adj_node]) {
	weight_sum_map[adj_node] = new_weight_sum;
	if (!in_queue[adj_node]) {
	  heap_push(adj_node, heap_q);
	  in_queue[adj_node] = 1;
	} else {
	  change_val(adj_node, heap_q);
	}
      }
    }
  }
  return weight_sum_map[q];
}

void solve() {
  //cout << endl;
  for (int i = 0; i < k; i++)
    cout << resolve_request(i) << endl;
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  while(read_input()) {
    solve();
  }
  return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}
ll N,m;
vector<vector<ll>> warshall_floyd(vector<vector<ll>> d){
  rep(k,m){
    rep(i,m){
      rep(j,m){
        d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
      }
    }
  }
  return d;
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  while(cin>>N>>m&&N&&m){
    int a,b,c,d;
    vector<vector<ll>> time(m+1,vector<ll>(m+1,inf));
    vector<vector<ll>> cost(m+1,vector<ll>(m+1,inf));
    ++m;
    rep(i,N){
      cin >> a >> b >> c >> d;
      time[a][b] = d;
      cost[a][b] = c;
      time[b][a] = d;
      cost[b][a] = c;
    }
    int k,p,q,r ;
    auto T = warshall_floyd(time);
    auto C = warshall_floyd(cost);
    cin >> k;
    rep(i,k){
      cin >> p >> q >> r;
      if(r){
        cout << T[p][q]<<endl;
      }
      else{
        cout << C[p][q]<<endl;
      }
    }
  }
  return 0;
}


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

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
const int inf=1000000007;
const int MOD=1000000007;
int n,m;
struct Map{
  vector<vector<int>> map;
  void mkmap(vector<int> const& a,vector<int> const& b,vector<int>& num){
    //num.resize(n*m+1,0);
    rep(i,n){
      map.push_back(vector<int> (m,inf));
    }
    rep(i,n){
      rep(j,m){
        map[i][j]=min(a[i],b[j]);
        if(a[i]==b[j])map[i][j]=999;
      }
    }
  }
  void show_map(){
    rep(i,n){
      rep(j,m){
        cout<<map[i][j]<<" ";
      }
      cout<<endl;
    }
  }
};
signed main(){
  cin>>n>>m;
  vector<int> a(n);
  vector<int> b(m);
  vector<int> sum;
  vector<int> memo(n*m+1,0);
  vector<int> num(n*m+1,0);
  vector<int> list;
  rep(i,n)cin>>a[i];
  rep(i,m)cin>>b[i];
  //Map grid;
  //grid.mkmap(a,b,num);
  rep(i,n){
    rep(j,m){
      if(a[i]==b[j]){
        sum.push_back(inf);
        ++num[a[i]];
        list.push_back(a[i]);
      }
      else{
        sum.push_back(min(a[i],b[j]));
      }
    }
  }
  sort(ALL(sum));
  rep1(i,n*m){
    memo[i]=memo[i-1];
    if(num[i]==1)++memo[i];
    else if(num[i]>1){
      cout<<0<<endl;
      return 0;
    }
  }
  int cur=1;
  int res=1;
  sum.push_back(inf);
  sum.push_back(inf);
  int last=sum[0];

  rep1(i,n*m){
    if(last==sum[i]){
      cur++;
      continue;
    }
    else{
      if(num[last]==1){
        rep1(k,cur){
          res*=last-1-memo[last]-i+k+1;
          res%=MOD;
        }
      }
      else{
        res*=cur;
        rep1(k,cur-1){
          res*=last-1-memo[last]-i+k+1;
          res%=MOD;
        }
      }
      cur=1;
    }
    //cout<<sum[i]-memo[sum[i]]-i<<endl;
    if(res<0)res=0;
    res%=MOD;
    last=sum[i];
    if(sum[i]==inf){
      break;
    }
  }

  cout<<res<<endl;
  //grid.show_map();
  /*rep(i,list.size()){
    cout<<list[i]<<endl;
  }*/
  //cout<<memo[182]<<endl;
  return 0;
}

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

int main(){
    int h,w;
    cin >> h >> w;
    int a[h][w];
    vector
orig;
    vector
after;
    rep(i,h){
        rep(j,w){
            int temp;
            cin >> temp;
            a[i][j] = temp;
        }
    }
    int count = 0;
    rep(i,h-1){
        rep(j,w){
            if(a[i][j] % 2 != 0){
                a[i][j]--;
                a[i+1][j]++;
                count++;
              P hoge;
                hoge.first = i+1;
                hoge.second = j+1;
                orig.push_back(hoge);
                hoge.first = i+2;
                after.push_back(hoge);
            }
        }
    }
    rep(i,w-1){
        if(a[h-1][i] % 2 != 0){
            a[h-1][i]--;
            a[h-1][i+1]++;
            count++;
          P hoge;
            hoge.first = h;
            hoge.second = i+1;
            orig.push_back(hoge);
            hoge.second = i+2;
            after.push_back(hoge);
        }
    }
    cout << count << endl;
    rep(i,count){
        cout << orig[i].first << ' ' << orig[i].second << ' ' << after[i].first << ' ' << after[i].second << endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
using edge = pair<int, long long>;
using Graph = vector<vector<edge>>;
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;}
const long long INF = 1e12;

vector<long long> dijkstra(const Graph &G, int s){
    using Pi = pair<long long, int>;
// 最終的に最短経路を保持する配列, INFで初期化
    vector<long long> dist(G.size(), INF);
// そこまでの距離, 頂点を保持する優先度付きキュー
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
// キューが空になるまで(max V 回)
    while(!que.empty()){
        long long sum_dist; int node;
// キューから取り出された時点で頂点が確定
        tie(sum_dist, node) = que.top(); que.pop();
// それより短い経路が分かっているときはcontinue(枝刈り)
        if (dist[node] < sum_dist) continue;
// 確定した頂点から到達できる頂点を探索
        for (auto &e : G[node]) {
            int next_node; long long dis;
            tie(next_node, dis) = e;
// 最短距離が更新されたときだけキューに追加
            if (chmin(dist[next_node], dist[node] + dis)) {
                que.emplace(dist[next_node], next_node);
            }
        }
    }
    return dist;
}

int main(){
    int N, K, Case;
    Graph G;
    vector<vector<long long>> dist;
    while(true){
        cin >> N >> K; if(N == 0 && K == 0)break;

        G.assign(N, vector<edge>());
        dist.assign(N, vector<long long>(N, INF));

        for (int i = 0; i < K; i++) {
            cin >> Case;
            if (Case == 0) {
                int s, g; cin >> s >> g; s--; g--;
                dist[s] = dijkstra(G, s);
                long long ans = dist[s][g];
                if (ans == INF) ans = -1;
                cout << ans << endl;
            } else {
                int a, b; long long c;
                cin >> a >> b >> c; a--; b--;
                G[a].push_back(make_pair(b, c));
                G[b].push_back(make_pair(a, c));
            }
        }
    }
    return 0;
}

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

int *p,*rank;
int V,E,sum=0,n=0;
struct edge {
    int u;
    int v;
    int cost;
};
edge *e;
bool operator<(const edge& x, const edge& y) {
        return x.cost < y .cost;
}

void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
}

void link(int x, int y) {
    if(rank[x] > rank[y]) p[y] = x;
    else {
        p[x] = y;
        if(rank[x] == rank[y]) rank[y] = rank[y]+1;
    }
}

int findSet(int x) {
    if(x != p[x]) p[x] = findSet(p[x]);
    return p[x];
}

void unit(int x, int y) {
    link(findSet(x), findSet(y));
}

int kruskal() {
    sort(e, e+E);
    for(int i=0; n != V-1; i++) {
        if(findSet(e[i].u) != findSet(e[i].v)) {
            unit(e[i].u,e[i].v);
            sum += e[i].cost;  n++;
        }
    }
}

int main() {
    int s,t,w;
    cin >> V >> E;
    p = new int[V];
    rank = new int[V];
    e = new edge[E];
    for(int i=0; i<V; i++) makeSet(i);
    for(int i=0; i<E; i++) {
        cin >> s >>t >> w;
        edge a = {s,t,w};
        e[i] = a;
    }
    kruskal();
    cout << sum << endl;
    delete[] p,rank,e;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

int main() {
  ll a, b, c;
  cin >> a >> b >> c;
  if ((a <= c && c <= b) || (a >= c && c >= b)) Yes();
  else No();
}


// @author PraveenKumarRana

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

#define pb push_back
#define mp make_pair
#define int long long int
#define vi vector<int>
#define pi pair<int, int>
#define IOS std::ios_base::sync_with_stdio(false);
#define all(x) x.begin(), x.end()
#define f(i, a, n) for (int i = a; i < n; i++)
#define fr(i, n, a) for (int i = n - 1; i >= a; --i)

const int mod = 1e9 + 7;
const int inf = 1e16 + 7;

void SOLVE() {
    // Write your code here ?
    int n, k;
    cin >> n >> k;
    map<int, int> mp;
    f(i, 0, k) {
        int x;
        cin >> x;
        mp[x] = 1;
    }

    while (true) {
        bool ok = true;
        int N = n;
        while (N > 9) {
            if (!mp[N % 10]) {
                N /= 10;
            } else {
                ok = false;
                break;
            }
        }

        if (N > 0 && !mp[N]) {
            N /= 10;
        } else {
            ok = false;
        }

        if (!ok) {
            N = n + 1;
            n++;
        } else {
            break;
        }
    }

    cout << n << endl;
}

int32_t main() {
    IOS;
    SOLVE();
    return 0;
}

#include <stdio.h>

int main() {

	int x=0, y= 0, z= 1;
	scanf("%d\n%d", &x, &y);
	for (int i = 0; i < x; i++) {
		if (2 * z <= z + y) {
			z *= 2;
		} else {
			z += y;
		}
	}

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


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define all(v) v.begin(), v.end() // sort(all(array))
#define INF 1000000000
#define mod 1000000007
using namespace std;
using ll = long long;
using vi = vector<int>; //vi array(要素数, 初期値)
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>; //vvi table(行数, vi(列数, 初期値))
using vvc = vector<vc>;
using vvs = vector<vs>;
using vvb = vector<vb>;

int main() {
    int n, x, t; cin >> n >> x >> t;

    if(n % x == 0) {
        cout << (n / x) * t << endl;
    }
    else cout << t + (n / x) * t << endl;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
 int N,K;
  cin >> N >> K;
  int X = 1;
   for(int i = 0; i < N; i++){
    if( X * 2 < X + K ){
     X = X * 2;
    }
    else{
     X = X + K;
    }
  }
  cout << X << endl;
}


#include <bits/stdc++.h>

#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,x,n) for(int i=x; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define all(X) (X).begin(),(X).end()
#define X first
#define Y second
#define pb push_back
#define eb emplace_back

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

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; }

template<class A, size_t N, class T> void Fill(A (&a)[N], const T &v){ fill( (T*)a, (T*)(a+N), v ); }

const ll INF = 1e9+7;

int main(){
	//ios_base::sync_with_stdio(false);
	int N;
	vector<pll> v;
	map<pll, int> mp;

	cin >> N;
	rep(i,N){
		int x, y;
		cin >> x >> y;
		v.emplace_back(x, y);
		mp[pii(x,y)] = i;
	}
	sort(all(v));

	vector<int> cv[2];
	auto check = [&](int i, int k){
		pll a = v[ cv[k][cv[k].size()-2] ];
		pll b = v[ cv[k][cv[k].size()-1] ];
		pll c = v[i];
		if(k == 0) return (b.Y-a.Y) * (c.X-a.X) < (c.Y-a.Y) * (b.X-a.X);
		if(k == 1) return (b.Y-a.Y) * (c.X-a.X) > (c.Y-a.Y) * (b.X-a.X);
	};
	// rep(k,2) rep(i,N){
	// 	while( cv[k].size() >= 2 && check(i, k) ) cv[k].pop_back();
	// 	cv[k].push_back(i);
	// }
	rep(i,N){
		int k = 0;
		while( cv[k].size() >= 2 && check(i, k) ) cv[k].pop_back();
		cv[k].push_back(i);
	}
	rrep(i,N-1){
		int k = 0;
		while( cv[k].size() >= 2 && check(i, k) ) cv[k].pop_back();
		cv[k].push_back(i);
	}

	vector<int> convex;
	for(auto t: cv[0]) convex.push_back(t);
	rrep(i, cv[1].size()-1) convex.push_back(cv[1][i]);
	convex.push_back( cv[0][1] );

	//for(auto i: convex) cout << v[i].X << "," << v[i].Y << endl;

	vector<double> ans(N);
	reps(i, 1, convex.size()-1){
		pll a = v[ convex[i-1] ];
		pll b = v[ convex[i] ];
		pll c = v[ convex[i+1] ];
		double vx1 = - (b.Y - a.Y);
		double vy1 = b.X - a.X;
		double vx2 = - (c.Y - b.Y);
		double vy2 = c.X - b.X;
		double cdot = (vx1*vx2 + vy1*vy2);
		double l1 = sqrt(vx1*vx1 + vy1*vy1);
		double l2 = sqrt(vx2*vx2 + vy2*vy2);
		double ang = acos( cdot / l1 / l2 );
		int n = mp[ v[convex[i]] ];
		ans[n] = ang / 2.0 / M_PI;
	}

	for(auto t: ans) printf("%.20f\n", t);

	return 0;
}

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

const int N = 5010 ;
const long long inf = 10000000000000000 ;
pair<int,int> zab [ N ] ;

bool cmp ( pair<int,int> A, pair<int,int> B ) {
    return A.first + A.second < B.first + B.second ;
}

long long dp [ N ][ 3 ];

int main() {
 //   freopen("input.txt", "r", stdin) ;
    int n ; cin >> n ;
    for ( int i = 0 ; i < n ; ++i )
        cin >> zab[i].first >> zab[i].second ;
    sort ( zab, zab+n, cmp ) ;
    int ans =0 ;
    for ( int i = 1 ; i <= n ; ++i ) {
        for ( int j = 0 ; j < n ; ++j ) {
            if ( i == 1 ) dp[j][i%2] = zab[j].first ;
            else if ( j + i <= n ) dp[j][i%2] = min ( 1ll*zab[j].first, dp[j+1][(i-1)%2] - zab[j].second );
            else dp[j][i%2] = -inf ;
        }
        bool kPosible = (dp[n-1][i%2] >= 0ll) ;
        for ( int j = n-2 ; j >= 0 ; --j ) {
            dp[j][i%2] = max ( dp[j][i%2], dp[j+1][i%2] ) ;
            kPosible |= (dp[j][i%2]>=0ll) ;
        }
        if ( kPosible ) ans = i ;
    }
    cout << ans << '\n' ;
}


// Wanna Hack? GL...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

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

const int maxn=310,mod=1e9+7;
const ll inf=1e18;

int dp[2][maxn][maxn];

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    string s;cin>>s;
    int k,t=0,n=sz(s);cin>>k;
    for(int r=0;r<n;r++){
	dp[0][r][r]=1;
	for(int l=r-1;l>=0;l--)
	    dp[0][l][r]=max(dp[0][l+1][r],max(dp[0][l][r-1],(s[l]==s[r]?2:0) + dp[0][l+1][r-1]));
    }
    for(int i=1;i<=k;i++){
	memset(dp[t^1],0,sizeof dp[t^1]);
	for(int r=0;r<n;r++){
	    dp[t^1][r][r]=1;
	    for(int l=r-1;l>=0;l--)
		dp[t^1][l][r]=max(dp[t^1][l+1][r],max(dp[t^1][l][r-1],s[l]==s[r]?2+dp[t^1][l+1][r-1]:2+dp[t][l+1][r-1]));
	}
	t^=1;
    }
    return cout<<dp[t][0][n-1]<<endl,0;
}


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

int DP[1010][1010];

int main() {
  string A, B;
  cin >> A >> B;
  int LA = (int) A.size();
  int LB = (int) B.size();
  fill(&DP[0][0], &DP[0][0]+1010*1010, 0);
  for (auto i=-1; i<LA; i++) {
    for (auto j=-1; j<LB; j++) {
      if (i == -1) {
        DP[i+1][j+1] = j+1;
      } else if (j == -1) {
        DP[i+1][j+1] = i+1;
      } else if (A[i] == B[j]) {
        DP[i+1][j+1] = DP[i][j];
      } else {
        // A[i] と B[j] が異なるならば、
        // B[j]を挿入するか、A[i]を削除するか、A[i]をB[j]に置換するか
        // のどれかは必ず実行しなくてはならない。
        // 残りの編集作業回数は、それぞれDP[i+1][j]、DP[i][j+1]、DP[i][j]
        // となる。それぞれが最善である例をつくることができる。
        DP[i+1][j+1] = min({DP[i+1][j], DP[i][j+1], DP[i][j]}) + 1;
      }
    }
  }
  cout << DP[LA][LB] << endl;
}

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int n, p;
int P;
VI cnt;

int check_winner(){
	rep(i, cnt.size()){
		if(cnt[i] == P) return i + 1;
	}
	return 0;
}

int solve(){
	P = p;
	int ans;
	rep(i, 1000000){
		rep(j, n){
			if(ans = check_winner()){
				return ans - 1;
			}else if(p == 0){
				p = cnt[j];
				cnt[j] = 0;
			}else{
				p--;
				cnt[j]++;
			}
		}
	}
	return 0;
}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	while(1){
		cnt.clear();
		cin >> n >> p;
		cnt.resize(n, 0);
		if(n == 0 && p == 0) break;

		cout << solve() << endl;
	}
}


#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T>
using vec = std::vector<T>;

int dfs(int now,vec<vec<int>> &G, vec<int> &tpl, vec<bool> &visited){
    if(tpl[now] != -1) return tpl[now];
    if(tpl[now] == -1 && visited[now]) return -1;
    visited[now] = true;

    if(G[now].size() == 0) {
        tpl[now] = 1;
        return 1;
    }

    int tmpOrder = -1;
    for(auto e:G[now]){
        int tmp = dfs(e,G,tpl,visited);
        if(tmp == -1) return -1;
        tmpOrder = max(tmpOrder,tmp+1);
    }
    tpl[now] = tmpOrder;
    return tpl[now];
}

vector<int> topological_sort(vec<vec<int>> &G){
    int N = G.size();
    vec<int> topolo(N,-1);
    vec<bool> visited(N,false);
    rep(i,N) dfs(i,G,topolo,visited);
    // {-1,-1,-1,…} の時はトポロジカルソート不可
    return topolo;
}

int main() {
    int N;
    cin >> N;
    vec<vec<int>> A(N, vec<int>(N - 1));
    rep(i, N) rep(j, N - 1) {
        cin >> A[i][j];
        --A[i][j];
    }
    vec<vec<int>> id(N, vec<int>(N, -1));
    int now = 0;
    rep(i, N) rep(j, N) {
        if (i == j || i > j) continue;
        id[i][j] = now, ++now;
    }

    vec<vec<int>> games(N,vec<int>(N-1));
    rep(i, N) rep(j, N - 1) {
        int me = i;
        int vs = A[i][j];
        if (me > vs) swap(me, vs);
        games[i][j] = id[me][vs];
    }

    vec<vec<int>> G(now,vec<int>());
    rep(i,N)rep(j,N-2){
        G[games[i][j+1]].push_back(games[i][j]);
    }

    vec<int> a = topological_sort(G);
    int ans = 0;
    for(auto e:a) {
        if(e==-1) {
            cout << -1 << endl;
            return 0;
        }
        ans= max(ans,e);
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define M 1000000007
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define f first
#define s second
#define b begin
#define e end
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define itfr(it,x) for(it=x.begin();it!=x.end();it++)
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

int main()
{
    flash;
    //sieve();
    ll T=1,t,n,m,q,k,i,j;
    // cin>>T;
    while(T--)
    {
        n=5;
        ll a[n];
        FOR(i,0,n)  cin>>a[i];
        cin>>k;
        int fl=1;
        FOR(i,0,n)
        {
            FOR(j,0,n)
                if((a[j]-a[i])>k)
                    fl=0;
        }
        if(fl)
            cout<<"Yay!";
        else
            cout<<":(";
    }
}


#include <iostream>
int main()
{
  int n;
  std::cin >> n;
  std::string line;
  std::getline(std::cin, line);
  while (n--) {
    std::getline(std::cin, line);

    int i;
    while ((i = line.find("Hoshino")) != std::string::npos) {
      line.replace(i, 7, "Hoshina");
    }
    std::cout << line << std::endl;
  }
  return 0;
}

#include <stdio.h>

int main(){
char name[12];

scanf("%[^\n]", &name);
name[3] = '8';
printf("%s", name);

	return 0;
}


#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define vint(a,n) vint a(n); rep(i, n) cin >> a[i];
#define vll(a,n) vll a(n); rep(i, n) cin >> a[i];
#define ALL(n) begin(n),end(n)
#define RALL(n) rbegin(n),rend(n)
#define MOD (1000000007)
#define INF (2e9)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint=vector<int>;
using vll=vector<ll>;
using vbool=vector<bool>;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
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; }

int main()
{
    ll a, b; cin >> a >> b;
    if(b>a) swap(a, b);

    map<ll, int> m;
    for(ll i=2;i*i<=b;i++){
        while(b!=1 && b%i==0){
            if(a%i==0) m[i]++;
            b/=i;
        }
    }

    if(b!=1 && a%b==0) m[b]++;

    pr(m.size()+1);
    return 0;}

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

//mod逆元を使ったnCkの計算(速)
const int64_t MAX = 510000;
const int64_t MOD = 1000000007;

int64_t fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++){
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
int64_t COM(int64_t n, int64_t k){
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {

    // input from txt （提出時にこの箇所は削除すること）
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());

    int64_t N, M;
    cin >> N >> M;
    COMinit();

    int64_t n = M;

    //素因数分解
    map<int64_t,int64_t> mp;
    int a = 2;
    while(n >= a*a){
      if(n %a == 0){
        if(mp.count(a)){
          mp[a]++;
        }
        else{
          mp[a] = 1;
        }
        n /= a;
      }
      else{
        a++;
      }
    }
    if(mp.count(n)){
      mp[n]++;
    }
    else{
      mp[n] = 1;
    }

    int64_t ans = 1;
    for(auto itr = mp.begin(); itr != mp.end(); itr++){
        int64_t x = itr->second;
        //cout << x << endl;
        ans *= COM(x+N-1,N-1);
        ans %= MOD;
    }
    if(M == 1){
        ans = 1;
    }

    cout << ans << endl;

}


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

int N;
ll D,A[202020];
vector<pair<ll,pair<int,int>>> G;

const int MAX_V=202020;

int par[MAX_V]; //　親
int rnk[MAX_V]; // rank, 木の深さ

void init(){
  for(int i=0;i<MAX_V;i++){
    par[i]=i;
    rnk[i]=0;
  }
}

int find(int x){
  if (par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y){
  x = find(x);
  y = find(y);
  if (x == y) return;

  if (rnk[x] < rnk[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}

void solve(int l, int r){
  if(r-l<=1) return;
  int mid=(l+r)/2;
  int miL=l;
  for(int i=l+1;i<mid;i++) if(A[i]-D*i<A[miL]-miL*D) miL=i;
  int miR=mid;
  for(int i=mid+1;i<r;i++) if(A[i]+D*i<A[miR]+miR*D) miR=i;
  for(int i=mid;i<r;i++) G.push_back({A[i]+A[miL]+D*(i-miL),{i,miL}});
  for(int i=l;i<mid;i++) G.push_back({A[i]+A[miR]+D*(miR-i),{i,miR}});
  solve(l,mid);
  solve(mid,r);
}

int main(){
  cin>>N>>D;
  rep(i,N) cin>>A[i];
  solve(0,N);

  sort(G.begin(),G.end());
  init();
  ll ans=0;
  for(auto& p:G){
    if(!same(p.second.first,p.second.second)){
      ans+=p.first;
      unite(p.second.first,p.second.second);
    }
  }
  cout<<ans<<endl;
  return 0;
}

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli;
#define fir first
#define sec second
#define Mp make_pair
const int MAXN = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;

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

ll a[MAXN];

int main(void)
{
	int n;
	ll d;
	scanf("%d%lld",&n,&d);
	for(int i=1; i<=n; ++i) scanf("%lld",&a[i]);

	ll ans=0;
	for(int i=1; i<=n; ++i) ans += a[i];

	for(int i=2; i<=n; ++i) chk_min(a[i], a[i-1]+d);
	for(int i=n-1; i>=1; --i) chk_min(a[i], a[i+1]+d);

	for(int i=1; i<=n; ++i) ans += a[i];
	printf("%lld",ans + d*(n-1) - a[1] - a[n]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;
typedef pair<int,int> P;

vector<int> num = {0,2,5,5,4,5,6,3,7,6};

string maxs(string S, string T){
    if(S.length() > T.length()){
        return S;
    } else if (S.length() < T.length()){
        return T;
    } else {
        return (S < T) ? T : S;
    }
}

int main(){
    int N, M; vector<int> A(9);

    cin >> N >> M;
    rep(i,M) cin >> A[i];

    // dp[i]: マッチちょうどi本で作れる最大値
    vector<string> dp(10010, "");
    rep(i,N) rep(j,M){
        if(dp[i] == "" && i != 0) continue;

        char c = A[j] + '0';
        if(i+num[A[j]] <= N){
            string next = dp[i] + c;
            dp[i+num[A[j]]] = maxs(dp[i+num[A[j]]], next);
        }
    }

    cout << dp[N] << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int match[10]={0,2,5,5,4,5,6,3,7,6};
    int n,m;
    cin>>n>>m;
    int a[m],x[m];
    for(int i=0;i<m;i++)cin>>a[i];
    sort(a,a+m,greater<int>());
    for(int i=0;i<m;i++)x[i]=match[a[i]];
    vector<int>dp(n+1,-1000000000);
    dp[0]=0;
    for(int i=0;i<n-1;i++){
        for(int j=0;j<m;j++)if(i+x[j]<=n)dp[i+x[j]]=max(dp[i+x[j]],dp[i]+1);
    }
    while(n){
        for(int i=0;i<m;i++){
            if(n-x[i]>=0 && dp[n-x[i]]==dp[n]-1){
                cout<<a[i];
                n-=x[i];
                break;
            }
        }
    }
    cout<<endl;
    return 0;
}


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

// #define int ll
#define REP(i,n) for (int i = 0; i < (n); ++i)
#define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << '\n'

const int MOD = (int)1e9 + 7;

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

    vector<int> vec(N);
    REP(i, N) {
        cin >> vec.at(i);
    }

    // 偶数indで、一番多いものに合わせる
    map<int, int> even_map;
    for(int i = 0; i < N; i+=2) {
        int val = vec[i];

        if (even_map.count(val) == 1) {
            even_map[val] += 1;
        } else {
            even_map[val] = 1;
        }
    }

    // 奇数indで、一番多いものに合わせる
    map<int, int> odd_map;
    for(int i = 1; i < N; i+=2) {
        int val = vec[i];

        if (odd_map.count(val) == 1) {
            odd_map[val] += 1;
        } else {
            odd_map[val] = 1;
        }
    }

    int odd_most_freq = -1;
    int odd_most_freq_cnt = 0;

    int odd_2nd_most_freq;
    int odd_2nd_most_freq_cnt = 0;

    for (auto x : odd_map){
        int key = x.first;
        int value = x.second;

        if (value > odd_most_freq_cnt) {
            odd_2nd_most_freq = odd_most_freq;
            odd_2nd_most_freq_cnt = odd_most_freq_cnt;

            odd_most_freq = key;
            odd_most_freq_cnt = value;
        } else if (value > odd_2nd_most_freq_cnt) {
            odd_2nd_most_freq = key;
            odd_2nd_most_freq_cnt = value;
        }
    }

    int even_most_freq = 0;
    int even_most_freq_cnt = 0;

    int even_2nd_most_freq;
    int even_2nd_most_freq_cnt = 0;
    for (auto x : even_map){
        int key = x.first;
        int value = x.second;
        debug(key);
        debug(value);

        if (value > even_most_freq_cnt) {
            even_2nd_most_freq = even_most_freq;
            even_2nd_most_freq_cnt = even_most_freq_cnt;

            even_most_freq = key;
            even_most_freq_cnt = value;
        } else if (value > even_2nd_most_freq_cnt) {
            even_2nd_most_freq = key;
            even_2nd_most_freq_cnt = value;
        }
    }

    // REP(i, N) {
    //     int v = vec.at(i);
    //     debug(v);
    // }

    debug(even_most_freq);
    debug(even_most_freq_cnt);
    debug(even_2nd_most_freq);
    debug(even_2nd_most_freq);

    debug(odd_most_freq);
    debug(odd_most_freq_cnt);
    debug(odd_2nd_most_freq);
    debug(odd_2nd_most_freq);

    int evens = N % 2 == 0 ? (N / 2) : (N+1) / 2;
    debug(evens);

    int ans_even = evens - even_most_freq_cnt;
    debug(ans_even);

    int odds = N / 2;
    debug(odds);

    int ans_odd = odds - odd_most_freq_cnt;
    debug(ans_odd);

    if (even_most_freq == odd_most_freq) {
        int a = odds - odd_2nd_most_freq_cnt + ans_even;
        int b = evens - even_2nd_most_freq_cnt + ans_odd;

        int c = a < b ? a : b;
        cout << c << endl;

        return 0;
    }

    cout << ans_even + ans_odd << endl;
}

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

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

    string s[3];
    rep(i, 3) cin >> s[i];

    int ans = 0;
    rep(i, n) {
        set<char> st;
        rep(j, 3) st.insert(s[j][i]);
        ans += st.size() - 1;
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

typedef std::pair<int, int> pii;

namespace IO {
	const int lim = 1 << 20 | 50;

	char buf[lim + 5], *S, *T;

	inline char gc() {
		if (S == T) {
			T = (S = buf) + fread(buf, 1, lim, stdin);
			if (S == T) return EOF;
		}
		return *S++;
	}

	inline int read() {
		int x; bool f; char c;
		for (f = 0; (c = gc()) < '0' || c > '9'; f = c == '-');
		for (x = c ^ '0'; (c = gc()) >= '0' && c <= '9'; x = x * 10 + c - '0');
		return f ? -x : x;
	}
}

const int MN = 1e5 + 5, MV = 5e5 + 5;

std::map<pii, int> mp;

int n, m, dist[MV], vn;

bool vis[MV];

std::deque<int> q;

std::vector<int> G[MV];

void solve() {
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0; q.push_back(1);
	for (; !q.empty(); ) {
		int u = q.front(); q.pop_front();
		if (vis[u]) continue; vis[u] = 1;
		for (unsigned int i = 0; i < G[u].size(); ++i) {
			int v = G[u][i];
			if (vis[v] || (dist[u] + (u <= n)) >= dist[v]) continue;
			dist[v] = dist[u] + (u <= n);
			if (u <= n) q.push_back(v);
			else q.push_front(v);
		}
	}
}

int main() {
	vn = n = IO::read();

	for (m = IO::read(); m; --m) {
		int x = IO::read(), y = IO::read(), c = IO::read();
		if (mp.find(pii(x, c)) == mp.end()) {
			mp[pii(x, c)] = ++vn;
			G[vn].push_back(x);
			G[x].push_back(vn);
		}
		if (mp.find(pii(y, c)) == mp.end()) {
			mp[pii(y, c)] = ++vn;
			G[vn].push_back(y);
			G[y].push_back(vn);
		}
		x = mp[pii(x, c)];
		y = mp[pii(y, c)];
		G[x].push_back(y);
		G[y].push_back(x);
	}

	solve();

	printf("%d\n", dist[n] == 0x3f3f3f3f ? -1 : dist[n]);

	return 0;
}
/*
8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5
*/

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mp make_pair
#define ld long double
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int> > small_heap;
typedef priority_queue<int> big_heap;
const int N = 1e6 + 100;
int T;
int a, b, m;
int aa[N], bb[N];
pair<pii, int> c[N];

int main() {
    ios::sync_with_stdio(false);
    cin >> a >> b >> m;
    for (int i = 1; i <= a; i++)cin >> aa[i];
    for (int i = 1; i <= b; i++)cin >> bb[i];
    for (int i = 1; i <= m; i++)cin >> c[i].first.first >> c[i].first.second >> c[i].second;
    int ans = *min_element(aa + 1, aa + a + 1) + *min_element(bb + 1, bb + b + 1);
    for (int i = 1; i <= m; i++) {
        ans = min(ans, aa[c[i].first.first] + bb[c[i].first.second] - c[i].second);
    }
    cout << ans << endl;
    return 0;
}


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

int main() {
	string a, b, c;
	int flag = 0;
	int sa = 0, sb = 0,sc = 0;

	cin >> a >> b >> c;

	while (1) {

		while (flag == 0) {

			if (a[sa] == 'a')flag = 0;
			else if (a[sa] == 'b')flag = 1;
			else if (a[sa] == 'c')flag = 2;
			 else if (a[sa] == '\0') {
				cout << 'A' << endl; return 0;
			}
			sa++;
		}
		while (flag == 1) {

			if (b[sb] == 'a')flag = 0;
			else if (b[sb] == 'b')flag = 1;
			else if (b[sb] == 'c')flag = 2;
			else if (b[sb] == '\0') {
				cout << 'B' << endl; return 0;
			}
			sb++;
		}
		while (flag == 2) {
			if (c[sc] == 'a')flag = 0;
			else if (c[sc] == 'b')flag = 1;
			else if (c[sc] == 'c')flag = 2;
			else if (c[sc] == '\0') {
				cout << 'C' << endl; return 0;
			}
			sc++;
		}
	}
}

#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <fstream>

using namespace std;

ifstream fin("../data.txt");

char sa[104], sb[104], sc[104], *s;
int ia, ib, ic, *i;
char winner;

int main() {

  cin.getline(sa, 104);
  cin.getline(sb, 104);
  cin.getline(sc, 104);

  s = sa;
  i = &ia;
  while (*i < strlen(s)) {
    *i += 1;
    if (s[(*i)-1] == 'a') {
      s = sa;
      i = &ia;
      winner = 'A';
    } else if (s[(*i)-1] == 'b') {
      s = sb;
      i = &ib;
      winner = 'B';
    } else if (s[(*i)-1] == 'c') {
      s = sc;
      i =
      winner = 'C';
    }
  }

  cout << winner;
  return 0;
}


#include <bits/stdc++.h>
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 (b<a) { a=b; return 1; } return 0; }

using ll = long long;
using lb = long double;
using P = pair<ll,ll>;
using graph = vector<vector<ll>>;
using V = vector<ll>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll hcnt[300005],wcnt[300005];

int main() {
  ll N,K;
  cin>>N>>K;
  vector<ll> v(N);
  rep(i,N) cin>>v[i];
  ll ans = -INF;
  rep(i,N+1){
    rep(j,N-i+1){
      if(i+j>K)continue;
      priority_queue<ll,vector<ll>,greater<ll>> q;
      rep(k,i) q.push(v[k]);
      rep(k,j) q.push(v[N-k-1]);
      rep(k,K-i-j){
        if(q.empty()) break;
        if(q.top()<0) q.pop();
      }
      ll cnt = 0;
      while(!q.empty()) {
        cnt += q.top();
        q.pop();
      }
      chmax(ans,cnt);
    }
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int n,c,dp[55][55][55][55];
vi v;

int f(int i,int cnt,int mxgap,int lst){
    if(i==n){
        mxgap=max(mxgap,i-lst-1);
        if(cnt+(n-cnt-mxgap)*2<=c)return 0;
        return -INF;
    }
    int &ans=dp[i][cnt][mxgap][lst+1];
    if(ans!=-INF)return ans;
    ans=v[i]+f(i+1,cnt+1,max(mxgap,i-lst-1),i);
    ans=max(ans,f(i+1,cnt,mxgap,lst));
    return ans;
}

signed main(){
    FASTER;
    cin>>n>>c;
    v=vi(n);cin>>v;
    for(int i=0;i<55;i++){
        for(int j=0;j<55;j++){
            for(int k=0;k<55;k++){
                for(int l=0;l<55;l++){
                    dp[i][j][k][l]=-INF;
                }
            }
        }
    }
    cout<<f(0,0,0,-1);
}


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

int main() {
    cin.tie(0)->sync_with_stdio(false);

    string s;
    cin >> s;

    map<char, bool> a;
    REP(i, 26) a['a' + i] = false;
    int n = s.size();
    REP(i, n) a[s[i]] = true;

    int cnt = 0;
    for (auto i : a)
        if (i.second == true) cnt++;

    string ans;
    if (cnt != 26) {
        char c;
        for (auto i : a)
            if (i.second == false) { c = i.first; break ; }
        ans = s + c;
    } else if (cnt == 26) {
        int index = n - 1;
        for (int i = n - 1; i > 0; i--) {
            if (s[i - 1] - s[i] > 0) index = i - 1;
            else break ;
        }
        if (index == 0) ans = "-1";
        else {
            string tmp = s.substr(index);
            sort(tmp.begin(), tmp.end());
            char c;
            REP(i, tmp.size())
                if (tmp[i] > s[index - 1]) { c = tmp[i]; break ; }
            s.erase(index - 1);
            ans = s + c;
        }
    }

    cout << ans << '\n';
    return 0;
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a*b/gcd(a, b);
}

int main()
{
    cout << fixed << setprecision(15);
    string S;
    cin >> S;

    ll N = S.length();

    map<char, bool> A;

    REP(i, N){
        A[S[i]] = true;
    }
    if(N<26){
        cout << S;
        REP(i, 26){
            // cout << char(i+'a') << endl;
            char c = char(i+'a');
            if(!A.count(c)){
                cout << c << endl;
                return 0;
            }
        }
    }

    vector<char> T;
    for(ll i=25; i>=0; i--){
        if(i ==25){
            T.push_back(S[i]);
            continue;
        }
        // cout << S[i] << endl;
        sort(T.begin(), T.end());
        // cout << T[0] << endl;
        if(i==0){
            if(S[0] != 'z'){
                cout << char(S[0]+1) << endl;
            }
            else{
                cout << -1 << endl;
            }
            return 0;
        }
        else if(S[i] < T[T.size()-1]){
            REP(j, i){
                cout << S[j];
            }
            for(auto v: T){
                if(v > S[i]){
                    cout << v << endl;
                    return 0;
                }
            }
        }
        T.push_back(S[i]);
    }
    return 0;
}


                                      // Believe in yourself
#include<bits/stdc++.h>

using namespace std;

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

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define Pair vector<pair<ll,ll>>
#define vec vector <ll>
#define all(a) a.begin(),a.end()
#define mo 1000000007
#define decimal(i,n) fixed << setprecision(i) << n
#define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl;
#define show_nl(a) for(auto xyz:a)cout<<xyz<<endl;

void eea(ll a,ll b,ll &x,ll &y)
{
    if (a == 1)
    {
        x = 1; y = 0;
        return;
    }
    ll x1, y1;
    eea(b%a,a,x1,y1);
    x=y1-(b/a)*x1;
    y=x1;
}
ll mi(ll a,ll m)
{
    ll x,y;
    eea(a,m,x,y);
    x= (x%m+m)%m;
    return x;
}

/*
Enter the test case

Some helpful points
1.) Calm yourself
2.) check for semicolon or data type mismatch
*/

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t=1;
    // cin>>t;
    while(t--)
    {
       int n,k;
       cin>>n>>k;
       double a[n];
       double s=0;
       double ans=0;
       for(int i=0;i<n;i++)
       {
           cin>>a[i];
           a[i]=a[i]+1;
       }
       for(int i=0;i<k;i++)
       s=s+a[i];
       for(int i=k;i<n;i++)
       {
           if(ans<s)
           ans=s;
           s=s-a[i-k]+a[i];
       }
       if(ans<s)
           ans=s;
       cout<<decimal(9,ans/2)<<"\n";
    }
}

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

// D - Dice in Line
int main() {
    int N, K;
    cin >> N >> K;

    long double p[N];
    int tmpP;
    for (int i = 0; i < N; i++) {
        cin >> tmpP;
        p[i] = ((tmpP + 1.0) / 2.0);
    }

    /*
    // debug
    for (int i = 0; i < N; i++) {
        cout << p[i] << endl;
    }
    */

    long double nowSum = 0, maxSum = 0;
    for (int i = 0; i < K; i++) {
        nowSum = nowSum + p[i];
    }
    // cout << "nowSum:" << nowSum << endl; // debug
    maxSum = nowSum;
    for (int i = K; i < N; i++) {
        nowSum = nowSum - p[i - K] + p[i];
        maxSum = max(nowSum, maxSum);
    }
    // cout << maxSum;
    printf("%Lf", maxSum);
}


#include<stdio.h>

int main(){
	char year[10], month[5], day[5];
	scanf("%[^/]/%[^/]/%s", &year, &month, &day);
	getchar();
	printf("2018/01/%s\n", day);
	return 0;
}

#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <list>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int Partition(int a[], int p, int r) {
	int x = a[r];
	int i = p-1;
	for(int j = p; j < r; j++) {
		if(a[j] <= x) {
			i++;
			int temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	}
	int temp = a[i+1];
	a[i+1] = x;
	a[r] = temp;
    return i+1;
}

int main() {
	int n;
	cin >> n;
	int a[n];
	for( int i = 0; i < n; i++) cin >> a[i];
	int b = Partition(a, 0, n-1);
	for( int i = 0; i < n; i++) {
		if(i == b) {
			if( i ) cout << " [" <<a[i] << "]";
			else cout << "[" << a[i] << "]";
		} else if( i ) cout << " " <<a[i];
		else cout << a[i];
	}
	cout << endl;
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define REP(e,v) for(auto e:v)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tii tuple<int,int,int>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define edge(v,a,b) v[a].pb(b);v[b].pb(a);
#define MAX_V 400010
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define ALL(c) (c).begin(),(c).end()
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}

#define INF 1e9+7
#define LLINF 1e18+7
ll MOD=1e9+7;
#define N 1055050
ll dp[50][1<<20];
bool judge[N];
ll pw[50];
main(){
    ios_base::sync_with_stdio(false);
    int n=in();
    int x[3];
    rep(i,3)x[i]=in();
    int sum=x[0]+x[1]+x[2];
    ll ans=0;
    pw[0]=1;
    rep(i,20)pw[i+1]=pw[i]*2;
    dp[0][0]=1;
    vec all;
    rep(i,1<<sum){
        if((i&(1<<(x[2]-1))&&(i&(1<<(x[2]+x[1]-1))&&(i&1<<(sum-1)))))judge[i]=1;
        else all.pb(i);
    }
    rep(i,n){
        for(auto k:all){
            if(dp[i][k]==0)continue;
            rep2(j,1,7){
                ll next=(k*pw[j]+pw[j-1])%pw[sum];
                if(!judge[next])
                dp[i+1][next]=(dp[i][k]+dp[i+1][next])%MOD;
            }
            dp[i+1][0]=(dp[i+1][0]+dp[i][k]*3)%MOD;
        }
    }
    ll p=1;
    rep(i,n){
        p=p*10ll%MOD;
    }
    rep(i,1<<sum){
        p=(p-dp[n][i]+MOD)%MOD;
    }
    cout<<p<<endl;
}

#include <bits/stdc++.h>

using namespace std;

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

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

int n;
int x, y, z;

map <vector<int>, int> mp;
vector<int> seq[205000];
int len[205000];
int seqSum[205000];
bool haiku[205000];
int cnt;
int mxSum = 0;
int mxNum = 0;

bool check(vector<int>& v) {
    int p = 0;
    int xx = x, yy = y, zz = z;
    while (p < sz(v) && xx > 0) {
        xx -= v[p];
        p++;
    }
    while (p < sz(v) && yy > 0) {
        yy -= v[p];
        p++;
    }
    while (p < sz(v) && zz > 0) {
        zz -= v[p];
        p++;
    }
    return xx == 0 && yy == 0 && zz == 0 && p == sz(v);
}

void go(vector<int>& v, int sum) {
    mp[v] = cnt;
    len[cnt] = sz(v);
    seq[cnt] = v;
    seqSum[cnt] = sum;
    haiku[cnt] = check(v);
    cnt++;

    for (int i = 1; i <= min(mxNum, mxSum - sum); i++) {
        v.push_back(i);
        go(v, sum + i);
        v.pop_back();
    }
}

long long dp[45][2][130500];
int mv[130500][12];

int main() {

    cin >> n >> x >> y >> z;
    mxSum = x + y + z;
    mxNum = max(max(x, y), z);

    vector<int> v;
    go(v, 0);

    // cerr << cnt << endl;

    for (int i = 0; i < cnt; i++) {
        for (int num = 1; num <= mxNum; num++) {
            int sum = seqSum[i] + num;
            int p = 0;
            while (p < sz(seq[i]) && sum > mxSum) {
                sum -= seq[i][p];
                p++;
            }
            vector<int> go;
            while (p < sz(seq[i])) {
                go.push_back(seq[i][p]);
                p++;
            }
            go.push_back(num);
            mv[i][num] = mp[go];
        }
    }

    dp[0][0][0] = true;
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= 1; j++)
            for (int k = 0; k < cnt; k++) {
                if (dp[i][j][k] == 0) continue;
                if (haiku[k] && j == 0) continue;
                if (len[k] > i) continue;

                for (int num = 1; num <= mxNum; num++) {
                    int goK = mv[k][num];
                    dp[i + 1][j | haiku[goK]][goK] = (dp[i + 1][j | haiku[goK]][goK] + dp[i][j][k]) % mod;
                }
                dp[i + 1][j][0] = (dp[i + 1][j][0] + dp[i][j][k] * (10 - mxNum)) % mod;
            }

    long long ans = 0;
    for (int i = 0; i < cnt; i++)
        ans = (ans + dp[n][1][i]) % mod;

    cout << ans << endl;

    return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define sz(x) (int)(x).size();
#define all(x) x.begin(), x.end()
#define trav(i,a) for(auto &i:a)
inline int in(){int x;scanf("%lld",&x);return x;}
int h[10]={0,2,5,5,4,5,6,3,7,6};
int32_t main()
{
	int n=in();int m=in();
	vector<char> a(m);
	trav(i,a)cin>>i;
	vector<string> dp(n+10);
	//dp[i] best answer with i sticks
	//making only given digits from a(m)
	dp[0]="";
	for(int i=0;i<m;i++)
	{
		dp[h[a[i]-'0']]=max(dp[h[a[i]-'0']],(string)""+a[i]);
	}
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(dp[i]=="")continue;
			if((i+h[a[j]-'0'])>n)continue;
			if(dp[i+h[a[j]-'0']].size()>dp[i].size()+1)continue;
			if(dp[i+h[a[j]-'0']].size()<dp[i].size()+1)
			{
				dp[i+h[a[j]-'0']]=dp[i]+a[j];
			}
			else
			dp[i+h[a[j]-'0']]=max(dp[i]+a[j],dp[i+h[a[j]-'0']]);

		}
	}
	cout<<dp[n];

}

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

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
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; }

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

ll n, m;

struct numinfo {
	int digit;
	int match;
	int min_match;
	int n;
};

vector<numinfo> nums;

bool dfs( int i, int remain_match, int remain_keta ){
	if ( i == SZ(nums) ){
		eprintf( "remain=%d %d   ", remain_match, remain_keta );
		rep(k,SZ(nums)){
			eprintf( "%d ", (int)nums[k].n );
		}
		eprintf( "\n" );
		if ( remain_match == 0 && remain_keta <= 0 )
			return true;
		return false;
	}
	for ( int k = remain_match/nums[i].match; k >= 0; k-- ){
		nums[i].n = k;
		int creatable_keta = (remain_match-k*nums[i].match)/nums[i].min_match;
		if ( remain_keta-k-creatable_keta > 0 )
			continue;
		if ( dfs( i+1, remain_match-k*nums[i].match, remain_keta-k ) )
			return true;
	}
	return false;
}

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

	vector<int> a(m);
	rep(i,m){
		cin >> a[i];
	}
	sort(a.rbegin(),a.rend());

	int digit_to_match[] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
	set<int> s;
	rep(i,m){
		int match = digit_to_match[a[i]];
		if ( s.count(match) == 0 )
			nums.push_back({a[i],match,100,0});
		s.insert(match);
	}
	m = nums.size();

	int min_match = 100;
	for ( int i = nums.size()-1; i >= 0; i-- ){
		nums[i].min_match = min_match;
		chmin(min_match, nums[i].match);
		eprintf( "digit,=%d match=%d min_match=%d\n", nums[i].digit, nums[i].match, nums[i].min_match );
	}

	for ( int keta = n / min_match; keta > 0; keta-- ){
		eprintf( "\n\nketa=%d\n", keta );
		if ( dfs( 0, n, keta ) ){
			sort(nums.begin(), nums.end(), [](numinfo &l, numinfo &r){
				return l.digit>r.digit;
			});
			rep(i,m){
				rep(j,nums[i].n){
					cout << nums[i].digit;
				}
			}
			cout << endl;
			return 0;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
using namespace std;
int n, a[100005];
vector<pair<int,int>> vec, vec2;
unordered_map<int,int> mp, mp2;
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifdef debug
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    cin >> n;
    for(int i = 1; i <= n; i++)cin >> a[i];
    for(int i = 1; i <= n; i+= 2)mp[a[i]]++;
    for(int i = 2; i <= n; i += 2)mp2[a[i]]++;
    for(auto i : mp)vec.pb({i.s, i.f});
    for(auto i : mp2)vec2.pb({i.s, i.f});
    sort(vec.begin(), vec.end()); sort(vec2.begin(), vec2.end());
    int cnt = 1e9;
    int delta = n/2;
    if(vec.size() == 1 && vec2.size() == 1){
        if(vec[0].s == vec2[0].s){
            cnt = delta;
        }
        else{
            cnt = 0;
        }
    }
    else{
        if(vec[vec.size()-1].s != vec2[vec2.size()-1].s){
            cnt = delta - vec[vec.size()-1].f + delta - vec2[vec2.size()-1].f;
        }
        else{
            if(vec.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-1].f + delta - vec[vec.size()-2].f);
            else cnt = min(cnt, delta + delta - vec2[vec2.size()-1].f);
            if(vec2.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-2].f + delta - vec[vec.size()-1].f);
            else cnt = min(cnt, delta + delta - vec[vec.size()-1].f);
        }
    }
    cout << cnt << endl;
    return 0;
}

#include<cstdio>
#include<algorithm>
using ll = long long;
struct edge{int u, v, cost;};
const static int MAX_N = 100000;
int par[MAX_N];
int rank[MAX_N];

void init_union_find(int n){
	for (int i = 0; i < n; ++i){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){
	if(par[x] == x) return x;
	else return par[x] = find(par[x]);
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y]) rank[x]++;
	}
}

bool same(int x, int y){
	return find(x) == find(y);
}

bool comp(const edge& e1, const edge& e2){
	return e1.cost < e2.cost;
}

int main(){
	int N;
	scanf("%d", &N);
	std::pair<int, int> x[N], y[N];
	for (int i = 0; i < N; ++i){
		x[i].second = y[i].second = i;
		scanf("%d %d", &x[i].first, &y[i].first);
	}
	std::sort(x, x + N), std::sort(y, y + N);
	edge es[2*(N - 1)];
	for (int i = 0; i < N - 1; ++i){
		es[i].u = x[i].second;
		es[i].v = x[i + 1].second;
		es[i].cost = x[i + 1].first - x[i].first;
	}
	for (int i = N - 1; i < 2*(N - 1); ++i){
		es[i].u = y[i + 1 - N].second;
		es[i].v = y[i + 2 - N].second;
		es[i].cost = y[i + 2 - N].first - y[i + 1 - N].first;
	}
	std::sort(es, es + 2*(N - 1), comp);
	init_union_find(N);
	int total = 0;
	for (int i = 0; i < 2*(N - 1); ++i){
		edge e = es[i];
		if(!same(e.u, e.v)){
			unite(e.u, e.v);
			total += e.cost;
		}
	}
	printf("%d\n", total);
	return 0;
}


#include <iostream>
using namespace std;

int main(){

    float d,t,s;

    cin>>d>>t>>s;

    float time = d/s;
    if (time > t){
        cout<<"No";
    }
    else{
        cout<<"Yes";
    }

}

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int i = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int i = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define MP make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1 << 30;
const ll LINF = 1LL << 61;
const int MOD = 1e9 + 7;

int main() {
    int N, H, W;
    cin >> N >> H >> W;
    cout << (N - W + 1) * (N - H + 1) << endl;
    return 0;
}


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

#define int ll

#define _GLIBCXX_DEBUG

#define REP(i,n) for (int i = 0; i < (n); ++i)
#define REPD(i,n) for (int i = (n-1); i >= 0; --i)
#define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define debug(x) cerr << #x << ": " << x << '\n'
#define hyphen() cerr << "--\n"

#define ALL(vec) (vec).begin(), (vec).end()
#define REVALL(vec) (vec).rbegin(), (vec).rend()

#define fst first
#define pb push_back

const int MOD = (int)1e9 + 7;
const int INF = numeric_limits<int>::max();
const int BIT = 10;

signed main() {
    int D, G;

    cin >> D >> G;

    vector<int> Ps(D);
    vector<int> Cs(D);
    REP(i, D) {
        cin >> Ps.at(i) >> Cs.at(i);
    }

    // どの問題をコンプリートしたかは、10ビットで表現できる。
    // そこから、それ以上コンプリートしないように注意しつつ、高い配点の問題から解いていく

    int ans = INF;

    REP(tmp, (1 << D)) {
        bitset<BIT> bs(tmp);

        int score = 0;
        int solved = 0;
        REP(i, D) {
            if (bs.test(i)) {
                solved += Ps[i];
                score += 100 * (i+1) * Ps[i] + Cs[i];
            }
        }

        // もし既に達成していたら、それでOK
        if (score >= G) {
            if (solved < ans) {
                ans = solved;

                debug(bs);
                debug(score);
                debug(solved);
                hyphen();
            }
            continue;
        }

        // 達成していない場合は、ポイントの高い問題から解いていく
        REPD(i, D) {
            if (bs.test(i)) {
                continue;
            }

            if (Ps[i] == 1) {
                continue;
            }

            int point = 100 * (i+1);
            int solve_but_one_score = score + (point * (Ps[i] - 1));
            if (solve_but_one_score < G) {
                // まだ足りない → 次へ
                score = solve_but_one_score;
                solved += (Ps[i] - 1);

                continue;
            } else {
                // 足りた → 解き過ぎないようにして〆
                // ceilはx.00001の時がこわい
                int solved_this;
                if ((G - score) % point == 0) {
                    solved_this = (G - score) / point;
                } else {
                    solved_this = (G - score) / point + 1;
                }
                score += point * solved_this;
                solved += solved_this;

                if (solved < ans) {
                    ans = solved;

                    debug(bs);
                    debug(score);
                    debug(solved);
                    hyphen();
                }

                break;
            }
        }

        // ここに辿り着いた時には、もう打つ手立てが無いので、
        // 更新処理を行わない

        // debug(bs);
        // debug(score);
        // debug(solved);
        // hyphen();
    }

    cout << ans << endl;
}

#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
using namespace std;
int main()
{
	int h,w;
	cin>>h>>w;
	int a[h+1][w+1];
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			char c;cin>>c;
			a[i][j]= c=='#';
		}
	}

	int b[h][w];

	for(int i=1;i<h;i++)
	{
		for(int j=1;j<w;j++)
		{
			int k=a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1];

			b[i][j]= k%2==0;

		}

	}

	int ht[w+1]={};
    int next[w+1];
    int prev[w+1];
    int ans=max(h,w);
	for(int i=1;i<h;i++)
	{
		for(int j=1;j<w;j++)
		    ht[j]=(b[i][j]==1)? ht[j]+1: 0;

		ht[w]=ht[0]=-1;

	    stack<int> st;
	    st.push(0);

	    for(int j=1;j<w;j++)
	    {
	    	while(ht[st.top()]>=ht[j])st.pop();
	    	prev[j]=st.top();
	    	st.push(j);
	    }
	    while(!st.empty())st.pop();

	    st.push(w);

	    for(int j=w-1;j>0;j--)
	    {
	    	while(ht[st.top()]>=ht[j])st.pop();
	    	next[j]=st.top();
	    	st.push(j);
	    }

	    for(int j=1;j<w;j++)
	    {
	    	ans=max(ans,(ht[j]+1)*(next[j]-prev[j]));
	    }

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


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

ll INF = 1000000000;
vector <int> dy = {0, 1, 0, -1};
vector <int> dx = {1, 0, -1, 0};

int main(){
    int h, w; cin >> h >> w;
    vector <string> s(h);
    int black_cnt = 0;
    rep(i, h){
        cin >> s.at(i);
        for(auto c: s.at(i)){
            if(c == '#') black_cnt ++;
        }
    }

    deque <pair <int, int>> d;
    vector <vector<int>> dist(h, vector<int>(w, INF));

    d.push_back(make_pair(0, 0));
    dist.at(0).at(0) = 0;

    while(!d.empty()){
        auto v = d.front();
        d.pop_front();
        int y, x;
        y = v.first; x = v.second;
        rep(i, 4){
            int ny, nx;
            ny = y + dy.at(i);
            nx = x + dx.at(i);

            if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
            if(s.at(ny).at(nx) == '#') continue;
            if(dist.at(ny).at(nx) <= dist.at(y).at(x) + 1) continue;

            dist.at(ny).at(nx) = dist.at(y).at(x) + 1;
            d.push_back(make_pair(ny, nx));
        }
    }

    if(dist.at(h - 1).at(w - 1) == INF){
        cout << -1 << endl;
        return 0;
    }

    //cout << dist.at(h - 1).at(w - 1) << endl;
    cout << h * w - black_cnt - dist.at(h - 1).at(w - 1) - 1 << endl;
}

#include<stdio.h>
#include<algorithm>
using namespace std;
int c[100000];
int d[100000];
int main(){
	int a,b;
	scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++){
		scanf("%d",c+i);
	}
	for(int i=0;i<b;i++){
		int e;
		scanf("%d",&e);
		for(int j=0;j<a;j++){
			if(e>=c[j]){
				d[j]++;
				break;
			}
		}
	}
	int at=0;
	int v=0;
	for(int i=0;i<a;i++)if(v<d[i]){
		v=d[i];
		at=i;
	}
	printf("%d\n",at+1);
}


#include <iostream>
#define N 100
#define WHITE 0
#define BLACK 1
#define INF 10000

using namespace std;

int M[N][N],n,d[N];

void prim(){
  int color[n],p[n];
  for(int i=0;i<n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[0]=0;
  p[0]=-1;
  while(1){
    int mincost=INF;
    int u;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(mincost==INF) break;
    //connect u to p[u]
    color[u]=BLACK;
    for(int v=0;v<n;v++){
      if(color[v]!=BLACK && M[u][v]!=-1){
        if(M[u][v]<d[v]){
          d[v]=M[u][v];
          p[v]=u;
        }
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> M[i][j];
  }
  prim();
  int sum=0;
  for(int i=0;i<n;i++) sum+=d[i];
  cout << sum << endl;
}

// AOJ ALDS 1_12_A
#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[x] )
		return x;
	else
		return par[x] = find( par[x] );
}

void unite( int x, int y )
{
	x = find( x );
	y = find( y );
	if( x == y )
		return;

	if( rank[x] < rank[y] )
		par[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	scanf( "%d", &n );
	rep( i, n )
	{
		rep( j, n )
		{
			int a;
			scanf( "%d", &a );
			if( ~a )
				es.push_back( edge( i, j, a ) );
		}
	}

	std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

	init();

	int ans = 0;
	rep( i, es.size() )
	{
		if( !same( es[i].from, es[i].to ) )
		{
			ans += es[i].cost;
			unite( es[i].from, es[i].to );
		}
	}

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

	return 0;
}


#define _GLIBCXX_DEBUG //エラーを出すためのおまじない
#include <bits/stdc++.h>
#include <algorithm>
//#include <cstdlib>  // abs() for integer
#include <cmath>    // abs() for float, and fabs()
#include <math.h>
using namespace std;
#define coYES cout << "YES" << endl
#define coNO cout << "NO" << endl
#define coYes cout << "Yes" << endl
#define coNo cout << "No" << endl
#define coyes cout << "yes" << endl
#define cono cout << "no" << endl
//#define cocount cout << count << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define Cinint(n) int n;\
cin >> n;
#define Cinvecint(a, n) vector<int> a(n);\
for (int i = 0; i < (int)(n); i++) {\
  int num;\
  cin >> num;\
  a.at(i) = num;\
}
#define Cinstring(s) string s;\
cin >>s;

int main() {
  Cinint(n);
  Cinstring(s);
  int64_t count = 0;
  int64_t Rcount = 0;
  int64_t Gcount = 0;
  int64_t Bcount = 0;
  rep(i, n) {
    if (s.at(i) == 'R') {
      Rcount++;
    }
    else if (s.at(i) == 'G') {
      Gcount++;
    }
    else {
      Bcount++;
    }
  }
  count = Rcount*Gcount*Bcount;
  rep(i, n-2) {
    for (int j = i+1; j < n-1; j++) {
      int sa = j-i;
      int k = j+sa;
      if (k <= n-1) {
        if (s.at(i) == 'R') {
          if (s.at(j) == 'G') {
            if (s.at(k) == 'B') {
              count--;
            }
          }
          else if (s.at(j) == 'B') {
            if (s.at(k) == 'G') {
              count--;
            }
          }
        }
        else if (s.at(i) == 'G') {
          if (s.at(j) == 'B') {
            if (s.at(k) == 'R') {
              count--;
            }
          }
          else if (s.at(j) == 'R') {
            if (s.at(k) == 'B') {
              count--;
            }
          }
        }
        else {
          if (s.at(j) == 'R') {
            if (s.at(k) == 'G') {
              count--;
            }
          }
          else if (s.at(j) == 'G') {
            if (s.at(k) == 'R') {
              count--;
            }
          }
        }
      }
    }
  }
  cout << count << endl;
}


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

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)
#define F first
#define S second

using namespace std;
int n,key[110];
string s;
char alp[52] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
              'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int main() {
  while(1){
    cin>>n;
    if(n==0) break;
    REP(i,0,n) cin>>key[i];
    cin>>s;
    REP(i,0,s.size()){
      int pos=-1;
      REP(j,0,52){
        if(alp[j]==s[i]){
          pos = j;
          break;
        }
      }
      cout<<alp[(pos - key[i%n] + 52*10000)%52];
    }
    cout<<endl;
  }
  return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

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

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

bool solve(){
  int n;
  int k[SIZE];
  char c[SIZE];

  scanf("%d",&n);

  if(n == 0) return false;

  for(int i=0;i<n;i++){
    scanf("%d",k+i);
  }

  scanf("%s",c);

  for(int i=0;c[i]!='\0';i++){

    for(int j=0;j<k[i%n];j++){
      if(c[i] == 'a')
        c[i] = 'Z';
      else if(c[i] == 'A')
        c[i] = 'z';
      else
        c[i]--;
    }

  }

  printf("%s\n",c);

  return true;

}

int main(){

  while(solve());

  return 0;
}


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

int main() {
	int n, a;
	vector<int> an;
	vector<vector<int>> AN;
	while (cin >> n) {
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> a;
			an.push_back(a);
		}
		AN.push_back(an); an.clear();
	}

	for (int i = 0; i < AN.size(); i++) {
		long long int max = 0, min = INT_MAX, MAX = 0, MIN = 0 , counter = 0, negative = 0;
		for (int j = 0; j < AN[i].size(); j++) {
			if (AN[i][j] >= 0) {
				max += AN[i][j];
				if (MAX < max)MAX = max;
				counter = 1;
				negative = 1;
			}

			else {
				if (negative == 0) {
						if(min > abs(AN[i][j]))min=abs(AN[i][j]);
					}
				else if (counter==1){
					if (AN[i][j] + max >= 0)max += AN[i][j];
					else {
						max = 0;
						counter = 0;
					}
				}
			}
		}
		if (negative == 0)cout << -1*min << endl;
		else cout << MAX << endl;
	}
	return 0;
}

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

template <typename Iterator>
auto findMaximumSum (Iterator begin , Iterator end) -> long long
{
    long long res = * begin;
    auto tmp = res;
    for (auto ite = begin + 1; ite != end; ++ ite)
    {
        tmp += * ite;
        res = std::max (res , tmp);
    }
    return res;
}

auto main () -> int
{
    int n;
    while (std::cin >> n && n)
    {
        std::vector <int> a (n);
        for (auto && elem : a)
        {
            std::cin >> elem;
        }
        long long ans = a [0];
        for (auto ite = a.begin (); ite != a.end (); ++ ite)
        {
            auto tmp = findMaximumSum (ite , a.end ());
            ans = std::max (ans , tmp);
        }
        std::cout << ans << std::endl;
    }
}


#include <iostream>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<queue>
using namespace std;
void ll(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(0);
}
int main()
{
	ll();

	priority_queue<int>pq;
	int arr[1000];
	int n, k, sum = 0;
	cin >> n >> k;
	for (int i = 0; i<n; i++)
	{
		cin >> arr[i];
		pq.push(arr[i]);
	}
	while (k--)
	{
		sum += pq.top();
		pq.pop();
	}
	cout << sum << endl;
}

/**
 *    author:  otera
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }

void solve() {
	string s; int w;
    cin >> s >> w;
    int n = (int)s.size();
    for(int i = 0; i < n; i += w) {
        cout << s[i];
    }
    cout << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int N, H, W;
    bool flag = false;
    cin >> N >> H >> W;
    int count = 0;
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            if (j + W - 1 <= N) {
                flag = true;
            } else {
                flag = false;
            }

            if (flag && i + H - 1 <= N) {
                ++count;
            }
        }
    }
    cout << count << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  int H;
  cin >> H;
  int W;
  cin >> W;
  cout << (N - H + 1) * (N - W + 1) << endl;
}


#include <iostream>
#include <climits>
using namespace std;
#define	int long long
const int INF=1e18;
const int MAX_V=2000;
int v,g[MAX_V][MAX_V],r;
void warshall(){
  for(int k=0;k<v;k++)
    for(int i=0;i<v;i++)
      for(int j=0;j<v;j++) g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
signed main(){
  int e;
  cin>>v>>e;
  for(int i=0;i<v;i++) for(int j=0;j<v;j++) g[i][j]=i==j?0:INF;
  for(int i=0,a,b,c;i<e&&cin>>a>>b>>c;i++) g[a][b]=c;
  warshall();
  //for(int i=0;i<v;i++) for(int j=0;j<v;j++) if(INF/2<g[i][j]&&g[i][j]<INF) r++;
  for(int i=0;i<v;i++) if(g[i][i]) r++;
  if(r){
    cout<<"NEGATIVE CYCLE"<<endl;
    return 0;
  }
  for(int i=0;i<v;i++){
    for(int j=0;j<v;j++){
      if(j) cout<<" ";
      if(g[i][j]>=INF/2) cout<<"INF";
      else cout<<g[i][j];
    }
    cout<<endl;
  }
  return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;
static const int SIZE = 101;
static const long long int INF = 1e18;
long long int d[SIZE][SIZE] = { 0 };

void solve()
{
	for (int i = 0; i < SIZE; ++i)
	{
		for (int j = 0; j < SIZE; ++j)
		{
			d[i][j] = INF;
		}
		d[i][i] = 0;
	}
	int V, E;
	cin >> V >> E;
	for (int i = 0; i < E; ++i)
	{
		int from, to, cost;
		cin >> from >> to >> cost;
		d[from][to] = cost;
	}
	for (int k = 0; k < V; ++k)
	{
		for (int i = 0; i < V; ++i)
		{
			for (int j = 0; j < V; ++j)
			{
				if (d[i][k] != INF && d[k][j] != INF)
				{
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
				}
			}
		}
	}
	bool isNegative = false;
	for (int i = 0; i < V; ++i)
	{
		if (d[i][i] < 0)
		{
			cout << "NEGATIVE CYCLE" << endl;
			return;
		}
	}
	for (int i = 0; i < V; ++i)
	{
		for (int j = 0; j < V; ++j)
		{
			if (j)
			{
				cout << " ";
			}
			if (d[i][j] == INF)
			{
				cout << "INF";
			}
			else
			{
				cout << d[i][j];
			}
		}
		cout << endl;
	}
}

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