/**
 *    author:  tourist
 *    created: 17.11.2019 02:03:54
**/
#include <bits/stdc++.h>

using namespace std;

class dsu {
 public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  vector<tuple<int, int, int>> e;
  auto Add = [&](int i, int j) { e.emplace_back(min(abs(x[i] - x[j]), abs(y[i] - y[j])), i, j); };
  {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) { return x[i] < x[j]; });
    for (int i = 0; i < n - 1; i++) Add(order[i], order[i + 1]);
  }
  {
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int i, int j) { return y[i] < y[j]; });
    for (int i = 0; i < n - 1; i++) Add(order[i], order[i + 1]);
  }
  sort(e.begin(), e.end());
  dsu d(n);
  long long ans = 0;
  for (auto& ee : e) {
    if (d.unite(get<1>(ee), get<2>(ee))) ans += get<0>(ee);
  }
  cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
// #define INF __INT32_MAX__
#define INF 1e9
#define LINF __LONG_LONG_MAX__

int main() {
    int D, G; cin >> D >> G;
    vi P(D), C(D); rep(i, D) cin >> P[i] >> C[i];

    int res = INF;
    for (int i = 0; i < 1<<D; i++) {
        vi tmp;
        vi notselected;
        for (int j = 0; j < D; j++) {
            if (i & 1<<j) {
                tmp.push_back(j);
            } else {
                notselected.push_back(j);
            }
        }

        int sum = 0;
        int cnt = 0;
        for (auto ii : tmp) {
            int x = 100 * (ii + 1) * P[ii] + C[ii];
            sum += x;
            cnt += P[ii];
        }
        if (sum >= G) {
            res = min(res, cnt);
            continue;
        }

        sort(notselected.begin(), notselected.end());
        reverse(notselected.begin(), notselected.end());
        int diff = G - sum;
        for (auto ii : notselected) {
            if (diff <= 0) break;
            int x = 100 * (ii + 1) * P[ii] + C[ii];
            if (diff - x > 0) {
                diff -= x;
                cnt += P[ii];
                continue;
            }

            for (int jj = 0; jj < P[ii]; jj++) {
                diff -= 100 * (ii + 1);
                cnt++;
                if (jj == P[ii] - 1) diff -= C[ii];
                if (diff <= 0) break;
            }
        }
        res = min(res, cnt);
    }

    cout << res << endl;
}


#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 X, Y;
  cin >> X >> Y;
  int money[] = {300000,
                 200000,
                 100000};
  int ans = 0;
  if (X < 4)
    ans += money[X - 1];

  if (Y < 4)
    ans += money[Y - 1];

  if (X == 1 && Y == 1)
    ans += 400000;
  cout << ans << endl;
  return 0;
}

#include <iostream>
#include<algorithm>
#include<string>
#include <cmath>
#include <vector>
#include <map>
#include <cstdio>
#include <iomanip>
#pragma region Macros
#define int long long
#define double long double
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define LAST(x) x[x.size()-1]
#define ALL(x) (x).begin(),(x).end()
#define swap(a,b) (a += b,b = a - b,a -= b)
#define CEIL(a,b) ((a+b-1)/b)
int FACT(int a) {
	if (a == 0)
		return 1;
	else
		return a * FACT(a - 1);
}
int nPr(int n, int r) {
	int s = n-r+1;
	int sum = 1;
	for(int i =s;i<=n;i++)
		sum *= i;
	return sum;
}
//int nCr1(int n, int r)
//int nCr2(int n, int r)
int GCD(int a, int b) // assuming a,b >= 1
{
	if (a < b)
		swap(a, b);
	if (b == 0)
		return a;
	if (a % b == 0)
		return b;
	return GCD(b, a % b);
}
int LCM(int a, int b) // assuming a,b >= 1
{
	return  a * b / GCD(a, b);
}
double LOG(int a, int b) {
	return log(b) / log(a);
}
inline bool BETWEEN(int x, int min, int max) {
	if (min <= x && x <= max)
		return true;
	else
		return false;
}

using namespace std;
#pragma endregion
signed main() {
	int x, y,z=0;
	cin >> x >> y;
	if (x == 1 && y == 1)
		cout << 1000000;
	else
		cout << max(z, 100000 * (4 - x)) + max(z, 100000 * (4 - y));
}


#include <iostream>
#include <cstdlib>

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

	int min = 56562;
	int diff;
	int num;
	int k = 1;
	while (true) {
		num = s / k % 1000;
		if (num < 100) break;
		diff = std::abs(753 - num);
		if (min > diff) min = diff;
		k *= 10;
	}

	std::cout << min << std::endl;

	return 0;
}

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

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

  int ans = 999;
  for(unsigned int i = 0; i < s.length() - 2; ++i)
  {
    int num = (s[i] - '0') * 100 + (s[i+1] - '0') * 10 +(s[i+2] - '0');
    ans = min(ans, abs(753 - num));
  }

  cout << ans << endl;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cstring>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for(long long i=0; i<n; i++)
#define reps(i, s, e) for(long long i=s; i<e; i++)
#define repr(i, n) for(long long i=n-1; i>=0; i--)
#define reprs(i, s, e) for(long long i=e-1; i>=s; i--)

