#include <bits/stdc++.h>

#define fi first
#define se second
#define DB double
#define U unsigned
#define P std::pair
#define LL long long
#define LD long double
#define pb emplace_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 5000+5;
const int ha = 998244353;
int n,m;

inline void add(int &x,int y){
    x += y-ha;x += x>>31&ha;
}

int pw[MAXN],pw1[MAXN];

int main(){
    scanf("%d%d",&n,&m);
    if(m == 1){
        puts("1");
        return 0;
    }
    pw[0] = 1;FOR(i,1,MAXN-1) pw[i] = 1ll*pw[i-1]*m%ha;
    pw1[0] = 1;FOR(i,1,MAXN-1) pw1[i] = 1ll*pw1[i-1]*(m-1)%ha;
    int ans = 0;
    FOR(x,2,m){
        int c = 1;
        FOR(k,2,n-1){
            c = 1ll*c*(m-x)%ha;
            int d = (pw1[k-1]+ha-c)%ha;
            add(ans,1ll*d*(n-k)%ha*pw[n-(k+1)]%ha);
        }
    }
    FOR(i,1,n) add(ans,1ll*pw1[i-1]*pw[n-i+1]%ha);
    printf("%d\n",ans);
    return 0;
}

// 解き直し.
// https://atcoder.jp/contests/arc105/editorial/170
// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
LL cW[11], bL[101010], bV[101010];

struct bridge{
    LL l, v; // 長さ, 耐荷重.
    bool operator < (const bridge& rhs) const{
        return (v != rhs.v) ? (v > rhs.v) : (l < rhs.l);
    }
};

int main(){

    // 1. 入力情報.
    int N, M;
    scanf("%d %d", &N, &M);
    LL maxW = 0, minV = 202020202020202020;
    rep(i, N){
        scanf("%lld", &cW[i]);
        maxW = max(maxW, cW[i]);
    }
    priority_queue<bridge> pq;
    rep(i, M){
        LL l, v;
        scanf("%lld %lld", &l, &v);
        minV = min(minV, v);
        pq.push({l, v});
    }

    // 2. 橋が崩落するパターンを除外.
    if(maxW > minV){
        puts("-1");
        return 0;
    }

    // 3. 橋の耐荷重に対する必要な最小間隔を保存.
    int idx = 0;
    while(!pq.empty()){
        bridge b = pq.top();
        pq.pop();
        // printf("b.l=%lld b.v=%lld\n", b.l, b.v);
        bL[idx + 1] = max(bL[idx], b.l);
        bV[idx + 1] = b.v;
        idx++;
    }
    // rep(i, M + 1) printf("l=%lld v=%lld\n", bL[i], bV[i]);

    // 4. ラクダの隊列を全探索.
    vector<int> z(N);
    iota(all(z), 0);
    LL ans = -202020202020202020;
    do{
        // 4-1. ラクダの隊列の重量(累積和).
        LL cWCum[N + 1];
        memset(cWCum, 0LL, sizeof(cWCum));
        rep(i, N) cWCum[i + 1] = cWCum[i] + cW[z[i]];
        // rep(i, N) printf("%d ", z[i]);
        // puts("");
        // rep(i, N + 1) printf("%lld ", cWCum[i]);
        // puts("");

        // 4-2. 制約.
        LL x[N][N];
        memset(x, 0LL, sizeof(x));
        rep(i, N){
            repx(j, i + 1, N){
                int at = lower_bound(bV, bV + M + 1, cWCum[j + 1] - cWCum[i]) - bV - 1;
                x[i][j] = max(x[i][j], bL[at]);
                x[j][i] = x[i][j];
            }
        }
        // rep(i, N){
        //     repx(j, i + 1, N) printf("%lld ", x[i][j]);
        //     puts("");
        // }

        // 4-3. グラフの最長路の長さは？
        // ex.
        // AtCoder Beginner Contest 022 (C - Blue Bird) を ベース に 実装.
        // https://atcoder.jp/contests/abc022/tasks/abc022_c
        // -> 具体的には, Warshall–Floyd法で,
        // repx(k, 1, N) repx(i, 1, N) repx(j, 1, N) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        // とあるものを, i = 0 に 固定するイメージで, 更新する形に変更.
        LL g[N][N], way = 0;
        memset(g, 0LL, sizeof(g));
        repx(i, 1, N){
            rep(j, i + 1){
                g[0][i] = min(g[0][i], g[0][j] - x[i][j]);
                way = min(way, g[0][i]);
                // printf("i=%d j=%d %lld \n", i, j, x[i][j]);
            }
        }

        // 4-4. 最長路の長さについて, 最小値を更新.
        ans = max(ans, way);
    }while(next_permutation(all(z)));

    // 5. 出力.
    printf("%lld\n", -ans);
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 500010, mod = 998244353;

int a[3];

int main() {

    string s1, s2;
    int d1 = 0, d2 = 0;
    int n;
    cin >> n;
    while (n --) {
        string s;
        int h;
        cin >> s >> h;
        if(h >= d1) d2 = d1, s2 = s1, d1 = h, s1 = s;
        else if (h > d2)    d2 = h, s2 = s;
    }
    cout << s2 << endl;

    return 0;
}

#define LOCAL0
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define debug(msg) cout << (msg) << endl
#define FOR(i, a, b) for (ll i=a; i<=(b); i++)
#define ROF(i, a, b) for (ll i=a; i>=(b); i--)
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sz(x) (ll)x.size()
#define JUDGE(x) if(x) cout << "Yes\n"; else cout << "No\n";
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

int main()
{
	#ifdef LOCAL
		freopen("in.txt", "r", stdin);
    	freopen("out.txt", "w", stdout);
	#endif
	int T;
	cin >> T;
	while(T--){
		ll l, r;
		cin >> l >> r;
		if(r-l<l) cout << 0 << endl;
		else cout << (r-l+1)*(r-2*l+2)-(r-2*l+2)*(r+1)/2 << endl;;
	}
    return 0;
}


//vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
//includeとusing
#include <bits/stdc++.h>
using namespace std;
//型名省略
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using P = pair<T,T>;
template <class T> using M = map<T,T>;
template <class T> using S = set<T>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T,V<T>,greater<T>>;
using ll = long long;
using st = string;
using vl = V<ll>;
using vvl = V<V<ll>>;
using vc = V<char>;
using vvc = V<V<char>>;
using vs = V<st>;
using vvs = V<V<st>>;
using vb = V<bool>;
using vvb = V<V<bool>>;
//定数
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
//マクロ
#define re(n) for(ll _ = 0; _ < (ll) n; _++)
#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 repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define rrepp(i,x,n) for(ll i = (ll) n - 1; i >= x; i--)
#define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++)
#define each(x,A) for(auto &(x): (A))
#define ALL(A) A.begin(), A.end()
#define SIZE(A) ll(A.size())
#define pb(a) push_back(a)
#define mp make_pair
#define PRECISE_COUT cout << setprecision(15) << fixed
//入力
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){ cin>>head; CINT(move(tail)...);}
#define CIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define DCIN(...) double __VA_ARGS__;CINT(__VA_ARGS__)
#define VCIN(A) for(auto &a : (A)) cin >> a
//出力
#define COUT(a) cout << a << endl
#define TEST(i,a) cout << (ll) i << " : " << (ll) a << endl
#define vout(A) for(auto &a : (A)) cout << a << " ";cout << endl
//正誤判定
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
//余り切り上げ
ll ceil(ll a,ll b) {return (a + b - 1) / b;}
//最大値・最小値
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b; return true;} return false;}
template <typename T> bool chmin(T &a, const T& b) {if(a > b) {a = b; return true;} return false;}
//最大公約数
ll gcd(ll a,ll b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
//最小公倍数
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
//n乗の余り
ll modpow(ll x,ll n,ll mod){ll res=1;for(ll i=0;i<n;i++){res=res*x%mod;}return res;}
ll MODpow(ll x,ll n){ll res=1;for(ll i=0;i<n;i++){res=res*x%MOD;}return res;}
//グリッドグラフの範囲外エラー
bool range(ll ny, ll nx, ll H, ll W){if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;}

/*----------------------------------------------------------------------*/

//main関数
int main() {
    CIN(s,p);
    rep(i,sqrt(p)+1){
        if(i * (s-i) == p){
            COUT("Yes");
            return 0;
        }
    }
    COUT("No");
}

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <iomanip>
#define INF 100000000
#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  s,p;
    cin >> s>>p;
    for(ll i=1;i<1000007;i++){
        if(p%i==0){
            if((p/i)==(s-i)){
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

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


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 300100
#define MOD 998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
	Setup_io() {
		ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
		cout << fixed << setprecision(15);
	}
} setup_io;
int mp[5010];

int main() {
	ll dp[10000];
	ll dp2[10000];
	vector<pair<pii, int> >pp;
    int x, y;
	int n, m, k;
	ll z, s;
	ll lq[N];
	ll rq;
	rep(i, N)lq[i] = 1;
	char c[3];
	cin >> n >> m >> k;
	rep(i, k) {
		rep(j, 3)c[j] = 0;
		cin >> x >> y;
		cin >> c;
		if (c[0] == 'R')pp.pb({ {x - 1,y - 1},1 });
		if (c[0] == 'D')pp.pb({ {x - 1,y - 1},2 });
		if (c[0] == 'X')pp.pb({ {x - 1,y - 1},3 });
	}
	int idx = 0;
	sort(all(pp), greater<pair<pii, int> >());
	z = 1;
	rep(j, m)mp[j] = 0;
	while (idx < k) {
		if (pp[idx].first.first < n - 1)break;
		else mp[pp[idx].first.second] = pp[idx].second;
		idx++;
	}
	rep3(j, m - 1, 0) {
		if (j == m - 1) {
			if (mp[j] == 0) {
				z = (z * 3) % MOD;
				lq[j] = (lq[j] * 3) % MOD;
			}
		}
		else {
			if (mp[j] == 2)z = 0;
			if (mp[j] == 0) {
				z = (z * 2) % MOD;
				lq[j] = (lq[j] * 3) % MOD;
			}
		}
		dp[j] = z;
	}
	rep(j, m)dp2[j] = dp[j];
	rep3(i, n - 2, 0) {
		rq = 1;
		rep(j, m)mp[j] = 0;
		while (idx < k) {
			if (pp[idx].first.first < i)break;
			else mp[pp[idx].first.second] = pp[idx].second;
			idx++;
		}
		rep3(j, m - 1, 0) {
			if (j == m - 1) {
				if (mp[j] == 0) {
					dp[j] = (dp2[j] * 2) % MOD;
					rq = (rq * 3) % MOD;
					lq[j] = (lq[j] * 3) % MOD;
				}
				if (mp[j] == 1)dp[j] = 0;
				if (mp[j] == 2)dp[j] = dp2[j];
				if (mp[j] == 3)dp[j] = dp2[j];
			}
			else {
				if (mp[j] == 0) {
					dp[j] = (lq[j] * dp[j + 1]) % MOD;;
					z = (rq*dp2[j]) % MOD;
					dp[j] = ((dp[j] + z) * 2) % MOD;
					rq = (rq * 3) % MOD;
					lq[j] = (lq[j] * 3) % MOD;
				}
				if (mp[j] == 1)dp[j] = (lq[j] * dp[j + 1]) % MOD;
				if (mp[j] == 2)dp[j] = (rq*dp2[j]) % MOD;
				if (mp[j] == 3) {
					dp[j] = (lq[j] * dp[j + 1]) % MOD;;
					z = (rq*dp2[j]) % MOD;
					dp[j] = (dp[j] + z) % MOD;
				}
			}
		}
		rep(j, m) dp2[j] = dp[j];
	}
	cout << dp2[0] << endl;
	return 0;
}

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

const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

char a[5005][5005];
mint dp[5005][5005];

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

  for(int i = 0; i < k; i++){
    int A, B; char ch;
    cin >> A >> B >> ch;
    A--; B--;
    a[A][B] = ch;
  }

  dp[0][0] = 1;
  mint t = mint(3).inv();
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(a[i][j]=='R') dp[i][j+1] += dp[i][j];
      if(a[i][j]=='D') dp[i+1][j] += dp[i][j];
      if(a[i][j]=='X'){
        dp[i][j+1] += dp[i][j];
        dp[i+1][j] += dp[i][j];
      }
      if(a[i][j]=='\0'){
        dp[i][j+1] += dp[i][j]*mint(2)*t;
        dp[i+1][j] += dp[i][j]*mint(2)*t;
      }
    }
  }
  mint ans = dp[h-1][w-1];
  ans *= mint(3).pow(h*w-k);
  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a1,a2,a3;
	cin>>a1>>a2>>a3;
	int t;
	if(a1>a2)
	{
		t=a1;
		a1=a2;
		a2=t;
	}
	if(a2>a3)
	{
		t=a2;
		a2=a3;
		a3=t;
	}
	if(a1>a2)
	{
		t=a1;
		a1=a2;
		a2=t;
	}
	if(a3-a2==a2-a1) cout<<"Yes";
	else cout<<"No";
}

