#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

void strcpy(auto & S, string * p, int sz, int offset_r, int offset_c, int offset_val) {
	for(int k = 0; k < sz; k++) for(int l = 0; l < sz; l++) if(p[k][l] != '.')
		S[offset_r+k][offset_c+l] = p[k][l] + offset_val * 4;
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N;
	cin >> N;
	if(N == 2) {
		cout << "-1\n";
		return 0;
	}
	string p3[] = {
		"aab",
		"d.b",
		"dcc"
	};
	string p4[] = {
		"aacd",
		"bbcd",
		"cdaa",
		"cdbb"
	};
	string p5[] = {
		"aabba",
		"b.cca",
		"bd..b",
		"ad..b",
		"abbaa",
	};
	string p7[] = {
		"....aba",
		"....aba",
		"....bab",
		"....bab",
		"aabb.cc",
		"bbaad..",
		"aabbd..",
	};
	vector<string> S(N);
	for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) S[i] += '.';
	int r = 1;
	while(N%2 == 0 && N >= 6) N /= 2, r *= 2;
	for(int rr = 0; rr < r; rr++) for(int rc = 0; rc < r; rc++) {
		int n = N;
		while(n >= 8 || n == 4) {
			n -= 4;
			strcpy(S, p4, 4, rr*N+n, rc*N+n, (rr+rc)%2);
		}
		if(n == 3) strcpy(S, p3, 3, rr*N, rc*N, (rr+rc)%2);
		if(n == 5) strcpy(S, p5, 5, rr*N, rc*N, (rr+rc)%2);
		if(n == 7) strcpy(S, p7, 7, rr*N, rc*N, (rr+rc)%2);
	}
	for(int i = 0; i < r*N; i++) cout << S[i] << "\n";
	return 0;
}

// look at my code
// my code is amazing

#pragma region

#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define _rep3(i, begin, end) for (register int i = (begin); i < (end); i++)
#define _irep3(i, begin, end) for (register int i = (end)-1; i >= (begin); i--)
#define _rep2(i, n) _rep3(i, 0, n)
#define _irep2(i, n) _irep3(i, 0, n)
#define _overload3(_1, _2, _3, name, ...) name
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define irep(...) _overload3(__VA_ARGS__, _irep3, _irep2, )(__VA_ARGS__)
#define out(x) cout << (x) << '\n';
#define debug(x) cerr << #x << " = " << (x) << " (line: " << __LINE__ << ") " << endl;
typedef long long ll;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "vec[ ";
  rep(i, vec.size()) i == 0 ? os << vec[i] : os << ", " << vec[i];
  os << " ]";
  return os;
}

#pragma endregion

#define MOD 1000000007
#define INF 2147483647

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(16);

  ll N;
  cin >> N;
  if (N == 2) {
    out(-1);
    return 0;
  }
  if (N == 3) {
    out("abb");
    out("a.a");
    out("bba");
    return 0;
  }
  if (N == 4) {
    out("abaa");
    out("abcc");
    out("ccba");
    out("aaba");
    return 0;
  }
  vector<string> ans(N, string(N, '.'));
  ans[0][0] = 'c';
  ans[0][1] = 'c';
  ans[1][0] = 'd';
  ans[2][0] = 'd';
  ans[0][N - 1] = 'c';
  ans[1][N - 1] = 'c';
  ans[0][N - 2] = 'd';
  ans[0][N - 3] = 'd';
  ans[N - 1][N - 1] = 'c';
  ans[N - 1][N - 2] = 'c';
  ans[N - 2][N - 1] = 'd';
  ans[N - 3][N - 1] = 'd';
  ans[N - 1][0] = 'c';
  ans[N - 2][0] = 'c';
  ans[N - 1][1] = 'd';
  ans[N - 1][2] = 'd';
  rep(i, N - 4) {
    char c = i % 2 == 0 ? 'a' : 'b';
    ans[i + 1][i + 2] = c;
    ans[i + 1][i + 3] = c;
    ans[i + 2][i + 1] = c;
    ans[i + 3][i + 1] = c;
  }
  rep(i, N) out(ans[i]);
}


#include <iostream>
#include <map>
#include <vector>
typedef long long ll;
using namespace std;
int main (int argc, char const* argv[]) {
  int N, id, price, num;
  const ll LIMIT = 1000000LL;
  while (true) {
    cin >> N;
    if (N == 0) break;
    map<int,pair<bool,ll> > dict;
    vector<int> id_list;
    for (int i = 0; i < N; i += 1) {
      cin >> id >> price >> num;
      ll tmp = (ll)price * (ll)num;
      // New
      if (dict.count(id) == 0) {
        if (LIMIT <= tmp) {
          dict.insert(make_pair(id,make_pair(true,0)));
        } else {
          dict.insert(make_pair(id,make_pair(false,tmp)));
        }
        id_list.push_back(id);
      // Add
      } else {
        if (dict[id].first) continue;
        if (LIMIT <= tmp + dict[id].second) {
          dict[id].first = true;
        } else {
          dict[id].second += tmp;
        }
      }
    }
    // Print
    bool printed = false;
    for (int i = 0; i < id_list.size(); i += 1) {
      if (dict[id_list[i]].first) {
        cout << id_list[i] << endl;
        printed = true;
      }
    }
    if (!printed) cout << "NA" << endl;
  }
  return 0;
}

#include <iostream>

using namespace std;

int order[4000];

bool search(int);

class Member {
public:
	Member() {sum = 0;}

	void add(long price, long num) {
		sum += (long long int)price*num;
	}

	bool milion()
	{
		if (sum >= 1000000) return true;
		else return false;
	}

private:
	long long int sum;
	int id;
};

bool search(int num)
{
	for (int i=0; i<4000; i++)
		if (order[i] == num ) return true;

	return false;

}

int main()
{
	int n, idx, memNum;
	long price, num;
	bool nothing;

	ios::sync_with_stdio();

	while (1) {
		Member *member = new Member[4000];
		memNum=0;
		nothing =true;

		for (int i=0; i<4000; i++) order[i] = -1;

		if (cin >> n, !n) break;
		for (int i=0; i<n; i++) {
			scanf("%d %ld %ld", &idx, &price, &num);
			member[idx].add(price, num);
			if (!search(idx)) order[memNum++] = idx;
		}

		for (int i=0; i<4000 && order[i] > 0; i++)
			if (member[order[i]].milion()) { cout << order[i] << endl; nothing = false; }
		if (nothing) cout << "NA" << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

#define fcout(d) cout << fixed << setprecision(d)
#define rep(i,n) for(int (i) = 0; (i) < (n); ++(i))
#define rep1(i,n) for(int (i) = 1; (i) <= (n); ++(i))
#define repU(i,bottom,ceiling) for(auto (i) = (bottom); (i) <= (ceiling); ++(i))
#define repD(i,ceiling,bottom) for(auto (i) = (ceiling); (i) >= (bottom); --(i))
#define l_bnd lower_bound
#define u_bnd upper_bound
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mkp make_pair
#define mkt make_tuple
#define fir first
#define sec second
#define divceil(n,d) ((n)/(d) + ((n)%(d) > 0))
#define parity(a,b) (((a) & 1LL)^((b) & 1LL)^1LL)

typedef long long ll;
typedef unsigned long long ull;
typedef double db;

const pair<int,int> DIR[] = { {1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1} };
const int INF_32 = -1 + (1 << 30);
const ll INF_64 = -1 + (1LL << 62);
const int MOD = (int)1e9 + 7;

ull next_subset(ull subset){
	ull tem = subset + (subset & -subset);
	ull btm = (~tem & subset) / (subset & -subset) >> 1;
	return btm | tem;
}

int V,E,r;
vector<pair<int,int> > adj[100000];
ll dist[100000];

void solve(){

	fill(dist,dist + V,INF_64);
	dist[r] = 0;

	queue<int> que;
	que.push(r);

	while(!que.empty()){
		int v = que.front();
		que.pop();
		rep(i,adj[v].size()){
			pair<int,int> p = adj[v][i];
			if(dist[p.fir] < dist[v] + p.sec) continue;
			que.push(p.fir);
			dist[p.fir] = dist[v] + p.sec;
		}
	}

	rep(i,V){
		if(dist[i] == INF_64) cout<<"INF\n";
		else cout<<dist[i]<<endl;
	}

	return;
}

void input(){

	cin>>V>>E>>r;
	rep(i,E){
		int s,t,d;
		cin>>s>>t>>d;
		adj[s].pub({t,d});
	}

	return;
}

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

	input();
	solve();

	return 0;
}

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

#define chmin(a,b) a=min(a,b)
#define rep(i,x) for(ll i=0;i<(x);++i)

const ll linf = 3e18;

ll cost[100][100];
ll d[100];
bool used[100];

ll N, K;

void dijkstra(ll s)
{
    fill_n(d, N, linf);
    fill_n(used, N, false);
    d[s] = 0;

    while (true) {
        ll v = -1;

        rep(u, N) {
            if (!used[u] && (v == -1 || d[v] > d[u])) v = u;
        }

        if (v == -1) break;
        used[v] = true;

        rep(u, N) {
            chmin(d[u], d[v] + cost[v][u]);
        }
    }
}

signed main()
{
    while (scanf("%lld %lld", &N, &K), N || K) {
        fill(cost[0], cost[N], linf);

        rep(i, K) {
            ll t; scanf("%lld", &t);
            if (t == 1) {
                ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c); a--; b--;
                chmin(cost[a][b], c);
                chmin(cost[b][a], c);
            } else {
                ll a, b; scanf("%lld %lld", &a, &b); a--; b--;
                dijkstra(a);
                printf("%lld\n", d[b] == linf ? -1 : d[b]);
            }
        }
    }
}


#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int scores[2] = {0, 0};
    int points[2][10];
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 10; ++j) {
            cin >> points[i][j];
        }
        sort(points[i], points[i]+10);
        for (int j = 0; j < 3; ++j) {
            scores[i] += points[i][9 - j];
        }
    }
    cout << scores[0] << ' ' << scores[1] << endl;
    return 0;
}

#include <iostream>

int main()
{
  int a, b, c;
  std::cin >> a >> b >> c;
  if (a==b) {
    if (a!=c) std::cout << "Yes" << std::endl;
    else std::cout << "No" << std::endl;
  }
  else {
    if (a==c || b==c) std::cout << "Yes" << std::endl;
    else std::cout << "No" << std::endl;
  }
  return 0;
}


#include <iostream>
using namespace std;

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

	int d;
	while(cin >> d){

	int sum = 0;
	for(int i = d; i < 600; i += d){
		sum += d * i * i;
	}

	cout << sum << endl;
	}

	return 0;
}

#include<stdio.h>
main(){
  int n;
  int i,j;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    if(i%3==0){
      printf(" %d",i);
    }else{
      j = i;
      while(j>0){
        if(j%10==3){
          printf(" %d",i);
          break;
        }
        j /= 10;
      }
    }
  }
  printf("\n");
}


#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int n,sum=0,sumy=0;
	while (1){
		sum = 0;
		sumy = 0;
		cin >> n;
		if (n == 0)break;
		int ma[101][101] = { 0 };
		for (int i = 0; i < n; i++){
			sum = 0;
			for (int i2 = 0; i2 < n; i2++){
				cin >> ma[i][i2];
				sum += ma[i][i2];

			}
			sumy += sum;
			ma[i][n] += sum;
		}
		ma[n][n] += sumy;
		for (int i = 0; i < n; i++){
			sum = 0;
			for (int i2 = 0; i2 < n; i2++){
				sum += ma[i2][i];
				sumy += sum;
			}
			ma[n][i] += sum;
		}
		n++;
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				printf("%5d", ma[i][i2]);
			}
			cout << endl;
		}
	}
}

#include <iostream>

using namespace std;