int main(){

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

    ll n; cin >> n;
    ll a[n];
    rep(i, n){
        cin >> a[i];
    }

    double a_sum = accumulate(a, a+n, 0LL);
    double ave = a_sum / n;

    double vmin = 1e18;
    ll i_ans = 0;
    rep(i, n){
        double dif = abs(ave - a[i]);
        if(dif < vmin){
            i_ans = i;
            vmin = dif;
        }
    }

    cout << i_ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;

int main(){
    int N;
    cin >> N;
    int a[N];
    int sum = 0;
    rep(i, N) cin >> a[i], sum += a[i];
    int ans = 0;
    rep(i, N){
        if(abs(a[ans]*N-sum) > abs(a[i]*N-sum)) ans = i;
    }
    cout << ans << endl;
}


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

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll long long

const int maxn = 2e3+10;
const int maxm = maxn*maxn;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

int add(int x,int y) {return x+y>mod?x+y-mod:x+y;}
int del(int x,int y) {return x-y<0?x-y+mod:x-y;}
int mul(int x,int y) {return 1ll*x*y-1ll*x*y/mod*mod;}

int n,k,f[maxn][maxn],fac[maxm],ifac[maxm],inv[maxm];

void prepare() {
    inv[0]=inv[1]=fac[0]=ifac[0]=f[0][0]=1;
    for(int i=2;i<maxm;i++) inv[i]=mul(mod-mod/i,inv[mod%i]);
    for(int i=1;i<maxm;i++) fac[i]=mul(fac[i-1],i);
    for(int i=1;i<maxm;i++) ifac[i]=mul(ifac[i-1],inv[i]);
}

int c(int nn,int mm) {return mul(fac[nn],mul(ifac[mm],ifac[nn-mm]));}

int main() {
    read(n),read(k);prepare();if(k==1) puts("1"),exit(0);
    for(int i=1;i<=n;i++)
        for(int j=0;j<=i;j++)
            f[i][j]=add(f[i-1][j],mul(f[i][j-1],mul(n-j+1,c(n*k-i-(k-1)*(j-1)-1,k-2))));
    write(f[n][n]);
    return 0;
}

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

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

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

const int N = 2e3 + 5, M = 4e6 + 5;

int DP[N][N], F[M], I[M];

int Power(int a, int b) {
	int res = 1;
	for (; b; b >>= 1, a = 1LL * a * a % MOD) if (b & 1) res = 1LL * res * a % MOD;
	return res;
}
int C(int n, int r) { return r > n ? 0 : 1LL * I[r] * I[n - r] % MOD * F[n] % MOD; }

int main() {

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

	F[0] = 1;
	for (int i = 1; i < M; i++) F[i] = 1LL * i * F[i - 1] % MOD;
	I[M - 1] = Power(F[M - 1], MOD - 2);
	for (int i = M - 2; i >= 0; i--) I[i] = 1LL * (i + 1) * I[i + 1] % MOD;

	int n, k; cin >> n >> k;
	if (k == 1) die(1);
	DP[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		DP[i][0] = 1LL * DP[i - 1][0] * C(i * (k - 1) - 1, k - 2) % MOD;
		for (int j = 1; j <= i; j++) DP[i][j] = (DP[i][j - 1] + 1LL * DP[i - 1][j] * C(i * (k - 1) + j - 1, k - 2) % MOD) % MOD;
	}
	cout << 1LL * DP[n][n] * F[n] % MOD << endl;

	return 0;
}


// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef long long ll;

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

const int N=1000+10;
const int mod=1e9+7;
int qpow(int a,int b) { int c=1;
	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;
	return c;
}

int n,a,b,c,d,dp[N][N];

int fac[N],ifac[N];
void init(int n) {
	fac[0]=1;
	for (int i=1;i<=n;++i) fac[i]=1ll*fac[i-1]*i%mod;
	ifac[n]=qpow(fac[n],mod-2);
	for (int i=n;i;--i) ifac[i-1]=1ll*ifac[i]*i%mod;
}
int C(int n,int m) { return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod; }

int main() {
	init(1000);
	n=read(),a=read(),b=read(),c=read(),d=read();
	for (int i=0;i<=b;++i) dp[0][i]=1;
	for (int i=1;i<=n;++i)
		for (int j=a;j<=b;++j) {
			dp[i][j]=dp[i][j-1];
			for (int k=c;k<=d;++k) {
				if (i-j*k<0) break;
				dp[i][j]=(dp[i][j]+1ll*dp[i-j*k][j-1]*C(n-i+j*k,j*k)%mod
						 *fac[j*k]%mod*ifac[k]%mod*qpow(ifac[j],k))%mod;
			}
		}
	printf("%d\n",dp[n][b]);
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1005,ha=1e9+7;

inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}

inline int ksm(int x,int y){
    int an=1;
    for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
    return an;
}

int jc[N],ni[N],f[N],n,a,b,c,d,ans,inv[N];

inline void init(){
    jc[0]=1;
    for(int i=1;i<=n;i++) jc[i]=jc[i-1]*(ll)i%ha;
    ni[n]=ksm(jc[n],ha-2);
    for(int i=n;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
}

inline void dp(){
    f[0]=1;
    for(int i=a;i<=b;i++) // 最外层枚举 队伍的人数
        for(int m=i*c,j=min(i*d,n);j>=m;j--) // 第二层枚举 已经选好的人数
            for(int k=c,now=ksm(ni[i],c),M=j-c*i;k<=d&&M>=0;k++,M-=i,now=now*(ll)ni[i]%ha)
                /* 第三层枚举 这种人数的队伍选几个*/
                ADD(f[j],jc[j]*(ll)now%ha*(ll)ni[M]%ha*(ll)f[M]%ha*(ll)ni[k]%ha);
}

int main(){
    scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
    init(),dp(),printf("%d\n",f[n]);
    return 0;
}


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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

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

void solve() {
    int H, W, A, B; cin >> H >> W >> A >> B;
    vector<string> sv(H, string(W, '0'));
    rep(i, H){
        rep(j, W){
            if(i < B && j < A || i >= B && j >= A){
                sv[i][j] = '0';
            }else if(i >= B && j < A || i < B && j >= A){
                sv[i][j] = '1';
            }
        }
    }
    rep(i, H){
        cout << sv[i] << endl;
    }
}

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

#include <iostream>
#include <vector>

int sort(std::vector<int> &v)
{
    int size = v.size();
    bool flag = true;
    int idx = 0;
    int swapCount = 0;
    while (flag) {
        flag = false;
        for (int i = size - 1; i >= idx + 1; --i) {
            if (v[i] < v[i-1]) {
                int tmp = v[i];
                v[i] = v[i-1];
                v[i-1] = tmp;
                ++swapCount;
            }
            flag = true;
        }
        ++idx;
    }
    return swapCount;
}

int main(void)
{
    int n;
    std::cin >> n;

    std::vector<int> v(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> v[i];
    }
    int cnt = sort(v);
    for (int i = 0; i < n; ++i) {
        std::cout << (i ? " " : "") << v[i];
    }
    std::cout << std::endl;
    std::cout << cnt << std::endl;
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include <iomanip>

std::vector<int> parse_line(std::string const& line) {
    using namespace std;
    vector<int> result;
    istringstream s(line);
    string element;
    while (getline(s, element, ' ')) {
        stringstream buf;
        int value = 0;
        buf << element;
        buf >> value;
        result.push_back(value);
    }
    return std::move(result);
}
int main(int argc, char* argv[])
{
    using namespace std;
    while (true) {
        string line;
        getline(std::cin, line);
        auto elements = parse_line(line);
        auto length = elements[0];
        if (length == 0) break;
        vector<int> total(length + 1);
        size_t width = length + 1;
        for (size_t i = 0; i < length; ++i) {
            getline(cin, line);
            elements = parse_line(line);
            int sum = 0;
            for (size_t n = 0; n < length; ++n) {
                int val = elements[n];
                sum += val;
                total[n] += val;
                cout << setw(5) << val;
            }
            cout << setw(5) << sum << endl;
            total[length] += sum;
        }
        for (size_t i = 0; i < width; ++i) {
            cout << setw(5) << total[i];
        }
        cout << endl;
    }

    return 0;
}

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

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

int main(){
  int n;
  while(cin>>n, n){
    int allsum = 0;
    vector<int> v(n);
    REP(i, n){
      int sum = 0;
      REP(j, n){
        int tmp;
        cin >> tmp;
        printf("%5d",tmp);
        allsum += tmp; sum += tmp;
        v[j] += tmp;
      }
      printf("%5d\n",sum);
    }
    REP(i, n)
      printf("%5d",v[i]);
    printf("%5d\n",allsum);
  }
  return 0;
}


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

int main() {
    map<ll,ll> mp;
    ll N, MOD;
    cin >> N >> MOD;
    ll sum[N+1];
    sum[0] =0;
    mp[0]++;
    for(int i=0;i<N;i++){
        ll a;
        cin >> a;
        sum[i+1] = sum[i] + a;
        sum[i+1] %=MOD;
        mp[sum[i+1]]++;
    }
    ll ans =0;
    for(auto itr = mp.begin();itr != mp.end();++itr){
        ll n = itr->second;
        ans += (n*(n-1))/2;
    }
    cout << ans << endl;

    return 0;
}

# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>
# include <unordered_map>
# include <unordered_set>
# include <thread>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ui32 = unsigned int;
constexpr ll INFLL = 1e18;

#define CHECK_IMPL1(cond)              \
if (!(cond)) {                         \
    DEBUG("expected cond: " << #cond); \
    assert(cond);                      \
}

#define CHECK_IMPL2(cond, message)                                            \
if (!(cond)) {                                                                \
    DEBUG("expected cond: " << #cond << " failed with message: " << message); \
    assert(cond);                                                             \
}
#define CHECK_IMPL(_1, _2, NAME, ...) NAME
#define CHECK(...) CHECK_IMPL(__VA_ARGS__, CHECK_IMPL2, CHECK_IMPL1, CHECK_IMPL0)(__VA_ARGS__)

#ifdef __APPLE__
#define DEBUG(message) std::cerr << message << "\n";
#else
#define DEBUG(message)
#endif

const int MOD = 1000 * 1000 * 1000 + 7;

struct Solver {
  void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

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

    int x = 0;
    for (int i = 0; i + 1 < n; i++) {
      x += 1LL * (a[i + 1] - a[i]) * (i + 1) % MOD * (n - i - 1) % MOD;
      if (x >= MOD) {
        x -= MOD;
      }
    }

    int y = 0;
    for (int i = 0; i + 1 < m; i++) {
      y += 1LL * (b[i + 1] - b[i]) * (i + 1) % MOD * (m - i - 1) % MOD;
      if (y >= MOD) {
        y -= MOD;
      }
    }

    cout << (1LL * x * y) % MOD << "\n";
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  int t = 1;
//  cin >> t;
  for (int i = 1; i <= t; i++) {
    Solver().solve();
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
#define rep(i, n) for (int i = 0; i < n; ++i)
char gf[1200][1200];
int main()
{
    int n;
    scanf("%d", &n);
    if (n < 3)
        printf("-1");
    else if (n == 3)
        printf("z..\nz..\n.zz");
    else if (n == 7)
        printf("a..bbaa\na...abb\n.az.a..\n.az..a.\n..azza.\nbba...a\naabb..a");
    else
    {
        int rn = n;
        if (n & 1)
            rn -= 5;
        for (int i = 0; i < rn; i += 2)
            gf[i][i] = gf[i + 1][i] = 'a',
            gf[i][i + 1] = gf[i + 1][i + 1] = 'b';
        for (int i = 2; i < rn; i += 2)
            gf[i][i - 2] = gf[i][i - 1] = 'c',
            gf[i + 1][i - 2] = gf[i + 1][i - 1] = 'd';
        gf[0][rn - 2] = gf[0][rn - 1] = 'c';
        gf[1][rn - 2] = gf[1][rn - 1] = 'd';
        if (n & 1)
            strcpy(&gf[rn][rn], "aabba"),
            strcpy(&gf[rn + 1][rn], "bz..a"),
            strcpy(&gf[rn + 2][rn], "bz..b"),
            strcpy(&gf[rn + 3][rn], "a.zzb"),
            strcpy(&gf[rn + 4][rn], "abbaa");
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
                if (gf[i][j])
                    printf("%c", gf[i][j]);
                else
                    printf(".");
            printf("\n");
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define uint unsigned long long
#define ll long long
#define db long double
#define ls rt << 1
#define rs rt << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi totor<int>
#define vl totor<ll>
#define rep(i, x, y) for(register int i = x; i <= y; i ++)
#define rrep(i, x, y) for(register int i = x; i >= y; i --)
#define eps 1e-15
#define lb(x) x & -x
#define sz(x) (x).size()
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
inline ll read1()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
const int mod = 1e9 + 7;
char ch[300010];
int n;
ll k, dp[300010][5][3];
int main()
{
	scanf("%s", ch);
	n = strlen(ch);
	dp[0][2][0] = 1;
	rep(i, 0, n - 1)
	{
        if (ch[i] != '0')
        {
            (dp[i + 1][4][1] += dp[i][4][1]) %= mod;
            (dp[i + 1][4][1] += dp[i][3][1]) %= mod;
            (dp[i + 1][3][1] += dp[i][2][0]) %= mod;
            (dp[i + 1][3][1] += dp[i][2][1]) %= mod;
            (dp[i + 1][2][0] += dp[i][1][0]) %= mod;
            (dp[i + 1][2][1] += dp[i][1][1]) %= mod;
            (dp[i + 1][1][0] += dp[i][0][0]) %= mod;
        }
        if (ch[i] != '1')
        {
            (dp[i + 1][4][1] += dp[i][4][1]) %= mod;
            (dp[i + 1][2][1] += dp[i][3][1]) %= mod;
            (dp[i + 1][1][0] += dp[i][2][0]) %= mod;
            (dp[i + 1][1][1] += dp[i][2][1]) %= mod;
            (dp[i + 1][0][0] += dp[i][1][0]) %= mod;
            (dp[i + 1][2][1] += dp[i][1][1]) %= mod;
            (dp[i + 1][1][0] += dp[i][0][0]) %= mod;
        }
    }
    cout << (dp[n][3][1] + dp[n][4][1]) % mod << endl;
    return 0;
}


#include<iostream>
using namespace std;

int x[10000000];
int G[20];
int v;
int w;
int cnt=0;
int m;
int n;

void sort1(int x[],int n,int g){
	for(int i=g;i<n;i++){
		v=x[i];
		w=i-g;
		while(w>=0 && x[w]>v){
			x[w+g]=x[w];
			w=w-g;
			cnt++;
		}
		x[w+g]=v;
	}
}

void sort2(int x[],int n){
	cnt=0;
	for(int h=0;h<20;h++){
		if(G[h]>n){
			m=h;
			break;
		}
	}
	cout<<m<<endl;
	for(int h=m-1;h>=0;h--){
		cout<<G[h];
		if(h){
			cout<<' ';
		}
		sort1(x,n,G[h]);
	}
	cout<<endl;
}

int main(){
	G[0]=1;
	for(int i=1;i<20;i++){
		G[i]=G[i-1]*3+1;
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x[i];
	}
	sort2(x,n);
	cout<<cnt<<endl;
	for(int i=0;i<n;i++){
		cout<<x[i]<<endl;
	}
	return 0;
}

#include <iostream>
#include <memory>
#include <cmath>

constexpr int max_size()
{
    return static_cast<int>(1e+6);
}

int insert(int* a, const int i, const int gear)
{
    if (i < gear) {
        return 0;
    }

    if (a[i - gear] <= a[i]) {
        return 0;
    }

    std::swap(a[i - gear], a[i]);
    return 1 + insert(a, i - gear, gear);
}

int insertion_sort(int* arr, const int n, const int gear)
{
    int insert_count = 0;
    for (int i = 0; i < n; i += gear) {
        insert_count += insert(arr, i, gear);
    }
    return insert_count;
}

int pow(const int a, const int x) {
    return x == 0 ? 1 : a * pow(a, x - 1);
}

void print(int* arr, const int n, char const delim) {
    int i = 0;
    for (; i < n - 1; ++i) {
        std::cout << arr[i] << delim;
    }
    std::cout << arr[i];
}

void shell_sort(int* arr, const int n) {
    static const double log3 = std::log(3);
    const int gear_index = static_cast<int>(std::log(2 * n + 1) / log3 - 1);
    int gear = (pow(3, gear_index + 1) - 1) >> 1;

    int gear_cache[100];
    int cache_idx = 0;
    int insert_count = 0;

    for (; gear > 0; gear = (gear - 1) / 3) {
        gear_cache[cache_idx] = gear;
        for (int i = 0; i < gear; ++i) {
            insert_count += insertion_sort(arr + i, n - i, gear);
        }
        ++cache_idx;
    }

    std::cout << cache_idx << std::endl;
    print(gear_cache, cache_idx, ' ');
    std::cout << std::endl;
    std::cout << insert_count << std::endl;

    return;
}

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

    int n;
    std::cin >> n;

    constexpr int N = max_size();
    const auto r = std::make_unique<int[]>(N);
    for (int i = 0; i < n; ++i) {
        std::cin >> r[i];
    }

    shell_sort(r.get(), n);
    print(r.get(), n, '\n');

    return 0;
}


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

#define ll long long

ll GCD(ll a, ll b) {
  return a % b ? GCD(b, a % b): b;
}

vector<pair<ll, ll> > prime_factorize(ll n) {
    vector<pair<ll,ll>> res;
    for (ll a = 2; a * a <= n; ++a) {
        if (n % a != 0) continue;
        ll ex = 0; // 指数

        // 割れる限り割り続ける
        while (n % a == 0) {
            ++ex;
            n /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (n != 1) res.push_back({n, 1});
    return res;
}

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

  ll g = GCD(max(A, B), min(A,B));

  vector<pair<ll,ll>> vec_f = prime_factorize(g);

  cout << vec_f.size() + 1 << endl;

}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,n,j) for(int i=(j);i<(n);++i)
#define ssort(n) sort((n).begin(),(n).end())
#define rsort(n) sort((n).begin(),(n).end(),greater<int>())
using ll=long long;
using ld=long double;
typedef pair<int,int> P;
typedef pair<P,int> COST;
#define repl(i,n) for(ll i=0;i<(n);++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
using Graf=vector<vector<int>>;
#define MAX 10000000

int main()
{
    ll a,b;
    vector<ll> num;
    cin >> a >> b;

    if(a==b&&a==1){
        cout << 1 << endl;
        return 0;
    }
    if(a<b){
        swap(a,b);
    }
    ll c=a;
    ll i;

    for(i=2;i*i<=c;++i){
        if(c%i!=0){
            continue;
        }
        while(c%i==0){
            c/=i;
        }
        num.push_back(i);
    }
    if(c!=1){
        num.push_back(c);
    }
    ssort(num);

    ll count=1;
    rep(i,num.size()){
        if(b>=num[i]){
            if(b%num[i]==0){
                count++;
            }
        }
    }
    cout << count << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
    int N, M;
    cin >> N >> M;
    int INF = -1e9;
    vector<int> dpk(N + 1, INF), A(M);
    for(int i = 0; i < M; i++) cin >> A[i];
    sort(A.begin(), A.end());
    vector<int> cost(M);
    for(int i = 0; i < M; i++){
        if(A[i] == 1) cost[i] = 2;
        else if(A[i] == 2 || A[i] == 3 || A[i] == 5) cost[i] = 5;
        else if(A[i] == 4) cost[i] = 4;
        else if(A[i] == 6 || A[i] == 9) cost[i] = 6;
        else if(A[i] == 7) cost[i] = 3;
        else cost[i] = 7;
    }
    dpk[0] = 0;
    for(int i = 1; i < N + 1; i++){
        for(int j = 0; j < M; j++){
            if(i - cost[j] >= 0) dpk[i] = max(dpk[i], dpk[i - cost[j]] + 1);
        }
    }
    int keta = dpk[N];
    vector<int> ke(keta);
    int i = 0;
    while(N > 0){
        for(int j = M - 1; j >= 0; j--){
            if(N - cost[j] >= 0 && dpk[N] == dpk[N - cost[j]] + 1){
                ke[i] = A[j];
                i++;
                N -= cost[j];
                break;
            }
        }
    }
    for(int i = 0; i < keta; i++){
        cout << ke[i];
    }
    cout << endl;
}

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

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

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

string dp[20000];

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> a(m);
    int cost[10] = {-1, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    rep(i, m)cin >> a[i];

    rep(i, n + 100)dp[i] = "-";
    dp[0] = "";

    rep(i, n){
        if(dp[i] == "-")continue;
        for(int now : a){
            if(dp[i + cost[now]].size() == dp[i].size() + 1){
                if(dp[i + cost[now]] < dp[i] + to_string(now))dp[i + cost[now]] = dp[i] + to_string(now);
            }
            else if(dp[i + cost[now]].size() < dp[i].size() + 1)dp[i + cost[now]] = dp[i] + to_string(now);
        }
    }
    cout << dp[n] << endl;
}


#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;

int main() {

    // 最小本数
    // 最大本数

    // 桁数

    // 桁目を何にしたか
    // 残り

    // 大きい順に優先
    // 残り桁数＊最小本数＜＝残り本数＆＆残り桁数＊最大本数＞＝残り本数なら可能

    map<int, int> u = {
            {1, 2},
            {2, 5},
            {3, 5},
            {4, 4},
            {5, 5},
            {6, 6},
            {7, 3},
            {8, 7},
            {9, 6},
    };

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

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

    int mi = INT_MAX;
    for (int i : v) cmin(mi, u[i]);
    int ma = 0;
    for (int i : v) cmax(ma, u[i]);

    vector<int> dp(n + 100, -1);
    vector<int> prev(n + 100, -1);
    dp[0] = 0;

    rep(i, n) {
        if (dp[i] == -1) continue;
        for (int j : v) {
            int use = u[j];
            int next_keta = dp[i] + 1;
            int next_use = i + use;
            if (dp[next_use] <= next_keta) {
                dp[next_use] = next_keta;
                prev[next_use] = j;
            }
        }
    }

    int k = dp[n];

    int a = n;
    while (a > 0) {
        for (int i : v) {
            int use = u[i];
            int back = a - use;
            if (back < 0) continue;
            if (dp[a] - 1 == dp[back]) {
                cout << i;
                a = back;
                break;
            }
        }
    }

}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int m;
vector<int> v;
int num[10] = {0,2,5,5,4,5,6,3,7,6};
int dp[10010][10];
int pre[10010][10];
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  for (int i = 0; i < 10010; i++) {
    for (int j = 0; j < 10; j++) {
      dp[i][j] = -1;
      pre[i][j] = -1;
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < m; i++) {
    dp[0][i] = 0;
  }
  int mx = -1;
  int pos = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = j; k < m; k++) {
        int use = num[v[j]];
        if (dp[i+use][k] < dp[i][j]+1) {
          dp[i+use][k] = dp[i][j]+1;
          pre[i+use][k] = j;
        }
        else if (dp[i+use][k] == dp[i][j]+1) {
          pre[i+use][k] = max(pre[i+use][k],j);
        }
        if (dp[i+use][k] > mx && i+use == n) {
          mx = dp[i+use][k];
          pos = k;
        }
        else if (i+use == n && dp[i+use][k] == mx) {
          pos = max(pos,k);
        }
      }
    }
  }
  int y = n;
  int x = pos;
  // for (int i = 0; i <= n; i++) {
  //   for (int j = 0; j < m; j++) {
  //     cout << pre[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  while (1) {
    // cout << x << " " << y << endl;
    cout << v[pre[y][x]];
    int kk = v[pre[y][x]];
    x = pre[y][x];
    y -= num[kk];
    if (y == 0) break;
  }
  cout << endl;
}

/*　【提出前に確認すること】
・コンパイルできるか
・テストケース試したか
・オーバーフローあるか
*/


#include <iostream>
using namespace std;

struct Dice{
	int f[7];
	void move(char dir){
		int sw;
		switch(dir){
		case 'E':
			sw = f[1];
			f[1] = f[4];
			f[4] = f[6];
			f[6] = f[3];
			f[3] = sw;
			break;
		case 'N':
			sw = f[1];
			f[1] = f[2];
			f[2] = f[6];
			f[6] = f[5];
			f[5] = sw;
			break;
		case 'S':
			sw = f[1];
			f[1] = f[5];
			f[5] = f[6];
			f[6] = f[2];
			f[2] = sw;
			break;
		case 'W':
			sw = f[1];
			f[1] = f[3];
			f[3] = f[6];
			f[6] = f[4];
			f[4] = sw;
			break;
		}
	}
};

int main() {
	Dice dice;
	for(int i = 1; i <= 6; ++i){
		cin >> dice.f[i];
	}
	string com;
	cin >> com;
	int n = com.length();
	for(int i = 0; i < n; ++i){
		dice.move(com[i]);
	}
	cout << dice.f[1] << endl;
	return 0;
}

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

int main(void){
	int N, M, i, j, maxp = 0, maxs;
	scanf("%d %d", &N, &M);
	int a[N], b[M], p[N];
	for (i = 0; i < N; ++i){
		cin >> a[i];
		p[i] = 0;
	}
	for (j = 0; j < M; ++j){
		cin >> b[j];
	}
	for (j = 0; j < M; ++j){
		for (i = 0; i < N; ++i){
			if (a[i] <= b[j]){
				p[i]++;
				break;
			}
		}
	}
	for (i = 0; i < N; ++i){
		if (p[i] > maxp){
			maxp = p[i];
			maxs = i+1;
		}
	}
	cout << maxs << endl;
	return 0;
}


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

int main()
{
	int n; char c; string s;
	while(cin >> n)
	{
		int cd[13]={0},pr[5]={0};

		cd[n-1]++;
		for(int i=0; i<4; i++)
		{
			cin >> c >> n;
			cd[n-1]++;
		}

		for(int i=0; i<13; i++)
		{
			pr[cd[i]]++;
		}

		bool st=false;
		for(int i=0; i<10; i++)
		{
			int sm=0;
			for(int j=0; j<5; j++) sm+=(cd[(i+j)%13]?1:0);
			if(sm==5) st=true;
		}

		                      s="null";
		     if(pr[4])        s="four card";
		else if(pr[3]&&pr[2]) s="full house";
		else if(st)           s="straight";
		else if(pr[3])        s="three card";
		else if(pr[2]==2)     s="two pair";
		else if(pr[2])        s="one pair";

		cout << s << endl;
	}
}

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

int main(){
	int hand[5];
	char s[4];
	while(cin >> hand[0] >> s[0] >> hand[1] >> s[1] >> hand[2] >> s[2] >> hand[3] >> s[3] >> hand[4]){
		int trump[14] = {};
		for(int i = 0; i < 5; i++)trump[hand[i]]++;
		for(int i = 1; i <= 10; i++){
			for(int j = 0; j < 5; j++){
				int k = i + j;
				if(k == 14)k = 1;
				if(trump[k] != 1)goto no;
			}
			cout << "straight" << endl;
			goto fin;
			no:;
		}
		sort(trump, trump + 14, greater<int>());
		if(trump[0] == 4){
			cout << "four card" << endl;
			goto fin;
		}else if(trump[0] == 3 && trump[1] == 2){
			cout << "full house" << endl;
			goto fin;
		}else if(trump[0] == 3){
			cout << "three card" << endl;
			goto fin;
		}else if(trump[0] == 2 && trump[1] == 2){
			cout << "two pair" << endl;
			goto fin;
		}else if(trump[0] == 2){
			cout << "one pair" << endl;
			goto fin;
		}
		cout << "null" << endl;
		fin:;
	}
}


#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
using namespace std;

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

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

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

	vector<string> S(3);
	for(int i = 0; i < 3; i++) cin >> S[i];

	int i = 0;

	while(S[i].size()){
		char r = S[i][0];
		S[i].erase(S[i].begin());
		i = r - 'a';
	}

	cout << (char)(i + 'A') << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
int A,B,C,D;
  cin>>A>>B>>C>>D;
  if(A<=B){
  if(C<=D)
    cout<<A+C<<endl;
    else
      cout<<A+D<<endl;
  }else{
    if(C<=D)
      cout<<B+C<<endl;
    else
      cout<<B+D<<endl;

  }
   return 0;
}


/*      ░░█ ▄▀█ █   █▀ █░█ █▀█ █▀▀ █▀▀   █▀█ ▄▀█ █▀▄▀█
        █▄█ █▀█ █   ▄█ █▀█ █▀▄ ██▄ ██▄   █▀▄ █▀█ █░▀░█     */

// Author : Udit "luctivud" Gupta @ (https://www.linkedin.com/in/udit-gupta-1b7863135/)

#include <bits/stdc++.h>
#pragma GCC optimize "trapv"

using namespace std;

typedef long long int lld;
typedef unsigned long long int llu;

#define       TESTCASES()  cin >> (T3X0); T353 = T3X0; while(T3X0--)
#define       _input(V3C)  for(auto &V3C_I7 : (V3C)) cin >> (V3C_I7)
#define mems(A77AY, V4LU)  memset((A77AY), (V4LU), sizeof((A77AY)))
#define  CH3K(I7, E4, S7)  (((S7)<0) ? (I7)>(E4) : (I7)<(E4))
#define for4(I7,S4,E4,S7)  for(auto I7=(S4); CH3K(I7,E4,S7); (I7)+=(S7))

void solveEachTest(lld T35TC453N = 1) {

	lld n; cin >> n;
	lld temp = n;
	lld suum = 0ll;
	while (temp) {
		suum += temp % 10;
		temp /= 10;
	}
	puts( (n % suum == 0) ? "Yes" : "No" );

	return;
}

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

	lld T3X0 = 0, T353 = 1;

	// TESTCASES()
	solveEachTest(T353 - T3X0);
	return 0;
}
// Random Thought :  null

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i <= (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define inf 2147483647 //int値の最大値
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define CST(x) cout<<fixed<<setprecision(x)//小数点以下の桁数指定
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int two(int n){
	ll ans=1;
	rep(i,n){
		ans*=2;
	}
	return ans;
}
int waru(int n){
    int s=0;
    while(n>0){
        s +=n%10;
        n=n/10;
    }
    return s;

}

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

    int n;
    cin>>n;

        if(n%waru(n)==0){
            cout<<"Yes\n";
        }
    else{
    cout<<"No"<<endl;
}

}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define pb emplace_back
#define ll long long

const int INF = 2e9 + 1;
const ll INFLL = 1e18 + 1;
const ll mod = 1e9 + 7;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, r;
    cin >> n >> r;
    cout << r + 100 * max(0, 10 - n);
    return 0;
}

#include<iostream>
using namespace std;
long long int MOD = 1000000007;
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);