#include<iostream>
#include<string>
using namespace std;
main()
{
	int n,x,a=0,b=0;
	string s;
	cin>>n>>x;
	cin>>s;
	for(int i=0; i<n; i++)
	{
		if(s[i]=='o')
			{
				x++;
			}
		else if(x>0)
		{
			x--;
		}
	}
   cout<<x<<endl;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1009;
struct Edges
{
	Edges(){}
	Edges(int x, char c):x(x), c(c){}
	int x;
	char c;
};
struct Vertices
{
	Vertices(){}
	Vertices(int x, int y, int d):x(x), y(y), d(d){}
	int x, y, d;
	bool operator < (const Vertices&a)
	const
	{
		return d > a.d;
	}
};
int n, m, d[N][N];
vector<Edges> g[N];
priority_queue<Vertices> q;
template<typename T>
T read()
{
	T num = 0; char c; bool flag = 0;
	while((c=getchar())==' ' || c=='\n' || c=='\r');
	if(c=='-') flag = 1; else num = c - '0';
	while(isdigit(c=getchar()))
			num = num * 10 + c - '0';
	return flag ? -num : num;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	n = read<int>(); m = read<int>();
	for(int i = 1; i <= m; i++)
	{
		int x = read<int>(), y = read<int>();
		char c;
		scanf("%c", &c);
		g[x].push_back(Edges(y, c));
		g[y].push_back(Edges(x, c));
	}
	memset(d, 0x3f, sizeof(d));
	q.push(Vertices(1, n, 0));
	d[1][n] = d[n][1] = 0;
	int ans = 2147483647;
	while(!q.empty())
	{
		Vertices now = q.top(); q.pop();
		for(int i = 0; i < g[now.x].size(); i++)
			for(int j = 0; j < g[now.y].size(); j++)
			{
				int x = g[now.x][i].x;
				int y = g[now.y][j].x;
				if(g[now.x][i].c!=g[now.y][j].c) continue;
				if((x==now.y) || (y==now.x))
					ans = min(ans, now.d+1);
				if(x==y)
					ans = min(ans, now.d+2);
				if(now.d+2>=d[x][y]) continue;
				d[x][y] = now.d + 2;
				d[y][x] = now.d + 2;
				q.push(Vertices(x, y, now.d+2));
			}
	}
	if(ans<2147483647)
		printf("%d\n", ans);
	else puts("-1");
	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 pii = pair<int, int>;

int n, m;
vector<ll> a, b;
vector<vector<int>> g;
vector<bool> check;

ll dfs(int v) {
	check[v] = true;
	ll ret = b[v] - a[v];
	for (auto nv : g[v]) {
		if (check[nv]) continue;
		ret += dfs(nv);
	}
	return ret;
}

int main() {
	cin >> n >> m;
	a.resize(n);
	b.resize(n);
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];
	g.resize(n);
	rep(i, m) {
		int c, d;
		cin >> c >> d;
		c--, d--;
		g[c].push_back(d);
		g[d].push_back(c);
	}
	check.resize(n);
	bool ok = true;
	for (int i = 0; i < n; i++) {
		if (check[i]) continue;
		if (dfs(i) != 0) ok = false;
	}
	if (ok) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi acos(-1.0)
const int inf = 1e9;
signed main() {

	int h, w, x, y;
	cin >> h >> w >> x >> y;
	string a[h + 1];
	for (int i = 1; i <= h; i++) {
		cin >> a[i];
		a[i] = '*' + a[i];
	}
	if (a[x][y] == '#') {
		cout << 0;
		return 0;
	}
	int ans = 0;
	{
		int i = x, j = y;
		int l = 0;
		while (j - l - 1 >= 1 && a[i][j - l - 1] == '.')
			l++;
		ans += l;
	}
	{
		int i = x, j = y;
		int l = 0;
		while (i - l - 1 >= 1 && a[i - l - 1][j] == '.')
			l++;
		ans += l;
	}
	{
		int i = x, j = y;
		int l = 0;
		while (j + l + 1 <= w && a[i][j + l + 1] == '.')
			l++;
		ans += l;
	}
	{
		int i = x, j = y;
		int l = 0;
		while (i + l + 1 <= h && a[i + l + 1][j] == '.')
			l++;
		ans += l;
	}
	cout << ans + 1;
}

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=1e5+5;
int n,m,k;
bool st[N];
pair<DB,DB> f[N],dp[N<<1];
int main() {
    //freopen("E:/OneDrive/IO/in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m>>k;
    for(int i=1;i<=k;i++) {
        int x;
        cin>>x;
        st[x]=true;
    }
    for(int i=n;~i;i--) {
        f[i].FI=f[i+1].FI-dp[i+1+m].FI+dp[i+1].FI;
        f[i].SE=f[i+1].SE-dp[i+1+m].SE+dp[i+1].SE;
        if(st[i]) dp[i]=MP(1,0);
        else {
            if(i<n)
            dp[i].FI=f[i].FI/m,dp[i].SE=f[i].SE/m+1;
        }
    }
    if(fabs(1-dp[0].FI)<1e-6) cout<<-1<<'\n';
    else cout<<fixed<<setprecision(4)<<dp[0].SE/(1-dp[0].FI)<<'\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
#define endl "\n"
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define TC(t) while (t--)
void OPEN(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}
bool isfirst(string s) {
    return (s[0] == '!');
}
string clear(string s) {
    if (s[0] == '!') s.erase(s.begin());
    return s;
}
string ans(vector <string> a) {
    set <string> s;
    for (string t : a) s.insert(t);
    for (auto it=s.begin();it!=s.end();it++) {
        string k = *it;
        if (isfirst(k)) {
            string e = clear(k);
            if (s.count(e) == 1) return e;
        }
    }
    return "satisfiable";
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <string> a;
    FOR(i,0,n) {
        string temp;
        cin >> temp;
        a.pb(temp);
    }
    cout << ans(a);
}

/*** In the name of Allah(swt), the most gracious, most merciful.***/
/*** Alhamdulillah for Everything ***/

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

typedef bool                   boo;
typedef int                    li;
typedef long                   il;
typedef unsigned long          ul;
typedef long long int          ll;
typedef unsigned long long     ull;
typedef double                 dd;
typedef string                 str;

#define Test                   ll t; std :: cin >> t; while(t--)
#define input(a , n)           for( int i = 0 ; i < n ; i++ ) std :: cin >> a[i];
#define lp(a , i , b)          for( ll i = a ; i < b ; i++ )
#define rlp(a , i , b)         for( ll i = a; i >= b ; i-- )
#define sz(x)                  x.size()
#define len(z)                 z.begin() , z.end()
#define ci(x)                  std :: cin >> x;
#define co(x)                  std :: cout << x nl;
#define fix(x)                 fixed << setprecision(x)
#define mem(z , l)             memset( z , l , sizeof(z) )
#define MP                     make_pair
#define pb                     push_back
#define F                      first
#define S                      second
#define nl                     << endl;
#define nll                    std :: cout << endl;
#define cy                     std :: cout << "Yes" << endl;
#define cn                     std :: cout << "No" << endl;
#define rn                     return;
#define Good_Bye               return 0;
#define gcd(a , b)             __gcd( a , b )
#define lcm(a , b)             ( a * ( b / gcd( a , b ) ) )
#define Faster                 ios_base :: sync_with_stdio( 0 ); cin.tie( 0 ); cout.tie( 0 );

const double eps =             1e-9;
const int inf =                2000000000;
const ll infLL =               9000000000000000000;
const ll MOD =                 1e+7;
const double PI =              3.141592653589793238462643383279;
//int dx[]={-1,1,0},dy[]={-1,1,0}
///_____________________________________________ L E T ' S  B E G I N ____________________________________________________________
int r,c,ans(0);
string s[11];
int main()
{
    /*#ifndef ONLINE_JUDGE
    freopen( "input.txt" , "r" , stdin );
    freopen( "output.txt" , "w" , stdout );
    #endif */

    Faster
    cin>>r>>c;
    lp(0,i,r)
    cin>>s[i];
    lp(0,i,r-1)
    {
        lp(0,j,c-1)
        {
            int f(0);
            lp(0,a,2)
            {
                lp(0,b,2)
                {
                    if(s[i+a][j+b]=='#')
                        ++f;
                }
            }
            if(f%2)
            ++ans;
        }
    }
    cout<<ans<<endl;
    Good_Bye
}
//                                                                                author : Md. Nahid Chowdhury(IIUC_CSE48th Batch)
///||||||||||||||||||||||||||||||||||||||||||||||| E N D ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
int A[100];
int main(){
  int k;
  int res;
  int opt = -1;
  int tmp;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(k=(2);k<(1001);k++){
    int i;
    tmp = 0;
    for(i=(0);i<(N);i++){
      if(A[i]%k==0){
        tmp++;
      }
    }
    if(opt < tmp){
      opt = tmp;
      res = k;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20201102-1

// --- original code ---
// int N, A[100];
// {
//   int res, opt = -1, tmp;
//   rd(N,A(N));
//   rep(k,2,1001){
//     tmp = 0;
//     rep(i,N) if(A[i]%k==0) tmp++;
//     if(opt < tmp) opt = tmp, res = k;
//   }
//   wt(res);
// }

#include<iostream>
#include <bits/stdc++.h>
#include<algorithm>
#include <cstring>
#include <string.h>
#define endl "\n"
#define SRK ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define EPS 1e-6
#define MAX 100000
typedef long long ll;
const ll MOD = 1e9;
using namespace std;

int freq[1005];

bool comparison(pair<ll, ll> a,pair<ll, ll> b)
{
    if (a.first == b.first)
    {
        return a.second > b.second;
    }
    else
    {
        return a.first < b.first;
    }
}

void divisors(int a)
{
    for (int i = 1; i * i <= a; i++)
    {
        if (a % i == 0)
        {
            freq[i]++;
            int j = a / i;
            if (i != j)
            {
                freq[j]++;
            }
        }
    }
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        divisors(x);
    }
    freq[1] = 0;
    int high = *max_element(freq,freq+1005);
    for (int i = 0; i < 1005; i++)
    {
        if (freq[i] == high)
        {
            cout << i << endl;
            break;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pint pair<int,int>
#define Pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000;
constexpr ll llINF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
ll gcd(ll a,ll b){
    if(b==0)return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
ll modpow(ll a,ll b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    ll memo=modpow(a,b/2);
    return memo*memo%mod;
}
vector<ll>kaijo,invkaijo;
void init_fact(int n){
    kaijo.resize(n+1);
    invkaijo.resize(n+1);
    kaijo[0]=1;
    for(ll i=1;i<=n;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
    rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2);
}
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0||b<0)return 0;
    return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod;
}
ll inv(ll x){
    x=modpow(x,mod-2);
    return x;
}
int dp[100010];
void solve(){
    int n;
    cin>>n;
    vector<ll>a(n),b(n),c,d;
    rep(i,n)cin>>a[i];
    rep(i,n)cin>>b[i];
    ll ans=0;
    for(int i=0;i<n;i+=2){
        ans+=max(a[i]+a[i+1],b[i+1]+b[i]);
        if(a[i]+a[i+1]>=b[i]+b[i+1]){
            c.pb(b[i]-a[i]);
            d.pb(b[i+1]-a[i+1]);
        }
        else{
            c.pb(a[i+1]-b[i+1]);
            d.pb(a[i]-b[i]);
        }
    }
    sort(all(d));
    sort(all(c));
    rep(i,c.size()){
        if(c[i]+d[i]>=0)ans+=c[i]+d[i];
    }
    cout<<ans<<endl;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    //int t;cin>>t;while(t--)
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long LL;
typedef pair <int, int> P;
const int N = 2e5 + 5;

int n, a[N], b[N], c[N];
LL ans, res;

int main() {
	cin>>n;
	rep (i, 1, n) scanf("%d", &a[i]), ans += a[i];
	rep (i, 1, n) scanf("%d", &b[i]), b[i] -= a[i];
	rep (i, 1, n) {
		if (i&1) b[(i + 1)>>1] = b[i];
		else c[i>>1] = b[i];
	}
	sort(b + 1, b + n/2 + 1);
	sort(c + 1, c + n/2 + 1);
	res = ans;
	per (i, n/2, 1) res += b[i] + c[i], ans = max(ans, res);
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define N 200005
#define INF 1000000005
#define INFLL 2e18
#define PI 3.1415926535897
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) int(x.size())
#define IN(a) cin>>(a)
#define OUT(a) cout<<(a)<<endl
typedef pair<int,int> P;
const int MAX = (1<<20);
const int MOD = 1000000007;

int par[MAX];
int depth[MAX];
int siz[MAX];
bool check[MAX];
//初期化
void init(){
    for(int i=0;i<MAX;i++){
        par[i]=i;
        siz[i]=1;
        check[i]=true;
    }
}
//根を探索
int root(int x){
    if(par[x]==x){
        return x;
    }else{
        return par[x]=root(par[x]);
    }
}

//統合
void unit(int x,int y){
    x=root(x);
    y=root(y);
    if(x==y){
        check[x]=false;
        return;
    }
    if(depth[x]>depth[y])swap(x,y);
    par[x]=y;
    siz[y]+=siz[x];
    check[y]&=check[x];
    if(depth[x]==depth[y])depth[x]++;
}
// 同じ集合か判定
bool issame(int x,int y){
    return root(x)==root(y);
}

//集合の大きさ
int size(int x){
    return siz[root(x)];
}
void solve(){
    int n;
    cin>>n;
    map<int,int>mp,cnt;
    init();
    vector<int>a(n),b(n);
    REP(i,n){
        cin>>a[i]>>b[i];
        a[i]--;b[i]--;
        unit(a[i],b[i]);
    }
    REP(i,n){
        if(0==cnt[a[i]]++)mp[root(a[i])]++;
        if(0==cnt[b[i]]++)mp[root(b[i])]++;
    }
    int ans=0;
    for(auto p:mp){
        ans+=p.second;
        if(check[p.first])ans--;
    }
    OUT(ans);
}

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

#include "bits/stdc++.h"
using namespace std;
typedef int ll;
typedef pair<ll ,ll> pll;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
#define REP(i,x,y) for(ll i = x;i <= y;++i)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
vector<string> vec_splitter(string s) {
    for(char& c: s) c = c == ','?  ' ': c;
    stringstream ss; ss << s;
    vector<string> res;
    for(string z; ss >> z; res.push_back(z))
        ;
    return res;
}
void debug_out(vector<string> args, int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
    if(idx > 0) cerr << ", ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
void localTest() {
    #ifndef ONLINE_JUDGE
        freopen("inp", "r", stdin);
        freopen("out", "w", stdout);
    #endif
}
const ll N = 2e5 + 5;
ll par[N], sz[N];
unordered_map<ll, ll> has[N];
ll find(ll x) {
	if(x == par[x]) {
		return x;
	}
	return par[x] = find(par[x]);
}
void merge(ll x, ll y) {
	ll rx = find(x), ry = find(y);
	if(rx == ry) {
		return;
	}
	if(sz[rx] < sz[ry]) {
		swap(rx, ry);
	}
	par[ry] = rx;
	sz[rx] += sz[ry];
	for(auto u : has[ry]) {
		has[rx][u.F] += u.S;
	}
	return;
}
int main() {

    localTest();
    fastio;
    ll n, q;
    cin >> n >> q;
    for(ll i = 1; i <= n; ++i) {
    	ll x;
    	par[i] = i;
    	cin >> x;
    	sz[x] = 1;
    	has[i][x]++;
    }
    while(q--) {
    	ll ty, x, y;
    	cin >> ty >> x >> y;
    	if(ty == 1) {
    		merge(x, y);
    	} else {
    		ll rx = find(x);
    		cout << has[rx][y] << "\n";
    	}
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);

template<typename T>
struct BIT{
	vector<T> bit;
	BIT(int n) : bit(n+1) {}
	void add(int a,T w){
		for(int x=++a; x < bit.size(); x+=(x&(-x)) ){
			bit[x] += w;
		}
	}
	// return v[0]+v[1]+...+v[a-1];
	T sum(int a){
		T ret=0;
		for(int x=a;x>0;x-=(x&(-x)) ){
			ret+=bit[x];
		}
		return ret;
	}
};

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

	int n,m,q;
	cin>>n>>m>>q;
	vector<array<ll,3>> v(q);
	map<int,int> mp;
	for(int i=0;i<q;i++){
		int a,b,c;
		cin>>a>>b>>c;
		b--;
		mp[c]=0;
		v[i]={a,b,c};
	}
	mp[0]=0;
	int now = 0;
	for(auto &i:mp){
		i.second = now;
		now++;
	}
	ll ans = 0;
	int k = now;
	BIT<ll> c1(k),c2(k),s1(k),s2(k);
	int a[n]={},b[m]={};
	c1.add(0,n);
	c2.add(0,m);
	for(int i=0;i<q;i++){
		auto [t,x,y] = v[i];
		int pos = mp[y];
		if(t == 1){
			int pre = mp[a[x]];
			ans -= a[x] * c2.sum(pre);
			ans -= s2.sum(k) - s2.sum(pre);
			c1.add(pre,-1);
			c1.add(pos,1);
			s1.add(pre,-a[x]);
			s1.add(pos,y);
			a[x]=y;
			ans += y * c2.sum(pos);
			ans += s2.sum(k) - s2.sum(pos);
		}
		else{
			int pre = mp[b[x]];
			ans -= b[x] * c1.sum(pre);
			ans -= s1.sum(k) - s1.sum(pre);
			c2.add(pre,-1);
			c2.add(pos,1);
			s2.add(pre,-b[x]);
			s2.add(pos,y);
			b[x]=y;
			ans += y * c1.sum(pos);
			ans += s1.sum(k) - s1.sum(pos);
		}
		cout << ans << "\n";
	}
}

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint1000000007;

#define int long long
#define rep(i, n) for(int i = 0, i##_len=(n); i < i##_len; ++i)
#define per(i, n) for(int i = (n)-1; i >= 0 ; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())

void _cin() {} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail) { cin >> head; _cin(forward<Tail>(tail)...); }
#define cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define cinv4(Type, xs, ys, zs, zzs, n) vector<Type> xs(n), ys(n), zs(n), zzs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i] >> zzs[i]
#define cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<Type>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]

void print() { cout << endl; }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }
template <class Type> void print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void print(vector<vector<Type>> &df) { for (auto& vec : df) { print(vec); } }

void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void yesno(bool b) { cout << (b ? "yes" : "no") << endl; }
template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a < b) { a = b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b < a) { a = b; return 1; } return 0; }

using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;

const int inf = 1ll << 62;
const int mod = 1000000007;

void Main() {
	cin(string, s);
	reverse(all(s));
	rep(i, 10) {
		string t = s;
		reverse(all(t));
		if (t == s) {
			print("Yes");
			return;
		}
		s += "0";
	}
	print("No");
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(16);
	//int t; cin >> t; while (t--)
	Main();
}


#include <bits/stdc++.h>
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
//#define ExecuteTime     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
#define pi  acos(-1)
#define pb  push_back
#define F   first
#define S   second
#define forr(i,a,b)     for(ll i = (ll)a, asdf = (ll)b ; i < asdf; ++i)
#define fore(i,a,b)     for(ll i = (ll)a, asdf = (ll)b ; i >= asdf; --i)
#define fori(i,a,b,c)   for(ll i = (ll)a, asdf = (ll)b, INC = (ll) c; i < asdf; i+=INC)
#define sz(x)           (ll)x.size()
#define all(a)          a.begin(),a.end()
#define rall(a)         a.rbegin(),a.rend()
#define b_p(n)          __builtin_popcountll(n)

using namespace std;

typedef long long ll;
typedef pair<ll,ll>  pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;

int main(){
    optimizar_io
    //freopen("prueba_input.txt","r",stdin);
    //freopen("prueba_output.txt","w",stdout);
    ll n;
    cin>>n;
    vpll idx(n), idy(n);
    vll dist;
    forr(i,0,n){
        cin>>idx[i].F>>idy[i].F;
        idx[i].S = i;
        idy[i].S = i;
    }
    sort(all(idx));
    sort(all(idy));
    ll index_x = idx[n-1].S;
    ll index_y = idy[n-1].S;
    ll index_x2 = idx[n-2].S;
    ll index_y2 = idy[n-2].S;
    vpll dx, dy, dx2, dy2;
    forr(i,0,n-1){
        dx.pb({ idx[i].S, abs( idx[i].F-idx[n-1].F ) });
        dy.pb({ idy[i].S, abs( idy[i].F-idy[n-1].F ) });
    }
    forr(i,0,n-2){
        dx2.pb({ idx[i].S, abs( idx[i].F-idx[n-2].F ) });
        dy2.pb({ idy[i].S, abs( idy[i].F-idy[n-2].F ) });
    }
    sort(all(dx));
    sort(all(dy));
    sort(all(dx2));
    sort(all(dy2));
    forr(i,0,sz(dx)){
        if(dx[i].F==dy[i].F && index_x==index_y)
            dist.pb(max(dx[i].S, dy[i].S));
        else{
            dist.pb(dx[i].S);
            dist.pb(dy[i].S);
        }
    }
    forr(i,0,sz(dx2)){
        if(dx2[i].F==dy2[i].F && index_x2==index_y2)
            dist.pb(max(dx2[i].S, dy2[i].S));
        else{
            dist.pb(dx2[i].S);
            dist.pb(dy2[i].S);
        }
    }
    sort(rall(dist));
    cout<<dist[1]<<"\n";
    //ExecuteTime;
    return 0;
}
// 2^n - 1LL = 1,2,4,...,2^(n-1).
//See the real limit per case and the total limit
// (n-1)% m + 1LL; - formula n%m==x and special case n%m==0;
// set<char>st(s.begin(),s.end()) // or vector<int> v(all(w.begin(),w.end()))
// put suffix  LL or ULL for constants integers, 1.0f float, 1.0 double, 1.0L long double
// read the question correctly. What are the exact constraints?. 'y' is considered a vowel in special cases.
// n&(n-1)==0 if this is true n is a power of 2.
// look out for SPECIAL CASES (n=1 or n=0 or n=limit ?) and overflow (ll vs int) ARRAY OUT OF BOUNDS

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef double db;
typedef pair<int, int> pii;typedef pair<ll, ll> pll;
typedef pair<int,ll> pil;typedef pair<ll,int> pli;
#define Fi first
#define Se second
#define _Out(a)	cerr<<#a<<" = "<<(a)<<endl
const int INF = 0x3f3f3f3f, MAXN = 1e6 + 50;
const ll LINF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353;
const db Pi = acos(-1), EPS = 1e-6;
void test(){cerr << "\n";}template<typename T,typename...Args>void test(T x,Args...args){cerr<<x<<" ";test(args...);}
inline ll qpow(ll a, ll b){return b?((b&1)?a*qpow(a*a%MOD,b>>1)%MOD:qpow(a*a%MOD,b>>1))%MOD:1;}
inline ll qpow(ll a, ll b,ll c){return b?((b&1)?a*qpow(a*a%c,b>>1,c)%c:qpow(a*a%c,b>>1,c)) %c:1;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll cede(ll a,ll b){if(b<0)return cede(-a,-b);if(a<0)return a/b;return (a+b-1)/b;}
inline ll flde(ll a,ll b){if(b<0)return flde(-a,-b);if(a<0)return (a-b+1)/b;return a/b;}
inline int sign(db x){return x<-EPS ? -1:x>EPS;}
inline int dbcmp(db l,db r){return sign(l - r);}
namespace Fast_IO{ //orz laofu
    const int MAXL((1 << 18) + 1);int iof, iotp;
    char ioif[MAXL], *ioiS, *ioiT, ioof[MAXL],*iooS=ioof,*iooT=ioof+MAXL-1,ioc,iost[55];
    char Getchar(){
        if (ioiS == ioiT){
            ioiS=ioif;ioiT=ioiS+fread(ioif,1,MAXL,stdin);return (ioiS == ioiT ? EOF : *ioiS++);
        }else return (*ioiS++);
    }
    void Write(){fwrite(ioof,1,iooS-ioof,stdout);iooS=ioof;}
    void Putchar(char x){*iooS++ = x;if (iooS == iooT)Write();}
    inline int read(){
        int x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof;
    }
    inline long long read_ll(){
        long long x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof;
    }
    void Getstr(char *s, int &l){
        for(ioc=Getchar();ioc==' '||ioc=='\n'||ioc=='\t';)ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(l=0;!(ioc==' '||ioc=='\n'||ioc=='\t'||ioc==EOF);ioc=Getchar())s[l++]=ioc;s[l] = 0;
    }
    template <class Int>void Print(Int x, char ch = '\0'){
        if(!x)Putchar('0');if(x<0)Putchar('-'),x=-x;while(x)iost[++iotp]=x%10+'0',x/=10;
        while(iotp)Putchar(iost[iotp--]);if (ch)Putchar(ch);
    }
    void Putstr(const char *s){for(int i=0,n=strlen(s);i<n;++i)Putchar(s[i]);}
} // namespace Fast_IO
using namespace Fast_IO;
ll fac[MAXN],invfac[MAXN];

void init()
{
    fac[0]=1;for(int i=1;i<MAXN;i++)fac[i]=fac[i-1]*i%MOD;
    invfac[MAXN-1]=qpow(fac[MAXN-1],MOD-2);
    for(int i=MAXN-2;i>=0;i--)invfac[i]=invfac[i+1]*(i+1)%MOD;

}
inline ll C(int n,int m)
{
    if(m<0||m>n)return 0;
    return fac[n]*invfac[m]%MOD*invfac[n-m]%MOD;
}
ll tot[333];
void work()
{
    int n=read(),k=read();
    for(int i=1;i<=n;i++)
    {
        ll x=read();
        ll now=1;
        for(int j=0;j<=k;j++)
        {
            tot[j]=(tot[j]+now)%MOD;
            now=now*x%MOD;
        }
    }
    ll mi2=1;
    for(int i=1;i<=k;i++)
    {
        mi2=mi2*2%MOD;
        ll ans=0;
        for(int j=0;j<=i;j++)
        {
            ans=(ans+tot[j]*tot[i-j]%MOD*C(i,j)%MOD)%MOD;
        }
        ans=(ans-tot[i]*mi2%MOD+MOD)*qpow(2,MOD-2)%MOD;
        printf("%lld\n",ans);
    }
}
int main()
{
    init();
    //std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //int T=read();for(int cas=1;cas<=T;cas++)
    //int T;scanf("%d",&T);for(int i=1;i<=T;i++)
    work();
    // Write();
}

/**
2
5 3 6
3 1 2 4 5

*/


#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<7-a+7-b+7-c;
return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define vpp vector<pair<pair<ll, ll>, ll>>
#define vp vector<pair<ll, ll>>
#define mk make_pair
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define N 1000000007
using namespace std;

void fun()
{
    // code begins
    ll a,b,c;cin>>a>>b>>c;
    ll k = 0;
    if( 2*a == b+c)
        k = 1;
    if( 2*b == a+c)
        k = 1;
    if( 2*c == a+b)
        k = 1;
    if(k)
    cout<<"Yes";
    else
    cout<<"No";
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    IOS;
    ll t; t = 1;
    //cin >> t;
    for (ll i = 1; i <= t; i++)
        fun();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
char s[20];
ll g1(ll x){
	vector<int> frec(10);
	int i = 0;
	while(x){
		frec[x%10]++;
		//s[i++]=x%10 + '0';
		x/=10;
	}
	for(int i = 9; i >= 0; i--)
		for(int j = 0; j < frec[i]; j++)
			x=x*10+i;
	return x;
}
ll g2(ll x){
	vector<int> frec(10);
	int i = 0;
	while(x){
		frec[x%10]++;
		x/=10;
	}
	for(int i = 1; i < 10; i++)
		for(int j = 0; j < frec[i]; j++)
			x=x*10+i;
	return x;
}
ll fun(ll x){
	return g1(x)-g2(x);
}
int main(){
	int n,k;
	scanf("%d %d",&n,&k);
	ll ans = n;
	for(int i = 1; i <= k; i++){
		ans = fun(ans);
	}
	printf("%lld\n", ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int mod = 1e9+7;
int add(int a, int b){
    int ans = a + b;
    while(ans >= mod){
        ans -= mod;
    }
    return ans;
}
void add_self(int & a, int b){
    a = add(a,b);
}
int sub(int a, int b){
    int ans = a - b;
    while(ans < 0){
        ans += mod;
    }
    return ans;
}
void sub_self(int & a, int b){
    a = sub(a,b);
}
int mul(int a, int b){
    int ans = ((int64_t)a * b) % mod;
    return ans;
}
int binexpo(int b, int e){
    int ans = 1;
    while(e){
        if(e & 1){
            ans = mul(ans,b);
        }
        b = mul(b,b);
        e >>= 1;
    }
    return ans;
}
int64_t dp[74][1<<21];
int main(){
    int64_t a,b;
    cin >> a >> b;
    int64_t zero = a;
    int64_t len = b - a + 1;
    int64_t ans = 0;
    vector<int>masks(len);
    vector<int>primes;
    vector<bool>is_prime(73,true);
    for(int i = 2; i < 73; ++i){
        if(is_prime[i]){
            primes.push_back(i);
            for(int j = i * i; j < 73; j+= i){
                is_prime[j] = false;
            }
        }
    }
    for(int j = 0; j < len; ++j){
        for(int k = 0; k < primes.size(); ++k){
            if((j + zero)%primes[k] == 0){
                masks[j] |= (1 << k);
            }
        }
    }
    dp[0][0] = 1;
    for(int i = 1; i <= len; ++i){
        for(int k = 0; k < (1<<20); ++k){
            dp[i][k] = dp[i-1][k];
        }
        for(int k = 0; k < (1<<20); ++k){
            if((masks[i-1] & k) == masks[i-1]){
                dp[i][k] += dp[i][k ^ masks[i-1]];
            }
        }
    }
    for(int i = 0; i < (1<<20); ++i){
        ans += dp[len][i];
    }
    cout << ans << "\n";
    return 0;
}
//12341234


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

int main(){
    ll t;cin>>t;
    rep(tt,t){
        ll n;cin>>n;
        string a,b,c;cin>>a>>b>>c;
        string ans;
        rep(i,n)ans+='0';
        rep(i,n)ans+='1';
        ans+='0';
        out(ans);
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ull unsigned long long
#define ln cout<<"\n"
#define all(x) x.begin(),x.end()

typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pi;
typedef pair<long long,long long> pl;
#define fs(i,a,n) for (int i = a; i <=b; i++)
#define fd(i,a,n) for (int i = a; i <=b; i--)
#define mod    1000000007

void solve(){
int a,b,c;
int n;
cin>>n;
vector<pair<pi,int>> v(n);

for (int i = 0; i < n; i++)
{
    cin>>a>>b>>c;
    v[i].s=a;
    v[i].f.f=b;
    v[i].f.s=c;
}

ll ans=0;
for (int i = 0; i < n; i++)
{
    for (int j = i+1; j < n; j++)
    {
        if(v[i].f.f<=v[j].f.f && v[i].f.s>v[j].f.s){
            ans++;continue;
        }
        if(v[j].f.f<=v[i].f.f && v[j].f.s>v[i].f.s){
            ans++;continue;
        }
        if(v[j].f.f<=v[i].f.f && v[i].f.f<v[j].f.s){
            ans++;continue;
        }
        if(v[i].f.f<=v[j].f.f && v[j].f.f<v[i].f.s){
            ans++;continue;
        }
        if(v[i].f.s==v[j].f.f){
            if((v[i].s==3 || v[i].s==1)&&(v[j].s==2 || v[j].s==1))ans++;
            continue;
        }
                if(v[j].f.s==v[i].f.f){
            if((v[j].s==3 || v[j].s==1)&&(v[i].s==2 || v[i].s==1))ans++;
            continue;
        }
    }

}
cout<<ans;

}

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n=1;
// cin>>n;
while(n--){
    solve();
}

}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 100005
#define maxm 200005
#define INF 1234567890
#define eps 1e-15
#define p 1000000007
const double pi=acos(-1);
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
template<class T,class ...S>inline bool read(T &x,S &...y){return read(x)&&read(y...);}
ll n,x,a[maxn],b[maxn],c[maxn],ans;
vector<ll>v[maxn];
int main()
{
    read(n);
    for(ll i=1;i<=n;i++)
    {
        read(x);
        a[x]++;
    }
    for(ll i=1;i<=n;i++)
    {
        read(b[i]);
        v[b[i]].push_back(i);
    }
    for(ll i=1;i<=n;i++)
    {
        read(x);
        c[x]++;
    }
    for(ll i=1;i<=n;i++)
    {
        for(ll j=0;j<v[i].size();j++)
        {
            ans+=a[i]*c[v[i][j]];
        }
    }
    print(ans,'\n');
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	ll n;cin>>n;
	vector<ll>a(n),b(n);
	for(ll i=0;i<n;++i)
	{
		cin>>a[i];
	}
	for(ll i=0;i<n;++i)
	{
		cin>>b[i];
	}
	ll l=a[0],r=b[0];
	for(ll i=1;i<n;++i)
	{
		if(a[i]>l)
		{
			l=a[i];
		}
		if(b[i]<r)
		{
			r=b[i];
		}
	}
	if(l<=r)
	cout<<r-l+1<<"\n";
	else cout<<"0\n";
}

// Date-- 24/04/2021
//practice
#include <bits/stdc++.h>
using namespace std;

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

#define   all(x)        (x).begin(),(x).end()
#define   rep(i,a,b)    for(ll i = a; i<b ; i++)
#define   sz(x)         (int)((x).size())
#define   PI             3.141592653589
#define   mod            1000000007
#define   MAX            1000001 //1e6+1
long long const INF = 1e18;

void cf()
{
    ll n; cin>>n;
    ll a[n],b[n];
    rep(i,0,n) cin>>a[i];
    rep(i,0,n) cin>>b[i];
    ll x = *max_element(a,a+n);
    ll y = *min_element(b,b+n);
    ll ans = y-x+1;
    if(ans<=0) cout<<0;
    else cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1; //cin>>T;
    while(T--)  cf();
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define mo 1000000007
#define ll long long
#define maxn 1005
using namespace std;

int n,i,j,k,f[2][2];
ll ans;

ll ksm(ll x,ll y){
	ll s=1;
	for(;y;y/=2,x=x*x%mo) if (y&1)
		s=s*x%mo;
	return s;
}

void reverse(){
	static int g[2][2];
	for(i=0;i<2;i++) for(j=0;j<2;j++)
		g[i^1][j^1]=f[j][i]^1;
	memcpy(f,g,sizeof(f));
}

void doit1(){
	static int f[maxn][2];
	f[1][0]=1;
	for(i=2;i<=n-1;i++){
		f[i][0]=(f[i-1][0]+f[i-1][1])%mo;
		f[i][1]=f[i-1][0];
	}
	printf("%d\n",f[n-1][0]);
}

int main(){
	scanf("%d",&n); char ch=getchar();
	for(i=0;i<2;i++) for(j=0;j<2;j++){
		while (ch!='A'&&ch!='B') ch=getchar();
		f[i][j]=ch-'A',ch=getchar();
	}
	if (f[0][1]==1) reverse();
	if (n==2||f[0][0]==0) printf("1"); else
	if (f[1][0]==0) doit1(); else
		printf("%lld\n",ksm(2,n-3));
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    int n2 = 2*n;
    V<ll> a(n2);
    V<char> c(n2);
    rep(i,n2) cin >> a[i] >> c[i];
    V<int> sum(3);
    rep(i,n2){
        if(c[i]=='R') sum[0]++;
        if(c[i]=='G') sum[1]++;
        if(c[i]=='B') sum[2]++;
    }
    if(sum[0]%2==0 && sum[1]%2==0 && sum[2]%2==0){
        cout << 0 << endl;
        return 0;
    }
    VV<ll> x(3);
    rep(i,n2){
        if(c[i]=='R') x[0].pb(a[i]);
        if(c[i]=='G') x[1].pb(a[i]);
        if(c[i]=='B') x[2].pb(a[i]);
    }
    rep(i,3) sort(all(x[i]));
    if(sum[0]%2==0) swap(x[0], x[2]);
    if(sum[1]%2==0) swap(x[1], x[2]);
    ll ans = 1e18;
    rep(i,x[0].size()){
        auto it = lower_bound(all(x[1]), x[0][i]);
        ans = min(ans, abs(*it-x[0][i]));
        if(it != x[1].begin()){
            it--;
            ans = min(ans, abs(*it-x[0][i]));
        }
    }
    rep(i,x[1].size()){
        auto it = lower_bound(all(x[0]), x[1][i]);
        ans = min(ans, abs(*it-x[1][i]));
        if(it != x[0].begin()){
            it--;
            ans = min(ans, abs(*it-x[1][i]));
        }
    }
    //cout << 1 << endl;
    if(x[2].size()==0){
        cout << ans << endl;
        return 0;
    }
    V<pll> t0, t1;
    rep(i,x[2].size()){
        auto it = lower_bound(all(x[0]), x[2][i]);
        if(it != x[0].end()) t0.eb(abs(*it-x[2][i]),i);
        if(it != x[0].begin()){
            it--;
            t0.eb(abs(*it-x[2][i]),i);
        }
    }
    rep(i,x[2].size()){
        auto it = lower_bound(all(x[1]), x[2][i]);
        if(it != x[1].end()) t1.eb(abs(*it-x[2][i]),i);
        if(it != x[1].begin()){
            it--;
            t1.eb(abs(*it-x[2][i]),i);
        }
    }
    sort(all(t0));
    sort(all(t1));
    //cout << 1 << endl;
    ll tmp = t0[0].first;
    rep(j,(int)t0.size()){
        rep(i,t1.size()){
            if(t1[i].second != t0[j].second){
                ans = min(ans, t0[j].first+t1[i].first);
                break;
            }
        }
    }
    //cout << 1 << endl;
    tmp = t1[0].first;
    rep(j,(int)t1.size()){
        rep(i,t0.size()){
            if(t0[i].second != t1[j].second){
                ans = min(ans, t1[j].first+t0[i].first);
                break;
            }
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
// #pragma optimize("-O3")
//  #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,avx,mmx,tune=native")
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 1e9
//#define BIG_INF 1e18
#define vi vector<ll>
#define sz(a) ll((a).size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1)
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
// typedef tree<
// ll,
// null_type,
// less<ll>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
typedef long long ll;
 const ll BIG_INF = 3e18;
ll mod = 1e9+7;
 ll fast_exp(ll a, ll b)
{
    if(b <= 0)
       return 1;
    else
    {
       ll res = 1;
       res = fast_exp(a,b/2);
       res = (res*res)%mod;
       if(b%2 == 1)
          res = (res*a)%mod;
       return res;

    }
}

typedef long double ld;
const long long N = 1e5+1000;
const long double pi = acos(-1.0);
using cd = complex<double>;
const double PI = acos(-1);

int par[2][N] = {};
int comp[2] = {};

int find_par(int v, int mode)
{
    if(par[mode][v] == v)
        return v;
    else
        return par[mode][v] = find_par(par[mode][v],mode);
}

void union_find(int a, int b, int mode)
{
    a = find_par(a,mode);
    b = find_par(b,mode);
    if(a != b)
    {
        par[mode][a] = b;
        comp[mode]--;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen("input.txt","r",stdin);
    int h, w;
    cin >> h >> w;
    char a[h][w] = {};
    for(int i = 0; i<h; i++)
        for(int j = 0; j<w; j++)
        {
            cin >> a[i][j];
        }
        a[0][0] = '#';
        a[h-1][0] = '#';
        a[0][w-1] = '#';
        a[h-1][w-1] = '#';
    for(int i = 0; i<h; i++)
        par[0][i] = i;
    comp[0] = h;
    for(int i = 0; i<w; i++)
        par[1][i] = i;
    comp[1] = w;

    for(int category = 0; category < w; category++)
    {
        int prev = -1;
        for(int i = 0; i<h; i++)
        {
            if(a[i][category] == '#')
            {
                if(prev == -1)
                    prev = i;
                else
                {
                    union_find(prev,i,0);
                    prev = i;
                }
            }
        }
    }

    for(int category = 0; category < h; category++)
    {
        int prev = -1;
        for(int i = 0; i<w; i++)
        {
            if(a[category][i] == '#')
            {
                if(prev == -1)
                    prev = i;
                else
                {
                    union_find(prev,i,1);
                    prev = i;
                }
            }

        }
    }

    int ans = min(comp[0],comp[1]);
    ans--;
    cout << ans << '\n';

    // cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
    return 0;

}

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native")

#include <bits/stdc++.h>

using namespace std ;

#define int int64_t //be careful about this
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)

#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue

#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))

template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

template<typename T>
void __p(T a) {
  cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
  cout<<"{";
  __p(a.first);
  cout<<",";
  __p(a.second);
  cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
  cout<<"{";
  for(auto it=a.begin(); it<a.end(); it++)
    __p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
  __p(a1);
  __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cout<<name<<" : ";
  __p(arg1);
  cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
  int bracket=0,i=0;
  for(;; i++)
    if(names[i]==','&&bracket==0)
      break;
    else if(names[i]=='(')
      bracket++;
    else if(names[i]==')')
      bracket--;
  const char *comma=names+i;
  cout.write(names,comma-names)<<" : ";
  __p(arg1);
  cout<<" | ";
  __f(comma+1,args...);
}

void setIO(string s = "") {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin.exceptions(cin.failbit);
	cout.precision(15);	cout << fixed;
  #ifdef ONLINE_JUDGE
  if(sz(s)){
  	freopen((s+".in").c_str(),"r",stdin);
  	freopen((s+".out").c_str(),"w",stdout);
  }
  #endif
}
const int inf = 1e9, A = 26;
signed main(){
  setIO() ;
  int n,m; rd(n,m);
  int dp[n][n];
  f(i,0,n) f(j,0,n) dp[i][j] = inf ;
  f(i,0,n) dp[i][i] = 0;
  vt<int> g[n][A];
  f(_,0,m){
  	int u,v; char c;
  	rd(u,v,c);
  	--u,--v;
  	ckmin(dp[u][v],int(1));
  	ckmin(dp[v][u],int(1));
  	g[u][c-'a'].pb(v);
  	g[v][c-'a'].pb(u);
  }
  PQ<ar<int,3>> q;
  f(i,0,n) f(j,0,n) q.push({-dp[i][j],i,j});
  while(sz(q)){
  	auto a = q.top(); q.pop();
  	int v = -a[0], i = a[1], j = a[2];
  	//if(dp[i][j] != v) continue;
  	f(k,0,A){
  		for(auto x : g[i][k]){
  			for(auto y : g[j][k]){
  				if(ckmin(dp[x][y],v + 2)){
  					q.push({-dp[x][y],x,y});
  				}
  			}
  		}
  	}
  }
  cout << (dp[0][n-1] == inf ? int(-1) : dp[0][n-1]);
}


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

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

int n;
vector<vector<int>> es;
vector<int> a, b;
vector<int> depth;

void DepthDFS(int a, int d){
    depth[a] = d;
    for (int next : es[a]){
        if (depth[next] == -1){
            DepthDFS(next, d+1);
        }
    }
}

vector<ll> dp;
void ImosDFS(int a, ll now){
    now += dp[a];
    dp[a] = now;
    for (int next : es[a]) {
        if (depth[next] > depth[a]) {
            ImosDFS(next, now);
        }
    }
}

int main(){
    cin >> n;
    es.resize(n);
    a.resize(n - 1);
    b.resize(n - 1);
    depth = vector<int>(n, -1);
    dp.resize(n);

    rep(i, n-1){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        es[a[i]].push_back(b[i]);
        es[b[i]].push_back(a[i]);
    }
    DepthDFS(0, 0);

    int q; cin >> q;
    rep(i, q){
        int t, e, x; cin >> t >> e >> x;
        e--;
        int va, vb;
        if (t == 1){
            va = a[e];
            vb = b[e];
        }
        else {
            vb = a[e];
            va = b[e];
        }
        if (depth[va] < depth[vb]) {
            dp[0] += x;
            dp[vb] -= x;
        }
        else {
            dp[va] += x;
        }
    }

    ImosDFS(0, 0);
    rep(i, n){
        cout << dp[i] << endl;
    }
    return 0;
}

#pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i)
#define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i)
#define all(x) begin(x),end(x)
using namespace std;
typedef long long ll;
const ll INF = 1e9;
template<class T=ll> using V = vector<T>;
template<class T=ll> using PQ = priority_queue<T>;
template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>;
const ll MOD = 1000000007LL;
void in() {}
template<class Head, class... Tail>
void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); }
#define IN(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem
#define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem
#define OUT(x) cout << x;
#define OUTL(x) cout << x << endl;
template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;}
template<class T>
string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); }
#pragma endregion RegionDefs

// 区間更新できる1-indexedなBIT
struct BIT {
    ll n; // 要素数
    vector<ll> bit[2]; // データ
    BIT(ll n) : n(n) {
        bit[0].assign(n+1, 0);
        bit[1].assign(n+1, 0);
    }
    // i番目にvを足す
    void add_sub(int p, int i, ll v) {
        if (!i) return;
        for(int x = i; x <= n; x += x & -x)
            bit[p][x] += v;
    }
    // 区間[l,r)にvを足す
    void add(int l, int r, ll v) {
        add_sub(0, l, -v * (l - 1));
        add_sub(0, r, v * (r - 1));
        add_sub(1, l, v);
        add_sub(1, r, -v);
    }
    ll sum_sub(int p, int i) {
        ll s = 0;
        for (int x = i; x > 0; x -= x & -x)
            s += bit[p][x];
        return s;
    }
    // [1,i]の総和
    ll sum(int i) {
        return sum_sub(0, i) + sum_sub(1, i) * i;
    }
    // [l,r]の総和
    ll sum(int l, int r) {
        return sum(r) - sum(l-1);
    }
};

void euler(V<V<ll>>& g, ll from, ll v, V<ll>& res, V<ll>& b, V<ll>& e)
{
    e[v] = b[v] = res.size();
    res.push_back(v);
    for (auto to : g[v]) {
        if (to == from) continue;
        euler(g, v, to, res, b, e);
        e[v] = res.size();
        res.push_back(v);
    }
}

void solve()
{
    IN(n);
    V<V<ll>> g(n);
    V<ll> a(n-1), b(n-1);
    rep(i, n-1) {
        cin >> a[i] >> b[i];
        --a[i]; --b[i];
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
    }
    V<ll> res;
    V<ll> bg(n), en(n);
    euler(g, -1, 0, res, bg, en);

    BIT bit(res.size());
    IN(q);
    rep(i, q) {
        IN(t, e, x);
        --e;
        ll from = t == 1 ? a[e] : b[e];
        ll noth = t == 1 ? b[e] : a[e];
        if (bg[from] < bg[noth]) {
            // nothはfromの子
            bit.add(1, res.size() + 1, x);
            bit.add(bg[noth] + 1, en[noth] + 2, -x);
        } else {
            bit.add(bg[from] + 1, en[from] + 2, x);
        }
    }
    rep(i, n)
        OUTL(bit.sum(bg[i] + 1, bg[i] + 1));
}

int main()
{
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(numeric_limits<double>::max_digits10);

    solve();

    return 0;
}


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

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifdef ARTHUR_LOCAL
	#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
	#define debug(x...)
#endif

using ll = long long;

#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) int((x).size())

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define randint(n) uniform_int_distribution<int>(1, (n))(rng)

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

	#ifdef ARTHUR_LOCAL
		ifstream cin("input.txt");
	#endif

	ll n;
	cin>>n;
	set<ll> S;

	for(int p=2; p<=50; p++){
		for(ll a=2; a<=100000; a++){
			int p2=p;
			ll cur=1LL;
			while(p2--) cur*=a;
			if(cur>n) break;
			S.insert(cur);
			// cout<<
		}
	}

	cout<<n-ll(len(S))<<endl;
}

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

int main() {
    int N,en,i;
    i=0;
    en=0;
    cin >> N;
    while (en<N) {
        en=en+i;
        i++;
    }
    cout << i-1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m; cin >> n >> m;
  vector<ll> W(n); rep(i, n) cin >> W[i];
  vector<pair<ll, ll>> LV(m);
  ll minV = longinf;
  rep(i, m) {
    cin >> LV[i].first >> LV[i].second;
    chmin(minV, LV[i].second);
  }
  sort(all(LV), [&](pair<ll, ll> p1, pair<ll, ll> p2) {
    return p1.second < p2.second;
  });
  vector<ll> L(m), V(m);
  rep(i, m) {
    L[i] = LV[i].first;
    V[i] = LV[i].second;
  }
  vector<ll> mx(m+1);
  rep(i, m) mx[i+1] = max(mx[i], L[i]);
  rep(i, n) {
    if(W[i] > minV) {
      cout << -1 << endk;
      return 0;
    }
  }
  vector<int> id(n); rep(i, n) id[i] = i;
  ll ans = longinf;
  auto solve = [&](vector<int> id) {
    vector<ll> sum(n+1); rep(i, n) sum[i+1] = sum[i]+W[id[i]];
    vector<vector<ll>> d(n, vector<ll>(n));
    rep(i, n) {
      REP(j, i+1, n) {
        int k = upper_bound(all(V), sum[j+1]-sum[i]-1)-begin(V);
        d[i][j] = mx[k];
      }
    }
    vector<ll> D(n);
    rep(i, n) {
      REP(j, i+1, n) {
        chmax(D[j], D[i]+d[i][j]);
      }
    }
    chmin(ans, D[n-1]);
  };
  do {
    solve(id);
  } while(next_permutation(all(id)));
  cout << ans << endk;
  return 0;
}

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

const int BS=524288;

int CNT,rc;
char inb[BS];

inline char gchar(void){
    if(CNT%BS==0){
        CNT=0;
        rc=fread(inb,1,BS,stdin);
    }
    return CNT<rc?inb[CNT++]:0;
}

inline int _readint(int *ptr){
    int n=0,c=0;
    *ptr=0;
    while(1){
        char read=gchar();
        if(read>='0'&&read<='9')read-='0',n*=10,n+=read;
        else{*ptr=n;return n==0?0:!c;}
        c++;
    }
    *ptr=n;
    return 0;
}

inline int readint(void){int ptr;while(_readint(&ptr));return ptr;}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,m;
	// cin >> n >> m;
	n=readint(); m=readint();
	vector<ll> w(n);
	for(int i=0;i<n;i++){
		// cin >> w[i];
		w[i]=readint();
	}
	sort(w.rbegin(), w.rend());
	vector<ll> l(m),v(m);
	for(int i=0;i<m;i++){
		// cin >> l[i] >> v[i];
		l[i]=readint(); v[i]=readint();
	}
	for(int i=0;i<m;i++){
		if(v[i]<w[0]){
			printf("-1\n");
			return 0;
		}
	}
	ll res=1e18;
	vector<int> p(n-2);
	for(int i=0;i<n-2;i++){
		p[i]=i+2;
	}
	do{
		vector<ll> vec(n,0);
		vector<int> pp(n);
		pp[0]=0;
		for(int i=0;i<n-2;i++){
			pp[i+1]=p[i];
		}
		pp[n-1]=1;
		for(int i=1;i<n;i++){
			ll mi=vec[i-1];
			for(int j=0;j<m;j++){
				ll sum=w[pp[i]];
				for(int k=i-1;k>=0;k--){
					if(abs(mi-vec[k])<l[j]){
						// cout << i << " " << k << " " << mi << endl;
						sum+=w[pp[k]];
						// cout << sum << endl;
						if(sum>v[j]){
							mi=vec[k]+l[j];
							break;
						}
					}
					else break;
				}
			}
			vec[i]=mi;
		}
		res=(res>vec[n-1]?vec[n-1]:res);
		// res=min(res,vec[n-1]);
	}while(next_permutation(p.begin(), p.end()));
	cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using isize = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using usize = size_t;

using f32 = float_t;
using f64 = double_t;

auto main() -> i32 {

    ios::sync_with_stdio(false), cin.tie(nullptr);

    usize n, m;
    cin >> n >> m;
    vector<usize> f(1000 + 1, 0);

    for (usize i = 0; i < n; i++) {
        usize a;
        cin >> a;
        f[a] = 1;
    }

    for (usize i = 0; i < m; i++) {
        usize b;
        cin >> b;
        f[b] = (f[b] + 1) % 2;
    }

    for (usize i = 0; i < 1000 + 1; i++) {
        if (f[i] == 1) {
            cout << i << " ";
        }
    }
    cout << "\n";
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

//#define endl '\n'
#define pb push_back
#define ll long long
#define int ll
#define FF first
#define SS second
#define f(a,b,c) for(int a=b;a<=c;a++)
#define fd(a,b,c) for(int a=b;a>=c;a--)
#define fspr(X) fixed<<setprecision(X)

int mod = 1000000007;

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}

signed main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1;//cin>>T;
   	while(T--)
    {
    	int n,m;cin>>n>>m;
    	int A[n],B[m];
    	f(i,0,n-1)cin>>A[i];f(i,0,m-1)cin>>B[i];
    	int i=0,j=0;
    	while(i<n || j<m){
    		if(i>=n){
    			while(j<m){
    				cout<<B[j]<<" ";j++;
				}
			}
			if(j>=m){
    			while(i<n){
    				cout<<A[i]<<" ";i++;
				}
			}
    		while(i<n && j<m && A[i]<B[j]){
    			cout<<A[i]<<" ";i++;
			}
			while(j<m && i<n && A[i]>B[j]){
				cout<<B[j]<<" ";j++;
			}
			while(i<n && j<m && A[i]==B[j]){
				i++;j++;
			}
		}
		cout<<endl;
    }
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+5;
int a[maxn];
int b[maxn];
int flag[maxn];
void solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i]*b[i];
    }
    if(sum==0)
        cout<<"Yes";
    else
        cout<<"No";
}
signed main() {
    cin.tie(),cout.tie();
    int _=1;
    //cin>>_;
    while(_--)
    {
        solve();
    }
}

#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 mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((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;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "{";
	rep(i, v.size()) {
		if (i) os << ",";
		os << v[i];
	}
	os << "}";
	return os;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
	rep(i, v.size()) { is >> v[i]; }
	return is;
}
const long long INFLL = 1LL << 60;
const int INF         = 1 << 30;
const double PI       = acos(-1);
int main() {
	int n;
	ll ans = 0LL;
	cin >> n;
	V a(n), b(n);
	cin >> a >> b;
	rep(i, n) { ans += a[i] * b[i]; }
	cout << solve(ans == 0LL) << endl;
}


#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0;
	bool zf=0;
	char c;
	while(((c=getchar())<'0'||c>'9')&&c!='-');
	if(c=='-')zf=1;
	else res=c-'0';
	while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
	if(zf)return -res;
	return res;
}
const int maxn=4e5+5,P=1e9+7;
int a[maxn];
int jc[maxn],inv[maxn];
inline int c(int x,int y){
	if(x<y){
		return 0;
	}
	y=min(y,x-y);
	if(!y){
		return 1;
	}
	return x;
}
int C(int x,int y){
	if(!y){
		return 1;
	}
	return C(x/3,y/3)*c(x%3,y%3)%3;
}
signed main(){
	int n=read();
	string s;cin>>s;
	for(register int i=0;i<n;++i){
		if(s[i]=='R'){
			a[i+1]=0;
		}
		else if(s[i]=='W'){
			a[i+1]=1;
		}
		else{
			a[i+1]=2;
		}
	}
	if(!(n&1)){
		for(register int i=1;i<n;++i){
			if(a[i]!=a[i+1]){
				a[i]=3-a[i]-a[i+1];
			}
		}
		--n;
	}
	int A=0;
	for(register int i=1;i<=n;++i){
		A=(A+a[i]*C(n-1,i-1))%3;
	}
	if(A==0){
		puts("R");
	}
	else if(A==1){
		puts("W");
	}
	else{
		puts("B");
	}
	return 0;
}

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>

using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;

#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)

#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)

ll g1(vl val)
{
    ll Max = 0;
    ll MaxNum=-1;
    ll N =10;
    MFOR(i, 1, N)
    {
        if(val[i]>0)
        {
            MaxNum = i;
            break;
        }
    }
    if(MaxNum==-1)
    {
        // val = 0
        return 0;
    }
    Max = MaxNum;
    val[MaxNum] --;
    MREP(i, N)
    {
        while(val[i]>0)
        {
            Max = Max*10+i;
            val[i] --;
        }
    }
    // cout << "Max:" << Max << endl;
    return Max;
}

ll g2(vl val)
{
    ll Max = 0;
    ll Min = 0;
    ll MinNum=-1;
    ll N =10;

    REP(i, N)
    {
        while(val[i]>0)
        {
            Min = Min*10+i;
            val[i] --;
        }
    }
    // cout << "Min:" << Min << endl;
    return Min;
}

vl make_vals(ll N)
{
    vl vals(10, 0);
    ll Min = N%10;
    vals[Min] ++;
    N/=10;
    while(N >0)
    {
        Min = N%10;
        vals[Min] ++;
        N /= 10;

    }
    return vals;
}

int main(void)
{

    ll  N, K;

    cin >> N >> K;

    vl NowVal = make_vals(N);
    vl anss;

    if(K==0)
    {
        cout << N << endl;
    }
    else
    {
        map<ll, ll> Val_Times;
        ll NowN = N;
        Val_Times[NowN] = 1;
        anss.push_back(N);
        ll Tim = 1;
        ll NextN = g1(NowVal)-g2(NowVal);
        vl NextVal = make_vals(NextN);

        anss.push_back(NextN);
        while(Val_Times[NowN] < K && NextN >0)
        {

            NowVal = NextVal;
            NowN = NextN;

            Val_Times[NextN] = ++Tim;

            // cout << NextN << " " << Tim << endl;
            NextN =g1(NowVal)-g2(NowVal);
            NextVal = make_vals(NextN);
            anss.push_back(NextN);
        }

        if(NextN==0)
        {
            cout << 0 << endl;
        }
        else
        {
            ll Ans = g1(NowVal)-g2(NowVal);
            cout << Ans << endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define w(t) ll t;cin>>t;while(t--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define fo(i,a,n) for(ll i=a;i<n;i++)
#define precision cout<<fixed<<setprecision(10)
const ll mod=1e9+7;
using namespace std;
int main()
{
	IOS
	ll n,s=0,r;
	cin>>n;
  	fo(i,0,n)
    {
      ll a,b;
      cin>>a>>b;
      r=((b*(b+1))/2)-((a*(a-1))/2);
      s=s+r;
    }
  	cout<<s;
}

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

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

const int INF=1e9;
const int MOD=1e9+7;

int main(){
    int N; cin >> N;
    ll sum=0;
    for(int i=0;i<N;i++){
        ll a,b; cin >> a >> b;
        sum+=(a+b)*(b-a+1)/2;
    }
    cout << sum << endl;
}


#include<iostream>
#include<stdlib.h>
#include<string>
#include<iomanip>
using namespace std;
int main() {
    int si, sj;
    cin >> si >> sj;
    int t[51][51],p[51][51],i,j,k;
    for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) cin >> t[i][j];
    for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) cin >> p[i][j];
    string idou;
    int idoumax=0,ugoku=0;
    int hikaku[5];
    //動けるところで一番大きいところを探して動く
    while (1) {
        //探す
        for (i = 0; i < 4; i++) {
            //上
            if (i == 0) {
                if (sj - 1 >= 0) {
                    if (t[si][sj] != t[si][sj - 1]) hikaku[0] = p[si][sj - 1];
                    else {
                        hikaku[0] = -1;
                        p[si][sj - 1] = -1;
                    }
                }
                else {
                    hikaku[0] = -1;
                    p[si][sj - 1] = -1;
                }
            }
            //左
            else if (i == 1) {
                if(si-1>=0){
                    if (t[si][sj] != t[si - 1][sj]) hikaku[1] = p[si - 1][sj];
                    else {
                        hikaku[1] = -1;
                        p[si - 1][sj] = -1;
                    }
                }
                else {
                    hikaku[1] = -1;
                    p[si - 1][sj] = -1;
                }
            }
            //下
            else if (i == 2) {
                if(sj+1<=49){
                    if (t[si][sj] != t[si][sj + 1]) hikaku[2] = p[si][sj + 1];
                    else {
                        hikaku[2] = -1;
                        p[si][sj + 1] = -1;
                    }
                }
                else {
                    hikaku[2] = -1;
                    p[si][sj + 1] = -1;
                }
            }
            //右
            else {
                if(si+1<=49){
                    if (t[si][sj] != t[si + 1][sj]) hikaku[3] = p[si + 1][sj];
                    else {
                        hikaku[3] = -1;
                        p[si + 1][sj] = -1;
                    }
                }
                else {
                    hikaku[3] = -1;
                    p[si + 1][sj] = -1;
                }
            }
        }
        idoumax = -1;
        for (int i = 0; i < 4; i++) if (idoumax < hikaku[i]) idoumax = hikaku[i];
        if (idoumax == -1) break;
        for (i = 0; i < 4; i++){
            if (idoumax == hikaku[i]) {
                ugoku = i;
                break;
            }
        }
        //動く
        //上
        if (ugoku == 0) {
            idou += 'L';
            p[si][sj] = -1;
            sj--;
        }
        //左
        else if (ugoku == 1) {
            idou += 'U';
            p[si][sj] = -1;
            si--;
        }
        //下
        else if (ugoku == 2) {
            idou += 'R';
            p[si][sj] = -1;
            sj++;
        }
        //右
        else if (ugoku == 3) {
            idou += 'D';
            p[si][sj] = -1;
            si++;
        }
        else break;
    }
    cout << endl;
    cout << idou << endl;
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const double PI = 3.141592653589793238;
int main(){
  auto start = chrono::system_clock::now();
  ll si, sj;
  cin >> si >> sj;
  ll nowi = si, nowj = sj;
  vector<ll> t(2500);
  for (ll i = 0; i < 50; ++i) {
    for (ll j = 0; j < 50; ++j) {
      cin >> t[50 * i + j];
    }
  }

  vector<ll> p(2500);
  for (ll i = 0; i < 50; ++i) for (ll j = 0; j < 50; ++j) cin >> p[50 * i + j];

  string lastans = "";
  ll lastpnt = 0;
  map<ll, bool> used;
  string ans = "";
  while (true) {
    ll id = 50 * nowi + nowj;
    ll maxi = -1;
    used[t[id]] = true;
    char togo = ' ';
    if (nowj != 0 && !used[t[id - 1]] && maxi < p[id - 1]) {
      togo = 'L';
      maxi = p[id - 1];
    }
    if (nowj != 49 && !used[t[id + 1]] && maxi < p[id + 1]) {
      togo = 'R';
      maxi = p[id + 1];
    }
    if (nowi != 0 && !used[t[id - 50]] && maxi < p[id - 50]) {
      togo = 'U';
      maxi = p[id - 50];
    }
    if (nowi != 49 && !used[t[id + 50]] && maxi < p[id + 50]) {
      togo = 'D';
      maxi = p[id + 50];
    }
    if(togo != ' ')ans += togo;
    switch (togo)
    {
    case 'L': --nowj;
      break;
    case 'R': ++nowj;
      break;
    case 'U': --nowi;
      break;
    case 'D': ++nowi;
      break;
    default:
      goto exit;
    }
  }
exit:;
  nowi = si, nowj = sj;
  lastans = ans;
  ll now = 50 * nowi + nowj;
  lastpnt += p[now];
  for (ll i = 0; i < ans.size(); ++i) {
    switch (ans[i]) {
    case 'L': --nowj;
      break;
    case 'R': ++nowj;
      break;
    case 'U': --nowi;
      break;
    case 'D': ++nowi;
      break;
    }
    lastpnt += p[now];
  }

  string sample = "LRUD";
  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> cha(0, 3), rand(1,1000);
  auto dur = chrono::system_clock::now() - start;

  double start_temp = 200, end_temp = 10;
  while (chrono::duration_cast<std::chrono::milliseconds>(dur).count() < 1980) {
    ans = lastans;

    uniform_int_distribution<> change(0, ans.size());
    ll ch = change(mt), at = cha(mt), r = rand(mt);
    if (ch == ans.size()) ans.push_back(sample[at]);
    else ans[ch] = sample[at];

    nowi = si, nowj = sj;
    now = 50 * nowi + nowj;
    map<ll, bool> used;
    used[t[now]] = true;
    ll pnt = p[now];
    for (ll i = 0; i < ans.size(); ++i) {
      switch (ans[i]) {
      case 'L': --nowj;
        break;
      case 'R': ++nowj;
        break;
      case 'U': --nowi;
        break;
      case 'D': ++nowi;
        break;
      }
      if (nowi < 0 || 49 < nowi || nowj < 0 || 49 < nowj) {
        pnt = -1;
        break;
      }
      now = 50 * nowi + nowj;
      if (used[t[now]]) {
        pnt = -1;
        break;
      }
      used[t[now]] = true;
      pnt += p[now];
    }
    double temp = start_temp + (end_temp - start_temp) * chrono::duration_cast<std::chrono::milliseconds>(dur).count() / 2000;
    double prob = exp((pnt - lastpnt) / temp);

    if (pnt >= lastpnt) {
      lastpnt = pnt;
      lastans = ans;
    }
    else if(pnt != -1 && r <= prob * 1000){
      lastpnt = pnt;
      lastans = ans;
    }
    dur = chrono::system_clock::now() - start;
  }
  cout << lastans << "\n";
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+100;
int n,a[N];
int sum[N];
signed main(){
    ios::sync_with_stdio(false),cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        sum[i]=sum[i-1]+a[i];
    }
    double ans=1e18;
    for(int i=1;i<=n;i++){
        double x=1.0*a[i]/2;
        ans=min(ans,x*i+(sum[n]-sum[i])-(n-i)*x);
    }
    cout<<fixed<<setprecision(20)<<ans/n<<endl;
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e12
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
// LMAX = 18446744073709551615 (1.8*10^19)
// IMAX = 2147483647 (2.1*10^9)
/*

*/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  ll A[100010];
  rep(i, N) cin >> A[i];
  ld high = oo;
  ld low = 0.0;
  ld mh = (high*2.0 + low)/3.0;
  ld ml = (high + low * 2.0)/3.0;
  ld ans = oo;
  while (high > low + 0.00001){
    ld lost1 = 0.0;
    ld lost2 = 0.0;
    rep(i, N){
      lost1 += mh + A[i] - min(2.0*mh, ld(A[i]));
      lost2 += ml + A[i] - min(2.0*ml, ld(A[i]));
    }
    if (lost1 > lost2) high = mh;
    else low = ml;
    mh = (high*2.0 + low)/3.0;
    ml = (high + low * 2.0)/3.0;
    chmin(ans, lost1/N);
    chmin(ans, lost2/N);
  }
  ans += 0.000000001;
  cout << ans << endl;
}


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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("input.in","r",stdin);
    int n,x;
    cin>>n>>x;
    while(n--){
        char ch;
        cin>>ch;
        if(ch=='o') x++;
        else x=max(0,x-1);
    }
    cout<<x<<endl;
}

#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define pdd pair<double,double>
#define pii pair<int,int>
#define pll pair<LL,LL>
#define mp make_pair
#define LL long long
#define ULL unsigned long long
#define sqr(x) ((x)*(x))
#define pi acosl(-1)
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
using namespace std;
#define Point pdd
#define Polygon vector<Point>
#define Line pair<Point,Point>
#define rank Rank
bool solve(){
    int n,k;
    scanf("%d %d",&n,&k);
    int cnt[300005];
    fill(cnt,cnt+n+1,0);
    for(int i = 0;i<n;i++){
        int x;
        scanf("%d",&x);
        cnt[x]++;
    }
    int last=k;
    LL ans=0;
    for(int i = 0;i<n;i++){
        ans+=max(last-cnt[i],0)*i;
        last=min(last,cnt[i]);
    }
    printf("%lld\n",ans);
    return true;
}
int main(){
    int t=1;//00000;
  //  scanf("%d",&t);
    while(t--)solve();
}
/*
1 4 10100
1 6 1010100
1 8 101010100
1 y 10100  (y+1)  y/2
2 5 1100100
2 6 11001100
2 7 110011000
2 8 1100110000
2 9 11001100100
*/


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<ctime>
using std::max;
using std::min;
using std::sort;
using std::swap;
typedef long long LL;
int T;
LL N;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
	#endif
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld",&N);
		if(N&1) puts("Odd");
		else
		{
			if(N%4==0) puts("Even");
			else puts("Same");
		}
	}
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;
//const int mod = 998244353;

//MINT
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

// POWER_MODver. N^k % MOD
ll mod_pow(ll n, ll k){
    ll res = 1;
    for(; k > 0; k >>= 1){
        if(k&1) res = (res*n)%mod;
        n = (n*n)%mod;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    vector<mint> a(n);
    rep(i, 0, n) cin >> a[i];
    vector<vector<mint>> dp(n, vector<mint> (2));
    vector<vector<mint>> dpp(n+1, vector<mint> (2));
    dpp[0][0] = 1;
    rep(i, 1, n){
        dpp[i][0] += dpp[i-1][0] + dpp[i-1][1];
        dpp[i][1] += dpp[i-1][0];
    }
    dp[0][0] = a[0];
    rep(i, 1, n){
        dp[i][0] += dp[i-1][0] + a[i]*mint(dpp[i-1][0]) + dp[i-1][1] + a[i]*mint(dpp[i-1][1]);
        dp[i][1] += dp[i-1][0] - a[i]*mint(dpp[i-1][0]);
    }
    cout << dp[n-1][0] + dp[n-1][1] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i64 i=0; i<n; i++)
#define all(v) v.begin(),v.end()
#define pp pair<int,int>
using Graph=vector<vector<int>>;
using i64=int64_t;

int main() {
    int n,m;cin>>n>>m;
    rep(i,m) {
        string s;
        cin>>s;
    }
    rep(i,20) {
        rep(j,20) {
            cout << "H";
        }
        cout << endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n';
const ll nmax = 1000000007;

int main(){

    ll a, b;
    cin >> a >> b;
    ll sa = 0, sb = 0;

    while(a > 0){
        sa += (a%10);
        a /= 10;
    }while(b > 0){
        sb += (b%10);
        b /= 10;
    }

    cout << (max(sa, sb)) << endl;
	return 0;
}


#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

template <typename T>
struct Dijkstra{
    struct Edge{
        int to;
        T cost;
        Edge(int to,T cost):to(to),cost(cost){}
        bool operator<(const Edge &o)const{return cost>o.cost;}
    };
    const T inf = (numeric_limits<T>::max())/2;//infを設定する
    vector<vector<Edge>> G;
    vector<T> ds;
    vector<int> bs;
    Dijkstra(int n):G(n){}

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

    void build(int s){
        int n=G.size();
        ds.assign(n,inf);
        bs.assign(n,-1);

        priority_queue<Edge> pq;
        ds[s]=0;
        pq.emplace(s,ds[s]);

        while(!pq.empty()){
            auto p=pq.top();pq.pop();
            int v=p.to;
            if(ds[v]<p.cost) continue;
            for(auto e:G[v]){
                if(ds[e.to]>ds[v]+e.cost){
                    ds[e.to]=ds[v]+e.cost;
                    bs[e.to]=v;
                    pq.emplace(e.to,ds[e.to]);
                }
            }
        }
    }

    T operator[](int k){return ds[k];}

    vector<int> restore(int to){
        vector<int> res;
        if(bs[to]<0) return res;
        while(~to) res.emplace_back(to),to=bs[to];
        reverse(res.begin(),res.end());
        return res;
    }
};

int n,m,k;
int c[20];
int d[20][20];
int dp[200010][20];

void solve(){
    cin >> n >> m;
    Dijkstra<int> dij(n);
    rep(i,m){
        int a,b;cin >> a >> b;a--;b--;
        dij.add_edge(a,b,1);
        dij.add_edge(b,a,1);
    }
    cin >> k;
    rep(i,k) {
        cin >> c[i];c[i]--;
    }
    rep(i,k){
        dij.build(c[i]);
        rep(j,k){
            d[i][j]=dij[c[j]];
        }
    }
    int U=((1<<k)-1);
    rep(S,U+1){
        rep(i,k) dp[S][i]=mod;
    }
    rep(i,k) dp[(1<<i)][i]=1;
    rep(S,U+1){
        rep(i,k){
            rep(j,k){
                if(S&(1<<i)) if(S&(1<<j)){
                    chmin(dp[S][i],dp[S^(1<<i)][j]+d[j][i]);
                }
            }
        }
    }
    int ans=mod;
    rep(i,k) chmin(ans,dp[U][i]);
    if(ans<mod)cout << ans << endl;
    else cout << -1 << endl;
}

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

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define uniq(x) x.resize(unique(all(x)) - x.begin())
#define ff first
#define ss second
#define pb push_back
#define emb emplace_back
using namespace std;
using ull = unsigned long long;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int N = 1e5 + 5, K = 17;
int n, m, k, c[K], dist[K][N], dp[1 << K][K];
queue<int> q;
vector<int> adj[N];

void bfs(int s, int d[]) {
  for (int i = 1; i <= n; ++i) d[i] = -1;
  d[s] = 0; q.emplace(s);
  while (!q.empty()) {
    int u = q.front(); q.pop();
    for (int &v: adj[u]) {
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        q.emplace(v);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for (int i = 1, a, b; i <= m; ++i) {
    cin >> a >> b;
    adj[a].emb(b);
    adj[b].emb(a);
  }
  cin >> k;
  for (int i = 0; i < k; ++i) {
    cin >> c[i];
    bfs(c[i], dist[i]);
  }
  for (int mk = 1; mk < (1 << k); ++mk) {
    if (__builtin_popcount(mk) == 1) {
      for (int i = 0; i < k; ++i) {
        if ((mk >> i) & 1) {
          dp[mk][i] = 1;
        } else {
          dp[mk][i] = -1;
        }
      }
    } else {
      for (int i = 0; i < k; ++i) {
        if ((mk >> i) & 1) {
          int omk = mk ^ (1 << i);
          dp[mk][i] = 1e9;
          for (int j = 0; j < k; ++j) {
            if (((omk >> j) & 1) && (dp[omk][j] != -1) && (dist[i][c[j]] != -1)) {
              dp[mk][i] = min(dp[mk][i], dp[omk][j] + dist[i][c[j]]);
            }
          }
          if (dp[mk][i] == 1e9) {
            dp[mk][i] = -1;
          }
        } else {
          dp[mk][i] = -1;
        }
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < k; ++i) {
    if (dp[(1 << k) - 1][i] != -1) {
      ans = min(ans, dp[(1 << k) - 1][i]);
    }
  }
  if (ans == 1e9) {
    ans = -1;
  }
  cout << ans << "\n";
  return 0;
}