int main() {

  int a,b,c;

  while(1) {
  cin >> a >> b >> c;
  if(a == 0 && b == 0 && c == 0) break;

  int n;
  cin >> n;

  int flg[301];

  for(int i=0;i<301;i++) flg[i] = 2;

  int d[1001],m[1001],k[1001],r;

  for(int i=0;i<n;i++) {
    cin >> d[i]>> m[i] >> k[i] >> r;
    if(r == 1) {
      flg[d[i]] = flg[m[i]] = flg[k[i]] = 1;
      i--;
      n--;
    }
  }
  int sum;
  for(int i=0;i<n;i++){
    sum = 0;
    if(flg[d[i]] == 1 ) sum++;
    if(flg[m[i]] == 1)  sum++;
    if(sum == 2) {
      flg[k[i]] = 0;
      continue;
    }
    if(flg[k[i]] == 1) sum++;
    if(sum == 2) {
      if(flg[d[i]] == 2) flg[d[i]] = 0;
      else flg[m[i]] = 0;
    }
  }

  for(int i=1;i<=a+b+c;i++) {
    cout << flg[i] << endl;
  }

  }

  return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main(){

  string S;
  cin>>S;
  char check='a';
  bool flag=0;
  for(check='a';check<='z';check++){

    flag=1;
    for(size_t i=0;i<=S.length()-1;i++){

      if(S[i]==check){

        flag=0;
        break;
      }
    }
    if(flag==1){

      cout<<check;
      return 0;
    }
  }
  cout<<"None";
  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<ll> vll;
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) {
	string s;
	cin>>s;
	bitset<26> b;
	for(char c:s)b[(int)(c-'a')]=1;
	int i = 0;
	for(;i<26;i++) {
		if(!b[i]){break;}
	}
	if(i<26)cout<<(char)(i+'a'); else cout<<"None";
}

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 <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1LL << 60;

int main(){
    int N, K;
    cin >> N >> K;
    vector<int> D(10, 0);
    for(int i=0; i<K; i++) {
        int a;
        cin >> a;
        D[a] = 1;
    }

    for(int i=N; i<=100000; i++) {
        int num = i;
        bool flg = true;
        while(num) {
            int x = num % 10;
            if (D[x] == 1) {
                flg = false;
                break;
            }
            num /= 10;
        }
        if(flg && i>= N) {
            cout << i << endl;
            return 0;
        }
    }
}

#include<iostream>
#include<unordered_map>
#include<bitset>
#include<math.h>
#include<vector>
#include<set>
#include<algorithm>
#include<ctype.h>
#include<unordered_set>
#include<string>
#include<iomanip>
#include<queue>
#include<limits>
#include<map>
#include<stack>
#include<iterator>
#include<cstring>
#include<deque>
#include<chrono>
using namespace std;
#define pi 3.141592653589793238

#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f0 get<0>
#define f1 get<1>
#define f2 get<2>
typedef vector<ll> vi;
typedef vector<vi> vvi;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    fast;
    ll T = 1, i, j;

    //cin >> T;
    while (T--) {
        ll n, k;
        cin >> n >> k;
        vector<ll> v(n);
        for(i =0 ; i <n ;i++){
            cin >> v[i];
        }
        while(k--){
            vector<ll> v2(n + 1);
            for(i =0 ; i <n ;i++){
                ll lo, hi;
                lo = max(0LL, i - v[i]);
                hi = min(i + v[i], n - 1);
                v2[lo]++;
                v2[hi + 1]--;
            }
            for(i = 1 ;i < n; i++){
                v2[i] += v2[i - 1] ;
            }
            for(i =0 ;i < n; i++){
                v[i] = v2[i];
            }
            bool flag = true;
            for(i =0 ;i < n; i++){
                if(v[i] != n){
                    flag = false;
                    break;
                }
            }
            if(flag){
                break;
            }
        }
        for(i =0 ;i < n; i++){
            cout << v[i] <<" ";
        }
    }

    return 0;
}


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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> s(H, vector<char>(W));
  int memo = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s.at(i).at(j);
      if (s.at(i).at(j) == '.') {
        memo++;
      }
    }
  }
  vector<vector<int>> M(H, vector<int>(W, 0));
  queue<int> X;
  queue<int> Y;
  X.push(0);
  Y.push(0);
  vector<int> vx = {1, -1, 0, 0}, vy = {0, 0, 1, -1};
  s.at(0).at(0) = '#';
  while (X.size() > 0) {
    for (int i = 0; i < 4; i++) {
      if ((X.front() + vx.at(i)) >= 0 && (Y.front() + vy.at(i)) >= 0
          && (X.front() + vx.at(i)) < H && (Y.front() + vy.at(i)) < W
          && s.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) == '.'
          && M.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) == 0) {
            X.push(X.front() + vx.at(i));
            Y.push(Y.front() + vy.at(i));
            M.at(X.front() + vx.at(i)).at(Y.front() + vy.at(i)) = M.at(X.front()).at(Y.front()) + 1;
      }
    }
    X.pop();
    Y.pop();
  }
  if (M.at(H - 1).at(W - 1) != 0) {
    cout << memo - (M.at(H - 1).at(W - 1) + 1) << endl;
  }
  else {
    cout << -1 << endl;
  }
}

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

// info
int board[8][8];
int res[8];

void fillBoard(int row, int col, int cnt);
void printBoard();
void recur(int row);
int main()
{
    fill(res, res+8, -1);
    // rep(i, 8){
    //     fill(board[i], board[i]+8, 0);
    // }

    int n; cin >> n;
    rep(i, n){
        int row, col;
        cin >> row >> col;
        fillBoard(row, col, 1);
        res[row] = col;
    }

    // printBoard();
    // rep(i, 8)
    // {
    //     rep(j, 8)
    //     {
    //         cout << board[i][j]
    //              << (j < 7 ? " " : "\n");
    //     }
    // }
    recur(0);
    return 0;
}

void fillBoard(int row, int col, int cnt)
{
    board[row][col] += cnt;

    rep(i, 8)
    {
        if (i != col)
        {
            board[row][i] += cnt;

            int j1 = row - col + i;
            if (j1 >= 0 && j1 < 8)
                board[j1][i] += cnt;

            int j2 = col + row - i;
            if (j2 >= 0 && j2 < 8) // here!!
                board[j2][i] += cnt;
        }

        if (i != row)
            board[i][col] += cnt;
    }
}
void printBoard()
{
    rep(i, 8)
    {
        rep(j, 8)
        {
            cout << (res[i] == j?"Q":".")
             << (j < 7 ? "" : "\n");
        }
    }
}
void recur(int row)
{
    if (row == 8)
    {
        printBoard();
        return;
    }

    if (res[row] < 0)
    {
        // int flag = -1;
        rep(col, 8)
        {
            if (board[row][col] == 0)
            {
                // flag++;

                fillBoard(row, col, 1);
                res[row] = col;
                recur(row + 1);
                res[row] = -1;
                fillBoard(row, col, -1);
            }
        }
        // if(flag < 0) return;
    }
    else
        recur(row + 1);
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
struct Graph{
  Graph(int n):E(n),d(n){}
  vector<vector<tuple<int,int,int>>>E;
  vector<int>d;
  void add_edge(int a,int b,int c,int d){
    E[a].push_back(make_tuple(b,c,d));
  }
  void dijkstra(int s,int f){
    priority_queue<P,vector<P>,greater<P>>q;
    fill(d.begin(),d.end(),1e9);
    q.push({d[s]=0,s});
    while(q.size()){
      auto p=q.top();q.pop();
      if(p.first>d[p.second])continue;
      for(auto& a:E[p.second]){
        int x=get<0>(a),y=get<1>(a)+p.first;
        if(f)y=get<2>(a)+p.first;
        if(y<d[x])d[x]=y,q.push({y,x});
      }
    }
  }
};
main(){
  int n,m,a,b,c,d,k;
  while(cin>>n>>m,n){
    Graph E(m);
    for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d;
    cin>>k;
    for(int i=0;i++<k;cout<<E.d[--b]<<endl){
      cin>>a>>b>>c;
      E.dijkstra(--a,c);
    }
  }
}

#include<iostream>
#include<queue>
#include<vector>
#include<limits>

using namespace std;

struct RequestObj {
	int begin, end, type;
};

int n, m;//???,???????????°
int k;//????????¨????????°
int time_table[100][100];
int cheap_table[100][100];
RequestObj request[200];

const int INF = 10000000;

int main() {
	while (cin >> n >> m, n * m) {

		for (int i = 0; i < m; i++)for (int j = 0; j < m; j++)time_table[i][j] = INF, cheap_table[i][j] = INF;

		for (int i = 0; i < n; i++) {
			int a, b;
			cin >> a >> b;
			cin >> cheap_table[a - 1][b - 1] >> time_table[a - 1][b - 1];
			cheap_table[b - 1][a - 1] = cheap_table[a - 1][b - 1];
			time_table[b - 1][a - 1] = time_table[a - 1][b - 1];
		}
		cin >> k;
		for (int i = 0; i < k; i++) {
			cin >> request[i].begin >> request[i].end >> request[i].type;
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (cheap_table[j][i] + cheap_table[i][k] < cheap_table[j][k]) {
						cheap_table[j][k] = cheap_table[j][i] + cheap_table[i][k];
					}
				}
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (time_table[j][i] + time_table[i][k] < time_table[j][k]) {
						time_table[j][k] = time_table[j][i] + time_table[i][k];
					}
				}
			}
		}
		for (int i = 0; i < k; i++) {
			if (request[i].type == 0) {
				cout << cheap_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
			else {
				cout << time_table[request[i].begin - 1][request[i].end - 1] << endl;
			}
		}

		for (int i = 0; i < m; i++)for (int j = 0; j < m; j++)time_table[i][j] = INF, cheap_table[i][j] = INF;

	}
	return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

#define MAX_LEN 1010

template<size_t N>
const bitset<N> operator+(const bitset<N>& lhs, const bitset<N>& rhs)
{
    bitset<N> a(lhs), b(rhs), ret(lhs ^ rhs);

    for (b = (a & b) << 1, a = ret; b.any(); b = (a & b) << 1, a = ret) ret ^= b;

    return ret;
}

template<size_t N>
int edit_distance_bitset(const string& s1, const string& s2)
{
    int m = s1.size();
    int n = s2.size();

    const bitset<N> ONE(1);
    bitset<N> Peq[0x100];
    bitset<N> Pv, Mv, Eq, Xv, Xh, Ph, Mh;
    int Score = m;

    for (int i = 0; i < m; i++) {
        Peq[s1[i]] |= ONE << i;
        Pv |= (ONE << i);
    }
    for (int j = 0; j < n; j++) {
        Eq = Peq[s2[j]];
        Xv = Eq | Mv;
        Xh = (((Eq & Pv) + Pv) ^ Pv) | Eq;
        Ph = Mv | ~(Xh | Pv);
        Mh = Pv & Xh;
        if ((Ph & (ONE << (m - 1))).any()) Score++;
        else if ((Mh & (ONE << (m - 1))).any()) Score--;
        Ph = Ph << 1 | ONE;
        Pv = (Mh << 1) | ~(Xv | Ph);
        Mv = Ph & Xv;
    }

    return Score;
}

int main(){

    string a,b;
    cin>>a>>b;
    cout<<edit_distance_bitset<MAX_LEN>(a,b)<<endl;
  return 0;
}

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

void solve(string, string);
int main(){
  string s, t;
  cin >> s >> t;

  solve(s, t);

  return 0;
}