int main() {

	//A
	int n, r;
	cin >> n >> r;
	if (n >= 10) {
		cout << r << endl;
	}
	else {
		cout << r + 100 * (10 - n) << endl;
	}
	return 0;

	//B
	/*int n;
	cin >> n;
	int k;
	cin >> k;
	int i = 0;
	while (n > 0) {
		n = (int)(n / k);
		i++;
	}
	cout << i << endl;
	return 0;*/
	//C
	/*int n;
	cin >> n;
	int x[100];
	int i;
	for (i = 0; i < n; i++) {
		cin >> x[i];
	}
	int sum, j, summin = 100000000;
	for (i = 0; i <= 100; i++) {
		sum = 0;
		for (j = 0; j < n; j++) {
			sum = sum + (x[j] - i) * (x[j] - i);
		}
		if (sum < summin)summin = sum;
	}
	cout << summin << endl;
	return 0;
	*/
	//D
	/*
	long long int n;
	cin >> n;
	long long int a, b;
	cin >> a >> b;
	if (n == 2) {
		cout << 0 << endl;
		return 0;
	}
	long long int sum = 1;
	int i;
	long long int x = 2, m = n;
	//2^nを計算
	while (m > 0) {
		if (m % 2 == 1) {
			sum = (sum * x) % MOD;
		}
		m = (int)(m / 2);
		x = (x * x) % MOD;
	}

	//cout << 1 << endl;

	long long int sum1 = 1;
	for (i = 1; i <= a; i++) {
		sum1 = (sum1 * (n + 1 - i)) % MOD;
	}
	for (i = 1; i <= a; i++) {
		sum1 = (sum1 * modinv(i, MOD)) % MOD;
	}
	sum = (sum - sum1 + MOD) % MOD;
	//cout << 2 << endl;

	sum1 = 1;
	for (i = 1; i <= b; i++) {
		sum1 = (sum1 * (n + 1 - i)) % MOD;
	}
	for (i = 1; i <= b; i++) {
		sum1 = (sum1 * modinv(i, MOD)) % MOD;
	}
	sum = (sum - sum1 + MOD) % MOD;
	sum = (sum + MOD - 1) % MOD;
	cout << sum << endl;
	return 0;
	*/
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
	return modpow(a, mod - 2, mod);
}


