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

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi(a, b) for(int i=a; i<=b; i++)
#define fj(a, b) for(int j=a; j<=b; j++)
#define fo(a, b) for(int o=a; o<=b; o++)
#define fdi(a, b) for(int i=a; i>=b; i--)
#define fdj(a, b) for(int j=a; j>=b; j--)
#define fdo(a, b) for(int o=a; o>=b; o--)

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while(false) {}
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
typedef unsigned long long ull;

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

int const MAX = 15;

int n, m;
int e[MAX][MAX];

int d1[(1 << MAX)];
int d2[(1 << MAX)][MAX];

bool bit(int m, int i) {
	return (1 & (m >> i));
}

void init() {
	for (int mask = 0; mask < (1 << n); mask++) {
		fi(0, n - 1) {
			fj(i + 1, n - 1) {
				if (bit(mask, i) && bit(mask, j)) {
					d1[mask] += e[i][j];
				}
			}
		}
	}

	for (int mask = 0; mask < (1 << n); mask++) {
		fo(0, n - 1) {
			fi(0, n - 1) {
				if (bit(mask, i)) {
					d2[mask][o] += e[i][o];
				}
			}
		}
	}
}

int d[(1 << MAX)][MAX];
int const INF = 1e9 + 41;

int ans;
int sum;

int getbest(int comp, int path) {
	int res = 0;

	fi(0, n - 1) {
		if (bit(path, i)) {
			res = max(res, d2[comp][i]);
		}
	}

	res += d1[comp];
	return res;
}

void solve() {
	init();
	for (int mask = 0; mask < (1 << n); mask++) {
		fo(0, n - 1) {
			d[mask][o] = -INF;
		}
	}
	d[(1 << (n - 1)) - 1][n - 1] = 0;

	for (int mask = (1 << n) - 1; mask > 0; mask--) {
		fo(0, n - 1) {
			if (d[mask][o] < 0) continue;
			fi(0, n - 1) {
				if (e[i][o] && bit(mask, i)) {
					int nmask = (mask ^ (1 << i));
					d[nmask][i] = max(d[nmask][i], d[mask][o] + e[i][o]);
				}
			}
			int sm = mask;
			while (sm > 0) {
				int nmask = (mask ^ sm);
				int pathmask = ( ((1 << n) - 1) ^ mask);
				int bs = getbest(sm, pathmask);
				d[nmask][o] = max(d[nmask][o], d[mask][o] + bs);
				sm = ((sm - 1) & mask);
			}
		}
	}

	ans = sum - d[0][0];
	err("d = %d sum = %d\n", d[0][0], sum);
	printf("%d\n", ans);
}

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

  scanf("%d %d", &n, &m);
  fi(1, m) {
  	int x, y, w;
  	scanf("%d %d %d", &x, &y, &w);
  	x--;
  	y--;
  	e[x][y] = e[y][x] = w;
  	sum += w;
  }
	solve();

#ifdef LOCAL
	err("ELAPSED TIME: %.3Lf\n", (ld) clock() / CLOCKS_PER_SEC);
#endif

	return 0;
}

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

using namespace std;

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

const int N = 16;
const int M = (1 << N);
const int INF = 0x3f3f3f3f;

int dp[N][M], g[N][N], n, m, rv[M];

int f(int st, int msk){
    if(st == n - 1) return 0;
    if(dp[st][msk] != -1) return dp[st][msk];
    int sol = INF;
    for(int msk1 = (msk - 1) & msk; msk1 ; msk1 = (msk1 - 1) & msk){
        int msk2 = msk ^ msk1;
        if(msk1 & (1 << (n - 1))) continue;
        if(msk2 & (1 << st)) continue;
        int sum = 0;
        for(int t1 = msk1; t1 ; t1 -= t1 & (-t1)){
            for(int t2 = msk2 ; t2 ; t2 -= t2 & (-t2)){
                sum += g[rv[t1 & (-t1)]][rv[t2 & (-t2)]];
            }
        }
        for(int t2 = msk2 ; t2 ; t2 -= t2 & (-t2)){
            if(g[st][rv[t2 & (-t2)]]){
                sol = min(sol, f(rv[t2 & (-t2)], msk2) + sum - g[st][rv[t2 & (-t2)]]);
            }
        }

    }
    return dp[st][msk] = sol;
}

int main(){
    memset(dp, -1, sizeof(dp));
    for(int i = 0;i<N;i++) rv[(1 << i)] = i;
    memset(dp, -1, sizeof(dp));
    scanf("%d%d", &n, &m);
    for(int i = 0;i<m;i++){
        int x, y;scanf("%d%d", &x, &y);x--;y--;
        scanf("%d", &g[x][y]);
        g[y][x] = g[x][y];
    }
    printf("%d\n", f(0, (1 << n) - 1));
}


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

int main() {
  int N;
  int A[8], B[8];
  int NUM[8];
  double sum = 0;
  cin >> N;
  rep(i, N) {
    cin >> A[i] >> B[i];
  }
  rep(i, N) NUM[i] = i;
  do {
    rep(i, N-1) {
      sum += sqrt(pow(A[NUM[i+1]]-A[NUM[i]], 2)
                + pow(B[NUM[i+1]]-B[NUM[i]], 2));
    }
  } while(next_permutation(NUM, NUM+N));
  rep(i, N) sum /= (i+1);
  cout << fixed << setprecision(10) << sum << endl;
  return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define print(x) cout << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T> >
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  rep(i,n+1){
    string ans = s.substr(0,i)+t;
    if(ans.substr(0,n) == s){
      cout<<sz(ans)<<endl;
      return 0;
    }
  }
  return 0;
}


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

int main()
{
    int n; cin >> n;
    vector<int> v(n);

    for(auto &x : v) cin >> x;

    for(auto it = v.crbegin(); it != v.crend(); ++it)
        cout << *it << (((it+1)==v.crend()? "" : " "));
    cout << endl;

    return 0;
}

// This template belongs to Juan Carlo Vieri

/*   ____   __
    /  _/  |  |    __         __
 __|  |__  |  |   /_/        |  |
|__    __| |  |   __    ____ |  | __   ___   _  ___
   |  |    |  |  |  |  /  _/ |  |/ /  / _ \ | |/ _/
   |  |    |  |  |  | |  |_  |     \ |  __/ |   /
   |__|    |__|  |__|  \___\ |__|\__\ \___\ |__|
*/

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

typedef int_fast64_t ll;
typedef long double ld;
typedef int_fast64_t intt;
typedef int_fast32_t tmp_m;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define from(x, y, arr) (arr[y] - ((x > 0) ? (arr[x - 1]) : (0)))
#define min(a, b) (a > b ? b : a)
#define max(a, b) (a <  b ? b : a)
#define puts(x) cout << x << endl
#define deb(x) cout << #x << ' ' << x << endl
#define rep(i, cmp1, cmp2) for (tmp_m i = (tmp_m)cmp1; i < (tmp_m)cmp2; ++i)
#define repp(i, cmp1, cmp2) for (tmp_m i = (tmp_m)cmp1; i >= (tmp_m)cmp2; --i)
#define pb push_back
#define st stack
#define pa pair
#define vc vector
#define qu queue
#define pq priority_queue
#define fr first
#define sc second
#define mp map
const ld pi = acos(-1.0);
const intt mod = 1e9 + 7;

struct pr {
  intt x;
  intt y;
  intt dst;
  pr() {}
  pr(intt x, intt y, intt dst) : x(x), y(y), dst(dst) {}
  inline bool operator<(const pr& cmp2) const {
    if (dst != cmp2.dst) return dst < cmp2.dst;
    if (x != cmp2.x) return x < cmp2.x;
    return y < cmp2.y;
  }
  inline bool operator==(const pr& cmp2) const {
    return (cmp2.dst == dst) && (cmp2.x == x) && (cmp2.y == y);
  }
  inline bool operator>(const pr& cmp2) const {
    if (dst != cmp2.dst) return dst > cmp2.dst;
    if (x != cmp2.x) return x > cmp2.x;
    return y > cmp2.y;
  }
};

struct point {
  intt x;
  intt y;
  point() {}
  point(intt x, intt y) : x(x), y(y) {}
  inline bool operator<(const point& cmp2) const {
    if (cmp2.x != x) return cmp2.x < x;
    return cmp2.y < y;
  }
  inline bool operator==(const point& cmp2) const {
    return (cmp2.x == x) && (cmp2.y == y);
  }
  inline bool operator>(const point& cmp2) const {
    if (cmp2.x != x) return cmp2.x > x;
    return cmp2.y > y;
  }
};

namespace std {

template<typename T>
struct hash<vector<T> > {
  size_t operator()(const vector<T>& k) const {
    size_t ret = 0xf3ee477d;
    for (size_t i = 0; i != k.size(); ++ i)
      ret ^= hash(hash(k[i]) + i);
    return ret;
  }
};

template<>
struct hash<pr>{
  size_t operator()(const pr& cmp1) const {
    return (hash<intt>()(cmp1.x)) ^ (hash<intt>()(cmp1.y)) ^
           (hash<intt>()(cmp1.dst));
  }
};

template<>
struct hash<point>{
  size_t operator()(const point& cmp1) const {
    return (hash<intt>()(cmp1.x)) ^ (hash<intt>()(cmp1.y));
  }
};
}

inline bool spoint(const point& cmp1, const point& cmp2) {
  // swap(cmp1, cmp2);
  if (cmp1.x != cmp2.x) return cmp1.x < cmp2.x;
  return cmp1.y < cmp2.y;
}

inline bool spr(const pr& cmp1, const pr& cmp2) {
  // swap(cmp1, cmp2);
  if (cmp1.dst != cmp2.dst) return cmp1.dst < cmp2.dst;
  if (cmp1.x != cmp2.x) return cmp1.x < cmp2.x;
  return cmp1.y < cmp2.y;
}

void sub() {

}

void solve() {
  // intt t; cin >> t;
  // while (t--) sub();
  intt n;
  cin >> n;
  vc<intt> arr(n);
  intt nol = 0;
  intt neg = 0;
  rep(i, 0, n){
    cin >> arr[i];
    if(arr[i] == 0)nol = 1;
    if(arr[i] < 0)++neg;;
  }
  if(neg % 2 == 0 || nol){
    intt ans = 0;
    rep(i, 0, n){
      ans += abs(arr[i]);
    }
    cout << ans << endl;
    return;
  } else{
    rep(i, 0, n){
      arr[i] = abs(arr[i]);
    }
    sort(arr.begin(), arr.end());
    intt ans = -(arr[0]);
    rep( i, 1, n){
      ans += arr[i];
    }
    cout << ans << endl;
  }
}

void init() {
  // remove if interactive
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  srand(time(0));
  // memset(dp, -1, sizeof dp);
}

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


#include <bits/stdc++.h>

#if ((_WIN32 || __WIN32__) && __cplusplus < 201103L)
#define lld I64d
#else
#define lld lld
#endif

//{{{
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
// clang-format off
#define debug(args...) {vector<string> _v = split(#args, ','); err(_v.begin(), args); cerr<<endl;}
vector<string> split(const string& s, char c) {vector<string> v; stringstream ss(s); string x; while
(getline(ss, x, c)) v.push_back(x); return v;} void err(vector<string>::iterator) {}
template<typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args)
{cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; err(++it, args...);}
// clang-format on
//}}}

int a[22][22];
int n;