void solve(string s, string t){
  int m=s.length(), n=t.length();
  s = " "+s; t = " "+t;

  int dp[m+1][n+1];
  for(int i=0; i<=m; i++) dp[i][0] = i;
  for(int i=0; i<=n; i++) dp[0][i] = i;

  for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
      int ins=dp[i-1][j]+1;
      int del=dp[i][j-1]+1;
      int rep;
      if(s[i]==t[j]){
        rep = dp[i-1][j-1];
        dp[i][j] = min(ins, min(del, rep));
      }
      else{
        rep = dp[i-1][j-1]+1;
        dp[i][j] = min(ins, min(del, rep));
      }
    }
  }

  cout << dp[m][n] << endl;
}


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

int main() {
  long n,ans;
  cin >> n;
  ans=n/11*2;
  n=n%11;
  if(n>0) ans++;
  if(n>6) ans++;
  cout << ans << endl;
}

#include <iostream>

int main()
{
    std::string s; std::cin >> s;

    for (size_t i = 0; i < s.size() - 8; i++) {
        std::cout << s[i];
    }
    std::cout << std::endl;
    return 0;
}


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

vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end(), greater<long long>());
    return ret;
}

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

    vector<long long> ret;
    ret = divisor(m);
    long long ans = 1;
    for(long long i = 0; i < ret.size(); i++){
        if(m/ret[i] >= n){
            ans = ret[i];
            break;
        }
    }

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

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

typedef long long ll;
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define SORT(s) sort((s).begin(),(s).end())

int main(){
    ll N,K; cin >> N >> K;
    if(N%K==0) cout << "0" << endl;
    else cout << "1" << endl;
}


// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_B: Matrix Chain Multiplication

#include <algorithm>
#include <climits>
#include <cstdio>

using uint = unsigned int;

static uint matrix_chain_multiplication(const uint p[], const uint n)
{
  uint m[n][n];
  for (auto i = 0u; i < n; ++i) m[i][i] = 0u;

  for (auto d = 1u; d < n; ++d) {
    for (auto i = 0u, j = i+d; j < n; ++i, ++j) {
      auto min = UINT_MAX;
      for (auto k = i; k < j; ++k) {
        // min(Mi..j = (Mi..k) * (Mk+1..j)
        //           = (Mi..k) + (Mk+1..j) + p[i]*p[k+1]*p[j+1])
        auto cost = m[i][k] + m[k+1][j] + p[i]*p[k+1]*p[j+1];
        min = std::min(min, cost);
      }
      m[i][j] = min;
    }
  }
  return m[0][n-1];
}

int main()
{
  uint n;
  std::scanf("%u", &n);
  uint p[n+1];
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &p[i], &p[i+1]);
  }
  auto c = matrix_chain_multiplication(p, n);
  std::printf("%u\n", c);
}

// eof


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

int main(){
  int N,X,a=0;
  cin>>N>>X;
  int l[N];
  for(int i=0;i<N;++i)cin>>l[i];

  sort(l,l+N);
  	for(int i=0;i<N;++i){
      X-=l[i];
      	if(X>=0)a++;
      	else break;
    }
  if(X>0)cout<<a-1;
  else cout<<a;

}

#include <bits/stdc++.h>

#define rep(i,n) for(ll i=0;i<n;++i)
#define all_map(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

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

    map<ll, ll> mp;
    ll ans = 0;
    ll sum = 0;
    rep(i, n){
        ll a;
        cin >> a;
        sum += a;
        if(sum % m == 0)ans++;

        if(mp.count(sum % m) > 0){
            ans += mp[sum % m];
            mp[sum % m]++;
        }
        else {
            mp[sum % m] = 1;
        }
    }

    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
void solveA()
{
  long long a,b,tem;
  cin>>a>>b;
  tem=(a+b);
  if(tem%2==0)
  {
    cout<<tem/2<<endl;
  }
  else
  {
    cout<<"IMPOSSIBLE"<<endl;
  }
}
void solveB()
{
  int n,i,j,count;
  cin>>n;
  int ar[n];
  for(i=0;i<n;i++)
  {
    cin>>ar[i];
  }
  j=1;
  count=0;
  for(i=0;i<n;i++)
  {
    if(ar[i]!=j)
    {
      count++;
    }
    if(count>2)
    {
      cout<<"NO"<<endl;
      return;
    }
    j++;
  }
  cout<<"YES"<<endl;
}
void solveC()
{
  int a,b,x,y;
  cin>>a>>b;
  x=max(a,b);
  y=min(a,b);
  if((x-y)>=2)
  {
    cout<<2*x-1<<endl;
  }
  else
  {
    cout<<x+y<<endl;
  }
}
void solveD()
{
  int n,i,j;
  cin>>n;
  int ar[n];
  for(i=0;i<n;i++)
  {
    cin>>ar[i];
  }
  int count=n;
  for(i=n-1;i>=0;i--)
  {
    for(j=i-1;j>=0;j--)
    {
      if(ar[i]<ar[j])
      {
        count--;
        break;
      }
    }
  }
  cout<<count<<endl;
}
void solveE()
{
  int h,w,x,y,H,W;
  cin>>H>>W;
  cin>>h>>w;
  x=h*W;
  y=(H-h)*w;
  cout<<(H*W)-(x+y)<<endl;
}
void solveF()
{
  int n,m,c,i,j;
  cin>>n>>m>>c;
  int b[m];
  int ar[n][m];
  for(i=0;i<m;i++)
  {
    cin>>b[i];
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
    {
      cin>>ar[i][j];
    }
  }
  int count=0;
  int sum;
  for(i=0;i<n;i++)
  {
    sum=0;
    for(j=0;j<m;j++)
    {
      sum=sum+ar[i][j]*b[j];
    }
    if((sum+c)>0)
    {
      count++;
    }
  }
  cout<<count<<endl;
}
void solveG()
{

}
int main()
{
 // solveA();
  //solveB();
 solveC();
 //solveD();
 //solveE();
// solveF();
  return 0;
}

#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

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

    while(--n >= 0){
        string s;
        getline(cin, s);
        int i = 0;
        for(;;){
            i = s.find("Hoshino", i);
            if(i == string::npos)
                break;
            s.replace(i, 7, "Hoshina");
        }
        cout << s << endl;
    }
}


#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'
#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'

using namespace std;

typedef long long ll;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/10;
const ll MOD = 1e9+7;

ll rev(ll x,int n){
  ll ret=0;
  repeat(i,n){
    ret *= 10;
    ret += x%10;
    x /= 10;
  }
  return ret;
}

ll dfs(ll D,int n,int cnt,int dig,ll N){
  if(cnt>=n/2){
    return rev(N,n)==N+D? N:-1;
  }
  if(cnt>=0){
    if(dig>=0){
      ll d=dig;
      repeat(c,n-1-cnt) d*=10;
      N += d;
    }else{
      ll d=-dig;
      repeat(c,cnt) d*=10;
      N += d;
    }
  }
  ll ret = -1;
  for(int i=-9;i<=(cnt<0? 0:9);i++){
    ret = max(ret,dfs(D,n,cnt+1,i,N));
  }
  return ret;
}