#include <bits/stdc++.h>

using namespace std;
#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define _dec(i,N,j) for(int i = (N-1);i >= (j);i--)
#define _rec(i,N,j) for(int i = (N);i >=(j);i--)
#define ALL(x) x.begin(),x.end()
#define MEM(a,n) memset(a,n,sizeof(a))
#define fst first
#define scd second
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

int get_int(){int x;scanf("%d",&x);return x;}
LL get_ll(){LL x;scanf("%lld",&x);return x;}
double get_db(){double x;scanf("%lf",&x);return x;}

template<typename T> int chmin(T &a,T b){if(a > b){a = b;return 1;}else return 0;}
template<typename T> int chmax(T &a,T b){if(a < b){a = b;return 1;}else return 0;}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {_for(i,0,v.size()) os << v[i] << " ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){for(auto a:v) os << a <<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& v){os << v.fst <<" "<<v.scd << " ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>& v){for(auto a:v) cout << a << endl;return os;}

static const int dx[8] = { -1,  0, 1, 0,  1, -1,  1, -1};
static const int dy[8] = {0,  1, 0,  -1,  1, -1, -1,  1};
const double pi  = acos(-1.0);
const double eps = 1e-8;
const LL  INF  = 0x3f3f3f3f3f3f3f3f;
const int inf  = 0x3f3f3f3f;

const int maxn = 1e5+5;
unsigned long long sum = 0;
LL ans = 0;
LL one = 1;

unsigned long long A[maxn];
int used[maxn];
int cnt[maxn];

char M[62][maxn];

int N;

int Gauss(int H,int W)
{
    int i,j,col;

    for(col = 0;col < W;col++){

        int pivot = -1;
        for(int row = 0;row < H;row++){
            int bit = 61 - row;
            if(used[bit]) continue;
            if(M[row][col] == 1){
                pivot = row;
                break;
            }
        }

        used[61 - pivot] = 1;
        for(int row = 0;row < H;row++){
            int bit = 61 - row;
            if(used[bit]) continue;
            if(M[row][col] == 1){
                for(j = col;j <= W;j++){
                    M[row][j] ^= M[pivot][j];
                }
            }
        }
    }

    return 0;
}

int bit_n(LL t){
    int ret = 0;
    _for(i,0,N){
        if(t & A[i]) ret++;
    }
    return ret;
}

int main()
{
    cin >> N;
    _for(i,0,N) {
        cin >> A[i];
    }
    MEM(used,0);

    for(int bit = 61;bit >= 0;bit--){
        LL t = one<<bit;
        cnt[bit] = bit_n(t);
    }
    MEM(M,0);

    LL ans = 0;

    for(int bit = 0;bit <= 61;bit++){
        LL t = one<<bit;

        if(cnt[bit] == 0) {
            used[bit] = 1;
            continue;
        }
        else if(cnt[bit] & 1) {
            ans += t;
            used[bit] = 1;
            continue;
        }
        else M[61 - bit][N] = 1;

        _for(i,0,N){
            if(t & A[i]){
                if(!used[bit])
                    M[61-bit][i] = 1;
            }
        }
    }

    /*
    _for(i,54,62){
        if(cnt[61-i] == 0 || (cnt[61-i]&1)) continue;
        _rep(j,0,N){
            printf("%d ",M[i][j]);
        }
        printf("\n");
    }
    */

    Gauss(62,N);

    _rep(i,0,61){
        int bit = 61 - i;
        if(cnt[bit] == 0 || (cnt[bit]) & 1) continue;
        int ok = 0;

        _for(j,0,N) if(M[i][j]) ok = 1;
        if(!M[i][N]) ok = 1;
        if(ok) ans += (one << (bit+1));

    }

    /*
   _for(i,54,62){
        if(cnt[61-i] == 0 || (cnt[61-i]&1)) continue;
        _rep(j,0,N){
            printf("%d ",M[i][j]);
        }
        printf("\n");
    }
    */
    cout << ans << endl;

    return 0;
}

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>

using namespace std;
typedef long long ll;

int main() {
	ll N; cin >> N;
	ll* A = new ll[N];
	ll m = 0;
	ll sum = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		sum = sum ^ A[i];
		m = m | A[i];
	}

	ll filter = m - sum;

	set<ll, greater<ll>> st;
	for (int i = 0; i < N; i++) {
		ll a = A[i] & filter;
		for (auto itr = st.begin(); itr != st.end(); itr++) {
			a = min(a, a ^ *itr);
		}
		if (a != 0) {
			st.insert(a);
		}
	}

	ll s = filter;
	for (auto itr = st.begin(); itr != st.end(); itr++) {
		s = min(s, (s ^ *itr) & filter);
	}
	cout << sum + (filter - s) * 2 << endl;

	return 0;
}


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

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

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

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

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

#define en "\n"

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

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

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

int N, L, LOGN; // LOGN = floor(log2(N))+1;
VI x;
vector<vector<int>> tab,tab2;
void doubling(){
    for(int i=0; i<N; i++){
        int it = UB(x, x[i]+L);
        if(it-1 == i) tab[i][0] = -1;
        else tab[i][0] = it-1;
    }

    for(int k=0; k<LOGN; k++)
        for(int i=0; i<N; i++){
            if(tab[i][k] == -1) tab[i][k+1] = -1;
            else tab[i][k+1] = tab[tab[i][k]][k];
        }
}

int query(int n, long long k){
    int ret = n, count = 0;
    while(k){
        if(k&1) ret = tab[ret][count];
        count++;
        k >>= 1;
        if(ret == -1) return ret;
    }
    return ret;
}

void doubling2(){
    for(int i=0; i<N; i++){
        int it = LB(x,x[i]-L);
        if(it == i) tab2[i][0] = -1;
        else tab2[i][0] = it;
    }

    for(int k=0; k<LOGN; k++)
        for(int i=0; i<N; i++){
            if(tab2[i][k] == -1) tab2[i][k+1] = -1;
            else tab2[i][k+1] = tab2[tab2[i][k]][k];
        }
}

int query2(int n, long long k){
    int ret = n, count = 0;
    while(k){
        if(k&1) ret = tab2[ret][count];
        count++;
        k >>= 1;
        if(ret == -1) return ret;
    }
    return ret;
}

