#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "poetry"
#define pll pair<ll, ll>
#define pii pair< ll, pair< ll, ll > >
#define fi first
#define se second

using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 5e3+5;
const ll mod = 1e9+7;
const ll modx = 2019201913;
const ll mody = 2019201949;
ll m, n, k, T, u, v, tong, lab[N];
ll dp[N][N], a[N], d[N], c[N], b[N], ans, len[N];
string s, t ;
vector<ll> kq, adj[N], adt[N], res;

void dfs(ll u)
{
    kq.pb(u);
    a[u] = -1;
    for(ll v : adj[u])
    {
        if(a[v] == 0)dfs(v);
    }
}

void sol()
{
    /*
    cin >> n;
    for(int i = 0; i < n; i ++)cin >> s[i];
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < n; j ++)
        {
            if(s[i][j] == '1')
            {
                adj[i].pb(j);
                adt[j].pb(i);
            }
        }
    }
    for(int i = 0; i < n; i ++)if(a[i] == 0)dfs(i);
    for(int i = kq.size()-1; i >= 0; i --)
    {
        if(a[kq[i]] == 1)
        {
            ++k;
            bfs(kq[i], k);
        }
    }
    cout << k << '\n';
    for(ll u : res)cout << u <<" ";
    */
    queue<ll> ds;
    cin >> n >> s >> t;
    if(s[0] != t[0])
    {
        ds.push(1);
    }
    for(int i = 2; i <= n; i ++)
    {
        if(s[i-1] == '1')
        {
            if(!ds.empty())
            {
                k = ds.front();
                ds.pop();
                s[i-1] = '0';
                ans += i - k;
                k = 0;
            }
        }
        if(s[i-1] != t[i-1])ds.push(i);
    }
    if(!ds.empty())cout << -1;
    else cout << ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(fopen(task".in", "r"))
    {
		freopen(task".in","r",stdin);
		freopen(task".out","w",stdout);
    }
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
8 6 5
*/

#include<bits/stdc++.h>
using namespace std;
const int buffer_size=1e5+5;
char buf[buffer_size],*S,*T;
bool flag_EOF;
inline char read_char()
{
    if(S==T)
        T=(S=buf)+fread(buf,1,buffer_size,stdin);
    return S!=T?*(S++):EOF;
}
inline int read_int()
{
    int flag=1;
    char c=read_char();
    while(c<'0'||c>'9')
    {
        if(c==EOF)
        {
            flag_EOF=true;
            return 0;
        }
        flag=(c=='-'?-1:flag);
        c=read_char();
    }
    int x=0;
    while(c>='0'&&c<='9')
    {
        x=x*10+(c^48);
        c=read_char();
    }
    return x*flag;
}
inline void Write(int x)
{
    if(!x)
    {
        putchar('0');
        return;
    }
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    static char st[13];
    int head=0;
    while(x)
    {
        st[++head]=x%10+'0';
        x/=10;
    }
    while(head>0)
        putchar(st[head--]);
}
const int max_n=5e5+5;
char s[max_n],t[max_n];
int q[max_n],head=1,tail;
int main()
{
	int n;
	scanf("%d%s%s",&n,s+1,t+1);
	for(int i=1;i<=n;++i)
	{
		if(s[i]=='1')
			q[++tail]=i;
	}
	long long ans=0;
	for(int i=1;i<=n;++i)
	{
		if(head<=tail&&q[head]==i)
			++head;
		if(s[i]!=t[i])
		{
			if(head>tail)
			{
				printf("-1");
				return 0;
			}
			ans+=q[head]-i;
			s[q[head++]]='0';
		}
	}
	printf("%lld",ans);
	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 = 998244353;
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<int mod>
struct ModInt {
    long long x;
    static constexpr int MOD = mod;

    ModInt() : x(0) {}
    ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    explicit operator int() const {return x;}

    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }

    ModInt operator-() const { return ModInt(-x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    ModInt operator%(const ModInt &p) const { return ModInt(0); }

    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }

    ModInt inverse() const{
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return ModInt(u);
    }

    ModInt power(long long n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1)
            ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    ModInt power(const ModInt p) const{
        return ((ModInt)x).power(p.x);
    }

    friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
        return os << p.x;
    }
    friend istream &operator>>(istream &is, ModInt<mod> &a) {
        long long x;
        is >> x;
        a = ModInt<mod>(x);
        return (is);
    }
};

using modint = ModInt<mod>;

int n,m,k;
modint power[200010];

void solve(){
    cin >> n >> m >> k;
    if(n==1){
        modint ans=0;
        Rep(V,1,k+1){
            ans+=((modint)(k-V+1)).power(m)-((modint)(k-V)).power(m);
        }
        cout << ans << endl;
        return;
    }
    modint ans=0;
    Rep(V,1,k+1){
        power[V]=((modint)V).power(n);
    }
    if(m==1){
        Rep(V,1,k+1){
            ans+=power[V]-power[V-1];
        }
        cout << ans << endl;
        return;
    }
    Rep(V,1,k+1){
        ans+=(power[V]-power[V-1])*((modint)(k-V+1)).power(m);
    }
    cout << ans << endl;
}

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

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

#define forn(i, n) for(int i=0; i<(n); i++)
#define readVec(v) forn(i, v.size()){cin >> v[i];}
#define printArr(arr, n) forn(i, n){if (i) cout << " "; cout << arr[i];} cout << endl;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f first
#define s second

typedef long long ll;
typedef double ld;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vii;

//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
  cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}

//2 147 483 647 int max
//9 223 372 036 854 775 807 ll max
void fast_io(){
   ios_base::sync_with_stdio(0);
   cin.tie(NULL);
   cout.tie(NULL);
}
void file_io(string taskname){
   string fin = taskname + ".in";
   string fout = taskname + ".out";
   const char* FIN = fin.c_str();
   const char* FOUT = fout.c_str();
   freopen(FIN, "r", stdin);
   freopen(FOUT, "w", stdout);
   fast_io();
}

struct DSU{
    vi parent;//Parent of each element. If parent[i] = i, it is a root of a set
    vi num_elements;//Number of elements in a set given by a root.
    int n;

    DSU(int n):n(n),parent(n),num_elements(n,1){
        forn(i,n){
            parent[i] = i;
        }
    }

    //Merge the two sets that x and y are in.
    void join(int x, int y){
        if(parent[x] == parent[y]){
            return; //already in same set
        }
        int xroot = root(x);
        int yroot = root(y);

        if(num_elements[xroot] < num_elements[yroot]){
            parent[xroot] = yroot;
            num_elements[yroot] += num_elements[xroot];
        }else{
            parent[yroot] = xroot;
            num_elements[xroot] += num_elements[yroot];
        }
    }

    //Find root of an element. Uses path splitting for efficiency. (compresses path)
    int root(int x){
        while (parent[x] != x){
            int prev = x;
            x = parent[x];
            parent[prev] = parent[x];
        }
        return x;
    }

    //Whether two elements are in the same set.
    bool together(int x, int y){
        return root(x) == root(y);
    }
};

int main(){
    fast_io();

    int n, m;
    cin >> n >> m;
    DSU d(n+m);
    forn(i, n){
      forn(j, m){
        char c;
        cin >> c;
        if(c=='#'){
          d.join(i, n+j);
        }
      }
    }
    d.join(0, n);
    d.join(n-1, n+m-1);
    d.join(0, n-1);
    int c = 0;
    int nsing=0, msing=0;
    forn(i, n+m){
      if(d.parent[i] == i){
        c++;
        if(d.num_elements[i] == 1){
          if(i<n) nsing++;
          else msing++;
        }
      }
    }
    cout << c-max(nsing, msing)-1 << "\n";
}


#pragma clang optimize on
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define shift 30

inline int get_digit() {
    int x = 0, f = 1;
    char c = getchar();
    while(c > '9' || c < '0') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x * 10) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}

int main() {
    int R = get_digit(), C = get_digit();
    int A[250000], B[250000];
    int dist[500000], seen[500000], cost, pt, to_hide = R * C;
    vector<ll> s;
    s.reserve(2 * R * C);
    priority_queue<ll, vector<ll>, greater<ll>> queue(s.begin(), s.end());
    for(int i = 0; i < R * C; ++i)
        if(i % C != C - 1) A[i] = get_digit();
    for(int i = 0; i < (R - 1) * C; ++i) B[i] = get_digit();
    dist[0] = -10000000;
    queue.push((ll)(-10000000) << shift);
    while(queue.size()) {
        pt = queue.top() & ((1 << shift) - 1);
        if(pt == to_hide - 1) break;
        cost = queue.top() >> shift;
        queue.pop();
        seen[pt] = true;
        if(pt < to_hide) {
            if(!seen[pt + 1] && pt % C != C - 1) {
                if(cost + A[pt] < dist[pt + 1]) {
                    dist[pt + 1] = cost + A[pt];
                    queue.push(((ll)dist[pt + 1] << shift) | (pt + 1));
                }
            }
            if(!seen[pt - 1] && pt % C != 0) {
                if(cost + A[pt - 1] < dist[pt - 1]) {
                    dist[pt - 1] = cost + A[pt - 1];
                    queue.push(((ll)dist[pt - 1] << shift) | (pt - 1));
                }
            }
            if(!seen[pt + C] && pt < to_hide - C) {
                if(cost + B[pt] < dist[pt + C]) {
                    dist[pt + C] = cost + B[pt];
                    queue.push(((ll)dist[pt + C] << shift) | (pt + C));
                }
            }
            if(!seen[pt + to_hide] && cost + 1 < dist[pt + to_hide]) {
                dist[pt + to_hide] = cost + 1;
                queue.push(((ll)dist[pt + to_hide] << shift) | (pt + to_hide));
            }
        } else {
            if(to_hide < pt - C && !seen[pt - C] && cost + 1 < dist[pt - C]) {
                dist[pt - C] = cost + 1;
                queue.push(((ll)(cost + 1) << shift) | (pt - C));
            }
            if(!seen[pt - to_hide] && cost < dist[pt - to_hide]) {
                dist[pt - to_hide] = cost;
                queue.push(((ll)cost << shift) | (pt - to_hide));
            }
        }
    }
    printf("%d\n", dist[to_hide - 1] - dist[0]);
}

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