int main(){
  ll D;cin >> D;
  if(D%9!=0){
    cout << 0 << endl;
    return 0;
  }
  int num10=0;
  while(D%10==0){
    D/=10;
    num10++;
  }
  string Dstr=to_string(D);
  int n = Dstr.length();
  //debug(n);
  ll ans = 0;
  repeat(d,7){
    int m = n+d;
    ll N=0;
    if(d>0){
      if(D%10!=9)break;
      if(d>1 && D%100!=89)break;
      repeat(c,d-1){
        N*=10;
        N+=9;
      }
      repeat(c,n){
        N*=10;
      }
      N+=1;
    }
    //debug(N);
    N = dfs(D,m,d-1,0,N);
    debug(N);
    if(N>0){
      string Nstr = to_string(N);
      while(Nstr.length()<m) Nstr = "0"+Nstr;
      ll a=1;
      repeat(j,(m+1)/2){
        int mi=Nstr[j]-'0',ma=Nstr[m-1-j]-'0';
        if(mi>ma)swap(mi,ma);
        a *= j==0? 9-ma+ (num10>0):10-(ma-mi);
      }
      debug(a);
      ans += a;
    }
  }
  if(ans>0&&num10>0){
    repeat(c,num10-1)ans *= 10;
    ans *= 9;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
array<Int, 18> POW10;
int main()
{
    POW10[0] = 1;
    for (int i = 1; i < POW10.size(); i++) POW10[i] = POW10[i-1] * 10;
    int D; cin >> D;
    int digits = 0;
    for (int val = 1; val <= D; val *= 10, digits++) {}
    Int ans = 0;
    for (int d = digits; d <= digits*2; d++) {
        vector<Int> A, S = {0};
        for (int k = d/2; k < d; k++) {
            A.push_back(POW10[k] - POW10[d-1-k]);
            S.push_back(S.back() + A.back());
        }
        function<void(int, Int, Int)> recur = [&](int idx, Int rest, Int acc) {
            int ten = (idx == A.size()-1 ? 9 : 10);
            if (idx == 0) {
                if (A[0] == 0) { if (rest == 0) ans += acc * ten; }
                else if (rest % A[0] == 0 && abs(rest / A[0]) <= 9) ans += acc * (ten - abs(rest / A[0]));
            } else {
                for (int i = -9; i <= 9; i++) {
                    Int r = rest - i * A[idx];
                    if (abs(r) <= abs(S[idx]*9)) recur(idx-1, r, acc * (ten - abs(i)));
                }
            }
        };
        recur(A.size()-1, D, 1);
    }
    cout << ans << '\n';
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,t;
    cin>>n>>t;
    vector<int>vec(n);
    for(int i=0;i<n;i++)
      cin>>vec[i];
    int sum=0;
    for(int i=0;i<n-1;i++)
      sum+=min(t,vec[i+1]-vec[i]);
    sum+=t;
    cout<<sum<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    ll x, y;
    cin >> x >> y;
    if(x == y)cout << 0 << endl;
    else if(x == -y)cout << 1 << endl;
    else if(abs(x) < abs(y)){
        ll ans = abs(y) - abs(x);
        if(x < 0 && y < 0)ans += 2;
        else if(x < 0 || y < 0)ans++;
        cout << ans << endl;
    }else{
        ll ans = abs(x) - abs(y);
        if(x > 0 && y > 0)ans += 2;
        else if(x > 0 || y > 0)ans++;
        cout << ans << endl;
    }

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#ifndef ONLINE_JUDGE
    #define debug(x) cerr << #x << ": " << x << endl;
#else
    #define debug(x)
#endif

vvll g;
vll color(110000, 0);
bool dfs(ll x, ll p = -1, ll c = 0) {
    color[x] = c;
    for(auto nx : g[x]) {
        if(color[nx] == c) return false;
        if(color[nx] ==0 && !dfs(nx, x, -c)) return false;
    }
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    ll n, m;
    cin >> n >> m;
    g = vvll(n);
    rep(i,m) {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    bool is_bipartite = dfs(0, -1, 1);
    if(is_bipartite) {
        ll cw = 0, cb = 0;
        rep(i, n) {
            if(color[i] == 1) cw++;
            else cb++;
        }
        cout << cw * cb - m << endl;
    }
    else cout << n * (n-1) / 2 - m << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i,n) for(long long i=0; i<n; i++)
#define repr(i,n,m) for(long long i=m; i<n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto&& itr : x) { cerr << (itr) << " "; }

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n-1+m), b(n-1+m), in(n);
  vector<vector<int>> G(n);
  for (int i = 0; i < n+m-1; ++i) {
    cin >> a[i] >> b[i];
    a[i] --, b[i] --;
    G[a[i]].push_back(b[i]);
    in[b[i]] ++;
  }

  int root = -1;
  int edge = n+m-1;
  for (int i = 0; i < n; ++i) {
    if (in[i] == 0) {
      root = i;
      break;
    }
  }

  queue<int> que;
  que.push(root);
  vector<int> dist(n);

  while (!que.empty()) {
    auto now = que.front();
    que.pop();

    for (auto to : G[now]) {
      chmax(dist[to], dist[now] + 1);
      in[to] --;
      if (in[to] == 0) {
        // 根の子の入次数は1だから必ずここには入る
        // 親に対しても有向辺を貼る可能性はないので途中で探索が終わることもない
        que.push(to);
      }
    }
  }

  vector<int> ans(n, 0);
  for (int i = 0; i < n; ++i) {
    for (auto&& to : G[i]) {
      if (dist[to] - dist[i] == 1) {
        ans[to] = i+1;
      }
    }
  }

  for (auto it : ans) {
    cout << it << endl;
  }
}


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

vector<int> G[1048576];

int main(){
	int N; cin >> N;
	int A[N];
	rep(i, N) cin >> A[i];
	int rui[N];
	rui[0] = A[0];
	rep(i, N-1){
		rui[i+1] = rui[i] ^ A[i+1];
	}
	int sum[N];
	rep(i, N){
		G[rui[i]].push_back(i);
		if(i == 0){
			if(rui[i] == 0) sum[i] = 1;
			else sum[i] = 0;
		}else{
			if(rui[i] == 0) sum[i] = sum[i-1] + 1;
			else sum[i] = sum[i-1];
		}
	}
	ll ans = 0, s = 1;
	if(rui[N-1] == 0){
		rep(i, sum[N-1] - 1) s = (s * 2) % mod;
		ans = s;
		for(int i=1; i<1048576; i++){
			if(!G[i].size()) continue;
			int p = G[i].size();
			ll dp[p], su = 0;
			dp[p-1] = 1, su = dp[p-1];
			for(int j=p-2; j>=0; j--){
				int nn = sum[G[i][j+1]] - sum[G[i][j]];
				dp[j] = (su * nn % mod + dp[j+1]) % mod;
				su = (su + dp[j]) % mod;
			}
			ans = (ans + su) % mod;
		}
	}else{
		int x = rui[N-1];
		ll dp[G[x].size()], su = 0;
		dp[G[x].size() - 1] = 1, su = 1;
		for(int i=G[x].size() - 2; i>=0; i--){
			if(i == G[x].size() - 2){
				int nn = sum[G[x][i+1]] - sum[G[x][i]];
				dp[i] = nn;
              	su = (su + nn) % mod;
				continue;
			}
			int nn = sum[G[x][i+1]] - sum[G[x][i]];
			dp[i] = (su * nn % mod + dp[i+1]) % mod;
			su = (su + dp[i]) % mod;
		}
		ans = (ans + su) % mod;
	}
	cout << ans << endl;
	return 0;
}

//高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

#define INTINF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007

int main() {
	ll ans = 0;
	int tmp;
	int n;
	cin >> n;
	map<int, int> o, e;
	REP(i, n){
		cin >> tmp;
		if(i %2 == 0) e[tmp] ++;
		else o[tmp]++;
	}
	vector< pair <int, int >> od , ev;
	for(auto it = e.begin(); it != e.end(); it++){
		ev.push_back(make_pair(it->second, it->first));
	}
	for(auto it = o.begin(); it != o.end(); it++){
		od.push_back(make_pair(it->second, it->first));
	}
	ev.push_back(make_pair(0, -1));
	od.push_back(make_pair(0, -1));
	sort(ev.begin(), ev.end(), greater<pair<int, int>>());
	sort(od.begin(), od.end(), greater<pair<int, int>>());
	if(ev[0].second != od[0].second){
		cout << n - ev[0].first - od[0].first;
	}
	else{
		cout << min(n - ev[1].first - od[0].first, n - ev[0].first - od[1].first);
	}
	//cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    cout<<n+(n*n)+(n*n*n)<<endl;
    return 0;
}

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

#define rep(i, n)     for (int i = 0; i < (int) (n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define each(i, mp)   for (auto &i : mp)

const int INF = 1001001001;

int main()
{
  int a;
  cin >> a;
  ll ans = 0;

  rep(i, 3) ans += pow(a, i + 1);
  cout << ans << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    sort(A.begin(), A.end());
    long long temp = 0, ans = 0;;
    long long i = 0, num;
    while(i < N){
        temp = 0;
        num = A.at(i);
        while(i < N && A.at(i) == num){
            temp++;
            i++;
        }
        if(temp % 2 == 1){
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

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

#define MAX_N 100
#define MAX_EDGE_COST 20000
int Graph[MAX_N+1][MAX_N+1];
int n;

int prim(int initial_vertex) {
  bool visited[n+1];
  for(int i=1;i<=n;i++) visited[i] = false;
  vector<int> vertexes;
  vertexes.push_back(initial_vertex);
  visited[initial_vertex] = true;
  int total_edge_cost = 0;
  while(vertexes.size() < n) {
    // find minimum edge cost in vertexes
    int minimum_edge_cost = MAX_EDGE_COST;
    int next_v;
    for(int i=0;i<vertexes.size();i++) {
      int v = vertexes[i];
      for(int j=1;j<=n;j++) {
        if(!visited[j]) {
          if(Graph[v][j] != -1) {
            if(Graph[v][j] < minimum_edge_cost) {
              minimum_edge_cost = Graph[v][j];
              next_v = j;
            }
          }
        }
      }
    }
    visited[next_v] = true;
    vertexes.push_back(next_v);
    total_edge_cost += minimum_edge_cost;
  }
  return total_edge_cost;
}

int main() {
  scanf("%d",&n);
  for(int i=1;i<=n;i++) {
    for(int j=1;j<=n;j++) {
      scanf("%d ",&Graph[i][j]);
    }
  }
  printf("%d\n",prim(1));
}


#include<bits/stdc++.h>
#define tmp(x) std::cout<<"& "<<(x)<<" &\n"
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
typedef long long ll;
#define  pii pair<int,int>
#define  pll pair<ll,ll>
#define  vi vector<int>

using namespace std;
const int maxn=3e5+100;
const int inf=(int)1e9+7;
int n;

vector<pair<int,char> > x[maxn],y[maxn],le[2*maxn],ri[2*maxn];
int ans=inf;
void work(vi v1,vi v2,int aa){

    if(!v2.empty()&&!v1.empty()){
        vector<int>::iterator it1,it2;
        sort(v2.begin(),v2.end());
        sort(v1.begin(),v1.end());
        it2=v2.begin(),it1=v1.begin();
        for(auto p1:v1){
            for(auto p2:v2){
                if(p2<p1)continue;
                else {
                    ans=min(ans,abs(p1-p2)*10/aa);
                    break;
                }
            }
        }

        /*while(*it1!=*v1.end()&&*it2!=*v2.end()){
            while(*it2!=*v2.end()&&*it2<*it1)++it2;
            if(*it2==*v2.end())break;
            ans=min(ans,abs(*it1-*it2)*10/aa);
            ++it1;
        }*/
    }
}
int main(){
//	freopen("in.txt","r",stdin);
    scanf("%d",&n);
    int xx,yy;char c;
    rep(i,1,n){
        scanf("%d%d %c",&xx,&yy,&c);

        x[xx].push_back(make_pair(yy,c));
        y[yy].push_back(make_pair(xx,c));
        le[xx-yy+200000].push_back(make_pair(xx,c));
        ri[xx+yy].push_back(make_pair(xx,c));
    }
    rep(i,0,200000){
        if(!x[i].empty()){
            vector<int>up,down;
            for(auto p:x[i]){
                if(p.second=='U')up.push_back(p.first);
                if(p.second=='D')down.push_back(p.first);
            }
            work(up,down,2);
        }
        if(!y[i].empty()){
            vector<int>right,left;
            for(auto p:y[i]){
                if(p.second=='R')right.push_back(p.first);
                if(p.second=='L')left.push_back(p.first);
            }
            work(right,left,2);
        }
    }

    rep(i,0,400000){
        if(!le[i].empty()){
            vector<int> right,down,left,up;
            for(auto p:le[i]){
                if(p.second=='D')down.push_back(p.first);
                else if(p.second=='R')right.push_back(p.first);
                else if(p.second=='U')up.push_back(p.first);
                else if(p.second=='L')left.push_back(p.first);
            }
            work(right,down,1);
            work(up,left,1);
        }
        if(!ri[i].empty()) {
            vector<int> right, up,left, down;
            for (auto p:ri[i]) {
                if (p.second == 'U')up.push_back(p.first);
                else if (p.second == 'R')right.push_back(p.first);
                else if (p.second == 'D')down.push_back(p.first);
                else if (p.second == 'L')left.push_back(p.first);
            }
            work(right, up,1);
            work(down, left,1);
        }
    }
    if(ans==inf)printf("SAFE");
    else  printf("%d",ans);

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,char> Q;

ll N;
vector<Q> v;
vector<vector<P>> V,H;
set<P> S;
vector<vector<ll>> LU,LD,RU,RD;

int main(){
    cin >> N;
    V.resize(200010);
    H.resize(200010);
    LU.resize(400010);
    LD.resize(400010);
    RU.resize(400010);
    RD.resize(400010);
    ll offset = 200000;
    for(int i=0;i<N;i++){
        ll x,y;
        char u;
        cin >> x >> y >> u;
        v.push_back({{x,y},u});
    }
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(S.find({x,y}) != S.end()){
            cout << 0 << endl;
            return 0;
        }
        S.insert({x,y});
    }
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(e.second == 'U'){
            V[x].push_back({y,0});
            RU[x+y].push_back(x);
            LU[x-y+offset].push_back(x);
        }
        if(e.second == 'D'){
            V[x].push_back({y,1});
            LD[x+y].push_back(x);
            RD[x-y+offset].push_back(x);
        }
        if(e.second == 'R'){
            H[y].push_back({x,0});
        }
        if(e.second == 'L'){
            H[y].push_back({x,1});
        }
    }
    ll ans = 1e18;
    for(int i=0;i<=200000;i++) sort(V[i].begin(),V[i].end());
    for(int i=0;i<=200000;i++) sort(H[i].begin(),H[i].end());
    for(int i=0;i<=200000;i++){
        ll tmp = -1;
        for(auto &e : V[i]){
            ll x = e.first;
            ll d = e.second;
            if(d == 0) tmp = x;
            else{
                if(tmp == -1) continue;
                ans = min(ans, (x-tmp) * 5);
            }
        }
    }
    for(int i=0;i<=200000;i++){
        ll tmp = -1;
        for(auto &e : H[i]){
            ll y = e.first;
            ll d = e.second;
            if(d == 0) tmp = y;
            else{
                if(tmp == -1) continue;
                ans = min(ans, (y-tmp) * 5);
            }
        }
    }
    for(int i=0;i<=400000;i++) sort(LU[i].begin(),LU[i].end());
    for(int i=0;i<=400000;i++) sort(LD[i].begin(),LD[i].end());
    for(int i=0;i<=400000;i++) sort(RU[i].begin(),RU[i].end());
    for(int i=0;i<=400000;i++) sort(RD[i].begin(),RD[i].end());
    for(auto &e : v){
        ll x = e.first.first;
        ll y = e.first.second;
        if(e.second == 'U'){
            continue;
        }
        if(e.second == 'D'){
            continue;
        }
        if(e.second == 'R'){
            auto itr_d = upper_bound(RD[x-y+offset].begin(),RD[x-y+offset].end(),x);
            if(itr_d != RD[x-y+offset].end()) ans = min(ans,((*itr_d) - x)*10);
            auto itr_u = upper_bound(RU[x+y].begin(),RU[x+y].end(),x);
            if(itr_u != RU[x+y].end()) ans = min(ans,((*itr_u) - x)*10);
        }
        if(e.second == 'L'){
            auto itr_d = upper_bound(LD[x+y].begin(),LD[x+y].end(),x);
            if(itr_d != LD[x+y].begin() && LD[x+y].size() != 0) ans = min(ans,(x - *(itr_d-1))*10);
            auto itr_u = upper_bound(LU[x-y+offset].begin(),LU[x-y+offset].end(),x);
            if(itr_u != LU[x-y+offset].begin() && LU[x-y+offset].size() != 0) ans = min(ans,(x - *(itr_u-1))*10);
        }
    }
    if(ans == 1e18) cout << "SAFE" << endl;
    else cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

template <typename T>
void printlnVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << endl;
  }
}

template <typename T>
void printVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << " ";
  }
  cout << endl;
}

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

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

using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);

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

  int TT = 1;
  // cin >> TT;
  while (TT--) {
    ll n,h,w;
    cin >> n >> h >> w;
    --h;
    --w;
    cout << max(ll(0), ((n-h)*(n-w))) << endl;
  }
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <unordered_set>
#include <queue>
#include <set>
#include <iomanip>
#include <stack>
#define int long long
#define ld long double
#define dbg(x) cout <<#x<<":"<<x<<'\n';
#define dbg2(x,y) cout<<#x<<":"<<x<<' '<<#y<<":"<<y<<'\n';
#define endl '\n'
#define inf 1000000010
#define eps 1e-8l
using namespace std;