void Main(){
    cin >> N; LOGN = floor(log2(N))+1; x.resize(N); tab.resize(N,VI(LOGN+1)), tab2.resize(N,VI(LOGN+1));
    REP(i,N) cin >> x[i];
    cin >> L;

    doubling(); doubling2();

    int Q; cin >> Q;
    REP(_,Q){
        int a,b; cin >> a >> b; a--; b--;
        int ans;
        if(a < b){
            int l = 0, r = N;
            while(r-l>1){
                int k = (l+r)/2;
                int loc = query(a,k);
                if(loc<b && loc!=-1) l = k;
                else r = k;
            }
            ans = r;
        }
        else{
            int l = 0, r = N;
            while(r-l>1){
                int k = (l+r)/2;
                int loc = query2(a,k);
                if(b<loc) l = k;
                else r = k;
            }
            ans = r;
        }
        cout << ans << en;
    }
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

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

int main() {
  int n,q;
  vector<int> S,T;

  cin >> n;
  for(int i=0;i<n;i++) {
    int s;
    cin >> s;
    S.push_back(s);
  }
  cin >> q;
  for(int i=0;i<q;i++) {
    int t;
    cin >> t;
    T.push_back(t);
  }

  // O(n^2)
  int C = 0;
  for(int t_itr=0;t_itr<q;t_itr++) {
    int key = T[t_itr];
    for(int s_itr=0;s_itr<n;s_itr++) {
      if(S[s_itr] == key) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}


#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define N 1100005
using namespace std;

typedef long long ll;

ll n, ans, kac, a[N], pre[N], dp[N], fen[N], of[N];
vector < int > g[N];

ll fe(ll a, ll us){
	if(us <= 1)
		return (us)?a:1ll;
	ll t = fe(a, us/2);
	return (us&1)?(t*t%mod)*a%mod : t*t%mod;
}

void up(int x, int y){
	for(; x < N; x += x&-x)
		fen[x] += y;
}

int qu(int x){
	int ans = 0;
	for(; x > 0; x -= x&-x)
		ans += fen[x];
	return ans;
}

int main() {
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	scanf("%lld",&n);
	for(int i = 1; i <= n; i++){
		scanf("%lld",a + i);
		pre[i] = pre[i - 1]^a[i];
		g[pre[i]].pb(i);
		if(!pre[i])
			up(i, 1);
	}
	if(pre[n] != 0){
		ll sif = 1;
		ll bir = 0;
		for(int i = 1; i <= n; i++){
			if(pre[i] == pre[n])
				bir = (bir + sif)%mod;
			else if(pre[i] == 0)
				sif = (sif + bir)%mod;
		}
		printf("%lld\n", sif);
		return 0;
	}
	kac = (int)g[pre[n]].size();
	ans = (ans + fe(2, kac - 1))%mod;

	// cout << ans << endl;

	for(int i = 1; i < N; i++){
		if(g[i].empty())
			continue;
		int u = (int)g[i].size(), ek = 1;
		of[0] = dp[0] = 1;
		// cout << i << " " << u << endl << " -> " << g[i][0] << " ";
		for(int j = 1; j < u; j++){
			int ara = qu(g[i][j]) - qu(g[i][j - 1]);
			dp[j] = (dp[j - 1] - 1 + of[j - 1]*ara%mod + 1)%mod;
			of[j] = (of[j - 1] + dp[j])%mod;
			// cout << "(" << ara << ") " << g[i][j] << " - " << dp[j] << " ";
		}
		ans = (ans + of[u - 1])%mod;
		// cout << " = " << of[u - 1] << endl;
	}

	printf("%lld\n", ans);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
using namespace std;

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

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair

const ll MOD = 998244353;
ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll Rev(ll x) {
	return bin_pow(x, MOD - 2);
}
ll W;

const int LOG = 20;
const int N = 1 << 20;
ll w[N];
int rev[N];

void initFFT() {
	for (ll x = 2;; x++) {
		ll y = x;
		for (int i = 1; i < LOG; i++)
			y = mult(y, y);
		if (y == MOD - 1) {
			W = x;
			break;
		}
	}
	w[0] = 1;
	for (int i = 1; i < N; i++)
		w[i] = mult(w[i - 1], W);
	rev[0] = 0;
	for (int mask = 1; mask < N; mask++) {
		int k = 0;
		while(((mask >> k) & 1) == 0) k++;
		rev[mask] = rev[mask ^ (1 << k)] ^ (1 << (LOG - 1 - k));
	}
}

ll F[2][N];
void FFT(ll *A) {
	for (int i = 0; i < N; i++)
		F[0][rev[i]] = A[i];
	int t = 0, nt = 1;
	for (int lvl = 0; lvl < LOG; lvl++) {
		int len = 1 << lvl;
		for (int st = 0; st < N; st += (len << 1))
			for (int i = 0; i < len; i++) {
				ll ad = mult(F[t][st + len + i], w[i << (LOG - 1 - lvl)]);
				F[nt][st + i] = add(F[t][st + i], ad);
				F[nt][st + len + i] = sub(F[t][st + i], ad);
			}
		swap(t, nt);
	}
	for (int i = 0; i < N; i++)
		A[i] = F[t][i];
}

ll f[N], rf[N];
ll a[N];

ll getC(int n, int k) {
	if (k < 0 || k > n) return 0;
	return mult(f[n], mult(rf[k], rf[n - k]));
}

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

	initFFT();

	f[0] = 1;
	for (int i = 1; i < N; i++)
		f[i] = mult(f[i - 1], i);
	rf[N - 1] = Rev(f[N - 1]);
	for (int i = N - 1; i > 0; i--)
		rf[i - 1] = mult(rf[i], i);

	for (int k = 0; k < N / 2; k++) {
		a[k] = mult(f[2 * k], mult(rf[k], rf[k]));
	}
	FFT(a);
	for (int i = 0; i < N; i++)
		a[i] = mult(a[i], a[i]);
	FFT(a);
	reverse(a + 1, a + N);
	ll rN = Rev(N);
	for (int i = 0; i < N; i++) {
		a[i] = mult(a[i], rN);
		a[i] = mult(a[i], mult(rf[2 * i], mult(f[i], f[i])));
		a[i] = sub(a[i], 1);
		a[i] = mult(a[i], (MOD + 1) / 2);
		a[i] += i;
	}

	int n, m;
	scanf("%d%d", &n, &m);
	if (n < m) swap(n, m);
	if (n == m) {
		printf("%lld\n", a[n]);
		return 0;
	}

	ll ans = 0;

	for (int k = 0; k <= m; k++) {
		int L = n - m + k - 1;
		ll p = getC(L + k, k);
		if (k > 0)
			p = sub(p, getC(L + k, k - 1));
		p = mult(p, getC(2 * (m - k), m - k));
		ans = add(ans, mult(p, add(L + 1, a[m - k])));
	}
	ans = mult(ans, mult(rf[n + m], mult(f[n], f[m])));
	printf("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    char conv[256];
    int conv2[256];

    for (int i = 0; i < 26; i++){
        conv[i] = 'a' + i;
        conv[i + 26] = 'A' + i;
        conv2['a' + i] = i;
        conv2['A' + i] = i + 26;
    }

    while (scanf("%d", &n) && n){
        int a[128];
        char s[128];

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

        scanf("%s", s);

        for (int i = 0; s[i]; i++){
            printf("%c", conv[(conv2[s[i]] - a[i % n] + 52) % 52]);
        }
        printf("\n");
    }

    return (0);
}

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

int main(){
    int n;
    while(cin>>n,n){
        vector<int> a;
        for(int i=0,t;i<n;++i){
            cin>>t;
            a.push_back(t);
        }
        string s,ans;
        cin>>s;

        string alpha;
        map<char, int> alphaM;
        int id = 0;
        for(char c = 'a'; c <= 'z'; c++) {
            alpha.push_back(c);
            alphaM[c] = id++;
        }
        for(char c = 'A'; c <= 'Z'; c++) {
            alpha.push_back(c);
            alphaM[c] = id++;
        }

        for(int i=0;i<s.size();++i){
            int x=a[i%a.size()];
            int id2 = alphaM[s[i]];
            ans += alpha[(id2-x+52)%52];
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>

// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>

typedef long long LL;
//typedef long double LD;
using namespace std;

//#define MOD 1000000007
//#define MAX 100100
//#define NIL -1
//#define INFTY 1000000000000000000

int main(){
    LL a;
    LL b;
    LL c;
    LL d;
    cin >> a >> b >> c >> d;
    if(a+b>c+d){
        cout << "Left" << endl;
    }else if(a+b==c+d){
        cout << "Balanced" << endl;
    }else{
        cout << "Right" << endl;
    }

    return 0;
}

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

using namespace std;

using ll = long long;
using ld = long double;

const ld pi = 3.141592653589793;

int w[4];

int main() {
  for (int i = 0; i < 4; i++) {
    cin >> w[i];
  }

  if (w[0] + w[1] > w[2] + w[3]) {
    cout << "Left" << endl;
    return 0;
  }
  if (w[0] + w[1] == w[2] + w[3]) {
    cout << "Balanced" << endl;
    return 0;
  }

  if (w[0] + w[1] < w[2] + w[3]) {
    cout << "Right" << endl;
    return 0;
  }
}


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

struct Dice {
	int d[6], E, S, U;
	void init(int a[]) {
		rep(i, 6)d[i] = a[i];
		E = 2; S = 1; U = 0;
	}
	void rotate(char C) {
		int a = E, b = S, c = U;
		switch (C) {
		case'E':E = U; U = 5 - a; break;
		case'N':U = S; S = 5 - c; break;
		case'S':S = U; U = 5 - b; break;
		case'W':U = E; E = 5 - c; break;
		case'R':S = E; E = 5 - b; break;
		case'L':E = S; S = 5 - a; break;
		}
	}
	int rotate(int a, int b) {
		rep(k, 2) {
			rep(i, 4) {
				rep(j, 4) {
					if (a == d[U] && b == d[S])return d[E];
					rotate('E');
				}
				rotate('S');
			}
			rotate('L');
		}
	}
	bool operator==(Dice b) {
		rep(k, 2) {
			rep(i, 4) {
				rep(j, 4) {
					if (b.d[b.U] == d[U] && b.d[b.S] == d[S] && b.d[b.E] == d[E] &&
						b.d[5 - b.U] == d[5 - U] && b.d[5 - b.S] == d[5 - S] && b.d[5 - b.E] == d[5 - E])
						return true;
					rotate('E');
				}
				rotate('S');
			}
			rotate('L');
		}
		return false;
	}
};
Dice d[100]; int a[100][6];
int main() {
	int n; cin >> n;
	rep(i, n) {
		rep(j, 6)cin >> a[i][j];
		d[i].init(a[i]);
	}
	rep(i, n)for (int j = i + 1; j < n; j++) {
		if (d[i] == d[j]) {
			puts("No"); return 0;
		}
	}
	puts("Yes");
}

#include <iostream>
using namespace std;
static const int MAX = 101;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim()
{
    int u, mincost;
    int d[MAX], p[MAX], color[MAX];
     for(int i=0; i<MAX; i++)
     {
         color[i] = WHITE;
         d[i] = INFTY;
         p[i] = -1;
     }
     d[0] = 0;
     while(1)
     {
         mincost = INFTY;
         for(int i=0; i<n; i++)
         {
             if(color[i] != BLACK && d[i] < mincost)
             {
                 mincost = d[i];
                 u = i;
             }
         }
         if(mincost == INFTY)
                break;
         color[u] = BLACK;
         for(int v=0; v<n; v++)
         {
             if(color[v] != BLACK && M[u][v] != -1)
             {
                 if(M[u][v] < d[v])
                 {
                     d[v] = M[u][v];
                     p[v] = u;
                     color[v] = GRAY;
                 }
             }
         }
     }
     int sum = 0;
     for(int i=0; i<n; i++)
     {
         if(p[i] != -1)
            sum += M[i][p[i]];
     }
     return sum;
}
int main()
{
     cin >> n;
     int e;
     for(int i=0; i<n; i++)
     {
         for(int j=0; j<n; j++)
         {
             cin >> e;
             if(e == -1)
                M[i][j] = INFTY;
             else
                M[i][j] = e;
         }
     }
     cout << prim() << endl;
     return 0;
}


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

#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define endl '\n'
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second

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

/*追加部分*/

/*main関数*/

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

    cout << ((a + b) > (c + d) ? "Left" : (a + b) == (c + d) ? "Balanced" :  "Right") << endl;
return 0;
}

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

int main(){
  int a,b,c; cin >> a >> b >> c;
  string ans = "Yes";
  if((a-c)*(b-c)>0) ans ="No";
  cout << ans << endl;
}


#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;

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

	int ans = INT_MAX;
	for (int i = 0; i < N; i++) {
		int n;
		cin >> n;

		int cnt = 0;
		while (n%2==0) {
			if (n / 2) {
				cnt++;
				n /= 2;
			}
			else {
				break;
			}
		}

		ans = min(ans, cnt);
	}

	cout << ans << endl;
}

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <deque>
using namespace std;

int main() {
    //input
    int n; cin >> n;
    long long sum = 0;
    int counter = 0;
    vector<long> a(n + 1);
    for(int i = 1; i < n + 1; i++) cin >> a[i];
    for(int i = 1; i < n + 1; i++){
        if(a[i] < 0){
           counter += 1;
        }
        a[i] = abs(a[i]);
    }

    sort(a.begin() + 1, a.end());
    for(int i = 1; i < n + 1; i++) sum += a[i];

    if(counter % 2 == 0 ){
      cout << sum << endl;
    }else{
      cout << sum - 2 * a[1] << endl;
    }

    //output
}


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

typedef long long i64;

int main()
{
	int n;
	vector<i64> as;

	scanf("%d", &n);
	as.resize(n);
	for(int i = 0; i < n; i++) {
		scanf("%lld", &as[i]);
	}

	i64 sum = 0;
	for(auto& a : as) {
		sum = sum ^ a;
	}

	for(auto& a : as) {
		a = a & (~sum);
	}

	i64 maxi = 0;

	for(int b = 62; b >= 0; b--) {

		i64 v = -1;

		for(int i = 0; i < n; i++) {
			if((as[i] & (1LL << b)) != 0) {
				v = as[i];
				break;
			}
		}

		if(v == -1) {
			continue;
		}

		for(int i = 0; i < n; i++) {
			if((as[i] & (1LL << b)) != 0) {
				as[i] = as[i] ^ v;
			}
		}

		maxi = max(maxi ^ v, maxi);
	}

	i64 ans = maxi * 2 + sum;
	printf("%lld\n", ans);

	return 0;
}

#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define int long long int
constexpr int INF = std::numeric_limits<int>::max();
constexpr int NINF = std::numeric_limits<int>::min();
using namespace std;

int g[10][10];
int f[10][10];

void solve() {
  int M;
  cin >> M;
  int ans = -1;
  int sum = 0;

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      int a = i+j;
      g[i][j] = a/10+a%10;
      f[i][j] = a >= 10 ? 2 : 1;
    }
  }

  int amari = 0;
  while (M--) {
    int c, d; cin >> d >> c;

    while (c > 1) {
      if (c%2 == 1) {
        c--;
        ans += f[amari][d];
        amari = g[amari][d];
      }

      c /= 2;
      ans += c * f[d][d];
      d = g[d][d];
    }

    ans += f[amari][d];
    amari = g[amari][d];
  }

  cout << ans << endl;
}

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


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

const int N = 4005;
const long long mod = 998244353;

long long C[N][N];
long long pow2[N];

void init(){
	C[0][0] = C[1][0] = C[1][1] = 1;
	for (int i = 2; i <= 4000; i++){
		C[i][0] = 1;
		for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
		C[i][i] = 1;
	}
	pow2[0] = 1;
	for (int i = 1; i <= 4000; i++) pow2[i] = pow2[i - 1] * 2 % mod;
}

int main(){
	init();
	int n, k, num1, num2, num3;
	long long ans;
	scanf("%d %d",&k,&n);
	for (int i = 2; i <= k * 2; i++){
		ans = 0;
		num1 = (min(i - 1, k) - max(i - k, 1) + 1) / 2;
		num2 = 1 - i & 1;
		num3 = k - num1 * 2 - num2;
		for (int p = 0; p <= num2; p++){
			for (int j = 0; j <= num1; j++){
				ans += C[num1][j] * pow2[j] % mod * C[n - 1 - p + num3][j + num3 - 1] % mod;
				ans %= mod;
			}
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,i,j,cnt,xlh,ans,op,a[302][302],b[302],v[302];
int main(){
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;i++)v[i]=1;
	ans=n;
	for(i=1;i<=n;i++)
	 for(j=1;j<=m;j++)scanf("%d",&a[i][j]);
	for(cnt=1;cnt<m;cnt++){
		for(i=1;i<=m;i++)b[i]=0;
		for(i=1;i<=n;i++)
		 for(j=1;j<=m;j++)if(v[a[i][j]]){
		 	b[a[i][j]]++;break;
		 }
		xlh=0;
		for(i=1;i<=m;i++){
			if(b[i]>xlh)op=i;
		 xlh=max(xlh,b[i]);
	 }
		ans=min(ans,xlh);
		v[op]=0;
	}
	printf("%d",ans);
}

#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<limits>
#include<ctime>
#include<stack>
#include<bits/stdc++.h>
#include<string>
#include<stdlib.h>
#include<stdio.h>

typedef long long ll;
using namespace std;
const ll x=1000000007;
int mod(ll a){
	return a%x;
}

int main(){
	map<string,int> a;
	int n,m,maks=0;
	cin>>n;
	string s;
	for(int i=0;i<n;i++){
		cin>>s;
		a[s]++;
		cout<<endl;
	}
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>s;
		a[s]--;
		cout<<endl;
	}
	for(map<string,int>:: iterator i=a.begin();i!=a.end();i++){
		maks=max(maks,i->second);
	}
	cout<<maks;
}


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

int main() {
    string sa,sb,sc;
    cin >> sa >> sb >> sc;
    queue<char> qa,qb,qc;
    for(int i = 0; i < sa.size(); i++) qa.push(sa[i]);
    for(int i = 0; i < sb.size(); i++) qb.push(sb[i]);
    for(int i = 0; i < sc.size(); i++) qc.push(sc[i]);
    qa.push('w'),qb.push('w'),qc.push('w');
    char x=qa.front();
    qa.pop();
    while(!qa.empty() && !qb.empty() && !qc.empty()){
        if(x=='a'){
            x=qa.front();
            qa.pop();
        }
        if(x=='b'){
            x=qb.front();
            qb.pop();
        }
        if(x=='c'){
            x=qc.front();
            qc.pop();
        }
    }

    if(qa.empty()) cout << 'A' << endl;
    else if(qb.empty()) cout << 'B' << endl;
    else cout << 'C' << endl;

    return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include <iomanip>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
ll ans=0;llfor;///////////////////////////////////////////////////////////
int digit(int n){
   if(n < 10)  return n;
   return digit(n/10) + n%10;
}
int main(){
  int A,B,C,K;
  cin>>A>>B>>C>>K;

  cout<<max({A,B,C})*pow(2,K)+A+B+C-max({A,B,C});
return 0;}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
ll factorial(int i) {       //階乗
    if (i == 0) return 1;
    return (factorial(i - 1)) * i;
}

int main() {
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> ac(n);
    vector<int> sum(n + 1, 0);
    rep(i, n) {
        if(i != n) {
            if(s[i] == 'A' && s[i + 1] == 'C') {
                ac[i]++;
                i++;
            }
        }
    }
    rep(i, n) {
        sum[i + 1] += sum[i] + ac[i];
    }
    /*cout << endl;
    rep(i, n) cout << ac[i] << ' ';
    cout << endl;
    rep(i, n + 1) cout << sum[i] << ' ';
    cout << endl;
    //確認済*/
    vector<vector<int>> l(q, vector<int>(2));
    rep(i, q) {
        rep(j, 2) cin >> l[i][j];
    }
    rep(i, q) {
        cout << sum[l[i][1] - 1] - sum[l[i][0] - 1] << endl;
    }

}

/**
*    author:  yuya1234
*    created: 17.07.2020 14:32:58
**/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
long double eps = 1.0E-14;

#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 FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define FORV(i,v) for(auto i=v.begin(); i!=v.end();i++)

#define SORT(s) sort((s).begin(),(s).end())
#define SORTD(s) sort((s).rbegin(),(s).rend())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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; }
template<class T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T>T lcm(T a,T b){return gcd(a,b)*(a/gcd(a,b))*(b/gcd(a,b));}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  //std::cout << std::fixed << std::setprecision(15);

  int n,q;
  cin>>n>>q;

  string s;
  cin>>s;
  int l[q],r[q];
  REP(i,q)cin>>l[i]>>r[i];

  int ss[SZ(s)];
  MEMSET(ss,0);
  int sum=0;
  FOR(i,1,SZ(s)-1)
  {
    if(s[i-1]=='A' && s[i]=='C')sum++;
    ss[i]=sum;
  }

  //REP(i,SZ(s))cout<<ss[i]<<endl;

  REP(i,q)
  {
    cout<<ss[r[i]-1]-ss[l[i]-1]<<endl;
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int N = 1e5+7;
int a[3][N];
int col[N * 3];

struct Fen {
int f[N];
void clear() {
    for (int i = 0; i < N; ++i) f[i] = 0;
}
void add(int i, int x) {
    for (; i < N; i |= i + 1)
        f[i] += x;
}
int get(int i) {
    int ans = 0;
    for (; i >= 0; i &= i + 1, --i) ans += f[i];
    return ans;
}
int get(int l, int r) {
    //check r < l!
    return get(r) - get(l - 1);
}
} f;

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(20);
    #endif

    auto get = [&] (vector <int> a) {
        reverse(all(a));

        f.clear();

        int ans = 0;
        for (auto e : a) {
            ans += f.get(e);
            f.add(e, 1);
        }
        return ans & 1;
    };

    int n;
    cin >> n;
    for (int i = 0; i < 3; ++i)
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
            col[a[i][j]] = j;
        }

    int x = 1;
    vector <int> inv(2);
    vector <int> p[2];
    for (int i = 0; i < n; ++i) {

        int c = col[x];

        vector <int> t;
        for (int j = 0; j < 3; ++j)
            t.app(a[j][c]);

        auto t1 = t;
        sort(all(t1));
        vector <int> w = {x, x + 1, x + 2};
        if (t1 != w) {
            cout << "No" << endl;
            exit(0);
        }
        if (t[1] != x + 1) {
            cout << "No" << endl;
            exit(0);
        }

        p[i&1].app(c);

        int was = (x - 1) / 3;
        if ((was & 1) != (c & 1)) {
            cout << "No" << endl;
            exit(0);
        }

        inv[i&1] ^= t[0] != x;
        x += 3;
    }

    int p_inv = get(p[0]) + get(p[1]);

    vector <int> ar(n), did(n);
    for (int i = 0; i < n; ++i) {
        ar.app(a[0][i] > a[2][i]);
    }
    for (int i = 0; i + 2 < n; ++i) {
        did[i] = ar[i];
        for (int j = 1; j <= 2 && j <= i; ++j) {
            did[i] ^= did[i - j];
        }
    }
    for (int i = n - 2; i < n; ++i) {
        int have = 0;
        for (int j = 1; j <= 2 && j <= i; ++j) {
            have ^= did[i - j];
        }
        if (have != ar[i]) {
            cout << "No" << endl;
            exit(0);
        }
    }

    if (get(p[0]) == inv[1] && get(p[1]) == inv[0]) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[233],odd[3],top;
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=k;i++)
	{
		cin>>a[i];
		if(a[i]%2)
		{
			odd[++top]=i;
		}
	}
	if(top>2)return puts("Impossible"),0;
	else if(top==2)
	{
		swap(a[1],a[odd[1]]);
		swap(a[k],a[odd[2]]);
	}
	else if(top==1)
	{
		swap(a[1],a[odd[1]]);
	}
	for(int i=1;i<=k;i++)cout<<a[i]<<' ';cout<<endl;
	cout<<k-(a[1]==1)+(k==1)<<endl;
	if(a[1]!=1)cout<<a[1]-1<<' ';
	for(int i=2;i<k;i++)cout<<a[i]<<' ';
	if(k!=1)cout<<a[k]+1<<endl;
	else cout<<1<<endl;
	return 0;
}


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

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

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

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

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

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while(cin >>n&&n){
    vector<vector<int>> v(n+1,vector<int>(n+1,0));
    rep(i,n){
      rep(j,n){
        cin >> v[j][i];
      }
    }
    rep(i,n){
      int sum = 0;
      rep(j,n){
        scout(5)<< v[j][i] ;
        sum += v[j][i];
      }
      v[n][i] = sum;
      scout(5)<< sum << endl;
    }
    rep(i,n+1){
      int sum = 0;
      rep(j,n){
        sum += v[i][j];
      }
      scout(5)<< sum ;
    }
    cout << endl;
  }
  return 0;
}

#include <bits/stdc++.h>

#define loop(v, f, l) for(int v = (f), v##_ = (l); v < v##_; ++v)

using namespace std;

typedef long long int lint;
static inline int in(){ int x; scanf("%d", &x); return (x); }
static inline lint inl(){ lint x; scanf("%lld", &x); return (x); }

int matrix[11][11];

int main()
{
	int n;

	while( (n = in()) != 0 )
	{
		loop(i, 0, 11)
		{
			loop(j, 0, 11)
			{
				matrix[i][j] = 0;
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][j] = in();
			}
		}

		loop(i, 0, n)
		{
			loop(j, 0, n)
			{
				matrix[i][n] += matrix[i][j];
				matrix[n][i] += matrix[j][i];
			}
			matrix[n][n] += matrix[i][n];
		}

		loop(i, 0, n + 1)
		{
			loop(j, 0, n + 1)
			{
				printf("%5d", matrix[i][j]);
			}
			printf("\n");
		}

	}

	return (0);
}


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

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

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl

int sign_x[4] = {1, -1, +1, -1};
int sign_y[4] = {1, -1, -1, +1};

ll board[8][8];

ll queen[8]; // i行目のどこに置いたか

bool in_range(ll x, ll y){
    if(0<=x && x<8 && 0<=y && y<8){
        return true;
    }else{
        return false;
    }
}

// 盤面に置く or 取り除く
// 置く場合 add = 1
// 除く場合 add = -1
void put(ll x, ll y, ll add){
    // cout << "put " << x << " " << y << endl;
    if(add>0){
        queen[y] = x;
    }else{
        queen[y] = -1;
    }

    // row
    FOR(i, 0, 8){
        board[y][i] += add;
    }
    // column
    FOR(i, 0, 8){
        board[i][x] += add;
    }
    // 足しすぎを引く
    board[y][x] -= add;
    // diagonal
    FOR(i, 1, 8){ // 幅
        FOR(j, 0, 4){
            ll nx = x + i * sign_x[j];
            ll ny = y + i * sign_y[j];
            if(in_range(nx, ny)){
                board[ny][nx] += add;
            }
        }
    }
}

// 解答用：クイーンの位置をQで表示する
void printQueen(){
    FOR(y, 0, 8){
        FOR(x, 0, 8){
            if(queen[y]==x){
                cout << 'Q';
            }else{
                cout << '.';
            }
        }
        cout << endl;
    }
}

// ボードの状態を数字で表示する
void debugBoard(){
    FOR(y, 0, 8){
        FOR(x, 0, 8){
            cout << setw(2) << board[y][x] << ' ';
        }
        cout << endl;
    }
}

void setQueen(ll y){
    // pn(y);
    if(y==8){
        printQueen();
        exit(0);
    }

    // すでに置かれている
    if(queen[y]!=-1){
        setQueen(y+1);
    }

    // どこに置こうかな
    FOR(x, 0, 8){
        // 置いてみる
        if(board[y][x]==0){
            put(x, y, 1);
            setQueen(y+1);
            // キャンセル
            put(x, y, -1);
        }
    }
}

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

    // input
    ll N;
    cin >> N;

    FOR(i, 0, 8){
        queen[i] = -1;
    }

    while(N--){
        ll y, x;
        cin >> y >> x;
        put(x, y, 1);
    }

    setQueen(0);
    return 0;
}

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