vector<int>G[22];
int ban[maxn];
int n, m;
set<int>se;
void dfs(int nowid, int sta) {
    if (nowid == n + 1) {
        se.insert(sta);
        return;
    }
    if (ban[nowid]) {
        dfs(nowid + 1, sta);
        return;
    }
    //不选
    dfs(nowid + 1, sta);
    //选
    for (int d : G[nowid])
        ban[d]++;
    dfs(nowid + 1, sta + (1 << nowid));
    for (int d : G[nowid])
        ban[d]--;
}

int del[22];
int vis[22];
//找有无奇环
int flag = 0;
int col[22];
void bfs(int nowid) {
    memset(col, -1, sizeof(col));
    col[nowid] = 0;
    queue<int>q;
    q.push(nowid);
    while (!q.empty()) {
        int nowid = q.front();
        vis[nowid] = 1;
        q.pop();
        for (int d : G[nowid]) {
            if (del[d])
                continue;
            if (col[d] == -1) {
                col[d] = col[nowid] ^ 1;
                q.push(d);
            }
            else if(col[d] == col[nowid]){
                flag = 1;
                return;
            }
        }
    }
}

void solve() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(1, 0);
    ll ans = 0;
    ll cnt = 0;

    //1号颜色
    for (int sta : se) {
        for (int i = 1; i <= n; i++) {
            if ((sta >> i) & 1)
                del[i] = 1;
            else
                del[i] = 0;
            vis[i] = 0;
        }
        cnt = 1ll;
        for (int i = 1; i <= n; i++) {
            if (vis[i] || del[i])
                continue;
            flag = 0;
            bfs(i);
            if (flag == 1) {
                cnt = 0;
                break;
            }
            cnt *= 2ll;
        }
        ans += cnt;
    }
    cout << ans << endl;
}

signed main() {
    // freopen("in.txt", "r", stdin);
    solve();
}


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

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define STRING(str) #str
#define ll long long
#define ld long double

template <typename T>
void print(T x, string name) {
	cout << name+": " << x << endl;
}

template <typename T >
void print_vec_1d(vector<T> x, string name) {
	cout << name << endl;
	cout << "[ ";
	for (int i=0;i<x.size();i++) {
		cout << x[i];
		if (i != x.size()-1) cout << ", ";
		else cout << " ]" << endl;
	}
}

template <typename T >
void print_vec_2d(vector<vector<T>> x, string name) {
	cout << name << endl;
	cout << "[ ";
	for (int i=0;i<x.size();i++) {
		cout << "[ ";
		for (int j=0;j<x[i].size();j++) {
			cout << x[i][j];
			if (j != x[i].size()-1) cout << ", ";
			else {
				cout << " ]";
				if (!((i == x.size()-1) && (j == x[i].size()-1))) {
					cout << "," << endl;
				}
			}
		}
	}
	cout << " ]" << endl;
}

template <typename T >
void print_vec_3d(vector<vector<vector<T>>> x, string name) {
	cout << name << endl;
	cout << "[ ";
	for (int i=0;i<x.size();i++) {
		cout << "[ ";
		for (int j=0;j<x[i].size();j++) {
			cout << "[ ";
			for (int k=0;k<x[i][j].size();k++) {
				cout << x[i][j][k];
				if (k != x[i][j].size()-1) cout << ", ";
				else {
					cout << " ]";
					if (!((i == x.size()-1) && (j == x[i].size()-1) && (k == x[i][j].size()-1))) {
						cout << "," << endl;
					}
				}
			}
			if (j != x[i].size()-1) cout << ", ";
			else {
				cout << " ]";
				if (!((i == x.size()-1) && (j == x[i].size()-1))) {
					cout << "," << endl;
				}
			}
		}
	}
	cout << " ]" << endl;
}

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

	int N;
	cin >> N;

	int n = (1<<N)-1;
	vector<string> ans_str;
	for (int i=0;i<n;i++) {
		string tmp = "";
		for (int j=0;j<(1<<N);j++) tmp += "B";
		ans_str.push_back(tmp);
	}

	for (int i=1;i<(1<<N);i++) {
		for (int j=0;j<(1<<N);j++) {
			if (__builtin_popcount(j&i)%2 == 1) ans_str[i-1][j] = 'A';
 		}
	}

	cout << n << "\n";
	for (int i=0;i<n;i++) {
		cout << ans_str[i] << "\n";
	}
}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

vector<string> rec(int n){
  if(n==2) return vector<string> (1,"AB");
  vector<string> base=rec(n/2);
  vector<string> revbase=base;
  rep(i,revbase.size()) rep(j,revbase[i].size()){
    if(revbase[i][j]=='A') revbase[i][j]='B';
    else revbase[i][j]='A';
  }

  vector<string> result;
  rep(i,base.size()) result.push_back(base[i]+base[i]);
  rep(i,base.size()) result.push_back(base[i]+revbase[i]);
  result.push_back(string(n/2,'A')+string(n/2,'B'));
  return result;
}

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

  int N;
  cin>>N;
  int M=(1<<N);

  vector<string> output=rec(M);
  cout<<output.size()<<"\n";
  rep(i,output.size()) cout<<output[i]<<"\n";

  return 0;
}


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

// #define NDEBUG
#include<cassert>

namespace Elaina{
    #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
    #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
    #define fi first
    #define se second
    #define mp(a, b) make_pair(a, b)
    #define Endl putchar('\n')
    #define mmset(a, b) memset(a, b, sizeof a)
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    template<class T>inline T fab(T x){ return x<0? -x: x; }
    template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); }
    template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); }
    template<class T>inline T readin(T x){
        x=0; int f=0; char c;
        while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
        for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
        return f? -x: x;
    }
    template<class T>inline void writc(T x, char s='\n'){
        static int fwri_sta[1005], fwri_ed=0;
        if(x<0) putchar('-'), x=-x;
        do fwri_sta[++fwri_ed]=x%10, x/=10; while(x);
        while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed);
        putchar(s);
    }
}
using namespace Elaina;

ll n, N;

ll fib[100]; // fib[82]=99194853094755497

int cnt[100];

vector<pii>need;
vector<int>cmd;

signed main(){
    fib[0]=fib[1]=1;
    rep(i, 2, 82) fib[i]=fib[i-1]+fib[i-2];
    N=n=readin(1ll);
    drep(i, 82, 1) if(n>=fib[i]){
        cnt[i]=n/fib[i];
        n=n%fib[i];
    }
    int step=0;
    rep(i, 1, 82) if(cnt[i]){
        // printf("need (%d, %d)\n", i, cnt[i]);
        need.push_back(mp(i, cnt[i]));
        step=i;
    }
    ll x=0, y=0; int pre;
    if(need.back().fi%2==0){
        x=need.back().se, pre=3;
        while(need.back().se)
            cmd.push_back(1), --need.back().se;
    }
    else{
        y=need.back().se, pre=4;
        while(need.back().se)
            cmd.push_back(2), --need.back().se;
    }
    // printf("init :> x == %lld, y == %lld\n",  x, y);
    need.pop_back();
    rep(i, 1, step){
        if(pre==4) cmd.push_back(pre=3), x=x+y;
        else cmd.push_back(pre=4), y=x+y;
        if(step-i==need.back().fi){
            if(need.back().fi%2==0){
                x+=need.back().se;
                while(need.back().se)
                    cmd.push_back(1), --need.back().se;
            }
            else{
                y+=need.back().se;
                while(need.back().se)
                    cmd.push_back(2), --need.back().se;
            }
            need.pop_back();
        }
    }
    // printf("x == %lld, y == %lld\n",  x, y);
    writc((int)cmd.size());
    for(int i=0, siz=cmd.size(); i<siz; ++i)
        writc(cmd[i]);
    return 0;
}

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

static const int N = 200005;

int n;
static std::pair<int, int> a[N], b[N];
static int p[N];
static int f[N];

void add(int i, int d) {
	for (++i; i < N; i += i & -i) {
		f[i] += d;
	}
}

int get(int i) {
	int s = 0;
	for (; i > 0; i -= i & -i) {
		s += f[i];
	}
	return s;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i].first);
		a[i].first += i;
		a[i].second = i;
	}
	for (int i = 0; i < n; ++i) {
		scanf("%d", &b[i].first);
		b[i].first += i;
		b[i].second = i;
	}
	std::sort(a, a + n);
	std::sort(b, b + n);
	for (int i = 0; i < n; ++i) {
		if (a[i].first != b[i].first) {
			printf("-1\n");
			return 0;
		}
		p[a[i].second] = b[i].second;
	}
	long long res = 0;
	for (int i = n - 1; i >= 0; --i) {
		res += get(p[i]);
		add(p[i], 1);
	}
	printf("%" PRId64 "\n", res);
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
using namespace std ;
int main(){
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
 	int n ;
 	cin >> n ;
 	ll ans = 0 ;
 	int t = 0 ;
 	while( ans < n ) {
 		t ++ ;
 		ans = ans + t ;
	 }
	 cout << t << endl ;

}

#include<bits/stdc++.h>

using namespace std;

#define PB push_back
#define fo(i0, n) for(i=0;i<n;i++)
#define fo(i1, n) for(i=1;i<=n;i++)

typedef long long ll;
typedef unsigned long long ull;

ull GCD(ull x,ull y){if(y==0) return x;else return GCD(y,x%y);}
ull LCM(ull a,ull b){return (a*b/(GCD(a,b)));}
bool PRIME(int p){if (p<=1)return false;if (p<=3)return true;if(p%2==0||p%3==0) return false;for(int i=5;p>=i*i;i=i+6){if(p%i==0||p%(i+2)==0) return false;}return true;}

int main()
{
    ll a,b,c,d,e,t,i,j,k,l,sum=0,ans=0,x,y,z=-1,m,n;
    string S,T,U;
    cin>>a;
    unordered_set<ll>A;
    for(i=2;i*i<=a;i++)
    {
        b=i*i;
        while(b<=a)
        {
            A.insert(b);
            b*=i;
        }
    }
    cout<<a-A.size()<<endl;

    return 0;
}


