#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
#include <iomanip>

using namespace std;
vector <int> edges[1000];
vector <int> cycle;
bool in_stack[1000];
bool done[1000];
set <pair <int, int> > edge_set;
int cycle_begin;
bool dfs_cycle(int cur){
  in_stack[cur] = true;
  cycle.push_back(cur);
  done[cur] = true;
  for(int i = 0; i < edges[cur].size(); i++){
    int next = edges[cur][i];
    if(in_stack[next]){
      cycle_begin = next;
      return true;
    } else {
      if(!done[next] && dfs_cycle(next)){
        return true;
      }
    }
  }
  cycle.pop_back();
  in_stack[cur] = false;
  return false;
}

int main(void){
  int N;
  int M;
  cin >> N >> M;
  for(int i = 0; i < M; i++){
    int a;
    int b;
    cin >> a >> b;
    edges[a - 1].push_back(b - 1);
    edge_set.insert(make_pair(a - 1, b - 1));
  }
  for(int i = 0; i < N; i++){
    if(!done[i]){
      if(dfs_cycle(i)){
        break;
      }
    }
  }

  if(cycle.size() == 0){
    cout << -1 << endl;
    return 0;
  }

  vector <int> new_cycle;
  for(int i = 0; i < cycle.size(); i++){
    if(cycle[i] == cycle_begin){
      for(int j = i; j < cycle.size(); j++){
        new_cycle.push_back(cycle[j]);
      }
      break;
    }
  }
  cycle = new_cycle;
  while(true){
    map <int, int> cycle_map;
    for(int i = 0; i < cycle.size(); i++){
      cycle_map[cycle[i]] = i;
      //cout << cycle[i] << " ";
    }
    //cout << endl;
    bool updated = false;
    for(int u = 0; u < N; u++){
      for(int j = 0; j < edges[u].size(); j++){
        int v = edges[u][j];
        if(cycle_map.find(u) != cycle_map.end()
          && cycle_map.find(v) != cycle_map.end()
          && (cycle_map[u] + 1) % cycle.size() != cycle_map[v]){
          vector <int> next_cycle;
          if(cycle_map[u] > cycle_map[v]){
            for(int k = cycle_map[v]; k <= cycle_map[u]; k++){
              next_cycle.push_back(cycle[k]);
            }
          } else {
            for(int k = cycle_map[v]; k < cycle.size(); k++){
              next_cycle.push_back(cycle[k]);
            }
            for(int k = 0; k <= cycle_map[u]; k++){
              next_cycle.push_back(cycle[k]);
            }
          }
          cycle = next_cycle;
          updated = true;
          break;
        }
      }
      if(updated){
        break;
      }
    }
    if(!updated){
      break;
    }
  }

  cout << cycle.size() << endl;
  for(int i = 0; i < cycle.size(); i++){
    cout << cycle[i] + 1 << endl;
  }

  for(int i = 0; i < cycle.size(); i++){
    if(edge_set.find(make_pair(cycle[i], cycle[(i + 1) % cycle.size()])) == edge_set.end()){
      cout << "shit no edge!" << endl;
    }
    for(int j = 0; j < cycle.size(); j++){
      if(i == j || (i + 1) % cycle.size() == j){
        continue;
      }
      if(edge_set.find(make_pair(cycle[i], cycle[j])) != edge_set.end()){
        cout << "shit" << endl;
      }
    }
  }

  return 0;
}

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

int n, m, pre[1001];
bool vis[1001];
vector<int> adj[1001];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
	}
	for(int i = 1; i <= n; i++){
		memset(vis, 0, sizeof(vis));
		memset(pre, 0, sizeof(pre));
		stack<pair<int, int>> que;
		que.push({i, 0});
		while(!que.empty()){
			pair<int, int> vtx = que.top();
			que.pop();
			vis[vtx.first] = true;
			pre[vtx.first] = vtx.second;
			for(auto j : adj[vtx.first])
				if(j == i){
					set<int> chn = {i};
					int cur = vtx.first;
					while(cur != i){
						chn.insert(cur);
						cur = pre[cur];
					}
					bool val = true;
					for(auto k : chn){
						int cnt = 0;
						for(auto l : adj[k])
							if(chn.find(l) != chn.end()) cnt++;
						if(cnt > 1) val = false;
					}
					if(val){
						cout << chn.size() << "\n";
						for(auto k : chn) cout << k << "\n";
						return 0;
					}
				}else if(!vis[j]) que.push({j, vtx.first});
		}
	}
	cout << "-1\n";
	return 0;
}


#include <bits/stdc++.h>

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

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll N, M;
    cin >> N >> M;
    vec s(M), c(M);
    REP(i,0,M) {
        cin >> s[i] >> c[i];
        --s[i];
    }

    ll d = 1;
    bool f = true;
    REP(i,0,N) d *= 10;
    REP(i,N==1?0:d/10,d) {
        string S = to_string(i);
        f = true;
        REP(j,0,M) {
            if(!(s[j] < SZ(S) && S[s[j]]-'0' == c[j])) {
                f = false;
                break;
            }
        }
        if(f) {
            PR(i);
            break;
        }
    }
    if(!f) PR(-1);

    return 0;
}

/*

*/

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

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

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

int main(void) {
    int H, W;
    cin >> H >> W;

    vector<vector<int>> board(H, vector<int>(W));
    for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
            cin >> board[h][w];
        }
    }

    vector<pair<pair<int, int>, pair<int, int>>> cmds;

    for (int h = 0; h < H; h++) {
        if (h % 2 == 0) {
            for (int w = 0; w < W; w++) {
                int next_h, next_w;
                if (w != W - 1) {
                    next_h = h;
                    next_w = w + 1;
                } else {
                    next_h = h + 1;
                    next_w = w;
                }

                if (next_h >= H) {
                    continue;
                }

                if (board[h][w] % 2 != 0) {
                    board[h][w]--;
                    board[next_h][next_w]++;
                    cmds.push_back(make_pair(make_pair(h, w), make_pair(next_h, next_w)));
                }
            }
        } else {
            for (int w = W - 1; w >= 0; w--) {
                int next_h, next_w;
                if (w != 0) {
                    next_h = h;
                    next_w = w - 1;
                } else {
                    next_h = h + 1;
                    next_w = w;
                }

                if (next_h >= H) {
                    continue;
                }

                if (board[h][w] % 2 != 0) {
                    board[h][w]--;
                    board[next_h][next_w]++;
                    cmds.push_back(make_pair(make_pair(h, w), make_pair(next_h, next_w)));
                }
            }
        }
    }

    cout << cmds.size() << endl;
    for (auto cmd : cmds) {
        pair<int, int> src = cmd.first;
        pair<int, int> dst = cmd.second;
        printf("%d %d %d %d\n", src.first + 1, src.second + 1, dst.first + 1, dst.second + 1);
    }

    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll,ll> P;

#define rep(i,n) for(int i = 0; i < int(n); i++)
using namespace std;

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

constexpr ll mod = 1e9 + 7;

struct UnionFind {
public:
	vector <ll> par; //
	vector <ll> siz; //

	UnionFind() {}

	UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}
	void init(ll sz_) {
		par.resize(sz_);
		siz.assign(sz_, 1LL);
		for (ll i = 0; i < sz_; ++i) par[i] = i;
	}

	ll root(ll x) {
		while (par[x] != x) {
			x = par[x] = par[par[x]];
		}
		return x;
	}

	bool merge(ll x, ll y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		par[y] = x;
		return true;
	}

	bool issame(ll x, ll y) {
		return root(x) == root(y);
	}

	ll size(ll x) {
		return siz[root(x)];
	}
};

int main() {
	ll n;
	cin >> n;
	vector<int> ans;
	for (int i = 0; i < 60; i++) {
		ll m = n % (1LL << (i+1));
		if (m) {
			ans.push_back(1);
			ll sub = 1LL << i;
			if (i % 2 == 1) {
				sub *= -1;
			}
			n -= sub;
		}
		else ans.push_back(0);
		if (n == 0) break;
	}
	reverse(ans.begin(), ans.end());
	for (auto a : ans) {
		cout << a;
	}
	cout << endl;
	return 0;
}

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    if(N == 0) {cout << 0 << endl; return 0;}
    int cnt = 0;
    string A;
    while(N != 0) {
        if(cnt % 2 == 0) {
            if(abs(N) % 2 == 1) {
                N -= 1;
                A = "1" + A;
            } else {
                A = "0" + A;
            }
        } else {
            if(abs(N) % 2 == 1) {
                N += 1;
                A = "1" + A;
            } else {
                A = "0" + A;
            }
        }
        N /= 2;
        //N *= -1;
        cnt++;
    }
    cout << A << endl;

    return 0;
}


#include <iostream>

#include <functional> // std::function
#include <vector> // std::vector

struct edge {
    using weight_type = size_t;

    size_t to;

    edge(size_t to) : to(to) { }

    size_t cost() { return 1; }
};

struct graph {
    using edge_type = edge;

    const bool is_directed;
    const size_t count_vertices;
    std::vector<edge> *edges;

    graph(size_t count_vertices, bool is_directed = true) : count_vertices(count_vertices), is_directed(is_directed) {
        edges = new std::vector<edge>[count_vertices];
    }

    ~graph() {
        delete [] edges;
    }

    void make_edge(size_t from, size_t to) {
        edges[from].push_back(edge(to));
        if (!is_directed) {
            edges[to].push_back(edge(from));
        }
    }
};

graph opposite_graph(const graph &g) {
    size_t n = g.count_vertices;
    graph op_g(n);
    for (size_t u = 0; u < n; ++u) {
        for (const edge &e: g.edges[u]) {
            size_t v = e.to;
            op_g.make_edge(v, u);
        }
    }
    return op_g;
}

/*
template <class Graph>
std::vector<size_t> post_order_traversal(const Graph &g, size_t root) {
    using Edge = typename Graph::edge_type;

    size_t n = g.count_vertices;
    std::vector<size_t> order;
    std::vector<bool> used(n, false);
    std::function<void(size_t)> dfs = [&](size_t u) {
        printf("start node %d\n", u);
        used[u] = true;
        for (const Edge &e: g.edges[u]) {
            size_t v = e.to;
            if (!used[v]) {
                dfs(v);
            }
        }
        printf("end node %d\n", u);
        order.push_back(u);
    };
    dfs(root);
    return order;
}
*/