using namespace std;

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

	int x, y;

	vector<int> y_x(8, -1);

	for (int i = 0; i < N; i++)
	{
		cin >> y >> x;

		y_x[y] = x;
	}

	vector<int> perm(8);

	for (int i = 0; i < 8; i++) { perm[i] = i; }

	bool ok = true;

	do
	{
		ok = true;

		for (int y = 0; y < 8; y++)
		{
			if (y_x[y] != -1 && perm[y] != y_x[y])
			{
				ok = false;
			}
		}

		if (ok == true)
		{
			for (int i = 0; i < 8; i++)
			{
				for (int j = 0; j < 8; j++)
				{
					if (i != j)
					{
						if (i - perm[i] == j - perm[j] || i + perm[i] == j + perm[j])
						{
							ok = false;
						}
					}
				}
			}
		}

		if (ok == true)
		{
			for (int i = 0; i < 8; i++)
			{
				for (int j = 0; j < 8; j++)
				{
					if (perm[i] == j)
					{
						cout << 'Q';
					}
					else
					{
						cout << '.';
					}
				}

				cout << endl;
			}
		}
	} while (next_permutation(perm.begin(), perm.end()));

	return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <climits>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int> v;
    for(int i = 0;i < n;i++)
    {
        int t;
        cin >> t;
        v.push_back(t);
    }
    bool f = true;
    int c = 0;
    while(f)
    {
        f = false;
        for(int i = 1;i < v.size();i++)
        {
            if(v.at(i) < v.at(i - 1))
            {
                int t = v.at(i - 1);
                v.at(i -1) = v.at(i);
                v.at(i) = t;
                f = true;
                c++;
            }
        }
    }
    for(int i = 0;i < v.size();i++)
    {
        cout << v.at(i);
        if(i + 1 != v.size())
        {
            cout << " ";
        }
        else
        {
            cout << endl;
        }
    }
    cout << c << endl;
}