// Problem: C - Squared Error
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Date: 2021-03-09 12:52:00
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=3e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define IOS ios::sync_with_stdio(false),cin.tie(0)
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\n",a[n]);
}
ll a[N],b[N],c[N];
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i]+b[i-1],c[i]=c[i-1]+a[i]*a[i];
	ll s=0;
	for(int i=2;i<=n;i++){
		s+=(i-1)*a[i]*a[i]+c[i-1]-2*a[i]*b[i-1];
	}cout<<s<<'\n';
	return 0;
}

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

#ifdef NO_PROBLEM_MAIN
#define DPRINTF(x) printf x
#else
static inline void nop() { return; }
#define DPRINTF(x) nop()
#endif

#define SOLVER(x) solve_D x

static void s_shuffle( string &s, int r )
{
    int l = s.length();
    for ( int i = 0; i < r; i++ )
    {
        int x, y;
        x = random() % l;
        y = random() % l;
        swap( s[x], s[y] );
    }
}

static void s_solve(void)
{
    int si, sj, ti, tj;
    int x, y;
    string s;

    srandom(19750220);

    for ( int i = 0; i < 1000; i++ )
    {
        cin >> si >> sj >> ti >> tj;
        x = tj - sj;
        y = ti - si;
        s = "";
        if ( x > 0 )
        {
            for ( int j = 0; j < x; j++ )
            {
                s += "R";
            }
        }
        else
        {
            for ( int j = x; j < 0; j++ )
            {
                s += "L";
            }
        }
        if ( y > 0 )
        {
            for ( int j = 0; j < y; j++ )
            {
                s += "D";
            }
        }
        else
        {
            for ( int j = y; j < 0; j++ )
            {
                s += "U";
            }
        }

        s_shuffle( s, 100 );
        cout << s << endl;
        fflush(stdout);

        long long b;
        cin >> b;
    }
}

void SOLVER((volatile bool *do_loop))
{
    do {
        s_solve();
    } while ((do_loop != NULL) && (*do_loop));
}

#ifndef NO_PROBLEM_MAIN
int main(void)
{
    SOLVER((NULL));
    return 0;
}
#endif


#include<bits/stdc++.h>
using namespace std;
int main() {
	long long n,w,count=0;
	cin>>n>>w;
	long long a=w;
	for(int i=0;i<n;i++){
	    if(n>=a){
	        count++;
	    }else if(n<a){
	        break;
	    }
	    a+=w;
	}
	cout<<count<<endl;
}

#include <iostream>

using namespace std;

int main()
{
    int a,b,sum;
    cin>>a>>b;
    sum=a/b;
    cout <<sum;

    return 0;
}


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

typedef long long LL;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 2e5 + 10;

int a[N], res[N], pos[N];

void Swap(int p, int &idx) {
	swap(pos[a[p]], pos[a[p + 1]]);
	swap(a[p], a[p + 1]);
	res[idx++] = p;
}

void solve() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		pos[a[i]] = i;
	}
	int idx = 1;
	for(int i = n; i >= 1; i--) {
		if(a[i] == i) continue;
		while(idx % 2 != pos[i] % 2) {
			if(idx & 1) Swap(1, idx);
			else Swap(2, idx);
		}
		while(pos[i] != i) {
			Swap(pos[i], idx);
		}
	}
	printf("%d\n", idx - 1);
	for(int i = 1; i < idx; i++) {
		printf("%d ", res[i]);
	}
	puts("");
}

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE

    int t; cin >> t; while(t--)
    solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,a[505],b[505],pr[505*505];bool ty;
void Swap(int x,int y){b[a[x]]=y;b[a[y]]=x;swap(a[x],a[y]);pr[++*pr]=x;ty^=1;}
void Swap2(int x){Swap(x-1,x);Swap(x,x+1);Swap(x-1,x);Swap(x,x+1),Swap(x-1,x);}
int main(){
	for(int T=read;T--;){
		s=read;*pr=0;ty=1;
		for(int i=1;i<=s;++i)b[a[i]=read]=i;
		for(int i=1;i<s-1;++i){
			if(b[i]==i)continue;
			if((b[i]^ty)&1)
				for(int j=b[i]-1;j>=i;--j)Swap(j,j+1);
			else{
				int w=b[i]-1;
				if(w!=i)Swap(w-1,w);
				else if(w==s-3){
					Swap(w+1,w+2);
					Swap(w+2,w+3);
					Swap(w+1,w+2);
					w=s-1;
				}else if(w==s-2){Swap2(i);w=i-1;}
				else Swap(w+3,w+4);
				for(int j=w;j>=i;--j)Swap(j,j+1);
			}
		}if(a[s-1]>a[s])
			if((b[s-1]^ty)&1)Swap(b[s-1]-1,b[s-1]);
			else Swap2(s-1);
		printf("%d\n",*pr);
		for(int i=1;i<=*pr;++i)printf("%d ",pr[i]);
		putchar('\n');
	}
	return 0;
}/*

      x
1 3 2 5 4
a c b
c a b
c b a
b c a
b a c
a b c
*/


#include <bits/stdc++.h>  // clang-format off
using namespace std;
using i64 = int64_t;
using pii = pair<i64, i64>;
#define rep(i, n) for (i64 i = 0; i < i64(n); i++)
#define per(i, n) for (i64 i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (i64 i = a; i < i64(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#ifdef LOCAL
#define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__)
#else
#define dbg(...)
#endif
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
    os << '(' << pa.first << ',' << pa.second << ')'; return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    for (auto& x : vec) { os << x << ' '; } return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &mat) {
    os << endl; for (auto& vec : mat) { os << vec << endl; } return os;
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
    cerr << " " << H; debug(T...);
}
template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1 << 30) - 1;
const i64 LINF = (1LL << 60) - 1;
// clang-format on

void solve() {
    i64 n, m;
    cin >> n >> m;
    vector<i64> W(n);
    rep(i, n) cin >> W[i];
    vector<i64> L(m), V(m);
    rep(i, m) cin >> L[i] >> V[i];
    i64 ma = 0;
    rep(i, n) chmax(ma, W[i]);
    rep(i, m) if (ma > V[i]) {
        cout << -1 << endl;
        return;
    }

    vector<pii> bridges(m);
    rep(i, m) {
        bridges[i] = {V[i], L[i]};
    }
    bridges.push_back({0, 0});
    sort(all(bridges));
    rep(i, m) {
        chmax(bridges[i + 1].se, bridges[i].se);
    }

    vector<int> perm(n);
    iota(all(perm), 0);
    i64 ans = LINF;

    do {
        vector<vector<i64>> E(n, vector<i64>(n));
        rep(i, n) {
            i64 weight = 0;
            weight = W[perm[i]];
            REP(j, i + 1, n) {
                weight += W[perm[j]];
                int idx = lower_bound(all(bridges), (pii){weight, -LINF}) - bridges.begin();
                if (idx == 0)
                    continue;
                else
                    E[i][j] = bridges[idx - 1].se;
            }
        }
        vector<i64> dist(n);
        rep(i, n) REP(j, i + 1, n) {
            chmax(dist[j], dist[i] + E[i][j]);
        }
        chmin(ans, dist.back());
    } while (next_permutation(all(perm)));
    cout << ans << endl;
}

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

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

int main(){
  int n,x=0,y=0,ans=100000,c=0;
  cin>>n>>x>>y;
  y*=n;
  rep(i,0,n-1){
    cin>>ans;
    c+=ans;
  }
  if(y-c<=x){
    if(y-c<0) co(0);
    else co(y-c);
  }
  else co(-1);

  return 0;
}


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

#define rng(x) x.begin(), x.end()
#define maxi(x, y) x = max(x, (y))
#define mini(x, y) x = min(x, (y))
#define pb push_back
#define F first
#define S second
#define el '\n'
#define ll long long
#define SZ(x) ((int)(x).size())
template<typename T>
istream&operator>>(istream&is,vector<T>&v){for(auto&it:v)is>>it;return is;}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.F << "," << P.S << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
    os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout<<name<<" : "<<arg1<<'\n';}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cout.write(names,comma-names)<<" : "<<arg1<<" |";__f(comma+1, args...);}
typedef pair<int,int> pi; typedef vector<int> vi; typedef vector<vi> vvi;

/*-----------------------------Code begins----------------------------------*/

class BIT{
public :
	int n;
	vi f;
	BIT(int n){
		this->n = n;
		f.assign(n, 0);
	}

	int get(int pos){
		int res = 0;
		for (; pos >= 0; pos = (pos & (pos + 1)) - 1){
		    res += f[pos];
		}
		return res;
	}

	int get(int l, int r){
		return get(r) - (l ? get(l - 1) : 0);
	}

	void increase(int pos, int val){
		for (; pos < int(f.size()); pos = (pos | (pos + 1))) {
			f[pos] += val;
		}
	}
};

void solve(){
    int n, q; cin>>n>>q;
    vector <BIT> bit(30, BIT(n));
    for(int i = 0; i < n; ++i){
    	int val; cin>>val;
    	for(int b = 0; b < 30; ++b){
    		bit[b].increase(i, val >> b & 1);
    	}
    }
    while(q--){
    	int op; cin>>op;
    	if(op == 1){
    		int i, val; cin>>i>>val;
    		i--;
    		for(int b = 0; b < 30; ++b){
	    		bit[b].increase(i, val >> b & 1);
	    	}
    	}
    	else{
    		int l, r; cin>>l>>r;
    		l--, r--;
    		int ans = 0;
    		for(int b = 0; b < 30; ++b){
	    		ans += (bit[b].get(l, r) % 2) * (1 << b);
	    	}
	    	cout<<ans<<el;
    	}
    }
}

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

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

int main(void)
{
    int A;
    int B;
    int count=0;
    cin >> A >>B;

    vector<int> E;
    E = vector<int>(A+B);

    vector<int> E_A;
    E_A = vector<int>(A);

    vector<int> E_B;
    E_B = vector<int>(B);

    if(A==B){
        rep(i,A){
            E_A[i]=i+1;
            E_B[i]=E_A[i]*(-1);
        }
    }
    else if(A>B){
        rep(i,A){
            E_A[i]=i+1;
        }
        rep(i,B){
            if(i<B-1){
                E_B[i]=(i+1)*(-1);
            }
            else{
                E_B[i]=(std::accumulate(E_A.begin(), E_A.end(), 0)+std::accumulate(E_B.begin(), E_B.end(), 0))*(-1);
            }
        }
    }
    else{
        rep(i,B){
            E_B[i]=(i+1)*-1;
        }
        rep(i,A){
            if(i<A-1){
                E_A[i]=(i+1);
            }
            else{
                E_A[i]=(std::accumulate(E_A.begin(), E_A.end(), 0)+std::accumulate(E_B.begin(), E_B.end(), 0))*(-1);
                }
        }
    }

    rep(i,A+B){
        if(i<A) E[i] = E_A[i];
        else E[i] = E_B[i-A];
    }

    rep(i,A+B-1) cout << E[i]<< " ";
    cout << E.back() << endl;
    return 0;
}


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <queue>
#include <vector>