template <class Graph>
std::vector<size_t> strongly_connected_component(const Graph &g) {
    using Edge = typename Graph::edge_type;

    size_t n = g.count_vertices;
    std::vector<bool> used(n, false);

    std::vector<size_t> post_order;
    std::function<void(size_t)> post_order_traversal = [&](size_t u) {
        used[u] = true;
        for (const Edge &e: g.edges[u]) {
            size_t v = e.to;
            if (!used[v]) {
                post_order_traversal(v);
            }
        }
        post_order.push_back(u);
    };
    for (size_t u = 0; u < n; ++u) {
        if (!used[u]) {
            post_order_traversal(u);
        }
    }

    Graph op_g = opposite_graph(g);
    std::vector<size_t> component(n, 123);
    std::function<void(size_t, size_t)> dfs = [&](size_t u, size_t component_index) {
        component[u] = component_index;
        used[u] = true;
        for (const Edge &e: op_g.edges[u]) {
            size_t v = e.to;
            if (!used[v]) {
                dfs(v, component_index);
            }
        }
    };
    size_t count_components = 0;
    fill(used.begin(), used.end(), false);
    for (int i = n - 1; i >= 0; --i) {
        size_t u = post_order[i];
        if (!used[u]) {
            dfs(u, count_components++);
        }
    }
    return component;
}

int main() {
    using namespace std;

    int V, E;
    cin >> V >> E;
    graph g(V);
    for (int i = 0; i < E; ++i) {
        int u, v;
        cin >> u >> v;
        g.make_edge(u, v);
    }
    auto component = strongly_connected_component(g);
    int Q;
    cin >> Q;
    for (int i = 0; i < Q; ++i) {
        int u, v;
        cin >> u >> v;
        cout << (component[u] == component[v]) << '\n';
    }
    cout << flush;
    return 0;
}

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

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n);
    vector<vector<int>> edge(n, vector<int>());
    vector<vector<int>> weight(n, vector<int>());
    for(int i=0; i<n-1; i++){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--; c[i];
        edge[a[i]].push_back(b[i]);
        edge[b[i]].push_back(a[i]);
        weight[a[i]].push_back(c[i]);
        weight[b[i]].push_back(c[i]);
    }

    int q, k;
    cin >> q >> k;
    k--;

    vector<int> dis(n, -1);
    #if 0
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(0, k));
    while(!pq.empty()){
        int d = pq.top().first;
        int p = pq.top().second;
        pq.pop();
        if(dis[p] != -1) continue;
        dis[p] = d;
        for(int i=0; i<edge[p].size(); i++){
            int next = edge[p][i];
            if(dis[next] == -1){
                pq.push(make_pair(d + weight[p][i], next));
            }
        }
    }
    #else
    stack<int> s;
    s.push(k);
    dis[k] = 0;
    while(!s.empty()){
        int p = s.top();
        s.pop();
        for(int i=0; i<edge[p].size(); i++){
            int next = edge[p][i];
            if(dis[next] == -1){
                s.push(next);
                dis[next] = dis[p] + weight[p][i];
            }
        }
    }
    #endif

    for(int i=0; i<q; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        cout << dis[x] + dis[y] << endl;
    }
    return 0;
}


#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    string str[2];

    for (int i = 0; i < 2; i++)
    {
        getline(cin, str[i]);
//        cout << "Input: " << str[i] << endl;
    }
    str[1] += ' ';

    int num = atoi(str[0].c_str());
//    cout << "Input Number: " << num << endl;

    long* a = new long[num];

    stringstream ss;
//    ss = stringstream(str[1]);
    ss << str[1];

    string buff;
    int n = 0;
    while (getline(ss, buff, ' '))
    {
        if (n >= num) break;

        a[n] = atoi(buff.c_str());
//        cout << "Input Number: " << a[n] << endl;

        n++;
    }

    for (int x = 0; x < num - 1; x++)
    {
        for (int y = 0; y < num - 1 - x; y++)
        {
            if (a[y] > a[y+1])
            {
                swap(a[y], a[y+1]);
            }
        }

//        for (int i = 0; i < num; i++)
//        {
//            cout << "Step " << x << ": " << a[i] << endl;
//        }

    }

    long min = a[0];
    long max = a[num - 1];
    long long sum = 0;
    for (int i = 0; i < num; i++)
    {
        sum += a[i];
//        cout << "Sorted Number: " << a[i] << endl;
    }

    delete [] a;

    cout << min << " " << max << " " << sum << endl;

    return 0;
}

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cstdlib>
#include<iomanip>
#include<queue>
#include<set>
#include <valarray>
#include<stack>
#include<sstream>
#include<math.h>

using namespace std;

void Solution()
{
    long int a;

    cin>>a;

    int b[10000] = {0};
    long int sum = 0;

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

        sum = sum + b[i];
    }

    sort(b, b + a);
    cout<<b[0]<<" "<<b[a - 1]<<" "<<sum<<endl;
}

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


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = (int)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int N;
string a0;

string ask(int i) {
    out(i);
    fflush(stdout);

    string res;
    cin >> res;
    return res;
}

void f(int l, int r) {
    int md = (l + r) / 2;

    string res = ask(md);
    if (res == "Vacant") return;

    if ((md % 2 == 0 && a0 == res)
        || (md % 2 == 1 && a0 != res)) {
        f(md + 1, r);
    }
    else {
        f(l, md);
    }
}

int main() {
    cin >> N;

    a0 = ask(0);
    if (a0 == "Vacant") return 0;

    f(1, N);

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1e9+7;

int main(){
    ll n;
    cin >> n;
    cout << 0 << endl;
    vector<string> ans(n);
    cin >> ans[0];
    if(ans[0] == "Vacant"){
        return 0;
    }
    cout << n-1 << endl;
    cin >> ans[n-1];
    if(ans[n-1] == "Vacant"){
        return 0;
    }
    ll right = n-1;
    ll left = 0;
    while(right-left > 1){
        ll mid = (right+left)/2;
        cout << mid << endl;
        cin >> ans[mid];
        if(ans[mid] == "Vacant") return 0;
        else{
            if((right-mid)%2 == 0 && ans[mid] != ans[right]){
                left = mid;
            }
            else if((right-mid)%2 == 1 && ans[mid] == ans[right]){
                left = mid;
            }
            else if((mid-left)%2 == 0 && ans[mid] != ans[left]){
                right = mid;
            }
            else if((mid-left)%2 == 1 && ans[mid] == ans[left]){
                right = mid;
            }
        }
    }

}


#include <bits/stdc++.h>

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

int main() {
    int n;
    cin >> n;
    cout << n / 3 << endl;
    return 0;
}

#include <stdio.h>

int main() {

    long long int N;
    long long int total = 0;

    scanf("%lld", &N);
    int i;
    for( i = 1; i <= N; i++){
        if(i % 3 != 0 && i % 5 != 0 ){
            total = total + i;
        }
    }

    printf("%lld\n", total);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
    double __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
// const int MOD = 998244353;
const ll LINF = 9e18;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};

void cinfast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

VL mul(const VL &a, const VL &b) {
    VL ret(a.size());
    REP(i, a.size()) { ret[i] = a[b[i]]; }
    return ret;
}

signed main() {
    LCIN(N);
    VL x(N);
    VECCIN(x);
    LCIN(M, K);
    VL a(M);
    REP(i, M) {
        cin >> a[i];
        a[i]--;
    }
    VL dx(N - 1);
    REP(i, N - 1) dx[i] = x[i + 1] - x[i];
    VL order(N - 1);
    iota(All(order), 0);
    REP(i, M) swap(order[a[i] - 1], order[a[i]]);
    while(K) {
        if(K & 1) dx = mul(dx, order);
        order = mul(order, order);
        K >>= 1;
    }
    VL ans(N);
    ans[0] = x[0];
    cout << ans[0] << "\n";
    REP(i, N - 1) {
        ans[i + 1] = ans[i] + dx[i];
        cout << ans[i + 1] << "\n";
    }
}

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

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

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}
/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, T;
  cin >> N >> T;
  llvec v(N);
  rep(i, N)cin >> v[i];
  ll ans = 0;
  rep(i, N-1){
    ans += min(T, v[i+1] - v[i]);
  }
  cout << ans +T<< endl;

  return 0;
}


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

int main(){
  string S;
  cin >> S;
  set<char> characters;
  for(int i=0; i<S.size(); i++){
    characters.insert(S.at(i));
  }
  char ans,check='a';
  bool flag = true;
  for(auto itr=characters.begin();
      itr!=characters.end(); itr++){
    if(*itr!=check){
      ans=check;
      break;
    }
    if(check=='z'){
      flag = false;
      break;
    }
    check++;
  }
  if(flag){
    cout << ans;
  }else{
    cout << "None";
  }
  return 0;
}

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

int main(void){
  string s;cin >> s;
  vector<int> v(26,0);
  for(char c:s) v[c-'a']++;
  for(int i;i<26;i++){
    if(!v[i]) {
      char c = 'a' +i;
      cout << c <<endl;
      return 0;
    }
  }
  cout << "None" << endl;
  return 0;
}


#include    <iostream>
#include    <vector>

using namespace std;

void printVector(const vector<int> &A) {
    for(int i = 0; i < A.size(); i++) {
        cout << A[i];
        if(i+1 < A.size()) cout << ' ';
    }
    cout << endl;
}