LL dp[1 << 16];
LL val[1 << 16];

LL dfs(int st)
{
    if (dp[st] != -1) return dp[st];

    if (__builtin_popcount(st) == 1) return dp[st] = 0;

    LL ret = val[st];
    for (int i = st; i; i = (i - 1) & st)
    {
        if (i == st) continue;
        int st1 = i;
        int st2 = st ^ i;

        LL v = dfs(st1) + dfs(st2);
        if (v > ret) ret = v;
    }
    return dp[st] = ret;
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
// freopen("out", "w", stdout);
#endif

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

        int lim = (1 << n) - 1;
        for (int st = 0; st <= lim; st++)
        {
            LL v = 0;
            for (int i = 0; i < n; i++)
            {
                if ((st & (1 << i)) == 0) continue;
                for (int j = i + 1; j < n; j++)
                {
                    if ((st & (1 << j)) == 0) continue;
                    v += a[i][j];
                }
            }
            val[st] = v;
        }

        clr(dp, -1);
        dp[0] = 0;
        LL ans = dfs(lim);
        cout << ans << endl;
    }

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

int main(){
	int n,dum;
	vector<int> p;

	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&dum);
		p.push_back(dum);
	}
	sort(p.begin(),p.end());
	if(n==2){
		for(int i=1;i<=p[0];i++){
			if(p[0]%i==0&&p[1]%i==0)
			printf("%d\n",i);
		}
	}
	else{
		for(int i=1;i<=p[0];i++){
			if(p[0]%i==0&&p[1]%i==0&&p[2]%i==0)
			printf("%d\n",i);
		}
	}
}


#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define emb emplace_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

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

template <typename T> inline T range(T l, T r) {
    return uniform_int_distribution<T>(l, r)(rng);
}

inline void setin(string s) {
    freopen(s.c_str(), "r", stdin);
}

inline void setout(string s) {
    freopen(s.c_str(), "w", stdout);
}

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

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

const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int N = 3e2 + 15;
int n, k;
string s;
int dp[2][N][N]; // dp[i][operation_applied][free_ones]
int a[N], p[N][N];

inline int add(int a, int b) {
    a += b;
    if(a >= mod)
        a -= mod;
    if(a < 0)
        a += mod;
    return a;
}

inline void add_t(int &a, int b) {
    a = add(a, b);
}

inline void construct() {
    int sz = 1;
    for(int i = 1; i <= n && s[i] == '1'; ++i)
        a[1] = i;
    for(int i = a[1] + 1; i <= n; ++i) {
        if(s[i] == '0') {
            if(i + 1 <= n && s[i + 1] == '0')
                a[++sz] = 0;
        }
        else {
            int j = i;
            while(j + 1 <= n && s[j + 1] == '1')
                ++j;
            a[++sz] = j - i + 1;
            i = j;
        }
    }
    n = sz;
}

inline void solve() {
    // dp[i][operation_applied][free_ones]
    dp[(n + 1) & 1][0][0] = 1;
    int sum = 0;
    for(int i = n; i >= 1; --i) {
        int bit = i & 1;
        memset(dp[bit], 0, sizeof(dp[bit]));
        for(int k = 0; k < N; ++k)
            for(int t = 0; t < N; ++t)
                p[k][t] = add(k && t ? p[k-1][t-1] : 0, dp[bit ^ 1][k][t]);

        for(int k = 0; k < N; ++k) {
            int val = 0;
            for(int t = N - 1; t >= 0; --t) {
                add_t(dp[bit][k][t], val);
                if(k - a[i] - 1 >= 0 && t - a[i] - 1 >= 0)
                    add_t(dp[bit][k][t], add(p[k][t], -p[k - a[i] - 1][t - a[i] - 1]));
                else
                    add_t(dp[bit][k][t], p[k][t]);
                add_t(val, dp[bit ^ 1][k][t]);
            }
        }
        sum += a[i];
    }
}

inline void output() {
    int ans = 0;
    for(int i = 0; i < min(N, k + 1); ++i)
        add_t(ans, dp[1][i][0]);
    cout << ans << endl;
}

main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // setin("input.txt");
    cin >> s >> k;
    n = s.size();
    s = '#' + s;
    construct();
    solve();
    output();
    return 0;
}

#include<iostream>
using namespace std;
int N;
string s;
long mod=1e9+7;
main()
{
	cin>>N>>s;
	long ans=1,cnt=0;
	for(int i=0;i<2*N;i++)
	{
		if(cnt%2==0)
		{
			if(s[i]=='B')
			{
				cnt++;
			}
			else
			{
				ans=ans*cnt--%mod;
			}
		}
		else
		{
			if(s[i]=='B')
			{
				ans=ans*cnt--%mod;
			}
			else
			{
				cnt++;
			}
		}
	}
	for(int i=1;i<=N;i++)ans=ans*i%mod;
	cout<<(cnt==0?ans:0)<<endl;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
  os << "{";
  for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i];
  return os << "}";
}

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

template <typename T, typename C>
ostream& operator<<(ostream& os, const multiset<T, C>& v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}
template <typename T1, typename T2, typename C>
ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin()) os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