using namespace std;

int64_t solve(int64_t L, int64_t R) {
	vector<int64_t> cnt(R + 1);
	for (int64_t g = R; g >= 2; --g) {
		cnt[g] = (R / g - (L - 1) / g) * (R / g - (L - 1) / g);
		for (int64_t x = g * 2; x <= R; x += g) {
			cnt[g] -= cnt[x];
		}
	}
	int64_t ans = accumulate(cnt.begin(), cnt.end(), 0LL);
	for (int g = max(int64_t(2), L); g <= R; ++g) {
		ans -= 2 * (R / g - (L - 1) / g) - 1;
	}
	return ans;
}

int main() {
	int64_t L, R;
	std::cin >> L >> R;
	cout << solve(L, R) << endl;
	return 0;
}

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

int l , r ;

int A[1000007] ;
int ans[1000007] ;
int prime_factor[1000007] ;

void ertstns(){
    memset(prime_factor,-1,sizeof(prime_factor)) ;
    prime_factor[0] = prime_factor[1] = 1 ;
    for(int i = 2 ; i < 1000007 ; i++){
        if(prime_factor[i] == -1){
            for(int j = i ; j < 1000007 ; j += i) prime_factor[j] = i ;
        }
    }
}

int main(){
    ertstns() ;
    cin >> l >> r ;
    rrep(i,max(l,2),r+1){
        int k = i ;
        vector<int> vec ;
        map<int,bool> use ;
        while(k != 1){
            if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
            use[prime_factor[k]] = true ;
            k /= prime_factor[k] ;
        }
        int t = vec.size() ;
        for(int S = 1 ; S < 1 << t ; S++){
            ll val = 1 ;
            for(int bit = 0 ; bit < t ; bit++){
                if(S >> bit & 1) val *= vec[bit] ;
            }
            ans[val]++ ;
        }
    }
    ll res = 0 , mns = 0 ;
    for(int i = max(l,2) ; i <= r ; i++){
        int k = i ;
        vector<int> vec ;
        map<int,bool> use ;
        while(k != 1){
            if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
            use[prime_factor[k]] = true ;
            k /= prime_factor[k] ;
        }
        int t = vec.size() ;
        for(int S = 1 ; S < 1 << t ; S++){
            ll count = 0 , val = 1 ;
            for(int j = 0 ; j < t ; j++) if(S >> j & 1){
                count++ ;
                val *= vec[j] ;
            }
            if(count % 2 == 0) res -= ans[val] ;
            else res += ans[val] ;
        }
        if(i * 2 <= r) mns += 2 * (r / i - 1) ;
        mns++ ;
    }
    cout << res - mns << endl ;
}


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

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		vector<ll> a(n);
		rep(i, 0, n) cin >> a[i];
		// x := 全部出した瞬間の全ての皿のxor
		// nが偶数
		// 先行: 全部出したとき先行 x!=0を目指す
		// 後攻: x=0
		// この場合は先行がでかいほうからとっていき同じ山に積むと、すべて同じでない限りx!=0になる。
		// 厳密には偶数indexの和>奇数indexの和となれば先行の勝ち

		// nが奇数
		// 先行: 全部出したとき後攻 x=0を目指す
		// 後攻: 全部出したとき先行 x!=0を目指す
		// 後攻が勝てそう. 先行が初手で何を選んでも、後攻はそれに一番でかいのを乗せれば最大の山が構成できる?
		if (n & 1)cout << "Second";
		else {
			sort(a.rbegin(), a.rend());
			ll even = 0, odd = 0;
			for (int i = 0; i < n; ++i) {
				if (i & 1) odd += a[i];
				else even += a[i];
			}
			if (even > odd) cout << "First";
			else cout << "Second";
		}
		cout << '\n';
	}
	return 0;
}

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

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

int T, N;
int A[100010];

signed main()
{
    cin >> T;
    while(T--)
    {
        cin >> N;
        rep(i, N)cin >> A[i];

        if(N % 2) cout << "Second" << endl;
        else
        {
            map<int, int> mp;
            rep(i, N)mp[A[i]]++;
            bool is_first = false;
            for(auto e : mp)
            {
                if(e.second % 2 == 1)
                {
                    is_first = true;
                }
            }

            if(is_first)cout << "First" << endl;
            else cout << "Second" << endl;
        }
    }
    return 0;
}


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

ll pow_m(ll a ,ll b,ll m){
  ll ans = 1;
  while(b){
    if(b&1) ans = ans * a %m;
    a = a *a % m;
    b >>= 1;
  }
  return ans;
}

int main(){
#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
#endif
   ll a,b,c;
   cin >> a >> b >> c;

   ll k = pow_m(b,c,4) ? pow_m(b,c,4) : 4;
   cout <<  pow_m(a,k,10);
   return 0;
}

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

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vpll = vector<pair<ll, ll>>;
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1<<30;
const ll INF = 1ll<<60;
const ll mod = 1000000007;
//const ll mod = 998244353;
//const ll dx[4] = {1, 0, -1, 0};
//const ll dy[4] = {0, 1, 0, -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 (a>b) { a=b; return 1; } return 0; }

template<class T> void disp1(vector<T> &array, bool blank=1) {
    rep(i, array.size()) cout << ((i&&blank) ? " " : "") << array[i];
    cout << endl;
    //cout << " ";
}
template<class T> void disp2(vector<vector<T>> &array, bool blank=1) {
    rep(i, array.size()) disp1(array[i], blank);
}

ll rem(ll a, ll b) { return (a % b + b) % b; }      // 余りを0~b-1に丸める
// 小数の入力が文字で与えられたとき、10^n倍したものを返す
ll x10(string str_f, ll n) {
    if (str_f[0] == '-') return -x10(str_f.substr(1), n);
    ll times = 1; rep(i,n) times *= 10;     // times = 10^n
    int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i;
    if (id == -1) { return stoll(str_f)*times; }
    string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1);
    while (str_dec.size() < n) str_dec += '0';
    return stoll(str_int)*times + stoll(str_dec);
}
// A^N (mod)
ll modpow(ll A, ll N, ll mod) {
    ll RES = 1;
    while (N) {
        if (N & 1) RES = RES * A % mod;
        A = A * A % mod;
        N >>= 1;
    }
    return RES;
}

//using mint = modint1000000007;

int main() {
    ll a, b, c; cin >> a >> b >> c;
    a %= 10;
    ll p = a;
    vll seen(10, 0), aa;
    while (1) {
        if (seen[a]) break;
        aa.pb(a);
        seen[a] = 1;
        a = a*p%10;
    }
    ll ans = aa[(modpow(b, c, sz(aa))+sz(aa)-1)%sz(aa)];
    cout << ans << endl;
    return 0;
}


#include<cstdio>

int abs(int u){
    return u>0?u:-u;
}

int getDis(int a,int b,int c,int d){
    if(a==c && b==d) return 0;
    else if((a+b)==(c+d) || (a-b)==(c-d) || abs(a-c)+abs(b-d)<=3) return 1;
    else{
        int x = (a+b)%2, y = (c+d)%2, z = (a-b)%2, w = (c-d)%2;
        if(z<0) z += 2;
        if(w<0) w += 2;
        if(x==y || z==w) return 2;
        return 3;
    }
}