#include <algorithm>
#include <cstdio>
#include <string>
#define MAXN 1000001
#define a first
#define b second

char buf[MAXN];
std::pair<int, int> s[MAXN];

int main() {
  using namespace std;

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", buf);
    s[i].a = s[i].b = 0;
    for (int j = 0; buf[j]; ++j) {
      if (buf[j] == '(')
        ++s[i].a;
      else {
        if (s[i].a)
          --s[i].a;
        else
          ++s[i].b;
      }
    }
  }

  auto cmp = [](const pair<int, int> &l, const pair<int, int> &r) {
    if (l.a <= l.b && r.a > r.b)
      return true;
    else if (l.a > l.b && r.a <= r.b)
      return false;
    else if (l.a > l.b && r.a > r.b)
      return l.b > r.b;
    else
      return l.a < r.a;
  };

  std::sort(s, s + n, cmp);

  int t = 0;
  for (int i = 0; i < n; i++) {
    if (t < s[i].a) {
      printf("No\n");
      return 0;
    }
    t += s[i].b - s[i].a;
  }
  if (t)
    printf("No\n");
  else
    printf("Yes\n");

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long a,b,i,A,B;int x;
map<int,int>m;
int main()
{
    cin>>A>>B;
    a=A;b=B;
    for(i=2;i*i<=A;i++)
    {while(a%i==0){a/=i;m[i]=1;}}
    if(a!=1)m[a]=1;
    for(i=2;i*i<=B;i++)
    {while(b%i==0){b/=i;if(m[i]==1){x++;m[i]=2;}}}
    if(b!=1){if(m[b]==1)x++;}
    cout<<x+1;
}