int main() {
  int h, w;
  cin >> h >> w;
  // 右へ動く最小値を保管
  // ある点から始まった場合、
  // 複数の最短経路結果を持ちたい。
  //

  map<int, int> mp;
  multiset<int> st;
  rep(i, 0, w) {
    mp[i] = i;
    st.insert(0);
  }

  rep(i, 0, h) {
    int a, b;
    cin >> a >> b;
    --a;
    // [a, b]はいけない
    auto itr = mp.lower_bound(a);
    int most_right = -1;
    while (itr != mp.end() && itr->first <= b) {
      most_right = max(most_right, itr->second);
      int x = itr->first - itr->second;
      st.erase(st.find(x));
      itr = mp.erase(itr);
    }

    if (most_right != -1 && b < w) {
      st.insert(b - most_right);
      mp[b] = most_right;
    }
    if (st.size() > 0)
      cout << *st.begin() + i + 1 << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;

int main(){
    ll d, g;
    ll min = INF;
    cin >> d >> g;
    vector<ll> p(d);
    vector<ll> c(d);
    for(int i = 0; i < d; i++) {
        cin >> p.at(i) >> c.at(i);
    }
    // {0, 1, ..., n-1} の部分集合の全探索
    for (int bit = 0; bit < (1<<d); ++bit)
    {
        int max_index = -1;
        ll score = 0, problem = 0;
        /* bit で表される集合の処理を書く */
        bitset<10> tmp(bit);
        /* bit で表される集合の処理を書く */
        for(int i = 0; i < d; i++){
            if(tmp.test(i)) {
                score += p.at(i) * (i+1) * 100 + c.at(i);
                problem += p.at(i);
            }
            else max_index = i;
        }

        if(problem > min) continue;
        if(score == 0){
            while(score < g) {
                problem++;
                score += (max_index + 1) * 100;
                if(problem > p.at(max_index)) {
                    problem = INF;
                    break;
                }
            }

        }
        else if(g > score && g - score < p.at(max_index) * 100 * (max_index+1)) {
            problem += (g - score) / (max_index+1) / 100;
        }
        else if(g > score) continue;

        if(problem < min) min = problem;
    }
    cout << min << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
#include <complex>
#include <omp.h>
using namespace std;
#define eps 0.000001
#define LONG_INF 100000000000000
#define GOLD 1.61803398874989484820458
#define seg_size 65536*4
#define REP(i,n) for(long long i = 0;i < n;++i)
unsigned long xor128() {
	static unsigned long x = time(NULL), y = 362436069, z = 521288629, w = 88675123;
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
vector<vector<pair<int, int>>> vertexs;
vector<pair<int, int>> unioning[1000001];
pair<int, int> union_tree[10000001];
int union_find(int now) {
	if (union_tree[now].first == now) return now;
	return union_tree[now].first = union_find(union_tree[now].first);
}
int union_merge(int a, int b) {
	a = union_find(a);
	b = union_find(b);
	union_tree[b].second += union_tree[a].second;
	union_tree[a].first = union_tree[b].first;
	union_tree[a].second = 0;
	return 0;
}
vector<int> ans, visited;
int main() {
	int n, m;
	cin >> n >> m;
	int base = n - 1;
	REP(i, n) {
		vertexs.push_back(vector<pair<int, int>>());
	}
	set<int> listings;
	REP(i, m) {
		int a, b, c;
		cin >> a >> b >> c;
		a--;
		b--;
		listings.insert(c);
		unioning[c].push_back(make_pair(a, b));
	}
	REP(i, n) {
		union_tree[i] = make_pair(i, 1);
	}
	for (auto tea = listings.begin(); tea != listings.end(); ++tea) {
		int i = *tea;
		set<int> as;
		for (int q = 0; q < unioning[i].size(); ++q) {
			int a = unioning[i][q].first;
			int b = unioning[i][q].second;
			union_merge(a, b);
			as.insert(a);
			as.insert(b);
		}
		map<int, int> gogo;
		for (auto q = as.begin(); q != as.end(); ++q) {
			int topping = union_find(*q);
			if (gogo[topping] == 0) {
				gogo[topping] = vertexs.size();
				vertexs.push_back(vector<pair<int, int>>());
			}
			vertexs[*q].push_back(make_pair(gogo[topping], 1));
			vertexs[gogo[topping]].push_back(make_pair(*q, 0));
		}
		for (auto q = as.begin(); q != as.end(); ++q) {
			union_tree[*q] = make_pair(*q, 1);
		}
	}
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next;
	next.push(make_pair(0, 0));
	ans.push_back(0);
	visited.push_back(0);
	for (int i = 1; i < vertexs.size(); ++i) {
		ans.push_back(10000000);
		visited.push_back(0);
	}
	while (next.empty() == false) {
		int now = next.top().second;
		next.pop();
		if (visited[now] == true) continue;
		visited[now] = true;
		for (int i = 0; i < vertexs[now].size(); ++i) {
			if (ans[vertexs[now][i].first] > ans[now] + vertexs[now][i].second) {
				ans[vertexs[now][i].first] = ans[now] + vertexs[now][i].second;
				next.push(make_pair(ans[vertexs[now][i].first], vertexs[now][i].first));
			}
		}
	}
	if (ans[base] == 10000000) {
		cout << -1 << endl;
	}
	else {
		cout << ans[base] << endl;
	}
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>
#include <iomanip>
#define intt long long

int main() {
	intt N, M; std::cin >> N >> M;
	std::vector<intt> s(M);
	std::vector<intt> c(M);
	for (int i = 0; i < M; ++i) {
		std::cin >> s[i] >> c[i];
		if (s[i] == 1 && c[i] == 0 && N != 1) {
			std::cout << -1 << std::endl;
			return 0;
		}
	}
	for (int i = 0; i < M; ++i) {
		if (s[i] > N) {
			std::cout << -1 << std::endl;
			return 0;
		}
		for (int j = 0; j < M; ++j) {
			if (s[i] == s[j] && c[i] != c[j]) {
				std::cout << -1 << std::endl;
				return 0;
			}
		}
	}
	intt ans = 0;
	bool m1 = false;
	bool m2 = false;
	bool m3 = false;
	int p1 = 0;
	int p2 = 0;
	int p3 = 0;

	for (int i = 0; i < M; ++i) {
		if (s[i] == 1) {
			m3 = true;
			p3 = i;
		}
		if (s[i] == 2) {
			m2 = true;
			p2 = i;
		}
		if (s[i] == 3){
			m1 = true;
			p1 = i;
		}
	}
	if (N == 3) {
		if (m3 == true)
			ans += c[p3] * 100;
		if (m2 == true)
			ans += c[p2] * 10;
		if (m1 == true)
			ans += c[p1];
	}
	else if (N == 2) {
		if (m3 == true)
			ans += c[p3] * 10;
		if (m2 == true)
			ans += c[p2];
	}
	else if (N == 1) {
		if (m3 == true)
			ans += c[p3];
	}

	if (N == 3 && ans < 100)
		ans += 100;
	if (N == 2 && ans < 10)
		ans += 10;

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

	return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int step(int s, vector<int> &dp, int n) {
	int cnt = 0;
	if (s == n) return 1;
	for (int i = 1; i <= 3; i++) {
		if (s + i <= n) {
			if (dp[s + i] == -1)
				cnt += step(s + i, dp, n);
			else
				cnt += dp[s + i];
		}
	}
	dp[s] = cnt;
	return cnt;
}

int main(void) {
	int n;
	vector<int> dp;
	vector<int> res;
	dp.resize(31, -1);
	while (cin >> n, n) {
		int cnt = 0;
		for (int i = 1; i <= 3; i++)
			cnt += step(i, dp, n);
		dp.clear();
		dp.resize(31, -1);
		res.push_back(cnt / 10 / 365 + 1);
	}
	for (int i = 0; i < res.size(); i++)
		cout << res[i] << endl;
}

#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define res(i,n) for(int i=n;;i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define REV(i,n) for(int i=n-1;i>0;i--)
#define req(i,n,m) for(int i=n;i<m;i++)
#define REQ(i,n,m,l) for(int i=n;i<m;i+=l)
#define _GLIBCXX_DEBUG
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
    unsigned day;
    if (dow < dow1) dow += 7;
    day = dow - dow1;
    day += 7 * n - 6;
    return day;
}
signed gcd(long long x, long long y) {
    if (y == 0)return x;
    return gcd(y, x % y);
}
signed lcm(long long x, long long y) {
    return x / gcd(x, y) * y;
}
unsigned DayToWeekNumber(unsigned day)
{
    return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
    return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main() {
    int N; cin >> N;
    for (long double i = N; i > 0; i--) {
        if (sqrt(i) == int(sqrt(i))) {
            cout << int(i) << endl;
            return 0;
        }
    }
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (n);i++)
using ll = long long;
const ll MOD=1000000007;
const double pi=acos(-1);

int main()
{
  ll N,K; cin>>N>>K;
  vector<ll> A(N);
  rep(i,N)
  {
    cin>>A.at(i);
  }
  rep(i,min(K,(ll)100))
  {
    vector<ll> hoge(N,0);
    vector<ll> sirusi(N,0);
    rep(j,N)
    {
      sirusi.at(max((ll)0,j-A.at(j)))++;
      if(j+A.at(j)<N-1)
      {
        sirusi.at(j+A.at(j)+1)--;
      }
    }
    rep(j,N)
    {
      if(j==0) {hoge.at(0)=sirusi.at(0); continue;}
      hoge.at(j)=hoge.at(j-1)+sirusi.at(j);
    }
    A=hoge;
  }
  rep(i,N)
  {
    if(i!=0) {cout<<" ";}
    cout<<A.at(i);
  }
  cout<<endl;
  return 0;
}

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 510, M = 1010;

int a, b, n = 100;
char g[N][N];

int main()
{
	cin >> a >> b;
	a -- , b -- ;

	for (int i = 1; i <= n; i ++ )
		for (int j = 1; j <= n; j ++ )
			g[i][j] = i <= 50 ? '#' : '.';

	for (int i = 1; a && i <= n / 2 - 1; i += 2)
		for (int j = 1; a && j <= n; j += 2, a -- )
			g[i][j] = '.';

	for (int i = n / 2 + 2; b && i <= n; i += 2)
		for (int j = 1; b && j <= n; j += 2, b -- )
			g[i][j] = '#';

	puts("100 100");
	for (int i = 1; i <= n; i ++ ) puts(g[i] + 1);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define endl '\n'
using ll=long long;

const int INF=1e9+5;
const ll lim=(ll)3e16;
const int N=510000;
const int mod=1e9+7;
const ll oo=1e18+5;

int main(){
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
	int n;
	cin>>n;
	if(n<3) return cout<<-1,0;
	if(n==3){
		cout<<"aa.\n..a\n..a"<<endl;
		return 0;
	}
	vector<string> matrixx[8];
	// 4
	matrixx[4].push_back("aabc");
	matrixx[4].push_back("ddbc");
	matrixx[4].push_back("bcaa");
	matrixx[4].push_back("bcdd");
	// 5
	matrixx[5].push_back("aabba");
	matrixx[5].push_back("bcc.a");
	matrixx[5].push_back("b..cb ");
	matrixx[5].push_back("a..cb");
	matrixx[5].push_back("abbaa");
	// 6
	matrixx[6].push_back("aabc..");
	matrixx[6].push_back("ddbc..");
	matrixx[6].push_back("..aabc");
	matrixx[6].push_back("..ddbc");
	matrixx[6].push_back("bc..aa");
	matrixx[6].push_back("bc..dd");
	//7
	matrixx[7].push_back("aabbcc.");
	matrixx[7].push_back("dd.dd.a");
	matrixx[7].push_back("..d..da");
	matrixx[7].push_back("..d..db");
	matrixx[7].push_back("dd.dd.b");
	matrixx[7].push_back("..d..dc");
	matrixx[7].push_back("..d..dc");

	int rem=n%4;
	int k=n/4;
	if(rem) --k;
	int times=-1;
	//~ int loopp=0;
	for(int i=0;i<k*4;++i)
	{
		if(i%4==0) ++times;
		for(int j=0;j<times*4;++j) cout<<".";
		cout<<matrixx[4][i%4];
		for(int j=times*4+4;j<n;++j) cout<<".";
		cout<<endl;
	}
	++times;
	if(rem){
		for(int i=0;i<4+rem;++i){
			for(int j=0;j<times*4;++j) cout<<".";
			cout<<matrixx[rem+4][i%(rem+4)];
			cout<<endl;
		}
	}
	return 0;
}

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
	char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0;
	in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
	in ll read()
	{
		ll x=0,y=1;while(nc=gc(),(nc<48||nc>57)&&nc!=-1)if(nc==45)y=-1;Bi=1;
		x=nc-48;while(nc=gc(),47<nc&&nc<58)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*y;
	}
	in db gf() {int a=read(),b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi):a);}
	in int gs(char *s) {char c,*t=s;while(c=gc(),c<32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
	in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
	in void flush() {if(C>1<<22) ot();}
	template <typename T>
	in void write(T x,char t)
	{
		int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
		if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
	}
	in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
}
using namespace fast_io;
const int N=1e3+5;
int n;
char ans[1005][1005];
string s3[3]={
"aab",
"b.b",
"baa",
};
string s4[4]={
"bacc",
"babb",
"ccda",
"aada",
};
string s5[5]={
"accbb",
"ad..a",
"bd..a",
"b.ddc",
"aabbc",
};
string s6[6]={
"oorrzz",
".a.b.c",
".a.b.c",
"d.e.f.",
"d.e.f.",
"oorrzz"
};
string s7[7]={
"..bbcca",
"ccdd..a",
".a..d.b",
".a..d.b",
"b....dc",
"b....dc",
"ccbbaa.",
};
int main()
{
	n=read();if(n<3) return puts("-1"),0;
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ans[i][j]=0;
	if(n==3) {for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) sr[++C]=s3[i-1][j-1];sr[++C]='\n';}return ot(),0;}
	if(n==4) {for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) sr[++C]=s4[i-1][j-1];sr[++C]='\n';}return ot(),0;}
	if(n==5) {for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) sr[++C]=s5[i-1][j-1];sr[++C]='\n';}return ot(),0;}
	if(n==6) {for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) sr[++C]=s6[i-1][j-1];sr[++C]='\n';}return ot(),0;}
	if(n==7) {for(int i=1;i<=n;i++){for(int j=1;j<=n;j++) sr[++C]=s7[i-1][j-1];sr[++C]='\n';}return ot(),0;}
	if(n%4==0) for(int i=1;i<=n;i+=4) for(int j=i;j<=i+3;j++) for(int k=i;k<=i+3;k++) ans[j][k]=s4[(j-1)%4][(k-1)%4];
	if(n%4==1)
	{
		for(int i=1;i<=n-5;i+=4) for(int j=i;j<=i+3;j++) for(int k=i;k<=i+3;k++) ans[j][k]=s4[(j-1)%4][(k-1)%4];
		for(int i=n-4;i<=n;i++) for(int j=n-4;j<=n;j++) ans[i][j]=s5[i-n+4][j-n+4];
	}
	if(n%4==2)
	{
		for(int i=1;i<=n-6;i+=4) for(int j=i;j<=i+3;j++) for(int k=i;k<=i+3;k++) ans[j][k]=s4[(j-1)%4][(k-1)%4];
		for(int i=n-5;i<=n;i++) for(int j=n-5;j<=n;j++) ans[i][j]=s6[i-n+5][j-n+5];
	}
	if(n%4==3)
	{
		for(int i=1;i<=n-7;i+=4) for(int j=i;j<=i+3;j++) for(int k=i;k<=i+3;k++) ans[j][k]=s4[(j-1)%4][(k-1)%4];
		for(int i=n-6;i<=n;i++) for(int j=n-6;j<=n;j++) ans[i][j]=s7[i-n+6][j-n+6];
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++) sr[++C]=(ans[i][j]?ans[i][j]:'.');
		sr[++C]='\n';ot();
	}
	return 0;
}
//Author: disangan233
//In my dream's scene,I can see the everything that in Cyaegha.
/*
----------
aab
b.b
baa
----------
bacc
babb
ccda
aada
----------
accbb
ad..a
bd..a
b.ddc
aabbc
----------
aabb..
b..aa.
ba....
.a..aa
..a..b
..a..b
----------
..bbcca
ccdd..a
.a..d.b
.a..d.b
b....dc
b....dc
ccbbaa.
----------

*/


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

template<typename T>
auto print(std::vector<T> array,bool reverse = false)
 -> void{
 	if(reverse){
		auto iterator = array.rbegin();
		std::cout<<*iterator;
		iterator++;
		for(;iterator != array.rend();++iterator){
			std::cout<<" "<< *iterator;
		}

	}else{
		auto iterator = array.begin();
		std::cout<<*iterator;
		iterator++;
		for(;iterator != array.end();++iterator){
			std::cout<<" "<< *iterator;
		}
	}
	std::cout<<"\n";
}

template<typename T>
auto println(std::vector<T> array)
 -> void{
	for(auto v : array){
		std::cout<< v <<"\n";
	}
}

int cnt = 0;

template<typename T>
auto insertionSort(std::vector<T> *A,int n,int g)
 -> void{
 	for(int i = g;i < n;i++){
 		auto v = (*A)[i];
 		auto j = i - g;
 		while(j >= 0 && (*A)[j] > v){
 			(*A)[j+g] = (*A)[j];
 			j = j - g;
 			cnt++;
 		}
 		(*A)[j+g] = v;
 	}
 }