int main() {
    int n, count = 0;
    auto swap = [](int &a, int &b) {
        int tmp = a;
        a = b;
        b = tmp;
    };

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

    for(int i = 0; i < n-1; i++) {
        for(int j = n-2; j >= i; j--) {
            if(A[j] > A[j+1]) {
                swap(A[j], A[j+1]);
                count++;
            }
        }
    }
    printVector(A);
    cout << count << endl;

    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

int main(int argc,char** argv) {
	int n,t;
	vector<int> a;
	cin >> n;
	for(int i = 0;i < n;i++) {
		cin >> t;
		a.push_back(t);
	}
	bool flag = true;
	int count = 0;
	while(flag) {
		flag = false;
		for(auto itr = a.begin() + 1;itr != a.end();itr++) {
			if(*itr < *(itr - 1)) {
				iter_swap(itr,itr-1);
				flag = true;
				count++;
			}
		}
	}
	for(int i = 0;i < a.size()-1;i++) cout << a[i] << " ";
	cout << a[a.size()-1] << endl << count << endl;
}


#include <bits/stdc++.h>
using namespace std;
int num(int N){
int a = 0 , b = 1, c;
for(int i = 0 ; i < 9 ; i++){
a += N / b % 10;
b *= 10;
}
c = N % a;
return c;
}
int main(){
  int N;
  cin >> N;
      if(num(N) == 0){
        cout << "Yes" <<endl;
      }
      else if(num(N) != 0){
        cout << "No" << endl;
      }
}

#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, a, b) for (int i = (a); i < (b); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using tl = tuple<ll, ll, ll>;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false; }

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    string s; cin>>s;
    ll f = 0;
    ll n = sz(s);
    rep(i, n) {
        f += s[i] - '0';
    }
    if (stoi(s) % f == 0) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define REP(i,n) for(int i = 0;i < (n);i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;

int main(){
  int n,q;
  cin >> n >> q;
  vector <int> res;
  int a[n];
  int sq = sqrt(n);
  int nsq = n/sq+1;
  int lazy[nsq];
  fill(a,a+n,2147483647);
  fill(lazy,lazy+nsq,-1);
  REP(i,q){
    int z;
    cin >> z;
    if(z == 0){
      int s,t,x;
      cin >> s >> t >> x;

      /*if(s == t){
	a[s] = x;
	}*/
      if(t/sq == s/sq){
	if(lazy[t/sq] >= 0){
	  fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
	  lazy[t/sq] = -1;
	}
	if(t-s+1 == sq){
	  lazy[t/sq] = x;
	}
	else{
	  fill_n(a+s,t-s+1,x);
	}
	//cout << "debug1" <<endl;
      }

      else{
	//cout << "debug3" << endl;
	int f = s,to = t;
	while(to%sq != (sq - 1)){
	  to--;
	}
	while(f%sq != 0){
	  f++;
	}
	if(lazy[t/sq] >= 0){
	  fill_n(a+(t/sq)*sq,sq,lazy[t/sq]);
	}
	if(lazy[s/sq] >= 0){
	  fill_n(a+(s/sq)*sq,sq,lazy[s/sq]);
	}
	fill_n(a+to+1,t-to,x);
	lazy[t/sq] = -1;
	fill_n(a+s,f-s,x);
	lazy[s/sq] = -1;
	f = f/sq;
	to = to/sq;
	if(f <= to){
	  fill_n(lazy+f,to-f+1,x);
	}

      }
      /*REP(i,n)
	cout << a[i] << "  ";
	cout << endl;*/
    }
    else{
      int x;
      cin >> x;
      int te = x / sq;

      if(lazy[te] < 0){
	//cout << a[x] << endl;
	res.pb(a[x]);
      }
      else{
	fill_n(a+te*sq,sq,lazy[te]);
	lazy[te] = -1;
	//cout << a[x] << endl;
	res.pb(a[x]);

      }

    }

  }
  for(int i = 0;i < res.size();i++){
    cout << res[i] << endl;
  }
  //cout << res.size() << endl;
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})

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

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = (1LL<<31)-1;
const int mod = 1e9 + 7;

struct SegmentTree {
  vector<int> data, lazy;
  int sz;
  SegmentTree(int n) {
    sz = 1; while(sz < n) sz <<= 1;
    data.resize(2*sz-1, inf);
    lazy.resize(2*sz-1, -1);
  }
  void lazy_evaluate(int k, int l, int r) {
    if(~lazy[k]) {
      data[k] = lazy[k];
      if(r - l > 1) {
	lazy[2*k+1] = lazy[k];
	lazy[2*k+2] = lazy[k];
      }
      lazy[k] = -1;
    }
  }
  void update(int a, int b, int x, int k, int l, int r) {
    lazy_evaluate(k, l, r);
    if(r <= a || b <= l) return;
    if(a <= l && r <= b) {
      lazy[k] = x;
      lazy_evaluate(k, l, r);
      return;
    }
    update(a, b, x, 2*k+1, l, (l+r)/2);
    update(a, b, x, 2*k+2, (l+r)/2, r);
  }
  void update(int a, int b, int x) {
    update(a, b, x, 0, 0, sz);
  }
  int query(int i, int k, int l, int r) {
    lazy_evaluate(k, l, r);
    if(r - l == 1) return data[k];
    if(l <= i && i <= r) {
      if(i < (l+r)/2) return query(i, 2*k+1, l, (l+r)/2);
      else return query(i, 2*k+2, (l+r)/2, r);
    }
    return inf;
  }
  int query(int i) {
    return query(i, 0, 0, sz);
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n, q;
  cin >> n >> q;
  SegmentTree seg(n);
  rep(i, q) {
    int com, s, t, x;
    cin >> com >> s;
    if(com == 0) cin >> t >> x, seg.update(s, t+1, x);
    else cout << seg.query(s) << endl;
  }

  return 0;
}


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

int main() {
  string a;
  int b;
  cin>>a>>b;
  for(int i=0;i<a.size();i+=b){
    cout<<a.at(i);
  }
  cout<<endl;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int MOD = 1e9+7;
using ll = long long;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(x) x.begin(), x.end()
int main() {
  ll a;
  cin >> a;
  ll ans = 999;
  while(a>99){
    ans = min(ans,abs(753-a%1000));
    a /= 10;
  }
  cout << ans;
}


#include <bits/stdc++.h>
#define f(i,j,k) for(int i=j;i<k;i++)
using namespace std;
int main(){
    string s;
    cin>>s;
    bool f=true;
    long long ans=0;
    if(s[0]!='A'){
        f=false;
    }
    f(i,2,s.size()-1){
        if(s[i]=='C'){
            ans++;
        }
        else if(s[i]-'a'<0){
            f=false;
        }
    }
    if(s[1]-'a'<0||s[s.size()-1]-'a'<0){
        f=false;
    }
    if(f&&ans==1){
        cout<<"AC"<<endl;
    }
    else{
        cout<<"WA"<<endl;
    }
    return 0;
}

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

vector<ll> enum_divisors(ll N) {
    vector<ll> res;
    for (ll i = 1; i * i <= N; ++i) {
        if (N % i == 0) {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N/i != i) res.push_back(N/i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}//vector<ll>で受け取ること！

int main() {
    ll n;cin >> n;
    vector<ll> a=enum_divisors(n);
    ll x=a[a.size()/2];
    ll y=n/x;
    ll p=1,q=1;
    while(x>=10){
        x /= 10;
        p++;
    }
    while(y>=10){
        y /= 10;
        q++;
    }

    cout << max(p,q) << endl;

    return 0;
}


#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

// vectorの重複削除
// std::sort(test.begin(), test.end());
// test.erase(std::unique(test.begin(), test.end()), test.end());

using namespace std;
using ll = long long;
using P = pair<int, int>; // vector<P> ba(n); みたいな感じですぐ使える ba[i] = P(a, b); make_pairをしなくてもいい
// typedef vector<int> v;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<v<<endl;}
#define rep(i, c) for(int i = 0; i < int(c); i++)
#define ALL(x) (x).begin(), (x).end() //sort(ALL(obj))みたいな
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値
#define pb push_back //
// const int MOD = 1000000007;
// string alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// printf("%.20f\n", ans); あれ
// const double PI = 3.141592653589793238462643383;

int main() {
  std::vector<int> v(3);
  rep(i,3) cin >> v[i];
  sort(v.rbegin(), v.rend());
  debug(v);
  int num; cin>>num;
  rep(i, num) {
    v[0] *= 2;
    sort(v.rbegin(), v.rend());
  }
  int sum = accumulate(ALL(v), 0);
  cout << sum << endl;

}

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

int main(){

  int n;
  cin >> n;
  vector<vector<int>> v(n,vector<int>(n-1));
  rep(i,n)rep(j,n-1)cin >> v[i][j],v[i][j]--;
  set<P> st;
  vector<P> nxt;
  rep(i,n){
    if(i < v[i][0])nxt.push_back(make_pair(v[i][0],i));
    else nxt.push_back(make_pair(i,v[i][0]));
  }
  vector<int> l(n,1);
  int roop = n*(n-1)/2;
  ll res = 0;
  while(roop){
    if(nxt.empty())break;
    vector<P> k;
    for(auto au : nxt){
      if(st.count(au)){
        roop--;
        if(l[au.first] < n-1)k.push_back(make_pair(max(au.first,v[au.first][l[au.first]]),min(au.first,v[au.first][l[au.first]])));
        if(l[au.second] < n-1)k.push_back(make_pair(max(au.second,v[au.second][l[au.second]]),min(au.second,v[au.second][l[au.second]])));
        l[au.first]++;
        l[au.second]++;
      }else{
        st.insert(au);
      }
    }
    nxt = k;
    res++;
  }
  cout << (!roop ? res : -1) << endl;

  return 0;
}


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

using namespace std;

void darts();

void longest_dis();

void sum_of_three_integers();

void otoshidama();

int main()
{
  //darts();
  //longest_dis();
  //sum_of_three_integers();
  otoshidama();
}

void darts()
{
  while (true)
  {
    int n, m;
    cin >> n >> m;

    // stop cond
    if (n == 0) break;

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

    // 2 darts combination
    // p = q, 0, p + q
    vector<int> q = p;
    q.push_back(0);
    for (int i = 0; i < n; ++i)
    {
      for (int j = 0; j <= i; ++j)
      {
        q.push_back(p[i] + p[j]);
      }
    }

    sort(q.begin(), q.end());

    // q's size = 1 + n + n(n + 1) / 2
    //cout << q.size() << "\n";
    //for (auto x : q) cout << x << " ";

    int ret = 0;
    int j = q.size() - 1;
    //i:q linear scan
    for (int i = 0; i < q.size(); ++i)
    {
      if (q[i] > m) break;
      while (q[i] + q[j] > m) --j;
      ret = max(ret, q[i] + q[j]);
    }

    cout << ret << "\n";
  }
}

void longest_dis()
{
  int n;
  cin >> n;

  vector<pair<int, int>> p(n);
  for (int i = 0; i < n; ++i) cin >> p[i].first >> p[i].second;

  double v_max = 0.0;
  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if ((p[i].first - p[j].first) * (p[i].first - p[j].first)
        + (p[i].second - p[j].second) * (p[i].second - p[j].second)
        >  v_max * v_max)
      {
        v_max = sqrt((p[i].first - p[j].first) * (p[i].first - p[j].first)
          + (p[i].second - p[j].second) * (p[i].second - p[j].second));
      }
    }
  }

  cout << v_max << "\n";
}

void sum_of_three_integers()
{
  int k, s;
  cin >> k >> s;

  int cnt = 0;
  //for (int i = 0; i <= k; ++i)
  //{
  //  for (int j = 0; j <= k; ++j)
  //  {
  //    for (int m = 0; m <= k; ++m)
  //    {
  //      if (i + j + m == s) ++cnt;
  //    }
  //  }
  //}
  for (int i = 0; i <= k; ++i)
  {
    for (int j = 0; j <= k; ++j)
    {
      int m = s - i - j;
      if (0 <= m && m <= k) ++cnt;
    }
  }

  cout << cnt << "\n";
}

void otoshidama()
{
  int n, Y;
  cin >> n >> Y;

  for (int x = 0; x <= n; ++x)
  {
    for (int y = 0; x + y <= n; ++y)
    {
      int z = n - x - y;
      if (x * 10000 + y * 5000 + z * 1000 == Y)
      {
        cout << x << " "<< y << " " << z << "\n";
        return;
      }
    }
  }

  cout << "-1 -1 -1\n";
}


#include <iostream>
#include <bits/stdc++.h>
#define mod 998244353
#define point complex<double>
#define pi acos(-1)
#define pb push_back

typedef long long ll;
typedef long double ld;

using namespace std;

void Fastio(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
ld n, dp[305][305][305];
ld solve(int i, int y, int j){
    if(i+y+j==0)
        return 0;
    if(dp[i][y][j]!=-1)
        return dp[i][y][j];
    ld res=1, c0 = ( i + y + j);
    if(i)res+=solve(i-1, y+1, j)*((ld)i)/n;
    if(y)res+=solve(i, y-1, j+1)*((ld)y)/n;
    if(j)res+=solve(i, y, j-1)*((ld)j)/n;
    return dp[i][y][j]=res / c0 * n;
}
int main()
{
    Fastio();
    int ttt=1; //cin>>ttt;
    while(ttt--){
        cin>>n;
        int c1=0, c2=0, c3=0;
        for(int i=0; i<n; i++){
            int x; cin>>x;
            if(x==1)c1++;
            else if(x==2)c2++;
            else c3++;
        }
        for(int i=0; i<305; i++){
            for(int y=0; y<305; y++){
                for(int j=0; j<305; j++){dp[i][y][j]=-1;}
            }
        }
        cout<<fixed<<setprecision(9)<<solve(c3, c2, c1)<<'\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    char turn = 'a';
    string a, b, c;
    int i = 0, j = 0, k = 0;
    cin >> a >> b >> c;
    while (i <= a.size() && j <= b.size() && k <= c.size())
    {
        if (turn == 'a')
            turn = a[i++];
        else if (turn == 'b')
            turn = b[j++];
        else
            turn = c[k++];
    }
    if (i > a.size()) cout << "A" << endl;
    else if (j > b.size()) cout << "B" << endl;
    else cout << "C" << endl;
}

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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 int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

string s[3];
int cnt[3];

int main() {
  cin >> s[0] >> s[1] >> s[2];
  int cur = 0;
  while (cnt[cur] < s[cur].size()) {
    int next = s[cur][cnt[cur]] - 'a';
    ++cnt[cur];
    cur = next;
  }
  char ans = 'A' + cur;
  cout << ans;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>
#include <stack>
#include <cstdint>

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rrep(i, n) for(ll i = (ll)(n-1); i >= 0; i--)
#define repi(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rrepi(i,a,b) for(ll i=(ll)(b);i>=(ll)(a);i--)

#define all(x) (x).begin(),(x).end()

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

typedef long long ll;

using namespace std;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;cin>>n;
  cout<<(n-2)*180<<endl;

  return 0;

}

/*
このコード、と～おれ!
Be accepted!
∧＿∧
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
					*/

#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>

#pragma GCC optimize("Ofast")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)//少数出力
#define printdbd(a) printf("%.16lf\n", a)//少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str())//string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2)//等差数列の和、初項,末項,項数
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg)/360.0)*2.0*PI)
#define rad_to_deg(rad) (((rad)/2.0/PI)*360.0)
#define Please return
#define AC 0

using ll = long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);//最初の改行対策
	while (c != '\n' && c != -1) {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

/*-----------------------------------------ここからコード-----------------------------------------*/

int main() {

	int a;
	scanf("%d", &a);
	printf("%d\n", 180 * (a - 2));

	Please AC;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<complex>
#define rep(i, a, n) for(int i=a;i<n;i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define R long double
#define P complex<R>
using namespace std;
const int mod = 1000000007;
const int INF = 1e12;
template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}
const R PI = acos(-1.0L);
const R inf = 1e13;
int x[110], y[110];
R ans[110];
inline R dist(int i, P q) {
   P p = P(x[i], y[i]);
   return norm(p - q);
}
signed main(){
    int n;
    cin>>n;
    for (int i=0;i<n;i++)
	{
		cin>>x[i]>>y[i];
    }
    int limit = 200000;
    rep(i, 0, limit){
        R cur = 2.0 * PI / limit * i;
        P point = polar(inf, cur);
        R d = 1e130;
        int idx = -1;
        rep(j, 0, n) if(chmin(d, dist(j, point))) idx = j;
        ans[idx] += 1.0;
    }
    cout.precision(20);
	rep(i, 0, n) cout << fixed << 1.0 * ans[i] / limit << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T    In(T& x){ cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Rect {
  int h, w, d;
  Rect(int h, int w) : h(h), w(w), d(sq(h) + sq(w)) {}
  bool operator<(const Rect& x) const {
    return d == x.d ? h < x.h : d < x.d;
  }
};

int main()
{
  vector<Rect> v;
  int h, w;
  for (int x = 1; x <= 150; x++){
    for (int y = 1; y < x; y++){
      v.push_back(Rect(y, x));
    }
  }
  sort(v);

  while (h = in(), w = in(), h || w){
    Rect r = Rect(h, w);
    int index = ubnd(v, r);
    printf("%d %d\n", v[index].h, v[index].w);
  }
  return (0);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <string>
#include <iterator>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define POF pop_front
#define POB pop_back
#define MA make_pair
#define ll long long
#define VE vector<int>
#define VP vector<pair<int,int> >
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
int N,M,B;
int A[1000];
int C[1000];
int main(){
  cin>>N>>M;
  FOR(i,0,N){
    cin>>A[i];
  }
  FOR(i,0,M){
    cin>>B;
    int j=0;
    while(B<A[j]){
      j++;
    }
    C[j]++;
  }
  int M=0,ans;
  FOR(i,0,N){
    if(M<C[i]){
      M=C[i];
      ans=i+1;
    }
  }
  cout<<ans<<endl;
  return 0;
}

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

int main()
{
   int a , b;
   cin>>a>>b;
int ans =0;
   for(int i=a;i<=b;i++)
   {
       string temp = to_string(i);
       string temp1 = temp;
       reverse(temp1.begin() , temp1.end());
       if( temp == temp1)
           ans++;
   }
cout<<ans;
}


#if 0
date +%Y.%m.%d

题解先咕着。
#endif
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;

struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
	inline operator ll () { ll x; return scanf("%lld", &x), x; }
	template<class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

const int maxn = 200005;
ll f[maxn << 2], fa[maxn << 2], fb[maxn << 2], tag;

void modify(int now, int L, int R, int p, ll x) {
	f[now] = std::min(f[now], x);
	fa[now] = std::min(fa[now], x + p);
	fb[now] = std::min(fb[now], x - p);
	if(L == R) return;
	int M = (L + R) >> 1;
	if(p <= M) modify(now << 1, L, M, p, x);
	else modify(now << 1 | 1, M + 1, R, p, x);
}

void build(int now, int L, int R) {
	f[now] = fa[now] = fb[now] = 1000000000000000000;
	if(L == R) return;
	int M = (L + R) >> 1;
	build(now << 1, L, M);
	build(now << 1 | 1, M + 1, R);
}

ll query(int now, int L, int R, int p) {
	if(R < p) return p + fb[now];
	if(L > p) return fa[now] - p;
	if(L == R) return f[now];
	int M = (L + R) >> 1;
	return std::min(query(now << 1, L, M, p), query(now << 1 | 1, M + 1, R, p));
}

void test(int now, int L, int R) {
	/* debug("%d-%d : %lld (+)%lld (-)%lld\n", */
	/* 		L, R, f[now] + tag, fa[now] + tag, fb[now] + tag); */
	if(L == R) {
		/* if(f[now] >= 1000000000) */
		/* 	debug(" inf"); */
		/* else */
		/* 	debug(" %lld", f[now] + tag); */
		return;
	}
	int M = (L + R) >> 1;
	test(now << 1, L, M);
	test(now << 1 | 1, M + 1, R);
}

int main() {
	int n = read, q = read, a = read, b = read;

	build(1, 1, n);
	modify(1, 1, n, a, 0);
	/* modify(1, 1, n, b, 0); */

	int last = b;
	while(q --) {
		int x = read;
		ll qu = query(1, 1, n, x) + tag;
		/* debug("qu = %lld\n", qu); */
		tag += std::abs(x - last);
		modify(1, 1, n, last, qu - tag);
		last = x;
		/* debug("tag=%lld\n", tag); */
		/* test(1, 1, n); debug("\n"); */
	}

	printf("%lld\n", f[1] + tag);
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define mp make_pair
#define uint unsigned
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){
	int x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(int a){
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(int a){
    if(a<0){
    	a=-a; putchar('-');
	}
	write(a); puts("");
}
const int N=200005;
int n,q,x,y,a[N];
ll sum[N],dp[N],ans;
#define lowbit(i) i&-i
struct BIT{
	ll c[N];
	void change(int pos,ll de){
		for(int i=pos;i<=n;i+=lowbit(i))c[i]=min(c[i],de);
	}
	ll ask(int pos){
		ll ans=4e18;
		for(int i=pos;i;i-=lowbit(i))ans=min(ans,c[i]);
		return ans;
	}
}T[2];
void insert(ll de,int pos){
	T[0].change(pos,de-pos);
	T[1].change(n-pos+1,de+pos);
}
int main(){
	n=read(); q=read(); x=read(); y=read();
	for(int i=1;i<=q;i++)a[i]=read();
	for(int i=2;i<=q;i++)sum[i]=sum[i-1]+abs(a[i-1]-a[i]);
	for(int i=1;i<=n;i++)T[0].c[i]=T[1].c[i]=4e18;
	insert(abs(x-a[1]),y);
	insert(abs(y-a[1]),x);
	ans=sum[q]+min(abs(x-a[1]),abs(y-a[1]));
	for(int i=1;i<q;i++){
		dp[i]=min(T[0].ask(a[i+1])+sum[i]+a[i+1],T[1].ask(n-a[i+1]+1)+sum[i]-a[i+1]);
		//if(i==3)cout<<dp[i]<<" "<<i<<" "<<T[0].ask(a[i+1])<<endl;
		//cout<<ans<<endl;
		ans=min(ans,dp[i]+sum[q]-sum[i+1]);
		insert(dp[i]-sum[i+1],a[i]);
	}
	cout<<ans<<endl;
}
/*
dp[i]=dp[j]+sum[i]-sum[j+1]+abs(a[i+1]-a[j])
*/


#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <unordered_map>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 345;
double dp[MAXN][MAXN][MAXN];
bool visit[MAXN][MAXN][MAXN];

double dfs(int c1, int c2, int c3, int N) {
    if (visit[c1][c2][c3]) return dp[c1][c2][c3];
    visit[c1][c2][c3] = 1;
    if (!c1 && !c2 && !c3) return 0.0;
    int c0 = N - c1 - c2 - c3;
    double res = (double)c0 / N;
    if (c1) res += (dfs(c1 - 1, c2, c3, N) + 1) * ((double)c1 / N);
    if (c2) res += (dfs(c1 + 1, c2 - 1, c3, N) + 1) * ((double)c2 / N);
    if (c3) res += (dfs(c1, c2 + 1, c3 - 1, N) + 1) * ((double)c3 / N);
    return dp[c1][c2][c3] = ((double)N / (N - c0) * res);
}

int main() {
    int N;
    scanf("%d", &N);
    vector<int> cnt(4, 0);
    for (int i = 0; i < N; i++) {
        int a;
        scanf("%d", &a);
        cnt[a]++;
    }
    printf("%.10lf\n", dfs(cnt[1], cnt[2], cnt[3], N));
    return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
by Benq;
*/

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
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; }
typedef long long ll;
using namespace std;
int main(void){
    int n;cin>>n;
    vector<vector<int>> a(n,vector<int>(n-1));
    rep(i,n)rep(j,n-1){
        cin>>a[i][j];
        a[i][j]--;
    }
    int f=0;
    vector<int> t(n,0);
    queue<int> que;
    rep(i,n)que.push(i);
    ll res=0;
    while(true){
        res++;
        queue<int> que2;
        bool used=false;
        vector<bool> ft(n,false);
        while(que.size()){
            int x=que.front();que.pop();
            if(ft[x])continue;
            int op=a[x][t[x]];
            if(ft[op])continue;
            if(a[op][t[op]]==x){
                used=true;
                ft[x]=ft[op]=true;
                t[x]++;t[op]++;
                if(t[x]==n-1)f++;
                else que2.push(x);
                if(t[op]==n-1)f++;
                else que2.push(op);
            }
        }
        if(!used){
            cout<<-1<<endl;
            return 0;
        }
        if(f==n){
            cout<<res<<endl;
            return 0;
        }
        swap(que,que2);
    }
}


#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

vector<string> split_naive(const string &s, char delim) {
	vector<string> elems;
	string item;
	for(char ch:s){
		if(ch==delim){
			if(!item.empty()) elems.push_back(item);
			item.clear();
		}
		else{
			item += ch;
		}
	}
	if(!item.empty()) elems.push_back(item);
	return elems;
}

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

	string S;
	while(cin>>S){
		auto SS=split_naive(S,',');
		vector<int> C(5);
		map<int,int> M;
		REP(i,5){
			sscanf(SS[i].c_str(),"%d",&C[i]);
			M[C[i]]++;
		}
		sort(ALL(C));

		int two=0,three=0,four=0;
		for(auto& x:M){
			if(x.second==2) two++;
			else if(x.second==3) three++;
			else if(x.second==4) four++;
		}
		if(four){
			out("four card");
		}
		else if(three){
			if(two){
				out("full house");
			}
			else{
				out("three card");
			}
		}
		else if(two){
			if(two==2){
				out("two pair");
			}
			else{
				out("one pair");
			}
		}
		else{
			bool straight = true;
			if(C[0]==1){
				if(C[1]==2 || C[1]==10){
					for(int i=1;i<4;i++){
						if(C[i+1]!=C[i]+1){
							straight = false;
							break;
						}
					}
				}
				else{
					straight = false;
				}
			}
			else{
				REP(i,4){
					if(C[i+1]!=C[i]+1){
						straight = false;
						break;
					}
				}
			}
			if(straight){
				out("straight");
			}
			else{
				out("null");
			}
		}
	}
	return 0;
}

#include <iostream>
#include <string>

using namespace std;

#define nullptr  NULL

struct node
{
	int val;
	node* right;
	node* left;
	node(const int& v = 0) : val(v), right(nullptr), left(nullptr)
	{}
};

struct Tree
{
	node* root;
};

node* insert(node* root, int v)
{
	if (!root)
	{
		root = new node(v);
		return root;
	}
	node* temp = root;
	while (1)
	{
		if (root->val > v)
		{
			if (!root->left)
			{
				root->left = new node(v);
				break;
			}
			root = root->left;
		}
		else
		{
			if (!root->right)
			{
				root->right = new node(v);
				break;
			}
			root = root->right;
		}
	}
	return temp;
}

node* remove(node* root, int v)
{
	if (!root)
		return nullptr;
	if (root->val > v)
		root->left = remove(root->left, v);
	else if (root->val < v)
		root->right = remove(root->right, v);
	else
	{
		if (!root->left || !root->right)
		{
			root = (root->left) ? root->left : root->right;
		}
		else
		{
			node* temp = root->right;
			while (temp->left)
				temp = temp->left;
			root->val = temp->val;
			root->right = remove(root->right, temp->val);
		}
	}
	return root;
}

node* find(node* root, int v)
{
	if (!root)
		return nullptr;
	while (root != nullptr && v != root->val)
	{
		if (v < root->val)
			root = root->left;
		else
			root = root->right;
	}
	return root;
}

void preorder(node* root)
{
	if (!root)
		return;
	cout << " " << root->val;
	preorder(root->left);
	preorder(root->right);
}

void inorder(node* root)
{
	if (!root)
		return;
	inorder(root->left);
	cout << " " << root->val;
	inorder(root->right);
}

int main()
{
	int m;
	node* root = nullptr;
	cin >> m;
	for (int i = 0; i < m; i++)
	{
		int k;
		string s;
		cin >> s;
		if (s == "insert")
		{
			cin >> k;
			root = insert(root, k);
		}
		else if (s == "delete")
		{
			cin >> k;
			root = remove(root, k);
		}
		else if (s == "find")
		{
			cin >> k;
			if (find(root, k) != nullptr)
				cout << "yes" << endl;
			else
				cout << "no" << endl;
		}
		else if (s == "print")
		{
			inorder(root);
			cout << endl;
			preorder(root);
			cout << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
// typedef unordered_map<lli,unordered_map<lli,lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

const lli e5 = 1 << 20;
const lli mod = 998244353;
const lli e3 = 1 << 12;

lli n,m,q;
lli s[e3][e3];
lli query[e5][4];
lli v_edge[e3][e3];
lli h_edge[e3][e3];

int main(){
  cin >> n >> m >> q;
  for(lli i = 1;i <= n;i++){
    string hoge;
    cin >> hoge;
    for(lli j = 0;j < m;j++){
      s[i][j+1] = hoge[j]-'0';
    }
  }
  for(lli i = 0;i < q;i++){
    for(lli j = 0;j < 4;j++) cin >> query[i][j];
  }
  for(lli i = 1;i < n;i++){
    for(lli j = 1;j <= m;j++){
      v_edge[i][j] = (s[i][j] == 1 && s[i+1][j] == 1);
    }
  }
  for(lli i = 1;i <= n;i++){
    for(lli j = 1;j < m;j++){
      h_edge[i][j] = (s[i][j] && s[i][j+1] == 1);
    }
  }
  // s
  for(lli i = 1;i <= n;i++){
    for(lli j = 1;j <= m;j++){
      s[i][j] += s[i-1][j];
    }
  }
  for(lli i = 1;i <= n;i++){
    for(lli j = 1;j <= m;j++){
      s[i][j] += s[i][j-1];
    }
  }
  // v_edge
  for(lli i = 1;i < n;i++){
    for(lli j = 1;j <= m;j++){
      v_edge[i][j] += v_edge[i-1][j];
    }
  }
  for(lli i = 1;i < n;i++){
    for(lli j = 1;j <= m;j++){
      v_edge[i][j] += v_edge[i][j-1];
    }
  }
  // h_edge
  for(lli i = 1;i <= n;i++){
    for(lli j = 1;j < m;j++){
      h_edge[i][j] += h_edge[i-1][j];
    }
  }
  for(lli i = 1;i <= n;i++){
    for(lli j = 1;j < m;j++){
      h_edge[i][j] += h_edge[i][j-1];
    }
  }
  for(lli i = 0;i < q;i++){
    lli vertices = s[query[i][2]][query[i][3]]
    -s[query[i][2]][query[i][1]-1]
    -s[query[i][0]-1][query[i][3]]
    +s[query[i][0]-1][query[i][1]-1];
    lli v = v_edge[query[i][2]-1][query[i][3]]
    -v_edge[query[i][2]-1][query[i][1]-1]
    -v_edge[query[i][0]-1][query[i][3]]
    +v_edge[query[i][0]-1][query[i][1]-1];
    lli h = h_edge[query[i][2]][query[i][3]-1]
    -h_edge[query[i][2]][query[i][1]-1]
    -h_edge[query[i][0]-1][query[i][3]-1]
    +h_edge[query[i][0]-1][query[i][1]-1];
    // cerr << vertices << " " << v << " " << h << endl;
    cout << vertices-v-h << endl;
  }

}

/* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define popb pop_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define ull unsigned long long
#define pii pair<int, int>
#define piii pair<int,pii>
#define beg begin
#define rep(i,n) for(int (i) = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define fill(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define int ll
using ll = long long;
const int N = 4e6 +5 ;
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace __gnu_pbds;
using namespace std;
using ordered_set =  tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ;

#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout if(false) cout
#define mycerr if(false) cerr
#endif

const int M = 2005 ;
// l = 0 , r = 1 , u = 2 , d = 3 , no = 4;
int dx[] = {0 , 0 , -1 , 1};
int dy[] = {-1, 1 , 0 , 0};
int ad[5][M][M];
int g[M][M] ;

int ok(int i , int j , int n , int m){
	bool ans1 = (i >= 0 && i < n);
	bool ans2 = (j >= 0 && j < m);
	return ans1 && ans2 ;
}

int sqsum(int k , int lx , int ly , int rx , int ry){
	int res = ad[k][rx][ry];
	if(lx) res -= ad[k][lx-1][ry];
	if(ly) res -= ad[k][rx][ly - 1];
	if(lx && ly) res += ad[k][lx-1][ly-1];
	return res ;
}
void solve()
{
	int n , m , q ;
	cin >> n >> m >> q;
	string s[n] ;
	rep(i , n){
		cin >> s[i] ;
		rep(j , m)
			g[i][j] = s[i][j] - '0';
	}
	rep(i , n) rep(j , m){
		if(g[i][j]){
			rep(k , 4) ad[k][i][j] = (ok(i + dx[k] , j + dy[k] , n , m) && g[i + dx[k]][j + dy[k]]);
		}
		ad[4][i][j] = g[i][j];
	}
	rep(k , 5){
		rep(i , n) rep(j , m){
			if(i) ad[k][i][j] += ad[k][i-1][j] ;
			if(j) ad[k][i][j] += ad[k][i][j-1] ;
			if(i && j) ad[k][i][j] -= ad[k][i-1][j-1];
		}
	}
	while(q--){
		int lx, ly, rx, ry;
		cin >> lx >> ly >> rx >> ry;
		lx--, ly--, rx--, ry-- ;
		int edges = 0;
		rep(k , 4) edges += sqsum(k , lx , ly , rx , ry);
		edges -= sqsum(0 , lx , ly , rx, ly);
		edges -= sqsum(1 , lx, ry, rx, ry);
		edges -= sqsum(2 , lx , ly , lx, ry);
		edges -= sqsum(3 , rx, ly, rx, ry);
		int nodes = sqsum(4, lx, ly, rx, ry);
		// cout << nodes << " " << edges/2 << "\n" ;
		cout << nodes - edges/2 << "\n" ;
	}

}

int32_t main(int32_t argc, char *argv[])
{
    double t1 = clock() ;
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int TC = 1, t = 0;
    //cin >> TC ;
    while(t++ < TC)
    {
        //cout << "Case #" << t << ": " ;
        solve();
        //cout << endl ;
    }
    mycerr << "Time : " << 1000 *(clock() - t1)/CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}


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

#define ASC(vec) vec.begin(), vec.end()    // 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend() // 降順ソート 例：sort(DESC(vec));
#define rep(i, n) for (int i = 0; i < (n); i++)
#define Rep(i, n) for (int i = 1; i < n; i++)
#define REP(i, vec) for (auto i = vec.begin(); i != vec.end(); ++i)

const int       mod = 1000000007;
const int       inf = (1 << 21);
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;
using vll = vector<pair<ll, ll>>;

template<class T>
inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T>
inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T>
inline T GCD(T a, T b) { return b ? GCD(b, a % b) : a; }
template<class T>
inline T LCM(T a, T b) { return (a + (b - 1)) / b; }
template<class T>
inline T round_int(T a, T b) {return (a + (b - 1)) / b; }

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };

// ──────────────────────────────────────────────────────────────────

int dp[101][9];

int main() {
    int h, w, k;
    cin >> h >> w >> k;

    vvi dp(h + 1, vi(w));
    dp[0][0] = 1;

    rep(i, h) {
        rep(j, w) {
            rep(bit, (1 << (w - 1))) {
                bool ok = true;

                rep(k, w - 2) {
                    if ((bit >> k) & 1 && (bit >> (k + 1)) & 1) {
                        ok = false;
                        break;
                    }
                }

                if (ok) {
                    if (j >= 1 && ((bit >> (j - 1)) & 1)) {
                        dp[i + 1][j - 1] += dp[i][j];
                        dp[i + 1][j - 1] %= mod;
                    }
                    else if (j <= w - 2 && ((bit >> j) & 1)) {
                        dp[i + 1][j + 1] += dp[i][j];
                        dp[i + 1][j + 1] %= mod;
                    }
                    else {
                        dp[i + 1][j] += dp[i][j];
                        dp[i + 1][j] %= mod;
                    }
                }
            }
        }
    }
    cout << dp[h][k - 1] << endl;
}

#include <bits/stdc++.h>

using namespace std;
const long long MOD=998244353;
long long fact[200009], inv_fact[200009];

long long modpow(long long a, long long n, long long mod){
  long long ret = 1;
  while(n>0){
    if(n&1)ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

long long modinv(long long a, long long mod){
  return modpow(a, mod-2, mod);
}

long long nCr(long long n, long long r, long long mod){
  return (((fact[n]*inv_fact[n-r])%MOD)*inv_fact[r])%MOD;
}

int main(){
  long long n, m, k;
  cin >>n >> m >> k;
  long long cnt=0;

  fact[0]=1;inv_fact[0]=1;
  for(int i=1;i<=n;i++){
    fact[i]=(fact[i-1]*i)%MOD;
    inv_fact[i] = modinv(fact[i], MOD);
  }
  for(int i=0;i<=k;i++){
    long long lcnt=m;
    lcnt = (lcnt * nCr(n-1, i, MOD))%MOD;
    lcnt = (lcnt * modpow(m-1, n-1-i, MOD))%MOD;
    cnt = (cnt + lcnt)%MOD;
  }
  cout << cnt << endl;
}


#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    while( n-- > 0 ){
        int out = 0, score = 0, base = 0;
        string event;
        while( out < 3 ){
            cin >> event;
            if( event == "OUT" ) out++;
            else if( event == "HIT" ){
                score += base/3;
                if( !(base/3) ) base++;
            }else if( event == "HOMERUN" ){
                score += base+1;
                base = 0;
            }
        }
        cout << score << endl;
    }
    return 0;
}

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

int main(){
	int n;
	cin >> n;
	for(int i=0;i<n;i++){
		int p = 0;
		int oc = 0;
		int rui[4] = {1,0,0,0};
		while(1){
			string s;
			getline(cin,s);
			if(s=="OUT") oc++;
			else if(s=="HIT"){
				if(rui[3]==1) p++;
				for(int j=3;j>0;j--) rui[j] = rui[j-1];
			}
			else if(s=="HOMERUN"){
				for(int j=1;j<4;j++){
					if(rui[j]==1) p++;
					rui[j] = 0;
				}
					p++;
			}
			if(oc==3) break;
		}
		cout << p << endl;
	}
}


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

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

  int mi = sum;
  for (int i = 0; i < n; ++i) mi = min(mi, abs(sum - n * a[i]));

  for (int i = 0; i < n; ++i) {
    if (abs(sum - n * a[i]) == mi) {
      cout << i << '\n';
      return 0;
    }
  }
  return 0;
}

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

int insSort(vector<int> &arr, int g) {
	int	v, cnt;

	cnt = 0;
	for(int i=g; i<arr.size(); ++i) {
		int j;
		v = arr[i];
		for(j=i-g; j>=0 && arr[j]>v; j-=g) {
			arr[j+g] = arr[j];
			++cnt;
		}
		arr[j+g] = v;
	}
	return cnt;
}

int main() {
	int	n, tmp, m, cnt;
	vector<int>	arr, G;

	scanf("%d", &n);
	arr.reserve(n);

	for(int i=0; i<n; ++i) {
		scanf("%d", &tmp);
		arr.push_back(tmp);
	}

	tmp = 4;
	G.push_back(1);
	for(m=1; tmp<n; ++m) {
		G.push_back(tmp);
		tmp = 3 * tmp + 1;
	}

	cnt = 0;
	for(auto it=G.rbegin(); it!=G.rend(); ++it) {
		cnt += insSort(arr, *it);
	}

	printf("%d\n", m);
	for(auto it=G.rbegin(); it!=--(G.rend()); ++it) {
		printf("%d ", *it);
	}
	printf("%d\n", G[0]);
	printf("%d\n", cnt);
	for(auto it=arr.begin(); it!=arr.end(); ++it) {
		printf("%d\n", *it);
	}
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <time.h>
#include <bitset>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
const double PI=acos(-1.0);

typedef long long Int;
typedef long long LL;
typedef unsigned long long UINT;
typedef vector <int> VI;
typedef pair <int, int> PII;
typedef pair <double, double> PDD;

const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL) INF;

const int MAX = 200007;
const int MAX2 = 1000007;
const int LEN = 21;
const int BASE = 1000000000;

const int MOD = 998244353;

int n;
int C[MAX];

int main()
{
  cin >> n;
  FOR (i,0,n)
  {
    int x;
    scanf("%d", &x);
    C[x] ++;

    if (i == 0 && x != 0)
    {
      cout << 0 << endl;
      return 0;
    }
  }

  int tot = 1;

  if (C[0] != 1)
  {
    cout << 0 << endl;
    return 0;
  }

  Int res = 1;

  FOR (i,1,n)
    FOR (j,0,C[i])
      res = res * C[i-1] % MOD;

  cout << res << endl;

  return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll N;
ll a[200001];
ll ans = 0;
ll temparr[200001];
void mergesort(ll left, ll right){
  if(right - left <= 1)
    return;
  ll mid = (left + right+1) / 2;
  mergesort(left, mid);
  mergesort(mid,right);
  ll id1 = left;
  ll id2 = mid;
  ll cnt = 0;
  for(ll i = left; i < right; i++){
    if(id1 == mid){
      temparr[i] = a[id2];
      id2++;
    }else if(id2 == right){
      temparr[i] = a[id1];
      id1++;
    }else{
      if(a[id1] < a[id2]){
	temparr[i] = a[id1];
	id1++;
      }else{
	temparr[i] = a[id2];
	cnt += (mid -  id1);
	id2++;
      }
    }
  }
  for(ll i = left; i < right; i++){
    a[i] = temparr[i];
  }

  //  cout << left << ' ' << right << ' ' << cnt << endl;

  ans += cnt;
}
int main(){
  cin >> N;
  for(ll i = 0; i < N; i++){
    cin >> a[i];
  }

  mergesort(0, N);

  cout << ans << endl;
}


#include<iostream>
#include<vector>
using namespace std;
const int MOD=1000000007;
void add(long long &a,long long b){a+=b;if(a>=MOD)a-=MOD;}

int H,W,K;
long long solve(){
    --K;
    vector<vector<long long> > dp(H+1,vector<long long>(W,0));
    dp[0][0]=1;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            for(int bit=0;bit<1<<(W-1);bit++){
                bool ok=true;
                for(int k=0;k<W-2;k++){
                    if((bit&(1<<k))&&(bit&(1<<(k+1)))) ok=false;
                }
                if(!ok)continue;
                int nj=j;
                if(bit&(1<<j))nj=j+1;
                else if(j>0&&(bit&(1<<(j-1))))nj=j-1;
                add(dp[i+1][nj],dp[i][j]);
            }
        }
    }
    return dp[H][K];
}

int main(){
    while(cin>>H>>W>>K)cout<<solve()<<endl;
}


#include <stdio.h>
#include <string.h>

int main(){

    long long num;
    scanf("%lld", &num);
    char str[100] ={0};
    int i = 0;
    num--;

    while(num >= 0){
         str[i] = num%26 + 'a';
         num /= 26;
        i++;
        num--;
    }
    int len = strlen(str);

    for(int j=len-1;j>=0;j--){
        printf("%c", str[j]);
    }

    return 0;
}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}

const int MAX_N = 610;
int result1[MAX_N][MAX_N];
int result2[MAX_N][MAX_N];

void dfs(const int N, int r, int c, const vector<pair<int,int>>& d, int result[MAX_N][MAX_N], const int val) {
  result[r][c] = val;
  for (auto p : d) {
    const int nr = r + p.first;
    const int nc = c + p.second;
    if (0 <= nr && nr < N && 0 <= nc && nc < N && result[nr][nc] == -1) {
      dfs(N, nr, nc, d, result, 1-val);
    }
  }
}

void solve(int N, int D, int result[MAX_N][MAX_N]) {

  vector<pair<int,int>> d;
  REP(r, N) REP(c, N) {
    if (r * r + c * c == D) {
      d.push_back({r, c});
      if (r > 0) d.push_back({-r, c});
      if (c > 0) d.push_back({r, -c});
      if (r > 0 && c > 0) d.push_back({-r, -c});
    }
  }

  REP(r, N) REP(c, N) if (result[r][c] == -1) {
    dfs(N, r, c, d, result, 0);
  }

  return;

}

int ans[MAX_N][MAX_N];

int main2() {
  int N = nextInt();
  int D1 = nextInt();
  int D2 = nextInt();

  CLR(result1, -1);
  solve(2*N, D1, result1);

  CLR(result2, -1);
  solve(2*N, D2, result2);

  vector<int> sz(4);
  REP(r, 2*N) REP(c, 2*N) {
    ans[r][c] = result1[r][c] * 2 + result2[r][c];
    sz[ ans[r][c] ]++;
  }

  int cnt = 0;
  int mai = distance(sz.begin(), max_element(ALL(sz)));
  REP(r, 2*N) REP(c, 2*N) {
    if (ans[r][c] == mai && cnt < N*N) {
      printf("%d %d\n", r, c);
      cnt++;
    }
  }
  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws)
    main2();
  return 0;
}


#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
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 long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int main(){
	int h,w;
	cin >>h>>w;
	VVi a(h,Vi(w,0));
	int bef;
	vector<pair<pair<int,int>,pair<int,int>>>ans;
	rep(i,h){
		rep(j,w){
			cin>>bef;
			a[i][j]+=bef;
			if(a[i][j]%2){
				if(j!=w-1){
					ans.push_back(mp(mp(i,j),mp(i,j+1)));
					a[i][j+1]++;
					a[i][j]--;
				}else if(i!=h-1){
					ans.push_back(mp(mp(i, j), mp(i+1, j )));
					a[i+1][j ]++;
					a[i][j]--;
				}
			}
		}
	}
	cout<<ans.size()<<endl;
	rep(i,ans.size()){
		printf("%d %d %d %d\n",1+ans[i].F.F,1+ans[i].F.S,1+ans[i].S.F,1+ans[i].S.S);
	}
}

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

char s[1000000];
ll t[3][1000001];

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

  V D, C;
  rep(i, n) {
    rep(j, 3) t[j][i+1] = t[j][i];
    switch(s[i]){
      case 'D':
        ++t[0][i+1];
        D.push_back(i+1);
        break;
      case 'M':
        ++t[1][i+1];
        break;
      case 'C':
        ++t[2][i+1];
        C.push_back(i);
        break;
    }
  }

  ll suma = 0, sumc = 0;
  for (auto &a : D) suma += t[1][a] * t[2][a];
  for (auto &c : C) sumc += t[1][c] * t[0][c];

  rep(_, q) {
    int k;
    scanf("%d", &k);
    ll ans = suma + sumc;
    for (auto &a : D) ans -= t[1][a] * t[2][min(a+k-1, n)];
    for (auto &c : C) ans -= t[1][c] * t[0][max(c-k+1, 0)];
    printf("%lld\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define sf scanf
#define pf printf
#define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
using namespace std;
#define ll long long
const int N=2e5+9;
void slv()
{
    int n;
    cin>> n;
    int a[n+3],b[n+3];
    for(int i=0;i<n;i++) cin >> a[i];
    for(int i=0;i<n;i++) cin >> b[i];
    int l=0,r=n-1;
    reverse(b,b+n);
    for(int i=0;i<n;i++) {
        if(a[i]==b[i]) {
            if(b[l]!=b[i] && a[l]!=b[i]) {
                swap(b[l],b[i]);
                l++;
            } else if(b[r]!=b[i] && a[r]!=b[i]) {
                swap(b[r],b[i]);
                r--;
            } else {
                puts("No");
                return ;
            }
        }
    }
    for(int i=0;i<n;i++) {
        if(a[i]==b[i]) {
            puts("No");
            return ;
        }
    }
    puts("Yes");
    for(int i=0;i<n;i++) cout<<b[i]<< " ";
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    int t;
    t=1;
    int cs=1;
    while (t--)
    {
        //cout<<"Case : "<<cs++<<endl;
        slv();
    }
    return 0;
}

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  int N;
  cin >> N;
  vector <int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int ans = 0;
  bool stop = false;
  while (!stop) {
    for (int i = 0; i < N; i++) {
      if (A[i]%2==0) {
        A[i] = A[i]/2;
      } else {
        stop = true;
        break;
      }
    }
    if (!stop) ans++;
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1000000000;

int main() {FIN

    int t = 0;
    char e;
    vs nm = {"A", "B", "C"};
    vs ply(3);
    vi pos(3, 0);
    cin >> ply[0];
    cin >> ply[1];
    cin >> ply[2];

    while (true) {
		if ((int)ply[t].size() == pos[t]) break;
		e = ply[t][pos[t]];
		pos[t]++;
		t = (e - 'a');
	}
    cout << nm[t] << "\n";

    return 0;
}

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

int main()
{
    int n;
    cin >> n;
    string a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;
    int count = 0;
    rep(i, n) {
        if(a[i] != b[i]) {
            if(a[i] == c[i]) count++;
            else if(b[i] == c[i]) count++;
            else count += 2;
        }
        else {
            if(a[i] != c[i]) count++;
        }
    }
    cout << count << endl;
    return 0;
}


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

#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define rep(i, n) for (int i = 0; i < (n); ++i)

using int64 = uint64_t;
using namespace std;

vector<int64> prime_factor(int64 n) {
    vector<int64> primes;
    for (int64 i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            primes.push_back(i);
            if (n / i != i)
                primes.push_back(n / i);
        }
    }
    sort(primes.begin(), primes.end());
    return primes;
}

int digits(int64 n) {
    int cnt = 0;
    while (n > 0) {
        n /= 10;
        cnt++;
    }
    return cnt;
}

int main() {
    fastIO;
    int64 n;

    cin >> n;
    vector<int64> primes = prime_factor(n);

    int digit = 11;
    int l = 0, r = (int)primes.size() - 1;
    while (l <= r) {
        if (primes.at(l) * primes.at(r) == n) {
            int l_digit = digits(primes.at(l));
            int r_digit = digits(primes.at(r));
            digit = max(l_digit, r_digit);
            r--;
        } else {
            l++;
        }
    }
    cout << digit << endl;
}

#include <iostream>
#include <cmath>

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

    for (int64_t a = std::floor(std::sqrt(n)); a >= 1; --a)
    {
        if (n % a == 0)
        {
            const int64_t b = n / a;
            std::cout << static_cast<int>(std::floor(std::log10(b) + 1)) << std::endl;
            break;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FILL0(x) memset(x,0,sizeof(x))

using namespace std;

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

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

bool cmp(P a, P b)
{
    return a.first < b.first;
}

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

    int N;
    ll M;

    cin >> N >> M;

    if(N==1)
    {
        cout  << M << endl;
        return 0;
    }

    ll ans=0;
    vector<P> div;

    for(ll i=2; i*i<=M; i++)
    {
        ll cnt = 0;
        if(M%i==0)
        {
            cnt=M/i;
            div.push_back(P(i, cnt));
            div.push_back(P(cnt, i));
        }
    }

    sort(div.begin(), div.end(), cmp);
    reverse(div.begin(), div.end());

    rep(i, div.size())
    {
        if(div[i].second>=N)
        {
            cout << div[i].first << endl;
            return 0;
        }
    }

    cout << 1 << endl;

    return 0;

}

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main()
{
  cout << fixed << setprecision(10);
  int N,M;
  cin >> N >> M;
  int n = M/N;
  for(int i=n;i>0;i--)
  {
    if((M-(N-1)*i)%i==0)
    {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}


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

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

int minimam(int a,int b,int c, int d);

int main() {
  int a,b,c,d; cin >> a >> b >> c >> d;
  int num;
  num = minimam(a,b,c,d);
  cout << num << endl;
  return 0;
}
int minimam(int a,int b,int c, int d) {
  return min(a,b)+min(c,d);
}

#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 mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL<<61;
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 h,w,a,b;
    cin >> h>>w>>a>>b;

  rep(i,b){
    rep(j,a) cout << 0;
    rep(j,w-a) cout << 1;
    cout << endl;
  }
  rep(i,h-b){
    rep(j,a) cout << 1;
    rep(j,w-a) cout << 0;
    cout << endl;
  }

}


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

int main() {
  int r;cin>>r;
  string ans;
  if (r >= 2800) ans = "AGC";
  else if (r >= 1200) ans = "ARC";
  else ans = "ABC";
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = LONG_LONG_MAX / 2;
const ll mod = 1000000000 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int N;
  cin >> N;
  for (int i = N; i < 1000; i++) {
    string s = to_string(i);
    if (s[0] == s[1] && s[1] == s[2]) {
      cout << i << endl;
      return 0;
    }
  }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define N 1000006

using namespace std;

int n, ans;
char s[N], t[N];

int main(){
	scanf("%d%s%s", &n, s + 1, t + 1);
	if(strcmp(s + 1, t + 1) == 0){
		puts("0");
		return 0;
	}
	int pos = n;
	queue < int > Q;
	for(int i = n; i; --i){
		if(t[i] == t[i - 1]) continue;
		pos = min(pos, i);
		while(s[pos] != t[i] && pos) pos--;
		if(!pos){
			puts("-1");
			return 0;
		}
		while(!Q.empty()){
			int u = Q.front(), sz = Q.size();
			if(u - sz + 1 > i) Q.pop();
			else break;
		}
		Q.push(pos);
		int sz = Q.size();
		if(i != pos) ans = max(ans, sz);
	}
	cout << ans + 1 << endl;
	return 0;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		vector<long long int> ss;
		ss.push_back(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.push_back(b);
			}
		}
		vector<long long int> ss2;
		for (long long int i = 0; i < ss.size(); i++) {
			ss2.push_back(ss[i]);
			for (long long int j = i; j < ss.size(); j++) {
				if (ss[i]+ss[j] <= m) {
					ss2.push_back(ss[i]+ss[j]);
				}
			}
		}
		sort(ss2.begin(), ss2.end());
		/*for (long long int i = 0; i < ss2.size(); i++) {
			cout << ss2[i] << endl;
		}*/
		long long int MAX = 0;
		long long int sz = ss2.size();
		for (long long int j = 0; j < sz; j++) {
			long long int a = m;
			for (long long int i = 0; i < 2; i++) {
				vector<long long int>::iterator ite = upper_bound(ss2.begin(), ss2.end(), a);
				if (ite != ss.begin()) {
					ite--;
					a -= *(ite);
				}
			}
			vector<long long int>::iterator ite2 = ss2.end();
			ss2.erase(ite2-1);
			MAX = max(MAX, m-a);
		}
		cout << MAX << endl;
	}
	return 0;
}


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

int main() {
  ll x, y;
  cin >> x >> y;
  ll absdif = abs(abs(x) - abs(y)), ans = 1e10;
  if (x <= y) ans = min(ans, y - x);                     // 0,0
  if (-1 * x <= y) ans = min(ans, y + x + 1);            // 1,0
  if (x <= -1 * y) ans = min(ans, -1 * y - x + 1);       // 0,1
  if (-1 * x <= -1 * y) ans = min(ans, -1 * y + x + 2);  // 1,1
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mod=1e9+7;
const int N=1e6+10;
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
void read(ll &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0') if(ch=='-') d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
int main()
{
    int n,x,t;
    read(n),read(x),read(t);
    if(n%x==0) printf("%d\n",n/x*t);
    else printf("%d\n",(n/x+1)*t);
    return 0;
}


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

// 小数点以下桁数
// fixed << setprecision(i)

int64_t gcd(int64_t a,int64_t b){return (a%b==0?b:gcd(b, a%b));}
int64_t lcm(int64_t a,int64_t b){return a/gcd(a, b)*b;}
int factorial(int a){int b=1;while(a){b*=a--;}return b;} //13以下対応
bool is_prime(int64_t a){if(a<=1)return false;for(int64_t i=2;(int64_t)i*i<=a;i++){if(a%i==0)return false;}return true;}
int get_adp(double x, int n){return (int)round((x-(int)x)*pow(10,n))%10;} // 小数点以下の指定桁の値を取得
int64_t sigma(int64_t s, int64_t n){return n*(2*s+n-1)/2;}

//順列の全列挙
// vector<int> v(N);
// iota(v.begin(), v.end(), 1);       // v に 1, 2, ... N を設定
// do {
//	for(auto x : v) cout << x << " "; cout << "\n";     // v の要素を表示
// } while( next_permutation(v.begin(), v.end()) );

//bit全探索
// for (int tmp = 0; tmp < (1 << ビット数); tmp++) {
//	bitset<ビット数> s(tmp);
//	// (ビット列sに対する処理)
// }

int main()
{
	int n, k;
	double ans, temp = 0;

	cin >> n >> k;
	vector<int64_t> p(n);
	vector<double> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> p[i];
		a[i] = (double)sigma(1, p[i]) / p[i];
	}
	for (int i = 0; i < k; i++)
		temp += a[i];
	ans = temp;
	for (int i = k; i < n; i++)
	{
		temp += a[i] - a[i - k];
		ans = max(ans, temp);
	}
	cout << fixed << setprecision(6) << ans << endl;
}

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

int main()
{
	ll h,w;
	cin >> h >> w;
	vector<string> s(h);
	for(auto &i:s)cin >> i;
	vector<vector<ll>> dis(h,vector<ll>(w,1e9));

	for(int i = 0;i<h;i++)
	{
		for(int j = 0;j<w;j++)
		{
			if(s[i][j]=='#')dis[i][j]=0;
		}
	}

	for(int i = 0;i<h;i++)
	{
		for(int j = 1;j<w;j++)
		{
			dis[i][j] = min(dis[i][j],dis[i][j-1]+1);
		}
	}

	for(int i = 0;i<h;i++)
	{
		for(int j = w-2;j>=0;j--)
		{
			dis[i][j] = min(dis[i][j],dis[i][j+1]+1);
		}
	}
	for(int i = 1;i<h;i++)
	{
		for(int j = 0;j<w;j++)
		{
			dis[i][j] = min(dis[i][j],dis[i-1][j]+1);
		}
	}

	for(int i = h-2;i>=0;i--)
	{
		for(int j = 0;j<w;j++)
		{
			dis[i][j] = min(dis[i][j],dis[i+1][j]+1);
		}
	}
	ll ans = 0;
	for(int i = 0;i<h;i++)for(int j = 0;j<w;j++)ans = max(ans,dis[i][j]);
	cout << ans << endl;

}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
int main() {
	int n, k;
	cin >> n >> k;
	bool ans = n % k;
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  if (n%k==0) {
    printf("0\n");
  } else {
    printf("1\n");
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using vl = vector<ll>;

int main() {
  ll n;
  cin >> n;
  vl x(n);
  rep(i, n) cin >> x[i];
  ll l, q;
  cin >> l >> q;
  vector<vl> r(20, vl(n));
  rep(i, n) r[0][i] = upper_bound(all(x), x[i] + l) - x.begin() - 1;
  rep(k, 19) rep(i, n) r[k + 1][i] = r[k][r[k][i]];
  rep(_, q) {
    ll a, b;
    cin >> a >> b;
    if(a > b) swap(a, b);
    a--;
    b--;
    ll ans = 1;
    for(ll k = 19; k >= 0; k--)
      if(r[k][a] < b) {
        a = r[k][a];
        ans += 1 << k;
      }
    cout << ans << '\n';
  }
}

#include <bits/stdc++.h>

using namespace std;

int main() {

    int n, m, v;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    char d;
    char ds[] = "NESW";

    while(cin >> n, n) {

        int rx = 10, ry = 10, cnt = 0;
        int graph[21][21];

        memset(graph, 0, sizeof(graph));

        for(int i = 0, x, y; i < n; ++i) {
            cin >> x >> y;
            graph[y][x] = 1;
        }

        cin >> m;

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

            cin >> d >> v;

            for(int j = 0; j < 4; ++j) {
                if(d == ds[j]) {
                    for(int k = 1; k <= v; ++k) {
                        rx += dx[j];
                        ry += dy[j];
                        if(graph[ry][rx] == 1) {
                            graph[ry][rx] = 0;
                            ++cnt;
                        }
                    }
                    break;
                }
            }

        }

        cout << (cnt == n ? "Yes" : "No") << endl;

    }

}


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

int main(){
    int n;cin>>n;
    string a,b,c;cin>>a>>b>>c;
    int acc = 0;
    for (int i=0;i<n;i++){
        vector<char> letters;
        letters.push_back(a[i]);
        letters.push_back(b[i]);
        letters.push_back(c[i]);

        sort(letters.begin(), letters.end());
        letters.erase(unique(letters.begin(), letters.end()), letters.end());
        if (letters.size() == 2){
            acc++;
        } else if (letters.size() == 3){
            acc += 2;
        }
    }
    cout << acc << endl;
    return 0;

}

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

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

const int MOD_TYPE = 1;
const ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ld PI = acos(-1.0);
const ld EPS = 1e-11;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);
  int n;
  cin >> n;
  string s[3];
  rep(i, 3) cin >> s[i];
  int cnt = 0;
  rep(i, n)
  {
    if (s[0][i] == s[1][i] && s[1][i] == s[2][i])
      continue;
    if (s[0][i] != s[1][i] && s[1][i] != s[2][i] && s[0][i] != s[2][i])
      cnt += 2;
    else
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}


#include<bits/stdc++.h>
#define int long long

using namespace std;

const int maxn = 200010;
int n, ans;

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

inline void write(int x)
{
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) putchar(sta[-- top] + 48);
}

signed main()
{
    n = read();
    ans = (n - 2) * 180;
    write(ans);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int a, int b) {
  return b != 0 ? gcd(b, a % b) : a;
}//MAX
int lcm(int a, int b) {
   return a * b / gcd(a, b);
   }
//MIN

 main() {
int n;
cin >> n;
cout << 180*(n-2)<<endl;
}