const int N=2e5+5;
std::vector<int> g[N],c(N),gd(N),p(N),r(N);
vector<bool> vis(N);
int power(int x,int y){int res = 1;while(y>0){if(y&1)res = (res*x);x = (x*x);y = y>>1;}return res;}

//int n,m;
int n,a,b,da,db,md,nd,m;

int const dpn=2e5+5;
int dp[dpn];

bool comp(pair<int,int> l,pair<int,int> r){
	if(l.first!=r.first)
		return (l.first<r.first);
	return l.second>r.second;
}

int find(int i){
	if(p[i]!=i)
		return p[i]=find(p[i]);
	return i;
}

void join(int x,int y){
	int px=find(x);
	int py=find(y);
	if(r[px]>r[py])
		p[py]=px;
	else if(r[px]<r[py])
		p[px]=py;
	else
		p[py]=px,r[px]++;
}

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		//int n;
		//int n;
		int d,t,s;
		cin>>d>>t>>s;
		if((double)d/(double)s<=(double)t)
			cout<<"Yes\n";
		else
			cout<<"No\n";

	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int d, t, s; cin >> d >> t >> s;
    cout << (t * s >= d ? "Yes" : "No") << endl;
    return 0;
}


// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

template<typename A>
using vpix = vector<pair<int, A>>;

vpix<char> u;
vpix<string> m;

int f(string s) {
  int c = 0, l = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    for (auto p : u) {
      if (s[i] == p.second) {
        c += p.first * (l <= p.first ? 1 : -1);
        l = p.first;
      }
    }
  }
  return c;
}

void solve() {
  u.pb(mp(1000, 'M'));
  u.pb(mp(500, 'D'));
  u.pb(mp(100, 'C'));
  u.pb(mp(50, 'L'));
  u.pb(mp(10, 'X'));
  u.pb(mp(5, 'V'));
  u.pb(mp(1, 'I'));
  string s;
  while (cin >> s) cout << f(s) << endl;
}

#include "bits/stdc++.h"
#include <unordered_set>

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

#define LL long long int
#define LD long double

#define INF 1000000000
#define PI 3.14159265358979323846

#define _CRT_SECURE_NO_WARNINGS

using namespace std;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<LL> vll;

//==============================================
vi A, G;

int cnt = 0;
int gSize;

void insertionSort(int n,int g) {
	REP2(i, g, n) {
		int v = A[i];
		int j = i - g;
		while (j >= 0 && A[j] > v) {
			A[j + g] = A[j];
			j -= g;
			cnt++;
		}
		A[j + g] = v;
	}
}

void shellSort(int n) {
	int h = 1;
	while (h <= n) {
		G.push_back(h);
		h = 3 * h + 1;
	}
	gSize = G.size();

	RREP(i, gSize) {
		insertionSort(n, G[i]);
	}

}

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

	int n;
	cin >> n;

	int tmp;
	REP(i, n) cin >> tmp, A.push_back(tmp);

	shellSort(n);

	cout << gSize << "\n";
	RREP(i, gSize) {
		cout << G[i];
		if (i)cout << " ";
	}
	cout << "\n";
	cout << cnt << "\n";

	REP(i, n) {
		cout << A[i] << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
char s[100];
int main(){
    scanf("%s", s+1);
    if(strlen(s+1) == 3) reverse(s+1, s+1+strlen(s+1));
    printf("%s", s+1);
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n+1); i++)
const long long INF = 1LL << 60;
int main(){
    string S;
  cin>>S;
    int a=S.size();
    if(a==2){
        cout<<S<<endl;
    }
    else{
        cout<<S[2]<<S[1]<<S[0]<<endl;
    }
}


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

using namespace std;

int main()
{
	int N, M;
	cin >> N >> M;
	vector<int> L(M), R(M);
	for (int i = 0; i < M; i++) {
		cin >> L[i] >> R[i];
	}

	int lmax = *max_element(L.begin(), L.end());
	int rmin = *min_element(R.begin(), R.end());
	int d = rmin - lmax + 1;
	if (d < 0)
		d = 0;
	cout << d << endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;

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

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define DEBUG_ARR(v,n) cout<<#v<<":";REP(i,n)cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

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;
}
template<class T>
void DEBUG_DP(T* viewdp, int ilen, int jlen){
    REP(i,ilen){
        REP(j,jlen){
            if(viewdp[i][j] != -1)
                printf("[% 4d]", viewdp[i][j]);
            else
                printf("[    ]");
        }
        puts("");
    }
}
const int N=81;
const int C=6400;
const int K=C*2+1;
int h,w;
int a[N][N], b[N][N];
bitset<K> dp[N][N];
int ans = INT_MAX;

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

    REP(i,N){
        fill(a[i], a[i]+N, -1);
        fill(b[i], b[i]+N, -1);
    }

    cin>>h>>w;

    REP(i,h) REP(j,w) cin>>a[i][j];
    REP(i,h) REP(j,w) cin>>b[i][j];

    //int sx=0,sy=0, gx=w-1, gy=h-1;
    dp[0][0][abs(a[0][0]-b[0][0]) + 6400]=true;

    REP(i,h) REP(j,w){
        int ab =abs(a[i+1][j]-b[i+1][j]);

        dp[i+1][j] |= dp[i][j] << ab;
        dp[i+1][j] |= dp[i][j] >> ab;

        ab =abs(a[i][j+1]-b[i][j+1]);
        dp[i][j+1] |= dp[i][j] << ab;
        dp[i][j+1] |= dp[i][j] >> ab;
    }

    REP(i,K){
        if(dp[h-1][w-1][i]) chmin(ans, abs(6400-i));
    }

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


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

typedef long long lli;

lli rec(int l, int r, vector<int> &A) {
  if(r-l <= 1) return 0;
  int m = (l+r)/2;
  lli res = rec(l, m, A) + rec(m, r, A);
  queue<int> a, b;
  for(int i = l; i < m; ++i) a.push(A[i]);
  for(int i = m; i < r; ++i) b.push(A[i]);
  int k = l;
  while(a.size() && b.size()) {
    if(a.front() <= b.front()) {
      A[k++] = a.front();
      a.pop();
    } else {
      A[k++] = b.front();
      b.pop();
      res += a.size();
    }
  }
  while(a.size()) {
    A[k++] = a.front();
    a.pop();
  }
  while(b.size()) {
    A[k++] = b.front();
    b.pop();
  }
  return res;
}

int main() {
  int n;
  vector<int> A;
  cin >> n;
  A.resize(n);
  for(int i = 0; i < n; ++i) cin >> A[i];
  cout << rec(0, n, A) << endl;
  return 0;
}