template<typename T>
auto shellSort(std::vector<T> *A,int n)
 -> void{
 	cnt = 0;

 	int h = 0;
 	std::vector<int> G;
    for(int k = 1;;k++){
    	h = static_cast<int>((pow(3,k)-1)/2);
    	if(h > n)
    		break;
    	G.push_back(h);
    }

 	std::cout<< G.size() <<std::endl;
 	print(G, true);

 	for(auto it = G.rbegin();it != G.rend();++it){
 		insertionSort<T>( A, n, (*it));
 	}

 }

auto main()
 -> int{

	std::cin.tie(0);
	std::ios_base::sync_with_stdio(false);

	int n;
	std::cin>>n;

	std::vector<int> array(n);
	for(int i=0;i < n;i++)
		std::cin>>array[i];

	shellSort( &array, n);

 	std::cout<< cnt <<"\n";
	println(array);

	return 0;
}

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

#define MAX 256

int main(void){

	int n;

	scanf("%d", &n);

	int i = 1, k = 0, x;

	while (i <= n){
		k = 0;
		x = i;
		if (i % 3 == 0)k = 1;
		if (x % 10 == 3)k = 1;
		while (x > 10){
			x = x / 10;
			if (x % 10 == 3)k = 1;
		}
		if (k == 1){
			printf(" %d", i);
		}
		i++;
	}

	printf("\n");
	return 0;
}


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

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
typedef pair<int,int> pii;
#define MAX_V 110
#define INF (1<<30)

struct edge{int to,cost;};
vector<edge> G[MAX_V],IN[MAX_V];
int d[MAX_V];

void dijkstra(int s,int V){
    priority_queue< pii,vector<pii>,greater<pii> > que;
    rep(i,V)d[i]=INF;
    d[s]=0;
    que.push(pii(0,s));

    while(que.size()){
        pii p=que.top();
        que.pop();

        int v=p.second;
        if(d[v]<p.first)continue;

        rep(i,G[v].size()){
            edge e=G[v][i];
            if(d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                que.push(pii(d[e.to],e.to));
            }
        }
    }
}

int N,M,o;
int maze[100][100],tim[100][100];
int main(void){
	while(cin>>N>>M,N+M){
		rep(i,MAX_V)G[i].clear();
		while(M--){
			cin>>o;
			if(o){
				int a,b,c;
				cin>>a>>b>>c;
				--a,--b;
				edge e = {b,c}, e1 = {a,c};
				G[a].push_back(e);
				G[b].push_back(e1);
			}else{
				int a,b;
				cin>>a>>b;
				--a,--b;
				dijkstra(a,N);
				cout<<(d[b]==(1<<30)?-1:d[b])<<endl;
			}
		}
	}

}

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

vector<pair<double, string>>x;