int main(){
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if(a==c && b==d) printf("0\n");
    else if((a+b)==(c+d) || (a-b)==(c-d) || abs(a-c)+abs(b-d)<=3) printf("1\n");
    else{
        int ans = getDis(a,b,c,d);
        for(int i = -2; i <= 2; i++){
            for(int  j = -2; j <= 2; j++){
                for(int k = -2; k <= 2; k++){
                    for(int l = -2; l <= 2; l++){
                        int cur = getDis(a+i,b+j,c+k,d+l);
                        if(i!=0 || j!=0) cur++;
                        if(k!=0 || l!=0) cur++;
                        if(ans>cur) ans = cur;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    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;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
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;}

int r1,c1,r2,c2;

bool ok(int x,int y){
    if(abs(x)+abs(y)<=3) return true;
    if(x+y==0) return true;
    if(x-y==0) return true;
    return false;
}

void solve(){
    cin >> r1 >> c1 >> r2 >> c2;
    if(r1==r2 && c1==c2){
        cout << 0 << endl;
        return;
    }
    if(r1+c1==r2+c2){
        cout << 1 << endl;
        return;
    }
    if(r1-c1==r2-c2){
        cout << 1 << endl;
        return;
    }
    if(abs(r1-r2)+abs(c1-c2)<=3){
        cout << 1 << endl;
        return;
    }
    int x=abs(r1-r2),y=abs(c1-c2);
    if((x+y)%2==0){
        cout << 2 << endl;
        return;
    }
    Rep(i,-2,3){
        Rep(j,-2,3){
            //cout << x+i << " " << y+j << endl;
            if(ok(x+i,y+j)){
                cout << 2 << endl;
                return;
            }
        }
    }
    cout << 3 << endl;
}

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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long

int a[200010], b[200010];
ll d[200010];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    for(int i = 0; i < n; ++i) {
        scanf("%d", &b[i]);
    }
    int maxa = a[0];
    d[0] = 1ll*maxa*b[0];
    printf("%lld\n", d[0]);
    for(int i = 1; i < n; ++i) {
        maxa = std::max(maxa, a[i]);
        d[i] = std::max(d[i-1], 1ll*b[i]*maxa);
        printf("%lld\n", d[i]);
    }
    return 0;
}

// Created by Pratik
// NIT PATNA

#include <bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;

using namespace std;
#define M                    1000000007
#define pi                   3.14159265358979323846
#define ll                   long long
#define lld                  long double

// Pair
#define pii                  pair<int, int>
#define pll                  pair<ll, ll>

// Vector
#define vl                   vector<ll >
#define vi                   vector<int>
#define vpi                  vector<pii>
#define vpl                  vector<pll>
#define pb                   push_back
#define mp                   make_pair

// Search
#define lb                   lower_bound
#define ub                   upper_bound
#define mina                 *min_element
#define mama                 *max_element
#define bsrch                binary_search

////////
#define F                    first
#define S                    second
#define cel(x,a)             ((x + a - 1) / a)
#define all(v)               v.begin(), v.end()
#define allrev(v)            v.rbegin(), v.rend()
#define lcm(m, n)            ((m / __gcd(m, n))*n)
#define deb(...)             cerr << "(" << #__VA_ARGS__ << "):", dbg(__VA_ARGS__)
#define ms(arr, v)           memset(arr, v, sizeof(arr))
#define ps(x,y)              fixed << setprecision(y) << x
#define _X_                  ios_base::sync_with_stdio(false); cin.tie(NULL)

//pbds
#define ordered                 tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

// Input / Output
#define scn(str)             scanf("%s", str)
#define pri(str)             printf("%s\n", str)

const int N = 5e5 + 4;

inline void  dbg()           { cerr << endl; }

template <typename T, typename... V>
inline void  dbg(T t, V... v) {cerr << ' ' << t; dbg(v...);}

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

bool good(int arr[],int n)
{
	for(int i = 1; i<n; i++)if(arr[i]<arr[i-1])return false;
	return true;
}

void solve()
{
	int n;
	cin >> n;
	int arr[n];
	for(int i = 0; i<n; i++)cin >> arr[i];
	vi ans;
	bool turn = 0;
	while(1)
	{
		if(good(arr,n))break;
		bool used = 0;
		for(int i = n-2; i>=0; i--)
		{
			if(i%2==turn && arr[i]>arr[i+1])
			{
				ans.pb(i+1);
				swap(arr[i],arr[i+1]);
				used = 1;
				break;
			}
		}
		if(!used)
		{
			swap(arr[turn],arr[turn+1]);
			ans.pb(turn+1);
		}
		turn = !turn;
		//deb(turn);
	}
	int k = ans.size();
	assert(k<=n*n);
	cout << ans.size() << "\n";
	for(int &i:ans)cout << i << " ";
	cout << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    // clock_t time_req = clock();
    int t = 1;
     cin>> t;
    while (t--) {
       solve();

   }

    // time_req = clock() - time_req;
    // cout << (float)time_req / CLOCKS_PER_SEC;
   return 0;
}


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

#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

typedef pair <int,int> pii;

const int maxn=2e5+5;

set <pii> s;
int n;
long long c[maxn];
set <pii> :: iterator it;

int lowbit(int x) {
	return x&-x;
}

void add(int x,int k) {
	while(x<=n) c[x]+=k,x+=lowbit(x);
}

long long ask(int x) {
	long long r=0;
	while(x) r+=c[x],x-=lowbit(x);
	return r;
}

int main() {
	n=read(9); long long ans=0;
	rep(i,1,n) s.insert(make_pair(read(9)+i,i));
	rep(i,1,n) {
		int x=read(9); it=s.lower_bound(make_pair(x+i,0));
		if(it==s.end() || (it->first)!=x+i) return puts("-1"),0;
		long long tmp=ask(it->second);
		add(1,1),add(it->second,-1);
		ans+=tmp+(it->second)-i;
		if(tmp+(it->second)-i<0) puts("UN");
		s.erase(it);
	}
	print(ans,'\n');
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int bit[300010];
int N;
void add(int a, int b){
	int x = a;
	while(x <= N){
		bit[x] += b; x += x & -x;
	}
}
int sum(int a){
	int res = 0;
	int x = a;
	while(x > 0){
		res += bit[x]; x -= x & -x;
	}
	return res;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> N;
	int A[200010]; int B[200010];
	int C[200010]; int D[200010];
	set<int> st;
	map<int, int> mp;
	for(int i = 0; i < N; i++){
		cin >> A[i]; A[i] += i; C[i] = A[i];
		st.insert(A[i]);
	}
	for(int i = 0; i < N; i++){
		cin >> B[i]; B[i] += i; D[i] = B[i];
	}
	sort(C, C+N); sort(D, D+N);
	for(int i = 0; i < N; i++){
		if(C[i] != D[i]){
			cout << -1 << "\n"; return 0;
		}
	}
	long long ans = 0; int idx = 1;
	for(auto it = st.begin(); it != st.end(); it++){
		mp[*it] = idx; idx ++;
	}
	for(int i = 0; i < N; i++) A[i] = mp[A[i]];
	for(int i = 0; i < N; i++) B[i] = mp[B[i]];
	vector<int> E[200010];
	for(int i = 0; i < N; i++){
		E[B[i]].push_back(i);
	}
	int itr[200010] = {};
	int F[200010] = {}; int G[200010] = {};
	for(int i = 0; i < N; i++){
		F[i] = E[A[i]].at(itr[A[i]]); itr[A[i]] ++;
		G[F[i]] = i;
	}
	// cout << G[0] << G[1] << G[2] << endl;
	for(int i = 0; i < N; i++){
		ans += (long long) sum(G[i] + 1);
		add(G[i] + 1, 1);
	}
	cout << (long long) N * (N - 1) / 2 - ans << "\n";
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int a, b, c;
double f[105][105][105];

double dp(int nowa, int nowb, int nowc) {
    if (nowa >= 100 || nowb >= 100 || nowc >= 100) return 0;
    if (f[nowa][nowb][nowc]) return f[nowa][nowb][nowc];
    //cout << 1 << endl;
    int sum = nowa + nowb + nowc;
    return f[nowa][nowb][nowc] =
               1.0 + (1.0 * nowa / sum * dp(nowa + 1, nowb, nowc) +
                      1.0 * nowb / sum * dp(nowa, nowb + 1, nowc) +
                      1.0 * nowc / sum * dp(nowa, nowb, nowc + 1));
}

int main() {
    cin >> a >> b >> c;
    printf("%.10lf",dp(a, b, c));
    return 0;
}

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <limits.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 110;
int H, W, A, B, ans = 0;
int nx, ny;
bool vis[20][20];
void solve(int s, int x, int y, int a, int b)
{
	if(s==H*W)
	{
		ans++;
		return ;
	}
	int nx = x+1, ny = y;
	if(x==W) nx = 1, ny++;
	if(vis[x][y]) return solve(s+1, nx, ny, a, b);
	if(a)
	{
		if(x!=W && !vis[nx][ny])
		{
			vis[x][y] = vis[nx][ny] = true;
			solve(s+1, nx, ny, a-1, b);
			vis[x][y] = vis[nx][ny] = false;
		}
		if(y<H)
		{
			vis[x][y] = vis[x][y+1] = true;
			solve(s+1, nx, ny, a-1, b);
			vis[x][y] = vis[x][y+1] = false;
		}
	}
	if(b)
	{
		vis[x][y] = true;
		solve(s+1, nx, ny, a, b-1);
		vis[x][y] = false;
	}
}
int main()
{
	//freopen("/Users/zhangkanqi/Desktop/11.txt", "r", stdin);
	cin >> H >> W >> A >> B;
	solve(0, 1, 1, A, B);
	cout << ans << endl;
	return 0;
}


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

#include <bits/stdc++.h>

using namespace std;

#define rep(i,n)      for(long long(i)=0;(i)<(n);(i)++)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

int main(){

    long long s,p;
    cin >> s >> p;

    for(long long i = 1; i * i <= p; i++){
        if(p % i == 0){
            if(i + p / i == s){
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

    cout << "No" << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int         long long
#define mp          make_pair
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
// #define vb          vector<bool>
// #define vc          vector<char>
#define vii         vector<pii>
//#define mi          map<int,int>
//#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
// #define ub(x)       upper_bound(x)
// #define lb(x)       lower_bound(x)
#define endl        '\n'
#define file        freopen("b1.in","r",stdin);freopen("output.txt","w",stdout)
#define fastio      ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// #define N            1e5+5
#define M           1000000007
#define MAX         50000
#define mem0(a)     memset(a,0,sizeof(a))
#define prec        cout<<std::setprecision(9)<<std::fixed
#define forn(i,a,b)  for(int i=a;i<b;i++)
#define pi          3.14159265358979323

struct cmp {
    bool operator() (pii a,pii b){
        return ((a.F-a.F/2)*a.S)<((b.F-b.F/2)*b.S);
    }
};
// priority_queue<pii,vector<pii>,cmp> q;

const int N=1e5+1;
int n,m,k;

vector<int> check(int n, vector<int> a, int x)
{
    multiset<int> s;
    for (auto e : a)
        s.insert(e);
    vector<int> res;
    for (int i = 0; i < n; i++)
    {
        auto it1 = s.end();
        it1--;
        int y = x - *it1;
        s.erase(it1);
        auto it2 = s.find(y);
        if (it2 == s.end())
            return {};
        res.push_back(x - y);
        res.push_back(y);
        x = max(x - y, y);
        s.erase(it2);
    }
    return res;
}

void Anon_mouS(){
    string s;
    cin>>s;
    // cout<<s[0]<<" "<<s[1]<<" "<<s[2]<<endl;
    if(s[0]==s[1] and s[1]==s[2])cout<<"Won"<<endl;
    else cout<<"Lost"<<endl;
}
int32_t main(){
    fastio;
    // func();
    int q=1;
    // sieve();
    // cin>>q;
    for(int it=1;it<=q;it++){
        // cout<<"Case #"<<it<<": ";
        Anon_mouS();
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main()
{
    string s;
    cin>>s;
    if((s[0]==s[1])&&(s[1]==s[2])){
        cout<<"Won\n";
    }else{
        cout<<"Lost\n";
    }

    return 0;
}


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

float satisfaction(float x, float y, float a,
                   float b, float c, float d, float r){
  if(a <= x+0.5 && x+0.5 <= c && b <= y+0.5 && y+0.5 <= d){
    float s = (c-a)*(d-b);
    return 1 - std::pow(1-min(r,s)/max(r,s), 2.0);
  }else{
    return 0;
  }
}

bool check(float a, float b, float c, float d, float r){
  float s = (c-a)*(d-b);
  return s == r;
}

int main(){
  int n;
  cin >> n;
  vector<float> x(n), y(n), r(n), a(n), b(n), c(n), d(n);
  for (int i=0;i<n;i++){
    cin >> x.at(i) >> y.at(i) >> r.at(i);
    a.at(i) = x.at(i);
    b.at(i) = y.at(i);
    c.at(i) = x.at(i)+1;
    d.at(i) = y.at(i)+1;
  }
  /*float sum_p = 0;
  for (int i=0;i<n;i++){
    sum_p = sum_p +
      satisfaction(x.at(i),y.at(i),
                   a.at(i),b.at(i),
                   c.at(i),d.at(i),r.at(i));
  }
  cout << sum_p << endl;*/
  for (int i=0;i<n;i++){
    cout << a.at(i) << " ";
    cout << b.at(i) << " ";
    cout << c.at(i) << " ";
    cout << d.at(i) << endl;
  }
}

/**
*    author:  shu8Cream
*    created: 06.03.2021 11:57:11
**/

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<tuple<int,int,int>> xyr(n);
    vector<tuple<int,int,int,int,int>> ans(n);
    rep(i,n){
        int x,y,r;
        cin >> x >> y >> r;
        xyr[i]={x,y,i};
    }

    int prex = 0, prey = 0, prer = 0;
    sort(all(xyr));
    rep(i,n){
        int x,y,r;
        tie(x,y,r)=xyr[i];
    }
    rep(i,n){
        int x,y,r;
        tie(x,y,r)=xyr[i];
        if(prex-1==x){
            ans[i-1]={prer,x,prey,x+1,prey+1};
            ans[i]={r,x,y,x+1,y+1};
        }else{
            ans[i]={r,prex, 0, x+1, 10000};
        }
        prex=x+1;
        prey=y;
        prer=r;
    }

    sort(all(ans));
    rep(i,n){
        int r,a,b,c,d;
        tie(r,a,b,c,d)=ans[i];
        printf("%d %d %d %d\n", a,b,c,d);
    }
}


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

#define MOD (long long)(1e9+7)
#define INF (1LL<<60)
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); i++)

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

// 最大公約数
ll gcd(ll a, ll b)
{
  if(b == 0) return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n)
{
  ll s = sqrt(n);
  ll r = 0;
  for(ll i = 2; i <= s; i++) {
    if((n % i) == 0) {
      r = 0;
      do {
	r++;
	n = n / i;
      } while((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if(n > s) {
    prim.push_back({n, 1});
  }
}

void solve()
{
  ll N; cin >> N;
  vector<int> atc(5050, 0), cgc(5050, 0);
  string s; cin >> s;
  int at = 0, cg = 0;
  ll ans = 0;
  rep(i, N) {
    switch(s[i]) {
    case 'A':
      atc[i + 1] = atc[i] + 1;
      cgc[i + 1] = cgc[i];
      break;
    case 'T':
      atc[i + 1] = atc[i] - 1;
            cgc[i + 1] = cgc[i];
      break;
    case 'C':
      atc[i + 1] = atc[i];
      cgc[i + 1] = cgc[i] + 1;
      break;
    case 'G':
      atc[i + 1] = atc[i];
      cgc[i + 1] = cgc[i] - 1;
      break;
    }
  }

  rep(i, N) {
    for(ll j = i + 1; j <= N; j++) {
      if(atc[i] == atc[j] && cgc[i] == cgc[j]) ans++;
    }
  }

  cout << ans << endl;

}

int main(void)
{
  // ll t; cin >> t; rep(i, t)
  solve();
}

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<int, int> P;
// typedef modint1000000007 mint;

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/*--------------------------------------------------*/
const int INF = 1e9;
// const ll INF = 1e18;
const int MAX = 1e5;

int h, w, a, b;
V<int> dh = {0, 1};
V<int> dw = {1, 0};

int ans = 0;

void dfs(VV<bool> used, int maia) {
    if (maia == a) {
        ans++;
        return;
    }
    rep(hi, h) {
        rep(wi, w) {
            if (used[hi][wi]) continue;
            used[hi][wi] = true;
            rep(i, 2) {
                int nh = hi + dh[i];
                int nw = wi + dw[i];
                if (nh < 0 || h <= nh) continue;
                if (nw < 0 || w <= nw) continue;
                if (used[nh][nw]) continue;
                used[nh][nw] = true;
                dfs(used, maia + 1);
                used[nh][nw] = false;
            }
        }
    }
}

void solve() {
    cin >> h >> w >> a >> b;
    // a: 2 * 1, b: 1 * 1;
    VV<bool> used(h, V<bool>(w));
    dfs(used, 0);
    cout << ans << "\n";
}

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


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

#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")

int bNum[2000][2000];
int cNum[2000][2000];
int main()
{
	int H, W, N, M;
	cin >> H >> W >> N >> M;
	pairs(A)
		set<pair<int, int>> K;
	set<pair<int, int>> G;
	vector<vector<int>> B(H+1);
	vector<vector<int>> C(W + 1);
	fx(N) {
		pairsp(A);
		G.insert(A[x]);
	}

	fx(M) {
		int c, d;
		cin >> c >> d;
		K.insert(make_pair(c, d));
		B[c].push_back(d);
		C[d].push_back(c);
	}

	fx(H+1) {
		pb(B[x],0)
		pb(B[x], W+1)
		sorts(B[x]);
	}

	fx(W + 1) {
		pb(C[x], 0)
		pb(C[x], H + 1)
		sorts(C[x]);
	}
	ll ans = 0;

	fx(N) {
		auto b = lower_bound(B[A[x].first].begin(), B[A[x].first].end(), A[x].second) - B[A[x].first].begin();
		auto c = lower_bound(C[A[x].second].begin(), C[A[x].second].end(), A[x].first)- C[A[x].second].begin();
		ans += B[A[x].first][b] - B[A[x].first][b - 1] + C[A[x].second][c] - C[A[x].second][c - 1] - 2;
		bNum[A[x].first][b - 1]++;
		cNum[A[x].second][c - 1]++;
	}

	fx(H) {
		fy(W) {
			if (K.count(make_pair(x + 1, y + 1))) { continue; }
			auto b = lower_bound(B[x+1].begin(), B[x+1].end(), y+1) - B[x+1].begin();
			auto c = lower_bound(C[y+1].begin(), C[y+1].end(), x+1) - C[y+1].begin();
			ans -= max(bNum[x+1][b - 1] - 1,0);
			ans -= max(cNum[y+1][c - 1] - 1, 0);
			if (!G.count(make_pair(x+1,y+1))&& bNum[x+1][b - 1]>0&& cNum[y+1][c - 1]>0) {
				ans -= 1;
			}
		}
	}
	ce(ans-N)

    return 0;
}

//Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,b) for(ll i=a;i<=b;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
using namespace std;
typedef vector<vector<ll>> matrix;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
void solve()
{
    ll n,m;
    cin>>n>>m;
    ll odd=0,even=0;
    for(int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        int cnt=0;
        for(auto ch:s)
        {
            if(ch=='1')
                cnt++;
        }
        if(cnt%2==1)
            odd++;
        else
            even++;
    }
    cout<<odd*even<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T=1;
    //cin>>T;
    int t=0;
    while(t++<T)
    {
        //cout<<"Case #"<<t<<":"<<' ';
        solve();
        //cout<<'\n';
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}


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

// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); // __VA_ARGS__可変引数マクロ
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest& ...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w;
// output
template<typename T>void MACRO_OUT(const T t) { std::cout << t; }
template<typename First, typename...Rest>void MACRO_OUT(const First first, const Rest...rest) { std::cout << first << " "; MACRO_OUT(rest...); }
#define OUT(...) MACRO_OUT(__VA_ARGS__);
#define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); // std::fixed 浮動小数点の書式 / setprecision 浮動小数点数を出力する精度を設定する。
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(w, a, n) for(int w=(a);w<(n);++w)
#define REP(w, n) for(int w=0;w<int(n);++w)
#define IN(a, x, b) (a<=x && x<b)
template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
template<class T> using V = std::vector<T>;
template<class T> using VV = V<V<T>>;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = (1 << 30) - 1;                    // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1;              // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60;                          // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);  // 9.22x10^18
constexpr double eps = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
template<class T, size_t N> void FILL(T(&a)[N], const T & val) { for (auto& x : a) x = val; }   //  int a[5] = {1,2,3,4,5};	FILL(a,10);
template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T & val) { for (auto& b : a) FILL(b, val); }  // ２次元配列でもそのまま使える
template<class T> void FILL(std::vector<T> & a, const T & val) { for (auto& x : a) x = val; }   //	使い方 vector<int> a(3);	FILL(a,10);
template<class ARY, class T> void FILL(std::vector<std::vector<ARY>> & a, const T & val) { for (auto& b : a) FILL(b, val); }  // ２次元配列でもそのまま使える
// ------------>8------------------------------------->8------------

int main() {
INIT;

// VAR(ld,x,y,a,b)
VAR(ll,x,y,a,b)
// ld n = log(b /x) / log(a);  // これだと、小数点桁落ちで、正確に値がでない

ll ans =0;
// for (int i = 1; x < b; i++) {
while(1){
    if( x >= y /a ) break;
    // if(a*x >= y) break;
    if(a*x >= x+b) break;
    x *= a;
    ans++;
}

// ll ans = (y - pow(a,n) * x ) / b;
ans += (y - 1 - x ) / b;

OUT(ans)

return 0;
}

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

int main(){
    long long X, Y, A, B;
    std::cin >> X >> Y >> A >> B;

    long long exp = 0;
    int flg_A = 0, flg_B = 0;

    while(flg_A == 0){
        if(X*(A - 1) < B && (double)X*A < 2e18){
            if(X*A < Y){
                X = X*A;
                exp++;
            }else{
                flg_A = 1;
                flg_B = 1;
            }
        }else{
            flg_A = 1;
        }
    }
    // std::cout << exp;
    // std::cout << (long long)round(floorl(((double)Y - (double)X - 1.0)/(double)B));

    //あとはずっとB
    if(flg_B == 0){
        exp = exp + (Y - X - 1)/B;
    }

    std::cout << exp;

}


#include <iostream>
#include <cctype>

using namespace std;

int main() {
  char S, T;
  cin >> S >> T;
  cout << (S == 'Y' ? (char)toupper(T) : T) << endl;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	string S, T;
	cin >> S >> T;
	if (S == "Y") {
	  cout << (char)(T[0]+'A'-'a') << endl;
	} else {
	  cout << T << endl;
	}
}


// {{{
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#define debug(x...)
#define debug_arr(x...)
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// }}}

const int N = 2e5 + 10;
int head[N], e;
struct E
{
    int v, next;
} edge[N << 1];

void addedge(int u, int v)
{
    edge[e].v = v, edge[e].next = head[u];
    head[u] = e++;
}

int st[N], ed[N], dep[N];
int tim;

void dfs(int u, int f, int d)
{
    tim++;
    st[u] = tim;
    dep[u] = d;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == f) continue;
        dfs(v, u, d + 1);
    }
    tim++;
    ed[u] = tim;
    // debug(u, st[u], ed[u], dep[u]);
}

int n, m;
vi vec[N];

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (cin >> n)
    {
        clr(head, -1), e = 0;
        for (int i = 2; i <= n; ++i)
        {
            int f;
            cin >> f;
            addedge(i, f);
            addedge(f, i);
        }

        tim = 0;
        dfs(1, -1, 0);
        for (int i = 0; i < n; ++i) vec[i].clear();

        for (int i = 1; i <= n; ++i)
        {
            int curd = dep[i];
            vec[curd].push_back(st[i]);
        }
        for (int i = 0; i < n; ++i) sort(all(vec[i]));

        // debug(vec[2]);

        cin >> m;
        for (int i = 1; i <= m; ++i)
        {
            int u, d;
            cin >> u >> d;
            if (d < dep[u])
            {
                cout << 0 << endl;
                continue;
            }

            int l = st[u], r = ed[u];
            // debug(u, l, r);

            auto iter1 = lower_bound(all(vec[d]), l);
            auto iter2 = lower_bound(all(vec[d]), r);

            int cnt = iter2 - iter1;
            cout << cnt << endl;
        }
    }

    return 0;
}

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

const int M = 2e5;
vector<int> adj[M];
long long c[18], k, memo[18][(1 << 18)];
map<long long, map<long long, long long>> dist;

long long solve(int i, int mask) {
    if (mask == (1 << k) - 1)
        return 0;

    long long &res = memo[i][mask];
    if (res == -1) {
        res = 1e18;
        for (int j = 0; j < k; j++)
            if (!((1 << j) & mask))
                res = min(res, dist[c[i]][c[j]] + solve(j, mask | (1 << j)));
    }
    return res;
}

void bfs(int i) {
    long long d[M] = {};
    bool v[M] = {};
    queue<int> q;
    q.push(i);
    v[i] = 1;

    while (!q.empty()) {
        int cur = q.front();
        q.pop();

        for (int x : adj[cur])
            if (!v[x])
                q.push(x), v[x] = 1, d[x] = d[cur] + 1;
    }
    for (int j = 0; j < k; j++)
        dist[i][c[j]] = dist[c[j]][i] = d[c[j]];
}

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

    memset(memo, -1, sizeof memo);
    int n, m;
    cin >> n >> m;
    while (m--) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    cin >> k;
    for (int i = 0; i < k; i++)
        cin >> c[i], c[i]--;

    for (int i = 0; i < k; i++)
        bfs(c[i]);

    for (int i = 0; i < k - 1; i++)
        for (int j = i + 1; j < k; j++)
            if (!dist[c[i]][c[j]])
                return cout << "-1\n", 0;

    long long ans = 1e18;
    for (int i = 0; i < k; i++)
        ans = min(ans, solve(i, (1 << i)));
    cout << ans + 1 << '\n';
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
//constexpr ll MOD = 1e9 + 7;
constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
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;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)

int main()
{
  vector<ll> P = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
                  31, 37, 41, 43, 47, 53, 59, 61, 67, 71};

  ll K = P.size();

  ll A, B;
  cin >> A >> B;
  ll N = B - A + 1;

  vector<ll> bb(N, 0);
  for (ll i = 0; i < N; i++)
  {
    for (ll j = 0; j < K; j++)
    {
      if ((A + i) % P.at(j) == 0)
        bb.at(i) ^= (1LL << j);
    }
  }

  vector<vector<ll>> dp(N + 1, vector<ll>((1LL << K), 0));
  dp.at(0).at(0) = 1;
  for (ll i = 0; i < N; i++)
  {
    for (ll b = 0; b < (1LL << K); b++)
    {
      dp.at(i + 1).at(b) += dp.at(i).at(b);

      if ((b & bb.at(i)) == 0)
        dp.at(i + 1).at(b ^ bb.at(i)) += dp.at(i).at(b);
    }
  }

  ll ans = 0;
  for (ll b = 0; b < (1LL << K); b++)
  {
    ans += dp.at(N).at(b);
    //cerr << dp.at(N).at(b) << endl;
  }
  cout << ans << endl;
}

//Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define booga cerr << "booga" << endl
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define safai(...) Clearing_out(__VA_ARGS__)

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

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

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

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

string to_string(char c) {
	string s;
	s += c;
	return s;
}

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

string to_string(bool b) {
	return (b ? "1" : "0");
}

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

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

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

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

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

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

void debug_out() { cerr << endl; }

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

void Clearing_out() { return; }

template <typename Head, typename... Tail>
void Clearing_out(Head &H, Tail & ... T) {
	H.clear();
	Clearing_out(T...);
}

void testcase(){
//	int dp[50][50];
	int h, w;
	cin >> h >> w;
	string grid[h];
	for(int i = 0; i < h; i++){
		cin >> grid[i];
	}
	int ans = 1;
	const int mod = 998244353;
	for(int col = 0; col < w; col++){
		int nr= 0, nb =0, nd = 0;
		int j = col;
		int i = 0;
		while(j >= 0 && i < h){
			if(grid[i][j] == '.')nd++;
			else if(grid[i][j] == 'R')nr++;
			else nb++;
			j--, i++;
		}
		if(nr > 0 && nb > 0){

			cout << "0";
			return;
		}
		else if(nr > 0) ans *= 1;
		else if(nb > 0) ans *= 1;
		else ans *= 2, ans %= mod;
	}
	for(int row = 1; row < h; row++){
		int nr= 0, nb =0, nd = 0;
		int i = row;
		int j = w-1;
		while(i < h && j>= 0){
			if(grid[i][j] == '.')nd++;
			else if(grid[i][j] == 'R')nr++;
			else nb++;
			j--, i++;
		}
		if(nr > 0 && nb > 0){

			cout << "0";
			return;
		}
		else if(nr > 0) ans *= 1;
		else if(nb > 0) ans *= 1;
		else ans *= 2, ans %= mod;
	}
	cout << ans << '\n';
}

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int tt = 1;
//	cin >> tt;
	while(tt--){
		testcase();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fo(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define tr(it,a) for(auto it=a.begin();it!=a.end();it++)
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
int main() {
	FASTIO

	int t = 1;
//	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		vpii v(n);
		fo(i, n) {
			cin>>v[i].F;
            cin>>v[i].S;
		}
		int ans = 0;
		fo(i, n) {
			int x1 = v[i].F;
			int y1 = v[i].S;
          //  cout<<x1<<" "<<y1<<"points"<<endl;
			for (int j = i + 1; j < n; j++) {
				int delx=v[j].F-x1;
				int dely=v[j].S-y1;
            //    cout<<delx<<" "<<dely<<endl;
				if ( abs(dely)<=abs(delx) ) {
					ans++;
				}
            }
		}
		cout << ans << endl;
	}

	return 0;
}

/*Allah Vorosha*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define n_p next_permutation
#define p_p prev_permutation
#define in insert
#define rev reverse
#define pf push_front
#define pob pop_back
#define uniq(v)         v.resize(distance(v.begin(),unique(v.begin(),v.end())))
#define all(x) (x).begin(),(x).end()
#define pof pop_front
#define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie();
#define scn scanf
#define prt printf
#define rep(i, a, n) for(int i = a; i < n; i++)
#define mod 720720
#define yes cout << "Yes\n"
#define no cout << "No\n";
#define take for(auto &it : a) cin >> it;
#define out cout << a << "\n";
#define l_b lower_bound
#define u_b upper_bound
#define Max 100005

using namespace std;

const int N = 10005;

void solve() {
    ll n, m;
    cin >> n >> m;
    for(ll i = 1; i * i <= m; ++i) {
        if(m % i == 0) {
            ll k = m / i;
            if(i + k == n) {
                yes;
                return;
            }
        }
    }
    no;
}

int main() {

    solve();
    return 0;
}


#include <iostream>
using namespace std;

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

	int x, y;
	cin >> x >> y;

	if (y > x) swap(x, y);

	if (y + 3 > x)
		cout << "Yes";
	else
		cout << "No";

	return 0;
}

#include <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<iomanip> //setprecision
#include<stack>
#include<set>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
    ll s, p;
    cin >> s >> p;
    for (int i = 1; i <= sqrt(p); i++) {
        if (p % i == 0) {
            ll m = p / i;
            if (i + m == s) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }
    cout << "No" << endl;
    return 0;
}


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

inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

int n;

signed main() {
	cin>>n;
	for(int i=1;i<=n;i++) {
		int ans=1,now=i;
		for(int j=2;1ll*j*j<=i;j++) {
			while(now%j==0) ans++,now/=j;
		}
		if(now!=1) ans++;
		cout<<ans<<' ';
	}
}

#include<bits/stdc++.h>
using namespace std;
int n,a[101010];
int main()
{
	cin>>n;
	a[1]=1;
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j*j<=i;j++)
		{
			if(i%j!=0)
			{
				continue;
			}
			a[i]=max(a[i],a[j]+1);
			if(j!=1)
			{
				a[i]=max(a[i],a[i/j]+1);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		cout<<a[i]<<' ';
	}
	cout<<endl;
	return 0;
}


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

int main() {
    int n, t;
    cin >> n;
    vector<pair<double, double>> v;
    double l, r;
    for (int i = 0; i < n; ++i) {
        cin >> t >> l >> r;
        if (t == 2) {
            r -= 0.1;
        } else if (t == 3) {
            l += 0.1;
        } else if (t == 4) {
            l += 0.1;
            r -= 0.1;
        }
        v.push_back(make_pair(l, r));
    }

    int ans = 0;
    double l1, r1, l2, r2;
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            tie(l1, r1) = v.at(i);
            tie(l2, r2) = v.at(j);
            if (r1 < l2 || r2 < l1) continue;
            ans++;
        }
    }

    cout << ans << endl;
}

#include <bits/stdc++.h>

long long extGCD(long long a, long long b, long long c, long long &x, long long &y){
    long long a_gcd = a;
    long long b_gcd = b;
    std::vector< long long > A, B;
    A.push_back(a_gcd);
    B.push_back(b_gcd);

    while(b_gcd != 0){
        long long tmp = a_gcd % b_gcd;
        a_gcd = b_gcd;
        b_gcd = tmp;
        A.push_back(a_gcd);
        B.push_back(b_gcd);
    }

    if(c % a_gcd != 0){
        x = -1e18;
        y = -1e18;
        return -1;
    }

    std::vector< long long > X(A.size()), Y(A.size());
    X[A.size() - 1] = c / a_gcd;
    Y[A.size() - 1] = 0;
    for(int i=A.size()-2; i>=0; i--){
        X[i] = Y[i+1];
        Y[i] = X[i+1] - (A[i] / B[i]) * X[i];
    }

    x = X[0];
    y = Y[0];

    b /= a_gcd;

    if(x >= 0){
        return x % b;
    }else{
        long long ret;
        ret = b - std::abs(x) % b;
        if(ret == b){
            ret = 0;
        }
        return ret;
    }

}

int main(){
    int T;
    std::cin >> T;
    std::vector< long long > ans(T);

    for(int t=0; t<T; t++){
        long long X, Y, P, Q;
        std::cin >> X >> Y >> P >> Q;

        long long min = 9223372036854775807;
        for(int p=P; p<P+Q; p++){
            for(int x=X; x<X+Y; x++){
                long long ret1, ret2;
                long long tmp = extGCD(P+Q, 2*(X+Y), x-p, ret1, ret2);
                if(tmp != -1){
                    min = std::min(min, p + (P+Q) * tmp);
                }
            }
        }

        ans[t] = min;

    }

    for(int i=0; i<T; i++){
        if(ans[i] == 9223372036854775807){
            std::cout << "infinity" << std::endl;
        }else{
            std::cout << ans[i] << std::endl;
        }
    }

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return false;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return false;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

int max_num = 101010;
using vi = vector<int>;
using Graph = vector<vi>;
Graph G(max_num);
vi cnt(max_num), color(max_num);
vector<bool> good(max_num);

void dfs(int v, int p = -1, int d = 0) {
    if(cnt[color[v]]==0) good[v]=true;
    cnt[color[v]]++;
    fore(nv, G[v]) {
        if (nv == p) continue;
        dfs(nv, v, d+1);
    }
    cnt[color[v]]--;
}

int n,a,b;

int main() {
    fio(); cin>>n;
    rep(i,n) cin>>color[i];
    rep(i,n-1) {
        cin>>a>>b;a--;b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0);
    rep(i,n) if(good[i]) print(i+1);
}

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <type_traits>
#include <cmath>
#include <cassert>
#include <cstdio>

using namespace std;
using namespace placeholders;

using LL = long long;
using ULL = unsigned long long;
using VI = vector< int >;
using VVI = vector< vector< int > >;
using VS = vector< string >;
using ISS = istringstream;
using OSS = ostringstream;
using PII = pair< int, int >;
using VPII = vector< pair< int, int > >;
template < typename T = int > using VT = vector< T >;
template < typename T = int > using VVT = vector< vector< T > >;
template < typename T = int > using LIM = numeric_limits< T >;
template < typename T = int > using OSI = ostream_iterator< T >;

template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; }
template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; }

void in_impl(){};
template < typename T, typename... TS > void in_impl( T &head, TS &... tail ){ cin >> head; in_impl( tail ... ); }
#define IN( T, ... ) T __VA_ARGS__; in_impl( __VA_ARGS__ );

template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); }

#define NUMBERED( name, number ) NUMBERED2( name, number )
#define NUMBERED2( name, number ) name ## _ ## number
#define REP1( n ) REP2( NUMBERED( REP_COUNTER, __LINE__ ), n )
#define REP2( i, n ) REP3( i, 0, n )
#define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i )
#define GET_REP( a, b, c, F, ... ) F
#define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2, REP1 )( __VA_ARGS__ )
#define FOR( e, c ) for ( auto &&e : c )
#define ALL( c ) begin( c ), end( c )
#define AALL( a ) ( remove_all_extents< decltype( a ) >::type * )a, ( remove_all_extents< decltype( a ) >::type * )a + sizeof( a ) / sizeof( remove_all_extents< decltype( a ) >::type )

#define SZ( v ) ( (int)( v ).size() )
#define EXISTS( c, e ) ( ( c ).find( e ) != ( c ).end() )

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

#define PB push_back
#define EM emplace
#define EB emplace_back
#define BI back_inserter

#define MP make_pair
#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

//     Λ   Λ＿_
//  ／(*ﾟーﾟ)／＼
// ／| ￣U U￣|＼／
//   |      |／

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

	IN( int, N, M );

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

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

	VPII res;
	res.EB( 1, 1 );
	--N;
	++M;
	for ( ; M; --M, --N )
	{
		const int l = res.back().snd + 1;
		res.EB( l, l + 1 );
	}
	res[0].snd = res.back().snd + 1;

	while ( N-- )
	{
		const int l = res.back().snd + 2;
		res.EB( l, l + 1 );
	}

	FOR( p, res )
	{
		cout << p.fst << ' ' << p.snd << '\n';
	}
	cout << flush;

	return 0;
}


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

#define int  long long
#define sz(x) (int)x.size()
int lcm(int A,int B){    return A * B / (__gcd(A, B));}

const int mod=1e9+7;
const int mxN=2e5;

//=======================

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; pri(x); cerr << endl;
#else
#define debug(x);
#endif

typedef unsigned long long ull;
typedef long double lld;

void pri(int t) {cerr << t;}
void pri(string t) {cerr << t;}
void pri(char t) {cerr << t;}
void pri(lld t) {cerr << t;}
void pri(double t) {cerr << t;}
void pri(ull t) {cerr << t;}

template <class T, class V> void pri(pair <T, V> p);
template <class T> void pri(vector <T> v);
template <class T> void pri(set <T> v);
template <class T, class V> void pri(map <T, V> v);
template <class T> void pri(multiset <T> v);
template <class T, class V> void pri(pair <T, V> p) {cerr << "{"; pri(p.ff); cerr << ","; pri(p.ss); cerr << "}";}
template <class T> void pri(vector <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(set <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(multiset <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T, class V> void pri(map <T, V> v) {cerr << "[ "; for (auto i : v) {pri(i); cerr << " ";} cerr << "]";}

//=======================

signed main() {

ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("1.txt", "w", stderr);
#endif

int n;
cin >> n;

string s;
cin >> s;

if(s[0] != s[n-1]){
  cout << 1 << '\n';
}
else{
bool ok=false;

for(int i = 1; i <n-1; i++){
  if(s[i] != s[n-1] and s[i+1]!=s[n-1]){
    ok=true;
    break;
  }
}
if(ok){
  cout << 2 << "\n";
}
else{
  cout << -1 << '\n';
}
}
}

#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<bool> VB;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) LL((a).size())

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL MOD = 1e9+7;

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

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

void Main()
{
    LL n,m;cin>>n>>m;

    VL a(n);
    REP(i,n){
        cin>>a[i];
    }

    LL result = n;
    VL c(n);

    REP(i,m){
        c[a[i]]++;
    }

    LL small = n;

    REP(i,n){
        if(c[i] == 0){
            small = i;
            result = i;
            break;
        }
    }

    FOR(i,m,n){
        LL plus = a[i];
        LL minus = a[i-m];
        c[plus]++;
        c[minus]--;
        if(c[minus] == 0){
            result = min(result,minus);
        }
    }

    cout<<result<<endl;

	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}


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

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

#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);

int n;
priority_queue<int, vector<int>, greater<int>> good;
vector<int> c(100005);
vector<queue<int>> edge(100005);
vector<int> s(100005, 0);

void dfs(int prev, int now) {
  if(s[c[now]] == 0) {
    good.push(now);
  }
  s[c[now]]++;

  while(!edge[now].empty()) {
    int tmp = edge[now].front();
    edge[now].pop();
    if(tmp != prev) {
      dfs(now, tmp);
    }
  }
  s[c[now]]--;
}

int main() {
  cin >> n;
  rep(i, n) cin >> c[i];

  rep(i, n-1) {
    int a, b; cin >> a >> b;
    a--; b--;
    edge[a].push(b);
    edge[b].push(a);
  }

  dfs(-1, 0);

  while(!good.empty()) {
    cout << good.top()+1 << endl;
    good.pop();
  }

  return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) {
    if(n == 0) return 1;

    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

ll my_gcd(ll a, ll b) {
    if(b == 0) return a;
    return my_gcd(b, a%b);
}

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}

//
// Created by mihai145 on 23.02.2021.
//

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

using namespace std;

const int NMAX = 1e5;

int N, w[NMAX + 5];
vector<int> g[NMAX + 5];

int dfs(int node, int parent = -1) {
    int ans = 1;

    w[node] = 1;

    vector<int> v;
    int evenLeft = 0;

    for (auto it : g[node]) {
        if (it != parent) {
            int nextRes = dfs(it, node);
            w[node] += w[it];

            if (w[it] % 2 == 0) {
                if (nextRes <= 0) {
                    ans += nextRes;
                } else {
                    evenLeft += nextRes;
                }
            } else {
                v.push_back(nextRes);
            }
        }
    }

    sort(v.begin(), v.end());
    v.push_back(evenLeft);

    for (int i = 0; i < (int) v.size(); i++)
        if (i % 2 == 0) {
            ans += v[i];
        } else {
            ans -= v[i];
        }

    return ans;
}

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

    cin >> N;

    for (int i = 2; i <= N; i++) {
        int parent;
        cin >> parent;
        g[parent].push_back(i);
    }

    cout << (N + dfs(1)) / 2 << '\n';

    return 0;
}