#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &u, const T z) { if (u < z) {u = z; return true;} else return false; }
template <typename T> bool chmin(T &u, const T z) { if (u > z) {u = z; return true;} else return false; }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
//typedef pair<ll, ll> P;
const ll INF=1LL<<60;
ll gcd(ll x,ll y){return y? gcd(y,x%y) :x;}
//写し
vector<pair<ll,int>>factorize(ll n){
    vector<pair<ll,int>>res;
    for(ll i=2;i*i<=n;++i){
        if(n%i)continue;
        res.emplace_back(i,0);
        while(n%i==0){
            n/=i;
            res.back().second++;
        }
    }
    if(n!=1)res.emplace_back(n,1);
    return res;
}
int main(){
    ll a,b;
    cin>>a>>b;
    ll g=gcd(a,b);
    auto f=factorize(g);
    int ans=f.size()+1;
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
//#include <atcoder/all>
#define ll long long int
#define MOD 1000000007
#define P pair<ll,ll>
#define INF 1000000000000000000

//using namespace atcoder;
using namespace std;

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

  vector<ll> a(5);
  for (int i = 0; i < 5; i++){
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  cout << (n + a[0] - 1)/a[0] + 4 << endl;

  return 0;
}

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

int main(){
    int n;
    cin >> n;
    map<int,int> m;
    rep(i,n){
        int a;
        cin >> a;
        m[a]++;
    }
    for(auto p : m){
        if(p.second > 1){
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    unsigned long long a[5000]={0},c=0,p,o,u;
    rep(i,n){
      cin>>p>>o>>u;
      a[p]+=o*u;
      if(a[p]>=1000000){cout<<p<<endl;c++;}
    }
    if(!c)cout<<"NA"<<endl;
  }
}

#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;
long long sum(long long n)
{return n*(n+1)/2;}
 int main(){
 int n;
 cin>>n;
 long long ans;
 ans=sum(n)-sum(n/3)*3-sum(n/5)*5+sum(n/15)*15;
 cout<<ans;
return 0;
 }

#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define put(x) cout << x << endl;
using namespace std;
int main()
{
    ll n;
    cin >> n;
    ll sum = 0;
    rep(j, n)
    {
        ll i = j + 1;
        if (i % 15 == 0)
        {
        }
        else if (i % 3 == 0)
        {
        }
        else if (i % 5 == 0)
        {
        }
        else
        {
            sum += i;
        }
    }
    put(sum)
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
int p = 998244353;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(long long i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a)*(a)
#define rev(s) reverse(s.begin(),s.end())
#define end_program(s) cout << s <<endl;return 0
int kai_size = 500001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
vel dist;
int RE() {
	vel v(3, 2);
	return v.at(4);
}
int ru(int a, int r) {
	if (r == 0) { return 1; }
	int ans = ru(a, r / 2);
	ans *= ans; ans %= p;
	if (r % 2 == 1) { ans *= a; }
	return ans % p;
}
int inv(int a) {
	return ru(a, p - 2);
}
void make_kai() {
	rep(i, kai_size-1) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
	rep(i, kai_size) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
	if (r < 0 || n < r) { return 0; }
	int ans = kai[n] * ink[r];
	ans %= p; ans *= ink[n - r]; ans %= p;
	return ans;
}
vel dis(int mid1, vvel &way) {
	int n = way.size();
	vel dist(n, -1); dist[mid1] = 0;
	queue<int> q;
	q.push(mid1);
	while (!q.empty()) {
		int st = q.front(); q.pop();
		rep(i, way[st].size()) {
			int to = way[st][i];
			if (dist[to] == -1) {
				dist[to] = dist[st] + 1;
				q.push(to);
			}
		}
	}
	return dist;
}
pin most_far(int now, int n, vvel &way) {
	vel dist1 = dis(now, way);
	pin ans = mkp(-1, 0);
	rep(i, n) {
		if (dist1[i] > ans.first) { ans = mkp(dist1[i], i); }
	}
	return ans;
}

int per(int a, int b) {
	int ans = a % b;
	if (ans < 0) { ans += b; }
	return ans;
}
vel uni(vel &v) {
	sor(v);
	vel ans(1, v[0]);
	for (int i = 1; i < v.size(); i++) {
		if (v[i] != v[i-1]) { ans.push_back(v[i]); }
	}
	v = ans;
	return v;
}
int s_gcd(int a, int b) {
	if (b == 0) { return a; }
	return s_gcd(b, a%b);
}
int gcd(int a, int b) {
	if (a < b) { swap(a, b); }
	return s_gcd(a, b);
}
bool is_prime(int i) {
	for (int j = 2; j*j <= i; j++) {
		if (i%j == 0) { return false; }
	}
	return true;
}
int get(int i, int j, vvel &ans) {
	int n = ans.size();
	if (i < 0 || n <= i || j < 0 || n <= j) { return 1; }
	return ans[i][j];
}
int lcm(int a, int b) {
	return a * b / gcd(a, b);
}
int solve(int a, int b) {
	if (a < b) { swap(a, b); }
	if (b == 1) { return 1; }
	return a % b;
}
signed main() {
	int n; cin >> n;
	vel prime(0);
	int mod = 510;
	for (int i = 1000; i < 10000; i++) {
		if (is_prime(i)) { prime.push_back(i); }
	}
	vvel ans(n, vel(n,1));
	rep(i, n) {
		rep(j, n) {
			if ((i + j) % 2 == 0) {
				int x = (i + j) / 2;
				int y = per((i - j) / 2, mod);
				ans[i][j] = prime[x] * prime[y];
			}
		}
	}
	rep(i, n) {
		rep(j, n) {
			if ((i + j) % 2 == 1) {
				j--;
				int x = (i + j) / 2;
				int y = per((i - j) / 2, mod);
				ans[i][j+1] = prime[x] * prime[y];
				j += 2;
				x = (i + j) / 2;
				y = per((i - j) / 2, mod);
				ans[i][j-1] *= prime[x] * prime[y];
				j--;
				ans[i][j]++;
			}
			cout << ans[i][j];
			if (j == n - 1) { cout << endl; }
			else { cout << " "; }
		}
	}
	return 0;
}

#include<iostream>
#include<cstdio>
using namespace std;
int n,sz;
int a[10101],d[10101];
long long an[1010][1010];
void init(int N)
{
	for(int i=2;i<=N;i++)
	{
		if(!d[i])a[++sz]=i;
		for(int j=1;j<=sz&&a[j]*i<=N;j++)
		{
			d[i*a[j]]=1;
			if(i%a[j]==0) break;
		}
	}
}
long long gcd(long long x,long long y){ return y?gcd(y,x%y):x;}
long long lcm(long long x,long long y)
{
	if(!x||!y) return x+y;
	return x/gcd(x,y)*y;
}
int main()
{
	scanf("%d",&n);
	init(10000);
	if(n==2)
	{
		an[1][1]=4;an[1][2]=7;an[2][1]=23;an[2][2]=10;
	}
	else
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			if((i+j)&1)
				an[i][j]=1LL*a[(i+j)/2]*a[(i-j+n)/2+1+n];
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			if(((i+j)&1)^1)
				an[i][j]=lcm(lcm(an[i-1][j],an[i+1][j]),lcm(an[i][j-1],an[i][j+1]))+1;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		printf("%lld ",an[i][j]);
		puts("");
	}
	return 0;
}


#include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
	int f=1;char k=getchar();x=0;
	for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
	for(;k>='0'&&k<='9';k=getchar()) x=(x<<3)+(x<<1)+k-'0';
	x*=f;
}
template<class T>il _print(T x){
	if(x/10) _print(x/10);
	putchar(x%10+'0');
}
template<class T>il print(T x){
	if(x<0) putchar('-'),x=-x;
	_print(x);
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int m,int mod){
	int res=1,bas=x;
	while(m){
		if(m&1) res=(1ll*res*bas)%mod;
		bas=(1ll*bas*bas)%mod,m>>=1;
	}
	return res;
}
const int MAXN = 2e3+5,mod = 998244353;
int n,k,C[MAXN<<1][MAXN<<1],bit[MAXN<<1];
it add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
it mul(int x,int y){return 1ll*x*y%mod;}
il inc(int &x,int y){x=add(x,y);}
il Init(){
	C[0][0]=1,bit[0]=1;
	for(ri i=1;i<=4000;++i){
		C[i][0]=1;
		for(ri j=1;j<=i;++j) C[i][j]=add(C[i-1][j],C[i-1][j-1]);
		bit[i]=mul(bit[i-1],2);
	}
}
it Solve(int x,int y,int z){
	int res=0;
	for(ri i=0;i<=z&&i<=x;++i)
		if(y+i-1>=0) inc(res,mul(mul(C[x][i],C[y+z-1][i+y-1]),bit[i]));
	return res;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	read(k),read(n),Init();
	for(ri i=2;i<=2*k;++i){
		int cnt=0;
		for(ri j=1;j<=k;++j) cnt+=(i-j>=1&&i-j<=k);
		if(i&1) print(Solve(cnt>>1,k-cnt,n)),puts("");
		else print(add(Solve(cnt>>1,k-cnt,n-1),Solve(cnt>>1,k-cnt,n))),puts("");
	}
	return 0;
}

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

ll ppow(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1)res=(res*a)%MOD;
		a=(a*a)%MOD;
		b>>=1;
	}
	return res;
}
ll C[5000][5000];

int main(){
	for(int i=0;i<5000;i++){
		for(int j=0;j<=i;j++){
			if(j==0||j==i)C[i][j]=1;
			else C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
		}
	}
	int k,n;scanf("%d%d",&k,&n);
	for(int i=2;i<=2*k;i++){
		int cnt1=0,cnt2=0;
		for(int j=1;j<=k;j++){
			if(j<i&&j<i-j&&i-j<=k)cnt1++;
			if(j>=i||i-j>k)cnt2++;
		}
		ll ans=0;
		for(int j=0;j<=cnt1;j++){
			if(j+cnt2==0)continue;
			(ans+=C[n-j+(j+cnt2-1)][j+cnt2-1]*ppow(2,j)%MOD*C[cnt1][j]%MOD)%=MOD;
			if(i%2==0)(ans+=C[n-j+(j+cnt2-1)-1][j+cnt2-1]*ppow(2,j)%MOD*C[cnt1][j]%MOD)%=MOD;
		}
		printf("%lld\n",ans);
	}
}


#include<stdio.h>
const int MAX_V=10000;
int prime[MAX_V+1];
int main(){
  int i,k,n,p,q,v;
  for(i=2;i<=MAX_V;i++){
    prime[i]=1;
  }
  for(i=2;i*i<=MAX_V;i++){
    if(prime[i]){
      for(k=2*i;k<=MAX_V;k+=i){
        prime[k]=0;
      }
    }
  }
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    for(v=2;v<=n-2;v++){
      int isprime=prime[v]*prime[v+2];
      if(isprime)p=v,q=v+2;
    }
    printf("%d %d\n",p,q);
  }
  return 0;
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int prime[2020],np;
bool vis[10101];
void findprime()
{
	memset(vis,0,sizeof(vis));
	np=0;
	for(int i=2;i<=10000;i++)
	{
		if(!vis[i])
			prime[++np]=i;
		for(int j=1;j<=np&&i*prime[j]<=10000;j++)
		{
			vis[i*prime[j]]=true;
			if(i%prime[j]==0)
				break;
		}
	}
}
long long ans[505][505];
long long gcd(long long x,long long y)
{
	if(y==0)
		return x;
	return gcd(y,x%y);
}
long long lcm(long long x,long long y)
{
	return x/gcd(x,y)*y;
}
int hx[4]={-1,1,0,0};
int hy[4]={0,0,-1,1};
int main()
{
	int n;
	findprime();
	scanf("%d",&n);
	if(n==2)
	{
		printf("4 7\n23 10");
		return 0;
	}
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=n+1;j++)
			ans[i][j]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if((i+j)%2==0)
				ans[i][j]=prime[(i+j)/2]*prime[(n-j+1+i)/2+n];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if((i+j)%2==1)
			{
				for(int k=0;k<4;k++)
					ans[i][j]=lcm(ans[i][j],ans[i+hx[k]][j+hy[k]]);
				ans[i][j]++;
			}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			printf("%lld ",ans[i][j]);
		printf("\n");
	}
	return 0;
}


#include<bits/stdc++.h>
const int maxn = 1001;
int vis[maxn][maxn];
int n,d1,d2;
void set(int d){
	int t = 0;
	for(;d % 4 == 0;d /= 4) ++ t;
	if(d % 4 == 1){
		for(int i = 0;i < n * 2;++i) for(int j = 0;j < n * 2;++j)
			vis[i][j] |= (i ^ j) >> t & 1;
	} else {
		for(int i = 0;i < n * 2;++i) for(int j = 0;j < n * 2;++j)
			vis[i][j] |= i >> t & 1;
	}
}
int main(){
	std::ios_base::sync_with_stdio(false),std::cin.tie(0);
	std::cin >> n >> d1 >> d2;
	set(d1); set(d2);
	for(int i = 0,cnt = 0;i < n * 2;++i) for(int j = 0;j < n * 2;++j)
		if(!vis[i][j]) {
			std::cout << i << ' ' << j << '\n';
			if(++cnt == n * n) return 0;
		}
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  if (v.size() == 0) {
    os << "(empty)";
    return os;
  }
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

bool
f(vector<int>&ln, int idx, int x, int y) {
  if (idx == 10) return true;
  int z = ln[idx];
  return (x < z && f(ln, idx + 1, z, y)) || (y < z && f(ln, idx + 1, x, z));
}

int main() {

  loop {
    vector<int>ls(10);
    char com;
    int v1, v2;

    int len = 0;
    rep(i, 10) {
      cin >> ls[i] >> com;
      len += ls[i];
    }
    if (!cin) break;

    cin >> v1 >> com >> v2;
    int V = v1 + v2;

    rep (i, 10) {
      ls[i] *= V;
    }

    int le1 = len * v1;
    for (int i=0; i<10; ++i) {
      le1 -= ls[i];
      if (le1 <= 0) {
        cout << -~i << endl;
        break;
      }
    }

  }

  return 0;
}