int n; double P, A, B, C, D, E, F, S, M; string L;
int main() {
	while (true) {
		x.clear();
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M;
			x.push_back(make_pair(100000000.00 - ((F*S*M - P) / (A + B + C + (D + E)*M)), L));
		}
		sort(x.begin(), x.end());
		for (int i = 0; i < n; i++) { cout << x[i].second << endl; }
		cout << "#" << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 int H, W;
 int seen[10000][10000];
  char S[10000][10000];
vector<pair<ll, ll>> prime_factorize(ll n) { //素因数分解と各素数の個数
    vector<pair<ll, ll>> res;

    for (ll i = 2; i * i <= n; i++)
    {
        if (n % i != 0)
        {
            continue;
        }
        int num = 0;

        while (n % i == 0)
        {
            ++num;
            n /= i;
        }
        res.push_back(make_pair(i, num));

    }
     if (n != 1)
        {
            res.push_back(make_pair(n, 1));
        }
        return res;

}
long long GCD(long long a, long long b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}
ll lcm(ll x, ll y) {
    return x / GCD(x, y) * y;//先に割り算をして掛けられる数を小さくして掛け算を行う
}
const int MAX = 510000;
const int MOD = 1000000007;

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

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

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

void dfs(int st, int fi) {
  //  cout << st << " " << fi <<  endl;
    if (st < 0 || fi < 0 || st >= H || fi >= W)
    {
        return ;
    }
    if (S[st][fi] == '#')
    {
        return ;
    }

    if (seen[st][fi] == 1)
    {
        return ;
    }

    if (S[st][fi] == '.')
    {
        seen[st][fi] = 1;
    }

    dfs(st + 1, fi);
    dfs(st, fi + 1);
    dfs(st, fi - 1);
    dfs(st - 1, fi);

}
int main() {
    int N;
    cin >> N;
    vector<int> p(N + 1);
    p[1] = 0;
    for(int i = 2; i <= N; i++) {
        int n = i;
        vector<pair<int, int>> b;
       for(int j = 2; j * j <= n; j++) {
           if(n % j != 0) continue;
           int ex = 0;

           while(n % j == 0) {
               ex++;
               n /= j;
           }

           b.push_back({j, ex});
       }
       if(n != 1) b.push_back({n, 1});

       for(auto v : b) {
           p[v.first] += v.second;
       }
    }

    /*for(int i = 1; i <= N; i++)
        cout << p[i] << endl;*/

    ll ans = 1;

    for(int i = 2; i <= N; i++) {
        ans *= (p[i] + 1);
        ans %= 1000000007;
    }

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

#include <bits/stdc++.h>
#define F(i, x, y) for (int i = (x); i <= (y); ++i)
#define Fr(i, x, y) for (int i = (x); i < (y); ++i)
using namespace std;
typedef long long ll;

const int N = 300000 + 5;
int n, A[N], cnt[N], B[N], Fl[N], Fr[N];

int tg[N];
void ad(int x, int t) {
	if (x < 0 || x >= n) return ;
	tg[x] += t;
}
void add(int l, int r) {
	if (l>r) return ;
//	cerr << "add: " << l << " " << r << '\n';
	ad(l, 1); ad(r+1, -1);
}
int main() {
	memset (Fl, -1, sizeof(Fl));
	memset (Fr, -1, sizeof(Fr));
	scanf ("%d", &n);
	Fr(i, 0, n) scanf ("%d", &A[i]), ++cnt[A[i]], Fr[A[i]] = i;
	for (int i = n-1; i >= 0; --i) Fl[A[i]] = i;
	Fr(i, 0, n) scanf ("%d", &B[i]), ++cnt[B[i]];
	if (*max_element(cnt, cnt + 300000) > n) return puts("No"), 0;
	Fr(i, 0, n) {
		// intersect: Fr = i initially Fr = Fr, i = 1
		// Fl = j, initially Fl = Fl, i = 1
		if (Fl[B[i]] == -1) continue;
		int stl = (i - Fr[B[i]] + n) % n, str = (i - Fl[B[i]] + n) % n;
		if (stl > str)	add(stl, n-1), add(0, str);
		else add(stl, str);
	}
	for (int i = 0; i < n; ++i) {
		if (i) tg[i] += tg[i - 1];
		if (!tg[i]){
			puts("Yes");
			int cur = i;
			for (int j = 1; j <= n; ++j) {
				printf("%d ", B[cur]);
				(cur += 1) %= n;
			}
			return 0;
		}
	}
	puts("No");
	return 0;
}


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

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

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

    vector<int> a(A);
    REP(i, A) cin >> a[i];

    vector<int> b(B);
    REP(i, B) cin >> b[i];

    vector<int> x(M), y(M), c(M);
    REP(i, M) cin >> x[i] >> y[i] >> c[i];

    REP(i, M) {
        x[i]--;
        y[i]--;
    }

    int minA = *min_element(a.begin(), a.end());
    int minB = *min_element(b.begin(), b.end());

    int ans = minA + minB;
    REP(i, M) {
        ans = min(ans, a[x[i]] + b[y[i]] - c[i]);
    }

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

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

int s[5];
char op[5];

void solve(int index, int cur_sum) {
    if (index == 3) {
        if (cur_sum == 7) {
            for(int i = 0; i < 4; ++i) {
                if (i > 0) cout << op[i];
                cout << s[i];
            }
            cout << "=7";
            exit(0);
        }
        return;
    }
    // +
    op[index + 1] = '+';
    solve(index + 1, cur_sum+s[index + 1]);

    //-
    op[index + 1] = '-';
    solve(index + 1, cur_sum-s[index + 1]);
}

int main () {
    for(int i = 0; i < 4; ++i) {
        char ch; cin >> ch;
        s[i] = ch - '0';
    }
    solve(0, s[0]);
}


#include <bits/stdc++.h>

using namespace std;

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

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

ll A[1005];
ll B[1005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    REP(i, N) {
        cin >> A[i];
    }
    REP(i, M) {
        cin >> B[i];
    }
    sort(A, A + N, greater<ll>());
    sort(B, B + M, greater<ll>());

    ll ai = 0, bi = 0;
    ll ans = 1;
    REP(i, N - 1) {
        if (A[i] == A[i + 1])
            ans = 0;
    }
    REP(i, M - 1) {
        if (B[i] == B[i + 1])
            ans = 0;
    }
    if (A[0] != N * M || B[0] != N * M)
        ans = 0;
    for (ll i = N * M; i > 0; --i) {
        if (A[ai + 1] == i)
            ai++;
        if (B[bi + 1] == i)
            bi++;

        ll n = ai + 1;
        ll m = bi + 1;
        if (A[ai] == i)
            n = 1;
        if (B[bi] == i)
            m = 1;

        ll p = n * m;
        if (A[ai] != i && B[bi] != i)
            p -= N * M - i;
        ans = (ans * p) % MOD;
    }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define R cin>>
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout<<(p)<<endl;
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll mod = 1e9+7;
//const ll MOD = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-11;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> void co(T e){cout << e << "\n";}
template<typename T> void co(const vector<T>& v){for(const auto& e : v)
    { cout << e << " "; } cout << "\n";}
ll gcd(ll a, ll b) {
    if (a < b)swap(a, b);
    if (b == 0) return a;
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}
ll lcm(ll a, ll b) {
    ll g = gcd(a, b);
    return a * b / g;
}
bool prime(ll n) {
    for (ll i = 2; i <= sqrt(n); i++) {
        if (n%i == 0)return false;
    }
    return n != 1;
}

int pow(int x, int y, int mod){
    int ret=1;
    for(;y;y>>=1,x=1ll*x*x%mod){
        if(y&1) ret=1ll*ret*x%mod;
    }
    return ret;
}

int n = 3010, maxn = 3000;
ll fac[3010], inv[3010];

void init(){
    fac[0]=1;
    REP(i, 1, maxn+1)fac[i]=1ll*fac[i-1]*i%mod;
    inv[maxn]=pow(fac[maxn], mod-2, mod);
    REPR(j,maxn-1,-1)inv[j]=1ll*inv[j+1]*(j+1)%mod;
}

int main() {
    sonic();
    map<char, string> mp;
    R mp['a'] >> mp['b'] >> mp['c'];
    char pre = 'a', nxt;
    while(mp[pre]!=""){
        nxt=mp[pre][0];
        mp[pre]=mp[pre].substr(1, mp[pre].length());
        pre=nxt;
    }
    pre-=32;
    co(pre);
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<cstdio>
using namespace std;

int main()
{
	double  x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	double X = max(x1, x2) - min(x1, x2);
	double Y = max(y1, y2) - min(y1, y2);

	printf("%.8lf\n", sqrt(X*X + Y*Y));

	return 0;

}

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
	double x1, y1, x2, y2;
	cin >> setprecision(10) >> x1;
	cin >> setprecision(10) >> y1;
	cin >> setprecision(10) >> x2;
	cin >> setprecision(10) >> y2;
	cout << setprecision(10) << sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) << endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main() {
  ll n,m; cin >> n >> m;
  if(n % m == 0){
    cout << "0";
  }
  else{
    cout << "1";
  }
}

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

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

  int S = A + B;
  int T = A - B;
  int U = A * B;

  if (S >= T && S >= U) {
    cout << S << endl;
  } else if (T >= S && T >= U) {
    cout << T << endl;
  } else {
    cout << U << endl;
  }
}


#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    n=n/3;
    printf("%d\n",n);
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#define maxn 510
#define ll long long

using namespace std;

int n;
ll a[maxn][maxn], primes[2020];
bool st[1000010];
int cnt;

void getprimes()
{
	for (int i = 2; i <= 10000; i ++ )
	{
		if (!st[i]) primes[++cnt] = i;
		for (int j = 1; primes[j] * i <= 10000; j ++ )
		{
			st[primes[j] * i] = 1;
			if (i % primes[j] == 0) break;
		}
	}
}

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

ll lcm(ll a, ll b)
{
	if (!a || !b) return a + b;
	return a / gcd(a, b) * b;
}

int main()
{
	getprimes();
	scanf("%d", &n);

	if (n == 2)
	{
		puts("2 3");
		puts("5 4");
		return 0;
	}

	for (int i = 1; i <= n; i ++ )
		for (int j = (i + 1 & 1) + 1; j <= n; j += 2)
			a[i][j] = primes[(i + j) / 2] * primes[n + (i - j) / 2 + (n + 1) / 2];

	for (int i = 1; i <= n; i ++ )
		for (int j = ((i & 1) + 1); j <= n; j += 2)
			a[i][j] = lcm(lcm(a[i][j - 1], a[i - 1][j]), lcm(a[i + 1][j], a[i][j + 1])) + 1;

	for (int i = 1; i <= n; i ++ )
	{
		for (int j = 1; j <= n; j ++ ) cout << a[i][j] << ' ';
		puts("");
	}

	return 0;
}


/*https://atcoder.jp/contests/abc084/tasks/abc084_b*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif
	int a, b;
	string s;
	cin >> a >> b >> s;
	if (s.size() != a + b + 1 || s[a] != '-')
	{
		cout << "No";
		return 0;
	}
	for (int i = 0; i < s.size(); i++)
	{
		if (i != a)
		{
			if (s[i] < 48 || s[i] > 57)
			{

				cout << "No";
				return 0;
			}
		}
	}
	cout << "Yes";
}

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

int main()
{
	int p,q,r,sum,maxm;
	cin >> p >> q >> r;
	sum = p + q + r;
	maxm = max({p,q,r});
	cout << sum - maxm;

}


#include <bits/stdc++.h>
using namespace std;
#define N 4444
#define mod 998244353
int n,k,C[N][N],pw2[N];
int c(int n,int m){
    if (m>n||m<0)
        return 0;
    return C[n][m];
}
int calc(int m,int n){return c(n+m-1,m-1);}
int Get(int v,int n,int k){
    int ans=0;
    int lim=max(0,v/2-max(0,v-1-k));
    for (int i=0;i<=lim;i++)
        ans=(1LL*pw2[i]*C[lim][i]%mod*calc(k-lim*2+i,n-i)+ans)%mod;
    return ans;
}
int main(){
    pw2[0]=1;
    for (int i=1;i<N;i++)
        pw2[i]=pw2[i-1]*2%mod;
    for (int i=0;i<N;i++)
        C[i][0]=C[i][i]=1;
    for (int i=1;i<N;i++)
        for (int j=1;j<i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
    scanf("%d%d",&k,&n);
    for (int i=2;i<=2*k;i++)
        if (i&1)
            printf("%d\n",Get(i,n,k));
        else
            printf("%d\n",(Get(i-1,n,k-1)+Get(i-1,n-1,k-1))%mod);
    return 0;
}

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
long long const mod = 1000000007;

int n, x[3], v;
vector< vector<long long> > dp;

bool g(int mem, int a){
    int m = mem, sum = 0, prc = 2;
    if(a == x[2]) prc--;
    else sum = a;
    while(m > 0){
        int tmp = 1;
        while(m%2 == 0){
            m >>= 1;
            tmp++;
        }
        sum += tmp;
        if(sum == x[prc]){
            sum = 0;
            if(prc-- == 0) return false;
        }else if(sum > x[prc]) return true;
        m >>= 1;
    }
    return true;
}

long long f(int id, int mem){
    if(id == n) return 1;
    if(dp[id][mem] != -1) return dp[id][mem];
    long long res = 0;
    for(int a = 1; a <= 10; a++){
        if(g(mem, a)) res += f(id+1, ((mem<<a)+(1<<(a-1)))%v);
    }
    return dp[id][mem] = res % mod;
}

int main(void){
    cin >> n >> x[0] >> x[1] >> x[2];
    v = (1<<(x[0]+x[1]+x[2]-1));
    dp.assign(n, vector<long long>(v, -1));
    long long ans = 1;
    for(int i=0;i<n;i++) ans = ans * 10 % mod;
    ans += mod - f(0, 0);
    cout << ans%mod << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TEMP_T T    cums(T& v){ T r(v.size()); partial_sum(v.begin(), v.end(), r.begin()); return r; }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    acum(vector<T>& v, T b=0){ return accumulate(v.begin(), v.end(), b); }
TEMP_T vector<T> inpt(int n){ vector<T> v(n); for (int i=0; i<n; i++) cin >> v[i]; return v; }

static inline int in(){ int x; scanf("%d", &x); return x; }

int main()
{
  int n, p;
  while (n = in(), p = in(), n || p){
    int id = 0;
    int stone[64] = {};
    int cnt = p;
    int have_user = 0;
    while (1){
      if (cnt == 1 &&
          ((stone[id] && have_user == 1) ||
          (!stone[id] && have_user == 0))){
        break;
      }
      if (cnt == 0){
        if (stone[id]) have_user--;
        cnt = stone[id];
        stone[id] = 0;
      }
      else {
        if (stone[id] == 0) have_user++;
        cnt--;
        stone[id]++;
      }
      id = (id + 1) % n;
    }
    printf("%d\n", id);
  }
  return (0);
}

#include<iostream>
#include<vector>
using namespace std;
int main() {
	int n, p;
	while (cin >> n >> p, n) {
		int now = 0,wan=p;
		vector<int>person(n,0);
		while (true) {
			if (wan) {
				person[now]++; wan--;
				if (person[now] == p) {
					cout << now << endl;
					break;
				}
			}
			else if (person[now]) {
				wan = person[now]; person[now] = 0;
			}
			now++;
			if (now >= n)now = 0;
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N=110;

int h,w,px,py,f[N][N][N][N],col[N][N],row[N][N];
bool mp[N][N];

void Init()
{
	scanf("%d%d",&h,&w);
	for (int i=1;i<=h;++i)
		for (int j=1;j<=w;++j)
		{
			char ch=getchar();
			while (ch!='o' && ch!='.' && ch!='E') ch=getchar();
			if (ch=='E') px=i,py=j;
			if (ch=='o') mp[i][j]=1;
			else mp[i][j]=0;
		}
}

int Col(int r,int l,int x)
{
	if (l>r) return 0;
	return col[r][x]-col[l][x];
}

int Row(int r,int l,int x)
{
	if (l>r) return 0;
	return row[x][r]-row[x][l];
}

void Solve()
{

	for (int i=1;i<=h;++i)
		for (int j=1;j<=w;++j)
		{
			col[i][j]=col[i-1][j]+mp[i][j];
			row[i][j]=row[i][j-1]+mp[i][j];
		}
	int ans=0;
	for (int a=px;a>0;--a)
		for (int b=px;b<=h;++b)
			for (int c=py;c>0;--c)
				for (int d=py;d<=w;++d) f[a][b][c][d]=-1e9;
	f[px][px][py][py]=0;
	for (int a=px;a>0;--a)
		for (int b=px;b<=h;++b)
			for (int c=py;c>0;--c)
				for (int d=py;d<=w;++d)
				{
					if (a>1 && a-1>b-px)
						f[a-1][b][c][d]=max(f[a-1][b][c][d],f[a][b][c][d]+Row(min(w-(py-c),d),max(c-1,d-py),a-1));
					if (b<h && h-b>px-a)
						f[a][b+1][c][d]=max(f[a][b+1][c][d],f[a][b][c][d]+Row(min(w-(py-c),d),max(c-1,d-py),b+1));
					if (c>1 && c-1>d-py)
						f[a][b][c-1][d]=max(f[a][b][c-1][d],f[a][b][c][d]+Col(min(h-(px-a),b),max(a-1,b-px),c-1));
					if (d<w && w-d>py-c)
						f[a][b][c][d+1]=max(f[a][b][c][d+1],f[a][b][c][d]+Col(min(h-(px-a),b),max(a-1,b-px),d+1));
					ans=max(ans,f[a][b][c][d]);
				}

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

int main()
{
	Init();
	Solve();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n, m;
int x, y;

int a[110][110];

char buf[110];
void in(){
	read(n, m);
	for(int i=1; i<=n; ++i){
		scanf("%s", buf+1);
		for(int j=1; j<=m; ++j){
			a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
			if(buf[j] == 'o'){
				++a[i][j];
			} else if(buf[j] == 'E'){
				x=i; y=j;
			}
		}
	}
}

int dp[102][102][102][102];

int rect(int L1, int R1, int D1, int U1, int L2, int R2, int D2, int U2){
	int L=max(L1, L2);
	int R=min(R1, R2);
	int D=max(D1, D2);
	int U=min(U1, U2);
	if(L>R || D>U) return 0;
	if(0) printf("%d %d %d %d\n", L, R, D, U);
	return a[R][U]-a[L-1][U]-a[R][D-1]+a[L-1][D-1];
}

int main()
{
	in();
	for(int w=1; w<=n; ++w){
		for(int h=1; h<=m; ++h){
			for(int l=1; l+w-1<=n; ++l){
				int r=l+w-1;
				if(x<l || r<x) continue;
				for(int d=1; d+h-1<=m; ++d){
					int u=d+h-1;
					if(y<d || u<y) continue;
					int me=dp[l][r][d][u];
					auto f = [&](int A, int B, int C, int D){
						int I=l+A;
						int J=r+B;
						int K=d+C;
						int L=u+D;
						int nv = rect(1+(r-x), n-(x-l), 1+(u-y), m-(y-d),
							(A||B)?(A?I:J):(I),
							(A||B)?(A?I:J):(J),
							(C||D)?(C?K:L):(K),
							(C||D)?(C?K:L):(L)
						);
						int& T=dp[I][J][K][L];
						if(T < me+nv){
							if(0) printf("upd from %d-%d, %d-%d to df %d %d %d %d : %d+%d = %d\n",
								l,r,d,u, A,B,C,D, me, nv, me+nv);
						}
						T=max(T, me+nv);
					};
					if(1 < l) f(-1, 0, 0, 0);
					if(r < n) f(0, 1, 0, 0);
					if(1 < d) f(0, 0, -1, 0);
					if(u < m) f(0, 0, 0, 1);
				}
			}
		}
	}
	printf("%d\n", dp[1][n][1][m]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
  cin>>N;
  vector<int64_t>A(N);
  for(int i=0;i<N;i++)
    cin>>A.at(i);
  vector<int64_t>B(1+N);
  map<int64_t,int64_t>M;
  M[1]=A.at(0);
  for(int i=2;i<=N;i++)
    if(i%2==1)
      M[i]=M.at(i-2)+A.at(i-1);
  B.at(0)=0;
  B.at(0)=0;
  B.at(2)=max(A.at(0),A.at(1));
  for(int i=3;i<=N;i++){
  if(i%2==0)
    B.at(i)=max(A.at(i-1)+B.at(i-2),A.at(i-2)+M.at(i-3));
    else
      B.at(i)=max(A.at(i-1)+B.at(i-2),A.at(i-2)+B.at(i-3));

  }cout<<B.at(N)<<endl;
   return 0;
}

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  ios:: sync_with_stdio(false);
  cin.tie();
  int x;
  cin >> x;
  int mi = x;
  int mx = x;
  for (int i = 0; i < 4; i++) {
    cin >> x;
    mi = min(mi, x);
    mx = max(mx, x);
  }
  int k;
  cin >> k;
  cout << ((mx - mi) <= k ? "Yay!" : ":(");
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
string str;
  cin>>str;
  if(str.size()==2){
    cout<<str<<endl;
  }
  else{
   cout<<str.at(2)<<str.at(1)<<str.at(0)<<endl;
  }
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(),(x).end()
#define endl "\n"

using ll = long long;
using P = pair<int,int>;
using mp =  map<string,int>;

const int MOD = 1e9 + 7;

int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

    string s;
    cin >> s;

    if(s.size() == 3) {
        reverse(all(s));
    }

    cout << s << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<=n;i++)
const ll MOD = 1000000007;
long long modinv(long long a, long long m=MOD) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
    /*MODの割り算の際に
    a/＝b;
    を
    a *= modinv(b,MOD);
    とする。
        */
}
int main(){
    int R;cin >> R;
    double ans = R * 2 * M_PI;
    printf("%0.5f\n", ans);
    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<functional>
#include<queue>
#include<stack>
#include<math.h>
#define INF 1000000001
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int main()
{
	int n;
	while (cin >> n) {
		int s = 0;
		for (int i = 1; i < 600 / n; i++) {
			s += (i*n)*(i*n)*n;
		}
		cout << s << endl;
	}

	return 0;
}


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

const ll MAXN = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, ans=1, tmp=0;
vector<char> A;
vector<ll> B;

signed main(){
    cin >> N;
    for(int i=0;i<2*N;i++) {
        char a;
        cin >> a;
        A.push_back(a);
    }
    ll ct=0;
    for(int i=0;i<A.size()-1;i++) {
        if(A[i]==A[i+1]) {
            ct++;
            //cout << ct << endl;
        }
        if(ct%2==1) {
            tmp = i+1-2*B.size();
            B.push_back(tmp);
            if(tmp<=0) {
                cout << 0 << endl;
                return 0;
            }
        }
    }
    if(A[0]=='W'||ct%2==0||B.back()!=1) {
        cout << 0 << endl;
        return 0;
    }
    for(int i=0;i<B.size();i++) {
        ans *= B[i];
        ans %= MOD;
        //cout << B[i] << endl;
    }
    //cout << ans << endl;
    for(int i=1;i<=N;i++) {
        ans *= i;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The Value Of " << #val << " is : " << val << '\n';
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll mininf = -922337203685477;
const ll nax = 1e5+5;
ll n, arr[nax];
unordered_map<ll, bool>vis;
unordered_map<ll, ll>cnt;
vector<ll>v;

int main(){
	IOS
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> arr[i];
		cnt[arr[i]]++;
		if(!vis[arr[i]]){
			vis[arr[i]]=1;
			v.pb(arr[i]);
		}
	}
	sort(v.rbegin(), v.rend());
	bool udah=0;
	ll satu;
	for(auto x:v){
		if(cnt[x]>=4 && !udah){
			cout << x * x << '\n';
			return 0;
		}
		if(cnt[x]>=2){
			if(udah){
				cout << satu * x << '\n';
				return 0;
			}
			else{
				udah=1;
				satu=x;
			}
		}
	}
	cout << 0 << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;
vector<vector<bool>> used(500,vector<bool>(500,false));
P bfs(int N,int M,vector<string> field,int sx,int sy){
    int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
    queue<P> que;
    que.push(P(sx,sy));
	used[sx][sy] = true;
	P ans = mp(0,0);
	if (field[sx][sy] == '.'){
		ans.fs++;
	}
	else{
		ans.sc++;
	}
    //キューが空になるまでループ
    while(que.size()){
        P p = que.front();
        que.pop();//先頭を取り出す
        for (int i = 0;i < 4;i++){
            int nx = p.fs + dx[i], ny = p.sc + dy[i];
            //nx,nyが進めるマスか、行ったことのないマスかどうかを判定
			if (field[p.fs][p.sc] == '.'){
 				if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '.' && !used[nx][ny]){
                	que.push(P(nx,ny));//追加
                	used[nx][ny] = true;
					ans.sc++;
           		}
			}
			else{
				if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' && !used[nx][ny]){
                	que.push(P(nx,ny));//追加
                	used[nx][ny] = true;
					ans.fs++;
            	}
			}
        }
    }
	return ans;
}
int main(){
	int h,w;cin>>h>>w;
	vector<string> s(h);
	for(int i = 0; i < h; i++) {
		cin>>s[i];
	}
	ll ans = 0;
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			if (!used[i][j]){
				P res = bfs(h,w,s,i,j);
				ans += (res.fs * res.sc);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>

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

ll mo=1000000007;
ll comb(ll N_, ll C_) {
    const int NUM_=310000;
    static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
    if (fact[0]==0) {
        inv[1]=fact[0]=factr[0]=1;
        for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
        for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
    }
    if(C_<0 || C_>N_) return 0;
    return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}
int32_t main() {
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall
    oneforall

    ini(n);
    ins(morty);
    ini(n1);
    FOR(i,0,n1){
        ini(x);
        int d = 0;
        int m = 0;
        int dm = 0;
        int tmp = 0;
        int i1 = 0;
        FOR(i,0,n){
            if(morty[i] == 'D')d++;
            else if(morty[i] == 'M'){
                m++;
                dm+=d;
            }
            else if(morty[i] == 'C'){
                tmp+=dm;
            }
            if(i>=x-1){
                if(morty[i1] == 'D'){
                    dm-=m;
                    d--;
                }
                else if(morty[i1] == 'M')m--;
                i1++;
            }
        }
        out(tmp);
    }

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
const double PI = 3.1415926535897932;

int main()
{
	int N, K;
	cin >> N >> K;
	vector<double> P(N);
	rep(i, N) {
		cin >> P[i];
		P[i] = (P[i] + 1) / 2.0;
	}
	vector<double> Sm(N, 0);

	for (int i = 0; i < K; ++i) {
		Sm[0] += P[i];
	}
	double max_sum = Sm[0];
	int idx = 0;

	for (int i = 1; i <= (N - K); ++i) {
		Sm[i] = Sm[i - 1] - P[i - 1] + P[i + K - 1];
		if (Sm[i] > max_sum) {
			idx = i;
			max_sum = Sm[i];
		}
	}
	cout << fixed << setprecision(10) << max_sum << endl;
	return 0;
}

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

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

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

int main(){

    int n;
    ll T;
    cin >> n >> T;
    ll ans = 0;
    vector<ll> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }
    for(int i = 1; i < n; i++){
        ans += min(T, v[i] - v[i-1]);
    }
    ans += T;
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define modo 1000000007
#define mod 998244353
#define pi acos(-1.0)
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) {
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P){if(P==0) return 1;
  if(P%2==0){ll t = jou(N, P/2);return t*t % mod;
  }return (N * jou(N, P-1)) % mod;}  //jou(n,mod-2)で逆元
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int x;
  cin>>x;
  if(x!=0){
    cout << 0 << endl;
    return 0;
  }

  vector<ll> p(n);
  rep(i,n-1){
    int y;
    cin>>y;
    if(y==0){
      cout << 0 << endl;
      return 0;
    }
    p[y]++;
  }

  repo(i,n-1){
    if(p[i]==0&&p[i+1]!=0){
      cout << 0 << endl;
      return 0;
    }
  }

  ll ans=1;
  for(int i=1;i<n-1;i++){
    ans*=jou(p[i],p[i+1]);
    ans%=998244353ll;
  }

  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x)  __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(),(x).end()
#define scf(x) scanf("%lld",&x)
#define rep(i,n) for(ll i=0;i<n;i++)
const int INF = (int)1e9;
const ll IINF=1LL<<62;
const int maxn=10000005;

//int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/

/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
ll modexpo(ll a,ll b)
{
     ll res=1LL;
     while(b>0)
     {
          if(b&1)res=(res*a)%mod;
          a=(a*a)%mod;
          b>>=1;
     }
     return res;
}

int dp[200][200];
int x[100][100],y[100][100];
int main()
{
    int n;
    cin>>n;
    int a[n];
    int mx=0;
    for(int i=0;i<n;i++){cin>>a[i];
    mx=max(mx,a[i]);}
    if(a[0]!=0){cout<<0;return 0;}
    int ct[100005]={0};
    for(int i=0;i<n;i++){
         ct[a[i]]++;
    }
    if(ct[0]!=1){cout<<0;return 0;}

    ll res=1LL;
    for(int i=0;i<mx;i++){
         for(int j=0;j<ct[i+1];j++){

              res=(res*(ct[i]%mod2))%mod2;
              res%=mod2;
         }

    }
    cout<<res;
}


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

int main() {
  string S;
  cin >> S;
  bool can = true;

  if(S.at(0)!='A') can = false;

  int num_C = 0;
  for(int i=2;i<S.size()-1;i++){
    if(S.at(i)=='C') num_C ++;
  }
  if(num_C!=1) can =false;

  int num_upper = 0;
  for(int i=0;i<S.size();i++){
    if(isupper(S.at(i))) num_upper ++;
  }
  if(num_upper!=2) can = false;

  if(can) cout << "AC" << endl;
  else cout << "WA" << endl;
}


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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main()
{
  int n,m;cin>>n>>m;
  vector<int> a(m);
  vector<int> b(m);
  int i;
  int even=m/2;
  int odd=m-even;
  rep(i,odd)//1 to odd*2
  {
    a[i]=i+1;
    b[i]=odd*2-i;
  }
  int add=odd*2;
  rep(i,even)
  {
    a[odd+i]=add+i+1;
    b[odd+i]=m*2+1-i;
  }
  rep(i,m)
  {
    cout<<a[i]<<' '<<b[i]<<endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define range(i,a,n) for(ll i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

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

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0  : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,m;
	cin>>n>>m;
	rep(i,m){
		if(n%2==0){
			puta(i+1,(i+1>((m+1)/2)?n-i-1:n-i));
		}
		else{
			puta(i+1,n-i-1);
		}
	}
}


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

#define rep(i,n) for(int i=0; i<int(n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

string name, str;
int length1, length2;

struct list {
	int a;
	int b;
	int c;
	int r;
};

int ok[1010];

int main() {
	while (1) {
		int A, B, C, N;
		cin >> A >> B >> C;
		if (A == 0 && B == 0 && C == 0)break;
		cin >> N;

		for (int i = 0; i < A + B + C; i++)
			ok[i] = 2;

		struct list d[1010];

		for (int i = 0; i < N; i++) {
			cin >> d[i].a >> d[i].b >> d[i].c >> d[i].r;
			if (d[i].r) {
				ok[d[i].a - 1] = 1;
				ok[d[i].b - 1] = 1;
				ok[d[i].c - 1] = 1;
			}
		}
		for (int i = 0; i < N; i++) {
			if (d[i].r != 1) {
				if (ok[d[i].a - 1] == 1 && ok[d[i].b - 1] == 1)
					ok[d[i].c - 1] = 0;
				else if (ok[d[i].b - 1] == 1 && ok[d[i].c - 1] == 1)
					ok[d[i].a - 1] = 0;
				else if (ok[d[i].a - 1] == 1 && ok[d[i].c - 1] == 1)
					ok[d[i].b - 1] = 0;
			}
		}
		for (int i = 0; i < A + B + C; i++)
			cout << ok[i] << endl;
	}
	return 0;
}

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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

struct FenwickTree{
	vector<ll> data;
	FenwickTree(int n):data(n+1){}
	void Add(int i,int x){
		for(i++;i<data.size();i+=i&-i)
			data[i]+=x;
	}
	ll Sum(int i){
		ll res=0;
		for(;i;i-=i&-i)
			res+=data[i];
		return res;
	}
	ll Sum(int i,int j){
		return Sum(j)-Sum(i);
	}
};

ll InversionNumber(const vi& a)
{
	FenwickTree ft(a.size());
	ll res=0;
	rep(i,a.size()){
		res+=ft.Sum(a[i],a.size());
		ft.Add(a[i],1);
	}
	return res;
}

bool test(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int n=src.size();
	rep(i,n){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):mt(z,y,x);
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):mt(z,y,x);
	}
	sort(all(src)); sort(all(dst));
	return src==dst;
}

tuple<bool,bool> calc(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst)
{
	int m=src.size(),flip=0;
	rep(i,m){
		int x,y,z;
		tie(x,y,z)=src[i];
		src[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
		tie(x,y,z)=dst[i];
		dst[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x));
	}
	map<tuple<int,int,int>,int> f;
	rep(i,m) f[dst[i]]=i;
	vi a(m);
	rep(i,m) a[i]=f[src[i]];
	return mt(InversionNumber(a)&1,flip&1);
}

bool solve(int n,vvi grid)
{
	array<bool,2> inv,flip;
	rep(i,2){
		vector<tuple<int,int,int>> src,dst;
		rep(j,n) if(j%2==i){
			if(j/2%2==0){
				src.emplace_back(mt(3*j+1,3*j+2,3*j+3));
				dst.emplace_back(mt(grid[0][j],grid[1][j],grid[2][j]));
			}
			else{
				src.emplace_back(mt(3*j+3,3*j+2,3*j+1));
				dst.emplace_back(mt(grid[2][j],grid[1][j],grid[0][j]));
			}
		}
		if(!test(src,dst)) return false;
		tie(inv[i],flip[i])=calc(src,dst);
	}
	return inv[0]==flip[1]&&inv[1]==flip[0];
}

int main()
{
	for(int n;cin>>n&&n;){
		vvi grid(3,vi(n));
		rep(i,3) rep(j,n) cin>>grid[i][j];
		cout<<(solve(n,grid)?"Yes":"No")<<endl;
	}
}


#pragma GCC optimize ("O3")

#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif

using namespace std;

#define TRACE(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << endl;}

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << "[" << *it << " = " << a << "]";
	err(++it, args...);
}

#define MAXN ((int)2e5+5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define LOG(x) if(DEBUG) cerr << x << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pii pair<int,int>
#define mid ((l+r)/2)

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll ans = 0;
	ll a;
	double b, f;
	cin >> a >> b;
	ll w = b*100 + 0.01;

	ans = (a*w) / 100;

	cout << fixed << ans << endl;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
*/

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

int main(){
    int n, m, ans = 0;
    cin >> n >> m;
    ans = n * m;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using ld = long double;
const ll ATCODER_AMARI = 1000000007; //10^9 + 7
const ll INF = 10000000000000;

//階乗関数　ATCODER_AMARIに注意
ll factorial(ll k){
  ll sum = 1;
  for(int i = 1 ; i < k + 1; i++) {
    sum *= i;
    //sum = sum % ATCODER_AMARI;
  }
  return sum;
}

int main() {
  ll n,q;
  cin >> n >> q;
  string s;
  cin >> s;

  ll count = 0;
  vector<ll> front(n,0);

  rep(i,s.size()) {
    if(i == 0 ) continue;

    if(s.at(i - 1) == 'A' && s.at(i) == 'C') {
      count++;
    }

    front.at(i) = count;
  }

  ll ans;

  rep(i,q) {
    ll l,r;
    cin >> l >> r;
    l--;
    r--;

    ans = front.at(r) - front.at(l);

    cout << ans << endl;

  }

}

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

struct StQuery
{
  StQuery() : m_nLeft(0), m_nRigt(0) { }

  int m_nLeft;
  int m_nRigt;
};

void fnInput(string& rsStr, vector<StQuery>& rvoQuery)
{
  int nStrSiz, nQuerySiz;
  cin >> nStrSiz >> nQuerySiz;
  rvoQuery.resize(nQuerySiz);

  cin >> rsStr;

  for (StQuery& roQuery : rvoQuery)
    cin >> roQuery.m_nLeft >> roQuery.m_nRigt;
}

void fnSubStrACCnt(string sStr, const vector<StQuery>& cnrvoQuery)
{
  const string cnsAC = "AC";
  int nACCnt = 0;
  vector<int> vnACCnt(sStr.size());

  for (int nBgn = 0; nBgn < vnACCnt.size() - 1; nBgn++)
  {
    if (sStr.substr(nBgn, 2) == cnsAC) nACCnt++;
    vnACCnt[ nBgn + 1 ] = nACCnt;
  }

  for (int i = 0; i < cnrvoQuery.size(); i++)
  {
    cout << vnACCnt[ cnrvoQuery[i].m_nRigt - 1 ] - vnACCnt[ cnrvoQuery[i].m_nLeft - 1];
    cout << endl;
  }
}

int main()
{
  string sStr;
  vector<StQuery> voQuery;

  fnInput(sStr, voQuery);
  fnSubStrACCnt(sStr, voQuery);

  return 0;
}


#include <iostream>
#include <queue>
#include <functional>
#include <vector>

using namespace std;

struct edge {
	int to;
	int cost;
};

typedef pair<int, int> P;

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

void dijkstra(int s, int h) {
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, 10000000);
	d[s] = 0;
	que.push(P(0, s));

	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first) {
			continue;
		}
		if (h == 0) {
			for (int i = 0; i < G[v].size(); i++) {
				edge e = G[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		} else {
			for (int i = 0; i < G2[v].size(); i++) {
				edge e = G2[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		}
	}
}

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		for (int i = 1; i <= m; i++) {
			G[i].clear();
			G2[i].clear();
		}
		V = m+1;
		int a, b, time, cost;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			edge e;
			e.to = b;
			e.cost = cost;
			G[a].push_back(e);
			e.to = a;
			G[b].push_back(e);
			e.to = b;
			e.cost = time;
			G2[a].push_back(e);
			e.to = a;
			G2[b].push_back(e);
		}
		int w;
		cin >> w;
		int p, q, r;
		for (int i = 0; i < w; i++) {
			cin >> p >> q >> r;
			dijkstra(p, r);
			cout << d[q] << endl;
		}
	}
	return 0;
}

#include <stdio.h>
#define MAXN 105

typedef struct _pair {
  int id, score;
} pair;

int N;
pair arr[MAXN];
int rank[MAXN];

void bubleSort() {
  int i, j;
  pair p;
  for(i = 0; i < N; ++i) {
    for(j = 1; j < N-i; ++j) {
      if(arr[j-1].score < arr[j].score) {
        p = arr[j-1];
        arr[j-1] = arr[j];
        arr[j] = p;
      }
    }
  }
}

int main() {
  int i, j;
  int A, B, C, D;
  scanf("%d", &N);
  for(i = 0; i < N; ++i) {
    arr[i].id = i;
    arr[i].score = 0;
  }
  for(i = 0; i < N*(N-1)/2; ++i) {
    scanf("%d %d %d %d", &A, &B, &C, &D);
    --A; --B;
    if(C == D) {
      arr[A].score += 1;
      arr[B].score += 1;
    } else if(C > D) {
      arr[A].score += 3;
    } else if(C < D) {
      arr[B].score += 3;
    }
  }

  bubleSort();
  for(i = 0, j = 1; i < N; ++i) {
    if(i-1 >= 0 && arr[i].score == arr[i-1].score) {
    } else {
      j = i+1;
    }
    rank[arr[i].id] = j;
  }

  for(i = 0; i < N; ++i) {
    printf("%d\n", rank[i]);
  }

  return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<algorithm>
#define N 2005
#define MOD 998244353
using namespace std;
int k,n;
int a[N];
int d[N][N];
int dy[N][N];
int main() {
	scanf("%d%d", &k,&n);
	int i, j;
	for (i = 1; i <= k; i++) d[1][i] = 1;
	for (i = 2; i <= n; i++) {
		int sum = 0;
		for (j = 1; j <= k; j++) {
			sum += d[i - 1][j]; sum %= MOD;
			d[i][j] = sum;
		}
	}
	dy[0][0] = 1;
	dy[1][0] = k;
	for (i = 1; i <= k; i++) {
		dy[0][i] = 1; dy[1][i] = k;
	}
	for (i = 2; i <= n; i++) {
		for (j = 1; j <= k; j++) {
			dy[i][0] += d[i][j]; dy[i][0] %= MOD;
		}
		for (int ban = 1; ban <= k; ban++) {
			dy[i][ban] = dy[i][ban - 1] - dy[i - 2][ban - 1];
			if (dy[i][ban] < 0) dy[i][ban] += MOD;
		}
	}
	for (i = 2; i <= k+1 ; i++) {
		printf("%d\n", dy[n][i / 2]);
	}
	for (i = k ; i >= 2; i--) printf("%d\n", dy[n][i / 2]);
	return 0;
}

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const double EPS = 1e-10;
bool used[4001];
ll fact[4001], inv_fact[4001];
ll comb(ll n, ll k) {
	if (n < k) return 0;
	ll ret = fact[n];
	(ret *= inv_fact[k]) %= MOD;
	(ret *= inv_fact[n - k]) %= MOD;
	return ret;
}
ll mod_pow(ll a, ll b) {
	if (b == 0) return 1;
	if (b % 2 == 0) {
		ll x = mod_pow(a, b / 2);
		return (x*x) % MOD;
	}
	return (a*mod_pow(a, b - 1)) % MOD;
}
int main() {
	int K, N;
	cin >> K >> N;
	fact[0] = 1;
	inv_fact[0] = 1;
	for (int i = 1; i <= 4000; i++) {
		fact[i] = (fact[i - 1] * i) % MOD;
		inv_fact[i] = (inv_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
	}
	for (int i = 2; i <= 2 * K; i++) {
		memset(used, 0, sizeof(used));
		int x = 0, y = 0;
		for (int j = 1; j <= K; j++) {
			if (used[j]) continue;
			used[j] = 1;
			if (1 <= i - j && i - j <= K) {
				if (!used[i - j]) {
					used[i - j] = 1;
					y++;
				}
			}
			else {
				x++;
			}
		}
		ll ans = 0;
		for (int j = 0; j <= min(N, y); j++) {
			ll t = comb(y, j);
			(t *= mod_pow(2, j)) %= MOD;
			(t *= comb(N + x - 1, N - j)) %= MOD;
			(ans += t) %= MOD;
		}
		if (i % 2 == 0) {
			for (int j = 0; j <= min(N - 1, y); j++) {
				ll t = comb(y, j);
				(t *= mod_pow(2, j)) %= MOD;
				(t *= comb(N + x - 2, N - 1 - j)) %= MOD;
				(ans += t) %= MOD;
			}
		}
		cout << ans << endl;
	}
}


#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
using namespace std;
#define N 305
int n,k,f[N][N][N];char s[N];
int dp(int l,int r,int k){
	if (k<0) return -1000;
	if (l>r) return 0;
	int &res=f[l][r][k];
	if (~res) return res;
	if (l==r) return res=1;
	res=0;
	if (s[l]==s[r]) res=max(res,dp(l+1,r-1,k)+2);
	else res=max(res,max(max(dp(l+1,r,k),dp(l,r-1,k)),dp(l+1,r-1,k-1)+2));
	return res;
}
int main(){
	scanf("%s%d",s+1,&k);n=strlen(s+1);
	memset(f,-1,sizeof(f));
	printf("%d\n",dp(1,n,k));
	return 0;
}

#include<map>
#include<set>
#include<bitset>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<chrono>
#include<stack>
#include<fstream>
#include<math.h>
#include<list>
#include<complex>
#define REP(i,x,y) for(ll i=x;i<=y;i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vtype(type) vector<type>
#define vvtype(type) vector<vector<type>>
#define MEMSET(a, n, m) for(ll i=0;i<=n;i++) a[i] = m
#define BIT(n) (ll(1)<<n)
#define UNIQUE(v) v.erase(unique(v.begin(),v.end()),v.end())
#define UNIQUE_ARRAY(a,x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a,n) sort(a+1,a+n+1)
#define SORT_O(a,n,order) sort(a+1,a+n+1,order)
#define PER(i,y,x) for(ll i=y;i>=x;i--)
typedef long long ll;
using namespace std;
struct wall
{
	long long r; ll c;
	bool operator<(const wall& rhs) const {
		if (r != rhs.r) {
			return r < rhs.r;
		}
		else {
			return c < rhs.c;
		}
	}
};

ll const MOD = 998244353;
ll mod_p(ll x, ll y) {
	//x %= MOD;
	//y %= MOD;
	return (x + y + MOD) % MOD;
}

ll mod_m(ll x, ll y) {
	x %= MOD;
	y %= MOD;
	return x * y%MOD;
}

ll mod_pow(ll x, ll t) {
	x %= MOD;
	if (t == 0) {
		return 1;
	}
	else {
		ll v = mod_pow(x, t / 2);
		if (t % 2 == 0) {
			return v * v % MOD;
		}
		else {
			return v * v%MOD * x %MOD;
		}
	}
}

ll mod_inv(ll x) {
	return mod_pow(x, MOD - 2);
}

vll fct(1e7 + 5);
vll fct_inv(1e7 + 5);

void fct_init() {
	fct[0] = fct_inv[0] = 1;
	REP(i, 1, 1e6) {
		fct[i] = mod_m(fct[i - 1], i);
		fct_inv[i] = mod_inv(fct[i]);
	}
}

ll c(ll n, ll r) {
	return mod_m(fct[n], mod_m(fct_inv[r], fct_inv[n - r]));
}

ll h(ll n, ll r) {
	return c(n - 1 + r, r - 1);

}

ll f(ll n, ll k, ll t, ll x) {
	ll ans = 0;
	ll sign = 1;
	REP(i, 0, x) {
		ll tmp = 0;
		if (n - 2 * i < 0) {
			break;
		}
	//	cout << "h " << h(n - 2 * i, k) << " " << c(x, i) << endl;
		tmp = mod_m(h(n - 2 * i, k), c(x, i));
		ans = mod_p(ans, sign*tmp);
		sign *= -1;
		//cout << ans << "?" << endl;
	}
	return ans;
}

int main() {
	ll n, k;
	cin >> k >> n;
	if (k == 1) {
		cout << 0 << endl;
		return 0;
	}
	fct_init();
	REP(t, 2, 2 * k) {
		ll x = 0;
		REP(i, 1, t) {
			ll j = t - i;
			if (i < j && j <= k) {
				x++;
			}
			if (i >= j) break;
		}
		//cout << x << "!" << endl;
		if (t % 2 == 1) {
			ll ans = f(n, k, t, x);
			cout << ans << endl;
		}
		else {
			ll u = f(n, k - 1, t, x);
			ll v = f(n - 1, k - 1, t, x);
			ll ans = mod_p(u, v);
			cout << ans << endl;
		}
	}
}


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

int main() {

  int N;
  cin >> N;
  vector<string> s(N);
  for( int i = 0; i < N; i++ ) {
    cin >> s.at(i);
  }

  int M;
  cin >> M;
  vector<string> t(M);
  for( int i = 0; i < M; i++ ) {
    cin >> t.at(i);
  }

  vector<int> count(N);

  for ( int i = 0; i < N; i++ ) {
    for( int j = 0; j < N; j ++ ) {
        if (s.at(i) == s.at(j) ) {
          count.at(i)++;
        }
    }
    for ( int j = 0; j < M; j++ ) {
        if (s.at(i) == t.at(j) ) {
          count.at(i)--;
        }
      }
  }

  int max = 0;
  for( int i = 0; i < N; i++ ) {
    if( count.at(i) > max ) {
      max = count.at(i);
    }
  }

  cout << max << endl;
}

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> P;
#define PI 3.141592653589793
#define MOD 1000000007
//#define MOD 998244353
#define ALL(obj) (obj).begin(),(obj).end()
template<class T>inline bool chmax(T& a,T b){if (a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if (a>b){a=b;return 1;}return 0;}
const ll INF = 1LL << 60;

//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}

int main() {
    ll n,m;
    cin >> n;
    map<string,ll> red;
    map<string,ll> blue;
    for (ll i=0;i<n;i++){
        string a;
        cin >> a;
        if (blue.count(a)){
            blue[a]++;
        }else{
            blue[a]=1;
        }
    }
    cin >> m;
    for (ll i=0;i<m;i++){
        string a;
        cin >> a;
        if (red.count(a)){
            red[a]++;
        }else{
            red[a]=1;
        }

    }
    ll ans=0;
    for (auto b: blue){
        string s=b.first;
        if (blue[s]>red[s]){
            ans=max(ans,blue[s]-red[s]);
        }
    }
    cout << ans << endl;
    return 0;
}


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

int main(){
  int N;
  cin >> N;
  for (int i = N; i > 0; i--)
  {
    if (sqrt(i) == floor(sqrt(i))){
      cout << i << endl;
      return 0;
    }

  }

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for (int i=n;^i;i--)
#define ALL(v) v.begin(),v.end()
// #define int ll
#define eps 1e-9
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))

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

signed main(void){
	ll sum;
	int d;
	while(cin>>d){
		sum = 0;
		for(int i=0;i<600;i+=d) sum += d*(i*i);
		cout << sum << endl;
	}
}


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

int main() {

    while(true){

        int n,p;

        cin >> n >> p;

        if(n == 0){
            break;
        }

        int maxp = p;

        list<pair<int,int> > l;

        for(int i=0; i<n; i++){
            l.push_back(make_pair(0,i));
        }

        list<pair<int,int> >::iterator itr = l.begin();

        while(true){

            if(p > 0){
                p--;
                itr->first++;
                if(itr->first == maxp){
                    cout << itr->second << endl;
                    break;
                }
            }
            else{
                p += itr->first;
                itr->first = 0;
            }

            itr++;

            if(itr == l.end()){
                itr = l.begin();
            }

        }

    }

    return 0;
}

#include <bits/stdc++.h>

#if ((_WIN32 || __WIN32__) && __cplusplus < 201103L)
#define lld I64d
#else
#define lld lld
#endif

//{{{
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
// clang-format off
#define debug(args...) {vector<string> _v = split(#args, ','); err(_v.begin(), args); cerr<<endl;}
vector<string> split(const string& s, char c) {vector<string> v; stringstream ss(s); string x; while
(getline(ss, x, c)) v.push_back(x); return v;} void err(vector<string>::iterator) {}
template<typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args)
{cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; err(++it, args...);}
// clang-format on
//}}}

int a[22][22];
int n;

LL dp[1 << 16];

LL dfs(int st)
{
    if (dp[st] != -1) return dp[st];

    if (__builtin_popcount(st) == 1)
    {
        return dp[st] = 0;
    }

    LL ret = 0;
    for (int i = 0; i < n; i++)
    {
        if ((st & (1 << i)) == 0) continue;
        for (int j = i + 1; j < n; j++)
        {
            if ((st & (1 << j)) == 0) continue;
            ret += a[i][j];
        }
    }

    for (int i = st; i; i = (i - 1) & st)
    {
        if (i == st) continue;
        int st1 = i;
        int st2 = st ^ i;

        LL v = dfs(st1) + dfs(st2);
        if (v > ret) ret = v;
    }
    return dp[st] = ret;
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
// freopen("out", "w", stdout);
#endif

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

        clr(dp, -1);
        dp[0] = 0;
        int lim = (1 << n) - 1;
        LL ans = dfs(lim);
        cout << ans << endl;
    }

    return 0;
}


#include <cstdio>
typedef long long ll;
#define ri register int
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = (x << 1) + (x << 3) + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
const int N=605;
int n,m,col1[N][N],col2[N][N],tag[2][2];
inline void solve(ri d,int col[N][N]){
	ri i,j,k;
	k=0;
	while(d%4==0) d>>=2,++k;
	if(d&1){
		for(i=0;i<m;++i)
			for(j=0;j<m;++j)
				if((i>>k)+(j>>k)&1)
					col[i][j]=1;
	}
	else{
		for(i=0;i<m;++i)
			for(j=0;j<m;++j)
				if((i>>k)&1)
					col[i][j]=1;
	}
}
int main(){
	ri d1,d2,i,j,x,y,cnt;
	gi(n);gi(d1);gi(d2);
	m=n<<1;
	solve(d1,col1);
	solve(d2,col2);
	for(i=0;i<m;++i)
		for(j=0;j<m;++j)
			++tag[col1[i][j]][col2[i][j]];
	for(i=0;i<=1;++i)
		for(j=0;j<=1;++j)
			if(tag[i][j]>=n*n){
				cnt=0;
				for(x=0;x<m;++x)
					for(y=0;y<m;++y)
						if(col1[x][y]==i&&col2[x][y]==j){
							print(x);putc(' ');print(y);putc('\n');
							if(++cnt==n*n) return 0;
						}
			}
}

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

using namespace std;

typedef long long ll;

int main() {
  int n,m,q;
  ll res;
  vector<ll> v;
  while(true) {
    cin >> n >> m;
    if(n == 0) break;
    v.clear();
    res = 0;
    for(int i = 0; i < n; i++){
      cin >> q;
      v.push_back(q);
    }
    sort(v.begin(), v.end(), greater<int>());
    for(int i=0; i < n; i++) {
      if(i % m != m - 1)
        res += v[i];
    }
    cout << res << endl;
  }

  return 0;
}