#include <stdio.h>
void merge(int *,int,int,int);
void mergesort(int*,int,int);
long long cnt=0;
int L[100002],R[100002];
int main(){
  int A[200000],i,n;
  scanf("%d",&n);
  for(i=0;i<n;i++)scanf("%d",&A[i]);
  mergesort(A,0,n);
  printf("%ld\n",cnt);
  return 0;
}
void merge(int *a,int left,int mid,int right){
  int i,k,j,n1=mid-left,n2=right-mid;
  for(i=0;i<n1;i++){
    L[i]=a[left+i];
  }
  for(i=0;i<n2;i++){
    R[i]=a[mid+i];
  }
  L[n1]=2147483647;
  R[n2]=2147483647;
  i=0;
  j=0;
  for(k=left;k<right;k++){
    if(L[i]<=R[j])a[k]=L[i++];
    else{
      a[k]=R[j++];
      cnt+=n1-i;
    }
  }
  return;
}
void mergesort(int *a,int left,int right){
  int mid;
  if(left+1<right){
    mid=(left+right)/2;
    mergesort(a,left,mid);
    mergesort(a,mid,right);
    merge(a,left,mid,right);
  }
  else return;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <numeric>
#include <iomanip>
#define fi first
#define se second
#define fcout(n) cout<<fixed<<setprecision((n))
#define cinl(str) getline(cin,(str))
using namespace std;
bool value(int y,int x,int R,int C){return 0<=y&&y<R&&0<=x&&x<C;}
typedef pair<int,int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long > vll;
typedef vector< vi > vvi;
double pie=acos(-1);
int INF=10000009;
int dx[4] = { 0,-1,0,1 };
int dy[4] = { -1,0,1,0 };
int main() {
	int day=0;
	int x,y;
	int md[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	string s[7]={"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"};
	while(true){
		cin>>x>>y;
		if(x==0) break;
		for(int i=0;i<x-1;i++){
			day+=md[i];
		}
		day+=y;
		day%=7;
		cout<<s[day]<<endl;
		day=0;
	}
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF (1<<28)-1
#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
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 dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

int n, k;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
    while (true) {
        cin >> n >> k;
        if (n == 0 && k == 0) break;
        vector<string> a(n);
        map<string, bool> mm;
        rep(i, 0, n) cin >> a[i];
        sort(all(a));
        do {
            string sub = "";
            rep(i, 0, k) sub += a[i];
            mm[sub] = true;
        } while (next_permutation(all(a)));
        cout << mm.size() << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define precise(n,k) fixed << setprecision(k) << n
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define uint  unsigned int
#define ll    long long
#define ull   unsigned long long
#define ld    long double

const int MOD = int(1e9+7);
const int oo  = int(1e9 + 20);
const ll lloo  = (ll)(1e18 + 10);

template <typename T, class F = function<T(const T&, const T&)>>
class SegmentTree {
    int NEUTRAL;
    int n;
    vector<int> tree;
    F func;
public:

    SegmentTree(vector<int> values, int neutral, const F& f) : func(f) {
        NEUTRAL = neutral;

        n = values.size();
        tree.resize(n*2);
        // Build
        for (int i=0; i < n; i++){
            tree[n+i] = values[i];
        }
        for (int i = n - 1; i > 0; --i) {
            tree[i] = func(tree[i*2], tree[i*2+1]);
        }
    }

    void update(int index, int value) {
        tree[index+n] = value;
        index = index + n;
        for (int i = index; i > 1; i >>= 1){
            tree[i/2] = func(tree[i], tree[i^1]);
        }
    }

    int query(int l, int r) {
        int ans = NEUTRAL;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l&1) {
                ans = func(ans, tree[l++]);
            }
            if (r&1) {
                ans = func(ans, tree[--r]);
            }
        }
        return ans;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin >> n;
    n++;
    vector<int> val(n);
    for(int i = 1; i < n; ++i) {
        cin >> val[i];
    }

    SegmentTree<int> st(val, -oo, [&](int x, int y) -> int {return max(x, y);});

    vector<bool> visited(n, false);
    int colors = 0;
    int last;
    for(int i = 1; i < n; ++i) {
        if(!visited[i]) {
            colors++;
            last = val[i];
            visited[i] = true;
            st.update(i, -oo);

            if(st.query(i+1, n) <= last) continue;

            for(int j = i + 1; j < n; ++j) {
                if(!visited[j] && last < val[j]) {
                    last = val[j];
                    visited[j] = true;
                    st.update(j, -oo);
                    if(st.query(j+1, n) <= last) break;
                }
            }
        }
    }
    cout << colors << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
ll dp[5009];
  ll a[5000];
int main()
{
  ll n;ll ans=0;
  multiset<ll>s;cin>>n;
  for(int i=0;i<n;i++){
      ll x;cin>>x;
      if(s.size()==0){
          s.insert(x);
          ans++;
          }
          else
          {
              auto z=s.begin();
              if(*z>=x){
                  s.insert(x);ans++
;              }else{
              auto it=s.lower_bound(x);it--;
              s.erase(s.find(*it));
               s.insert(x);
}

}
  }cout<<ans;

}


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

int next_turn(char c) {
  return int(c) - int('a');
}

int main()
{
  string S[3];
  cin >> S[0] >> S[1] >> S[2];
  int pos[3] = {0};
  int turn = 0;
  for (;;) {
    if (pos[turn] >= S[turn].length()) {
      cout << char(int('A')+turn) << endl;
      break;
    }
    int next = next_turn(S[turn][pos[turn]]);
    pos[turn]++;
    turn = next;
  }
  return 0;
}

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

int main(){
    vector<queue<char>> s(3);
    for(int ix=0;ix<3;ix++){
        string tmp;
        cin >> tmp;
        for(int ixx=0;ixx<tmp.size();ixx++) s.at(ix).push(tmp.at(ixx));
    }
    char card;
    int turnplayer = 0;
    while(!s.at(turnplayer).empty()){
        card = s.at(turnplayer).front();
        s.at(turnplayer).pop();
        turnplayer = card - 'a';
    }
    if(turnplayer == 0) cout << "A" << endl;
    if(turnplayer == 1) cout << "B" << endl;
    if(turnplayer == 2) cout << "C" << endl;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll md=2038077799,mul=1400312813;
int n;
ll ai[200010],bi[200010],a[200010],b[200010];
vector<int> v;
ll hasha,hashb,etc;
ll ksm(int x)
{
	if (!x) return 1ll;
	return (((ksm(x/2)*ksm(x/2))%md)*(x%2 ? mul : 1))%md;
}
int main()
{
	cin >>n;
	for (int i=0;i<n;i++) cin >>ai[i];
	for (int i=0;i<n;i++) cin >>bi[i];
	for (int i=0;i<n;i++) a[i]=ai[i]^ai[(i+1)%n],b[i]=bi[i]^bi[(i+1)%n];
	etc=ksm(n-1);
	for (int i=0;i<n;i++)
	{
		hasha=(((hasha*mul)%md)+a[i])%md;
		hashb=(((hashb*mul)%md)+b[i])%md;
	}
	for (int i=0;i<n;i++)
	{
		if (hasha==hashb) v.push_back(i);
		hasha=(hasha+md-((a[i]*etc)%md))%md;
		hasha=(((hasha*mul)%md)+a[i])%md;
	}
	for (int i=0;i<v.size();i++) cout <<v[i]<<" "<<(ai[v[i]]^bi[0])<<"\n";
	return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
const ll MOD = 998244353;
const ll INF = 1e18;
using namespace std;

ll dp[1 << 20], pre[1 << 20];

main(){
    #ifdef XOX
        freopen("D:\\V S Code\\cpp\\competitiveProgramming\\Input.txt", "r", stdin);
        freopen("D:\\V S Code\\cpp\\competitiveProgramming\\OPT.txt", "w", stdout);
    #endif

    ios::sync_with_stdio(!cin.tie(NULL));

    int n;
    cin >> n;
    memset(dp, 0, sizeof(dp));
    memset(pre, 0, sizeof(pre));

    ll tmp, mask;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> tmp;
            mask = (1 << i)|(1 << j);
            dp[mask] += tmp;
        }
    }

    for(int i = 0; i < n; i++){
        for(int mask = 0; mask < (1 << n); mask++){
            if(mask&(1 << i))   dp[mask] += dp[mask^(1 << i)];
        }
    }

    for(int mask = 0; mask < (1 << n); mask++){
        pre[mask] = dp[mask];
        for(int sub = mask; sub; sub = (sub - 1)&mask){
            pre[mask] = max(pre[mask], pre[sub] + dp[mask^sub]);
        }
    }

    cout << pre[(1 << n) - 1] / 2;

}


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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3")
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x)
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  __MAKE_TIME__  Problem: __PROBLEM__ / Link: __CONTEST_URL__  ----- */
/* ------問題------

-----問題ここまで----- */
/* -----解説等-----

----解説ここまで---- */

struct UnionFind {
	vector<int> data;
	UnionFind() {}
	UnionFind(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
	void initV(int x) { data[x] = -1; }
};

UnionFind uf(1e5);
vector<vector<PII>> f(const vector<PII>&edge) {
	for (const auto& it : edge) {
		uf.initV(it.first);
		uf.initV(it.second);
	}
	for (const auto& it : edge) {
		uf.unionSet(it.first, it.second);
	}
	int rootid = 0;
	unordered_map<int, int>roots; // mapでもたいしてかわらない
	for (const auto& it : edge) {
		if (roots.find(uf.root(it.first)) == roots.end()) {
			roots[uf.root(it.first)] = rootid++;
		}
	}

	vector<vector<PII>>res(rootid);
	for (const auto& it : edge) {
		res[roots[uf.root(it.first)]].push_back(it);
	}
	return res;

}

VI Dijkstra(vector<vector<PII>>& G, int s) {// BFS
	VI dist(SZ(G), INF);
	dist[s] = 0;
	queue<PII> que;
	que.push(PII(0, s));

	while (!que.empty()) {
		PII p = que.front(); que.pop();
		int v; /* 頂点*/ int d; /* 頂点vまでの距離 */
		tie(d, v) = p;

		FOR(i, 0, (int)G[v].size()) {
			int nv = G[v][i].first;
			if (dist[nv] > dist[v] + G[v][i].second) {
				dist[nv] = dist[v] + G[v][i].second;
				que.push(PII(dist[nv], nv));
			}
		}
	}
	return dist;
}
int read() { int in; scanf("%d", &in); return in; }
const int Max = 1e6;
vector<PII>Cedge[Max];
int main() {
	LL N = read(), M = read();

	FOR(i, 0, M) {
		int p = read() - 1, q = read() - 1, c = read() - 1;
		Cedge[c].push_back(PII(p, q));
	}
	vector<vector<PII>>G(N + M);
	int cid = N;
	FOR(c, 0, 1e6) {
		auto res = f(Cedge[c]);
		for (auto subedges : res) {
			for (auto e : subedges) {
				G[cid].push_back(PII(e.first, 1));
				G[e.first].push_back(PII(cid, 1));
				G[cid].push_back(PII(e.second, 1));
				G[e.second].push_back(PII(cid, 1));
			}
			cid++;
		}
	}
	auto res = Dijkstra(G, 0);
	LL ans = res[N - 1] / 2;
	if (ans == INF / 2)ans = -1;
	cout << ans << "\n";

	return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

struct edge {
    ll to, cost;
};

int V;
vector<edge> G[1000000];
vector<edge> G2[1000000];
ll d[1000000];

void addedge(int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

void addedge2(int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G2[st].push_back(e);
}

ll ptlab[3000000];
map<plglg, ll> mp;

void BFS01(int n) {
    fill(d, d + V, llmax);
    deque<int> que;
    d[n] = 0;
    que.push_front(n);
    while (que.size() > 0) {
        int base = que.front();
        que.pop_front();
        int s = G[base].size();
        for (int i = 0; i < s; i++) {
            edge e = G[base][i];
            if (e.cost == 1 && d[e.to] > d[base] + 1) {
                d[e.to] = d[base] + 1;
                que.push_back(e.to);
            } else if (e.cost == 0 && d[e.to] > d[base]) {
                d[e.to] = d[base];
                que.push_front(e.to);
            }
        }
    }
}

int main() {
    ll N, M;
    cin >> N >> M;
    ll p[M], q[M], c[M];
    REP(i, 0, M) {
        cin >> p[i] >> q[i] >> c[i];
        addedge2(p[i] - 1, q[i] - 1, c[i]);
        addedge2(q[i] - 1, p[i] - 1, c[i]);
    }
    REP(i, 0, N) {
        mp[plglg(i, -1)] = i;
        ptlab[i] = i;
    }
    ll pt = N;
    REP(i, 0, N) {
        REP(j, 0, G2[i].size()) {
            edge e = G2[i][j];
            if (mp.count(plglg(e.to, e.cost)) == 0) {
                mp[plglg(e.to, e.cost)] = pt;
                ptlab[pt] = e.to;
                pt++;
            }
        }
    }
    V = pt;
    REP(i, N, pt) {
        ll label = ptlab[i];
        addedge(label, i, 1);
    }
    REP(i, 0, N) {
        REP(j, 0, G2[i].size()) {
            edge e = G2[i][j];
            if (mp.count(plglg(i, e.cost)) == 1) {
                addedge(mp[plglg(i, e.cost)], mp[plglg(e.to, e.cost)], 0);
            }
            addedge(mp[plglg(e.to, e.cost)], ptlab[e.to], 0);
        }
    }
    BFS01(0);
    ll ans = d[N - 1];
    if (ans != llmax) {
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
}


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

int main(){

  int n, k[100], x, i, j;
  string sta="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", s;

  while(cin>>n, n){
    for(i=0;i<n;i++) cin>>k[i];
    cin>>s;

    for(i=0;i<s.size();i++){
      for(j=0;j<56;j++){
    if(s[i]==sta[j]){
      x=j-k[i%n];
      if(x<0){
        x+=52;
        break;
      }else break;
    }
      }
      cout<<sta[x];
    }
    cout<<endl;
  }
  return 0;
}

// King's Inspection

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

using namespace std;

int main() {
    int n;
    int now=0;
    int k[102];
    string station;
    string ans;
    string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    while(1) {
      // Input
        cin>>n;
        if(n == 0) {
            break;
        }
        for(int i=0; i<n; i++) {
            cin>>k[i];
        }
        cin.ignore();

	// Output
	now = 0;
	getline(cin, station);

	for(int i=0; i<station.length(); i++) {
	    int tmp;
	    tmp = alpha.find(station[i]);
	    ans[i] = alpha[(tmp-k[now]+52)%52];
	    now++;
            if(now == n) {
                now = 0;
            }
	}
        for(int i=0; i<station.length(); i++) {
            cout<<ans[i];
        }
        cout<<endl;
    }
}


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

int main() {
    int n, q, a, b;
    scanf("%d%d%d%d", &n, &q, &a, &b);
    a--, b--;
    vector<int> x(q);
    for (int i = 0; i < q; ++i) {
        scanf("%d", &x[i]);
        x[i]--;
    }
    constexpr int64_t kInf = 1'000'000'000'000'000'000;
    vector<int64_t> dp(n, kInf);
    vector<int64_t> pref(n + 1, kInf), suff(n + 1, kInf);

    auto Modify = [&](int p, int64_t v, vector<int64_t> &fw) {
        for (int i = p + 1; i <= n; i += i & -i) fw[i] = min(fw[i], v);
    };

    auto Query = [&](int p, const vector<int64_t> &fw) {
        int64_t res = kInf;
        for (int i = p + 1; i > 0; i -= i & -i) res = min(res, fw[i]);
        return res;
    };

    int64_t sum = 0;
    dp[a] = 0;
    Modify(a, -a, pref);
    Modify(n - 1 - a, a, suff);
    for (int i = 0, v = b; i < q; ++i) {
        int64_t res = min(Query(x[i], pref) + x[i], Query(n - 1 - x[i], suff) - x[i]) + sum;
        sum += abs(x[i] - v);
        dp[v] = min(dp[v], res - sum);
        Modify(v, dp[v] - v, pref);
        Modify(n - 1 - v, dp[v] + v, suff);
        v = x[i];
    }
    int64_t ans = *min_element(dp.begin(), dp.end()) + sum;
    cout << ans << "\n";
    return 0;
}

#include<iostream>
#include<vector>
using namespace std;
using ll = long long;

int main(){
    int n,k;
    cin>>n>>k;
    vector<int> x(n);
    for(int i=0;i<n;i++) cin>>x[i];
    ll ans=1e10;
    for(int i=0;i+k<=n;i++){
        ll l=abs(x[i])+abs(x[i+k-1]-x[i]);
        ll r=abs(x[i+k-1])+abs(x[i+k-1]-x[i]);
        ans=min(ans,min(l,r));
    }
    cout<<ans<<endl;
}


#include<stdio.h>
int main(){
	int N;
	long long int result = 0;
	scanf("%d", &N);
	for(int i = 1 ; i <= N ; i++){
		if(i % 3!=0 && i % 5 !=0){
			result += i;
		}
	}
	if(result > 266666333332){
		printf("266666333332");
	}else{
		printf("%lld", result);
	}

	return 0;
}

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

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n);
    ll minus=0;
    bool allplus=false;
    ll abs_tot=0;
    ll abs_min = pow(10,9);
    for(int i=0; i<n; i++){
        cin >> a.at(i);
        if(a.at(i)==0)allplus=true;
        if(a.at(i)<0)minus++;
        abs_tot += abs(a.at(i));
        abs_min=min(abs_min, abs(a.at(i)));
    }
    if(minus%2==0) allplus=true;
    if(allplus) cout << abs_tot << endl;
    else cout << abs_tot - 2*abs_min << endl;

}


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

int main(){
  int p, q, r; cin >> p >> q >> r;

  int m = max(p, max(q, r));
  int ans = p + q + r - m;
  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
//#define LOCAL
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define forn(i,a,b) for(ll i=a;i<b;i++)
#define ft first
#define sd second
#define sz size()
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<double,double> pdd;
typedef pair<long long,long long> pll;
//cout << fixed << setprecision(12) <<x<<endl;

int main(){
fastIO;
#ifdef LOCAL
     freopen("in.txt", "r", stdin);
#endif

int a[3];
int sum=0;
forn(i,0,3){ cin>>a[i];
sum+=a[i];
}
sort(a,a+3);
cout<<sum-a[2]<<endl;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll<<60;

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

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

ll A, B, M;
ll a[100010], b[100010];
ll mi = INF;
std::vector<P> cost[100010];

int main()
{
  std::cin >> A >> B >> M;

  rep( i, A )
    std::cin >> a[i];

  rep( i, B )
    std::cin >> b[i], chmin( mi, b[i] );

  rep( i, M ) {
    ll x, y, c;
    std::cin >> x >> y >> c;
    --x; --y;
    cost[x].emplace_back( y, c );
  }

  ll ans = INF;

  rep( i, A ) {
    chmin( ans, a[i]+mi );

    for( auto p : cost[i] )
     chmin( ans, a[i]+b[p.first]-p.second );
  }

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

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define int long long
int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;}
#define in hmt()
#define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;})
#define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define forb(i,BS) for(int i=BS._Find_first();i< BS.size();i = BS._Find_next(i))
#define forv(a,b) for(auto &a:b)
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define reset(f,x) memset(f,x,sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define onbit(x,i) (x|(1<<(i-1)))
#define offbit(x,i) (x&~(1<<(i-1)))
const int N=200010;
int n,Q,A,B,a[N],oo,s[N],f[N],ans;
struct bit
{
	int t[N];
	void rs()
	{
	    reset(t,127);oo=t[0];
	}
	void upd(int i,int x)
	{
		for(;i<=n;i+=i&-i) t[i]=min(t[i],x);
	}
	int get(int i)
	{
		int g=oo;
		for(;i;i-=i&-i) g=min(g,t[i]);
		return g;
	}
}T[2];
void add(int i,int x)
{
	T[0].upd(i,x-i);
	T[1].upd(n-i+1,i+x);
}
main()
{
    //freopen("C.inp","r",stdin);
	n=in,Q=in,A=in,B=in;
	forinc(i,1,Q) a[i]=in;
	forinc(i,2,Q) s[i]=s[i-1]+abs(a[i]-a[i-1]);
	T[0].rs();T[1].rs();
	add(B,abs(A-a[1]));
	add(A,abs(B-a[1]));
	ans=s[Q]+min(abs(A-a[1]),abs(B-a[1]));
	forinc(i,2,Q)
	{
		f[i]=min(T[0].get(a[i])+a[i],T[1].get(n-a[i]+1)-a[i])+s[i-1];
		add(a[i-1],f[i]-s[i]);
		ans=min(ans,f[i]+s[Q]-s[i]);
	}
	cout<<ans<<"\n";
}


#include <bits/stdc++.h>
#include <iostream>
#include <ctime>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

#define SEARCHED_NODE 2
#define START_NODE 1
#define ROUTE_NODE 1
#define INIT_NODE 0

int _flag[1000];
vector<vector<int>> *_edge;
vector<int> *_invloop;
vector<int> *_nextinvloop;
int _startnode;
bool search(int c, int p)
{
    if( _flag[c] == SEARCHED_NODE) return false;
    _flag[c] = ROUTE_NODE;
    vecrep(itr, (*_edge)[c])
    {
        if( _flag[*itr] == ROUTE_NODE )
        {
            _flag[*itr] = START_NODE;
            _invloop->push_back(c);
            return true;
        }
    }

    vecrep(itr, (*_edge)[c])
    {
        if( search(*itr, c) )
        {
            if(  _flag[*itr] != ROUTE_NODE )
            {
                _flag[*itr] = INIT_NODE;
            }
            else
            {
                _invloop->push_back(c);
            }
            return true;
        }
    }
    _flag[c] = SEARCHED_NODE;

    return false;
}

pair<int, int> checkloop()
{
    int ls = _invloop->size();
    rep(i, ls)
    {
        int next = i - 1;
        if( next < 0 ) next = ls + next;
        int node = (*_invloop)[i];
        int nextnode = (*_invloop)[next];
        vecrep(itr, (*_edge)[node])
        {
            if(  _flag[*itr] == ROUTE_NODE && *itr != nextnode ) return make_pair(node, *itr);
        }
    }
    return make_pair(-1, -1);
}

void resetflag(int n)
{
    rep(j, n) _flag[j] = 0;
    vecrep(itr, (*_invloop)) _flag[*itr] = ROUTE_NODE;
}

void newloop(int s, int e)
{
    int loopindex = 0;
    while((*_invloop)[loopindex] != s ) loopindex++;
    _nextinvloop->clear();

    while((*_invloop)[loopindex] != e )
    {
        _nextinvloop->push_back((*_invloop)[loopindex]);
        loopindex = (loopindex + 1) % _invloop->size();
    }
    _nextinvloop->push_back(e);
    swap(_invloop, _nextinvloop);
}

int main()
{
    int n, m; cin >> n >> m;
    vector<vector<int>> edge(n);
    _edge = &edge;
#if 1
    rep(i, m)
    {
        int a, b; cin >> a >> b; a--; b--;
        edge[a].push_back(b);
    }
#else
    rep(i, m)
    {
        int a, b; cin >> a >> b; a--; b--;
        edge[0].push_back(i + 1);
    }
#endif
    vector<int> invloop; _invloop = &invloop;
    vector<int> nextinvloop; _nextinvloop = &nextinvloop;
    rep(j, n) _flag[j] = INIT_NODE;
    rep(i, n)
    {
        if( search(i, -1) )
        {
            while( true )
            {
                resetflag(n);
                pair<int, int> ret = checkloop();
                if( ret.first < 0 ) break;
                newloop(ret.first, ret.second);
            }
            sort(_invloop->begin(), _invloop->end());
            cout << _invloop->size() << endl;
            vecrep(itr, *_invloop) cout << *itr + 1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <stdio.h>
#include <algorithm>
#include <tuple>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
vector<int> e[1010];

int v[1010], last[1010];
vector<int> r(2000, 0);
void bfs(int node) {
	queue<int> q;
	for (int x : e[node])
		q.emplace(x), v[x] = last[x] = node;
	while (!q.empty()) {
		int cur = q.front();
		q.pop();

		for (int x : e[cur])
			if (v[x] != node)
				q.emplace(x), v[x] = node, last[x] = cur;
	}
	if (v[node] != node)
		return;
	int pop = node;
	vector<int> path;
	path.emplace_back(node);
	while (last[pop] != node) {
		path.emplace_back(last[pop]);
		pop = last[pop];
	}
	if (path.size() < r.size())
		r.swap(path);
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		e[a].emplace_back(b);
	}
	for (int i = 1; i <= n; i++)
		bfs(i);
	if (n < r.size())
		printf("-1\n");
	else {
		printf("%d\n", r.size());
		for (int v : r)
			printf("%d\n", v);
	}
}


 #include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>
 #include<queue>
 #include<deque>
 #include<functional>
 #include<utility>

 using namespace std;

 const int MAX_V=100+5;
 const int INF=100000000;

 int main()
 {
   int graph[2][MAX_V][MAX_V];
   int path,V;
   while(cin>>path>>V&&(path||V))
   {
     for(int m=0;m<2;m++)
     {
       for(int i=0;i<V;i++)
       {
         for(int j=0;j<V;j++)
         {
           if(i==j) graph[m][i][j]=0;
           else graph[m][i][j]=INF;
         }
       }
     }
     while(path--)
     {
       int p,q;
       int cost,time;
       cin>>p>>q>>cost>>time;
       graph[0][p-1][q-1]=cost;
       graph[1][p-1][q-1]=time;
       graph[0][q-1][p-1]=cost;
       graph[1][q-1][p-1]=time;
     }
     for(int m=0;m<2;m++)
     {
       for(int k=0;k<V;k++)
       {
         for(int i=0;i<V;i++)
         {
           for(int j=0;j<V;j++)
           {
             graph[m][i][j]=min(graph[m][i][j],graph[m][i][k]+graph[m][k][j]);
           }
         }
       }
     }
     int que;
     cin>>que;
     while(que--)
     {
       int start,end,type;
       cin>>start>>end>>type;
       cout<<graph[type][start-1][end-1]<<endl;
     }
   }
   //while(1);
   return 0;
 }

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    for(;;){
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)break;
        vector<int> ns(n+1);
        for(int i=0;i<n;i++){
            scanf("%d",&ns[i]);
        }
        ns[n]=0;
        vector<int> ntwos;
        for(int i=0;i<n+1;i++){
            for(int j=i;j<n+1;j++){
                ntwos.push_back(ns[i]+ns[j]);
            }
        }
        sort(ntwos.begin(),ntwos.end());
        int ma=0;
        for(int i=0;i<ntwos.size();i++){
            vector<int>::iterator it;
            if((it=upper_bound(ntwos.begin(),ntwos.end(),m-ntwos[i]))!=ntwos.begin()){
                ma=max(ma,ntwos[i]+*(it-1));
            }
        }
        printf("%d\n",ma);
    }
}


#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(c) begin(c), end(c)
#define range(i,a,b) for(int i = a; i < (int)(b); i++)
#define rep(i,b) range(i,0,b)
#define eb emplace_back
typedef long long ll;
auto const inf = numeric_limits<ll>::max()/4;

using Weight = ll;
using Capacity = int;
struct Edge {
    int src, dst; Weight weight;
    Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<Weight> dijkstra(const Graph &g, int s){
    const Weight inf = numeric_limits<Weight>::max()/4;
    typedef tuple<Weight,int> State;
    priority_queue<State> q;
    vector<Weight> dist(g.size(), inf);
    dist[s] = 0; q.emplace(0,s);
    while (q.size()) {
        Weight d; int v;
        tie(d,v) = q.top(); q.pop(); d *= -1;
        /* if(v == t) return d; */
        if (dist[v] < d) continue;
        for(auto & e : g[v]){
            if (dist[e.dst] > dist[v] + e.weight) {
                dist[e.dst] = dist[v] + e.weight;
                q.emplace(-dist[e.dst], e.dst);
            }
        }
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int V,E,r;
    while(cin >> V >> E >> r){
        Graph g(V);
        rep(i,E){
            int a,b,c;
            cin >> a >> b >> c;
            g[a].eb(a,b,c);
        }
        vector<Weight> dist = dijkstra(g,r);
        for(int i = 0; i < (int)dist.size(); i++){
            if(dist[i] == inf) cout << "INF";
            else cout << dist[i];
            cout << endl;
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i,x) for(int i = 0;i < x;++i)
#define FOR(i,x,y) for(int i = x;i < y;++i)
#define ALL(a) a.begin(),a.end()
using ll = long long;

int v,e,r;
struct edge
{
    int to,cost;
};
vector<edge>graph[114514];

using P = pair<int,int>;
const int INF = 1234567890;
int dist[114514];
void dijkstra()
{
    fill(dist,dist+114514,INF);
    dist[r] = 0;
    priority_queue<P,vector<P>,greater<P>>que;
    que.push({0,r});
    while(!que.empty())
    {
        P p = que.top(); que.pop();
        int ds = p.first;
        int now = p.second;
        if(dist[now] < ds)continue;
        for(auto& ed : graph[now])
        {
            if(dist[ed.to] > dist[now] + ed.cost)
            {
                dist[ed.to] = dist[now] + ed.cost;
                que.push({dist[ed.to],ed.to});
            }
        }
    }
}

int main()
{
    cin >> v >> e >> r;
    REP(i,e)
    {
        int s,t,d;
        cin >> s >> t >> d;
        graph[s].push_back({t,d});
    }
    dijkstra();
    REP(i,v)
    {
        int dd = dist[i];
        if(dd >= INF)
        {
            cout << "INF" << endl;
        }
        else
        {
            cout << dd << endl;
        }
    }
    return 0;
}


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

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() {
    cout << '\n';
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) cout << " ";
    cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << std::endl;
}

int main() {
    Int n, m, q;
    cin >> n >> m >> q;
    vector<pair<Int, Int>> a(m);
    for (Int i = 0; i < m; i++) {
        cin >> a[i].first >> a[i].second;
        a[i].first--, a[i].second--;
    }
    vector<pair<Int, Int>> b(q);
    for (Int i = 0; i < q; i++) {
        cin >> b[i].first >> b[i].second;
        b[i].first--, b[i].second--;
    }
    vector<vector<Int>> v(n);
    for (Int i = 0; i < m; i++) {
        v[a[i].first].push_back(a[i].second);
    }
    for (Int i = 0; i < n; i++) {
        sort(all(v[i]));
        v[i].push_back(INF);
    }
    for (Int i = 0; i < q; i++) {
        Int res = 0;
        for (Int j = 0; j < n; j++) {
            if (j < b[i].first) continue;
            if (b[i].second < j) break;
            res += upper_bound(all(v[j]), b[i].second) - v[j].begin();
        }
        print(res);
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

template<typename T>
struct Dijkstra {
    explicit Dijkstra(size_t size) : size(size), G(size) {}

    struct edge {
        edge(int to, T cost) : to(to), cost(cost) {}

        int to;
        T cost;
    };

    using P = pair<T, int>;
    const T inf = numeric_limits<T>::max();

    size_t size;
    vector<vector<edge>> G;
    vector<T> d;

    void add_edge(int u, int v, T c) {
        G[u].push_back(edge(v, c));
    }

    void build(int s) {
        d.assign(size, inf);
        priority_queue<P, vector<P>, greater<>> q;

        d[s] = 0;
        q.push(P(0, s));

        while (!q.empty()) {
            P p = q.top();
            q.pop();
            int v = p.second;
            if (d[v] < p.first) continue;
            for (auto &e : G[v])
                if (d[e.to] > p.first + e.cost) {
                    d[e.to] = p.first + e.cost;
                    q.push(P(d[e.to], e.to));
                }
        }
    }

    T operator[](int v) {
        return d[v];
    }
};

int main() {
    int V, E, r;
    cin >> V >> E >> r;

    auto g = Dijkstra<int>(V);
    for (int i = 0; i < E; ++i) {
        int s, t, c;
        cin >> s >> t >> c;
        g.add_edge(s, t, c);
    }

    g.build(r);
    for (int i = 0; i < V; ++i) {
        if (g[i] != g.inf) {
            cout << g[i] << endl;
        } else {
            cout << "INF" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
using namespace std;
typedef pair<int,int> P;
const int INF = ~(1<<31) / 2;

int c[100][100];
int t[100][100];
int n,m,k;

int main(){
    while(1){
        scanf("%d %d",&n,&m);
        if(n==0) break;
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                c[i][j] = 1000000;
                t[i][j] = 1000000;
            }
        }
        for(int i=0;i<n;i++){
            int a,b,co,ti;
            scanf("%d %d %d %d",&a,&b,&co,&ti);
            a--;b--;
            c[a][b] = co;
            c[b][a] = co;
            t[a][b] = ti;
            t[b][a] = ti;
        }

        for(int k=0;k<m;k++){
            for(int i=0;i<m;i++){
                for(int j=0;j<m;j++){
                    c[i][j] = min(c[i][j],c[i][k]+c[k][j]);
                    t[i][j] = min(t[i][j],t[i][k]+t[k][j]);
                }
            }
        }

        scanf("%d",&k);
        for(int i=0;i<k;i++){
            int p,q,r;
            scanf("%d %d %d",&p,&q,&r);
            p--;q--;
            if(r)printf("%d\n",t[p][q]);
            else printf("%d\n",c[p][q]);
        }
    }
    return 0;
}

#include <iostream>

#define MX 1000000000

using namespace std;
int M[100][100],N; //M[i][j]:i©çjÜÅÌRXg

int dijkstra(int s , int g){
  bool visited[100];
  int d[100];
  for(int i=0;i < N;i++) d[i] = MX , visited[i] = false;
  d[s] = 0;
  while(1){
    int u;
    int mincost = MX;
    for(int i=0;i<N;i++)if(!visited[i] && d[i] < mincost) mincost = d[i] , u = i;
    if(mincost == MX) break;
    visited[u] = true;
    for(int v=0;v<N;v++){
      if(visited[v] || M[u][v] == MX) continue;
      if(d[v] > d[u] + M[u][v]) d[v] = d[u] + M[u][v];
    }
  }
  return d[g];
}

int main(){
  int k;
  while(cin >> N >> k){
    for(int i=0;i<N;i++)for(int j=0;j<N;j++) M[i][j] = MX;
    while(k--){
      int x,a,b,c;
      cin >> x;
      if(!(x)){
        int cost;
        cin >> a >> b;
        cost = dijkstra(a-1,b-1);
        if(cost == MX) cout << "-1" << endl;
        else cout << cost << endl;
      }
      else {
        cin >> a >> b >> c;
        if(M[a-1][b-1] > c && M[b-1][a-1] > c) M[a-1][b-1] = M[b-1][a-1] = c;
      }
    }
  }
  return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>

using namespace std;

#define sz(x) (int) x.size()
#define unique(x) x.erase(unique(x.begin(), x.end()), x.end())
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))
#define y1 aksjdaskdjksjfksdjf
#define left kdnvldvoiwejifejg
#define right lkdsjflksdjfdjfk
#define prev asdasfsadjkjsdfjs
#define tm aklsjdasjdasjdkdjjf
#define lcm hddalkwjeidjjhasda
#define random_shuffle asdasdd

const int MAXN = 2 * 105000;
const long long INF = 1e15;

int n;
int a[MAXN];
// long long dpl[MAXN], dpr[MAXN];
// pair<long long, long long> pl[MAXN], pr[MAXN];
long long sum;
long long total;
multiset <long long> st1, st2;

int main() {

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    total += a[i];
  }

  for (int i = n - 1; i > 0; i--) {
    sum += a[i];
    st2.insert(sum);
  }

  sum = 0;
  long long ans = INF;
  for (int i = 0; i + 1 < n; i++) {
    sum += a[i];
    if (st2.find(total - sum) != st2.end()) {
      st2.erase(st2.find(total - sum));
    }

    if (sz(st1) && sz(st2)) {
      vector<long long> v1, v2;
      auto it = st1.lower_bound(sum / 2);
      if (it != st1.end()) {
        v1.push_back(*it);
      }
      if (it != st1.begin()) {
        it--;
        v1.push_back(*it);
      }

      it = st2.lower_bound((total - sum) / 2);
      if (it != st2.end()) {
        v2.push_back(*it);
      }
      if (it != st2.begin()) {
        it--;
        v2.push_back(*it);
      }

      for (auto x : v1) {
        for (auto y : v2) {
          long long mn = min(x, y);
          long long mx = max(x, y);
          mn = min(mn, min(sum - x, total - sum - y));
          mx = max(mx, max(sum - x, total - sum - y));
          ans = min(ans, mx - mn);
        }
      }
    }

    st1.insert(sum);
  }

  // for (int i = 0; i < n; i++) {
  //   sum += a[i];
  //   dpl[i] = INF;
  //   if (sz(st)) {
  //     auto it = st.lower_bound(sum / 2);
  //     if (it != st.end()) {
  //       long long cur = *it;
  //       dpl[i] = min(dpl[i], abs(cur - (sum - cur)));
  //     }
  //     if (it != st.begin()) {
  //       it--;
  //       long long cur = *it;
  //       dpl[i] = min(dpl[i], abs(cur - (sum - cur)));
  //     }
  //   }
  //   st.insert(sum);
  // }

  // st.clear();
  // sum = 0;
  // for (int i = n - 1; i >= 0; i--) {
  //   sum += a[i];
  //   dpr[i] = INF;
  //   if (sz(st)) {
  //     auto it = st.lower_bound(sum / 2);
  //     if (it != st.end()) {
  //       long long cur = *it;
  //       dpr[i] = min(dpr[i], abs(cur - (sum - cur)));
  //     }
  //     if (it != st.begin()) {
  //       it--;
  //       long long cur = *it;
  //       dpr[i] = min(dpr[i], abs(cur - (sum - cur)));
  //     }
  //   }
  //   st.insert(sum);
  // }

  // for (int i = 0; i < n; i++) {
  //   cerr << dpl[i] << " ";
  // }
  // cerr << endl;

  /* long long ans = INF;
  for (int i = 1; i + 2 < n; i++) {
    ans = min(ans, max(dpl[i], dpr[i + 1]));
  } */

  cout << ans << endl;

  return 0;
}

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

multiset<int> se;

int main()
{
    // freopen("powers.in", "r", stdin);
    // freopen("powers.out", "w", stdout);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        int x;
        scanf("%d", &x);
        se.insert(x);
    }
    int answer = 0;
    while (!se.empty())
    {
        multiset<int>::iterator it = se.end();
        it--;
        int back = *it;
        se.erase(it);
        int i = 1;
        for (; i <= back; i <<= 1)
            ;
        if (se.find(i - back) != se.end())
        {
            se.erase(se.find(i - back));
            answer++;
        }
    }
    printf("%d\n", answer);
    return 0;
}