#include <iostream>
#include<algorithm>

using namespace std;

typedef unsigned long long ull;
string x;
ull m;

bool check(ull mid)
{
    ull ans=0;
    int flag=1;
    for(int i=0;i<x.size();i++)
    {
        if(ans > m / mid) return false;
        ans=ans*mid+(x[i]-'0');
    }
    return ans <= m;
}

int main()
{
    char c='0';
    cin>>x>>m;
    for(int i=0;i<x.size();i++)
    {
        c=max(c,x[i]);
    }
    ull t=c-'0';
    if(x.size()==1)
    {
        if(t-1<=m) cout<<1<<endl;
        else cout<<0<<endl;
    }
    else
    {
        ull l=0,r=1e18;
        while(l<r)
        {
            ull mid=(l+r+1)>>1;
            if(check(mid)) l=mid;
            else r=mid-1;
        }
        if(l<t) cout<<0<<endl;
        else cout<<l-t<<endl;
    }

    return 0;
}

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

#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i,(int)a.size()) cin >> a[i]
#define debug(a) rep(i,(int)a.size()) cout << a[i] << " "
#define pb push_back
#define eb emplace_back
#define dame {puts("-1"); return 0;}
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;

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

const int di[] = {0, -1, 0, 1};
const int dj[] = {1, 0, -1, 0};
const int MOD = 1e9+7;
const int INF = 1e9;
const char nl = '\n';

ll power(ll a, ll b) {return b? power(a*a%MOD, b/2)*(b%2?a:1)%MOD:1;}

ll C(int n, int k)
{
	ll x = 1, y = 1;
	for (int i = 1; i <= k; ++i)
	{
		x = x*(n-i+1)%MOD;
		y = y*i%MOD;
	}
	return x*power(y, MOD-2)%MOD;
}

struct UF
{
	vi d;
	UF(int n) : d(n, -1) {}
	int root(int x)
	{
		if (d[x] < 0) return x;
		return d[x] = root(d[x]);
	}
	bool unite(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (d[x] > d[y]) swap(x, y);
		d[x] += d[y];
		d[y] = x;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int size(int x) { return -d[root(x)]; }
};

bool f(ll base, string x, ll m)
{
	ll sum = 0;
	rep(i, x.size())
	{
		if (log10(sum) + log10(base) >= 18.5) return false;
		sum *= base;
		sum += x[i]-48;
	}
	return sum <= m;
}

int main()
{
	string x; cin >> x;
	ll m ; cin >> m;
	if (x.size() == 1)
	{
		cout << (stoll(x) <= m) << endl;
		return 0;
	}
	int n = 0;
	rep(i, x.size()) chmax(n, x[i]-48);
	ll left = 1;
	ll right = m+1;
	while (right - left > 1)
	{
		ll mid = left + (right-left)/2;
		if (f(mid, x, m)) left = mid;
		else right = mid;
	}
	cout << max(0LL, left-n) << endl;
}


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

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>

const ll mod = 1000000007;
const int INF = 1001001001;

int main(){
    int a, b;
    cin >> a >> b;
    int c = a + b;
    int ans;
    if(c >= 15 && b >= 8){
        ans = 1;
    }else if(c >= 10 && b >= 3){
        ans = 2;
    }else if(c >= 3){
        ans = 3;
    }else{
        ans = 4;
    }
    cout << ans << endl;
}

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

const int mod = 1e9 + 7;
const int N = 1e6 + 5;

int g[N];

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

	int l, r;
	cin >> l >> r;
	for (int i = 2; i <= r; i++) {
		int x = ceil(1.0 * l / i), y = floor(1.0 * r / i);
		g[i] = (y - x + 1) * (y - x + 1);
	}

	int ans = 0;
	for (int i = max(l, 2ll); i <= r; i++)
		ans -= r / i * 2 - 1;

	for (int i = r; i >= 2; i--) {
		for (int j = 2; i * j <= r; j++) {
			g[i] -= g[i * j];
		}
		ans += g[i];
	}

	cout << ans;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

#define all(x)                      (x).begin(),(x).end()
#define Sort(x)                     sort(all((x)))
#define X                           first
#define Y                           second
#define sep                         ' '
#define endl                        '\n'
#define sz(x)                       (int)(x.size())
#define debug(x)                    cerr << #x << ": " <<  x << endl;
#define bug(x)                      cerr << #x << ": " <<  x <<  " ";
#define alog(a, n)                  for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl;
#define FOR(n)                      for (int i = 0; i < n; i++)
#define fast_io                     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

ll poww(ll a, ll b, ll md) {
    if (b == -1) return poww(a, md - 2, md);
    return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}

const ll N = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

int n, m, t;
int c[N][2];

int main(){
    fast_io
    cin >> n >> m >> t;

    for(int i = 0; i < m; i++)
        cin >> c[i][0] >> c[i][1];

    int ch = n, lst = 0;

    for(int i = 0; i < m; i++){
        if(c[i][0] - lst >= ch)
            return cout << "No" << endl, 0;
        ch -= c[i][0] - lst;
        ch = min(n, ch + c[i][1] - c[i][0]); lst = c[i][1];
    }

    if(t - c[m - 1][1] >= ch)
        return cout << "No" << endl, 0;

    cout << "Yes" << endl; return 0;

}

#include"bits/stdc++.h"
//#include"atcoder.h"
#include<iostream>
using namespace std;
typedef long long ll;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
const ll inf = ll(1e18) + 1;
const ll mod = 1000000007;

int main() {

    ll n,m,t;
    cin >> n >> m >> t;

    vector<ll>a(m);
    vector<ll>b(m);
    rep(i,m) {
        cin >> a[i] >> b[i];
    }

    ll now = 0;
    ll ma = n;

    rep(i,m) {
        n -= a[i] - now;

        if (n <= 0) {
            cout << "No" << endl;
            return 0;
        }

        n += b[i] - a[i];

        if (n > ma) {
            n = ma;
        }
        now = b[i];
    }

    if (n > t-now) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double pi=3.1415926535897932384626;
inline ll read(){
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const ll mod=998244353;
const int maxn=5e2+5;
ll n,m;
char s[maxn][maxn];
ll q_pow(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1) ans=ans*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return ans;
}
int main(){
    n=read(),m=read();
    for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
    ll cnt=0,flag=1;
    for(int i=1;i<=n;++i){
        int x=i,y=1,r=0,b=0,tmp=0;
        while(x>=1&&y<=m){
            if(s[x][y]=='.') ++tmp;
            else if(s[x][y]=='R') r=1;
            else b=1;
            --x;
            ++y;
        }
        if(r==1&&b==1) flag=0;
        else if(r+b==0&&tmp) ++cnt;
    }
    for(int i=2;i<=m;++i){
        int x=n,y=i,r=0,b=0,tmp=0;
        while(x>=1&&y<=m){
            if(s[x][y]=='.') ++tmp;
            else if(s[x][y]=='R') r=1;
            else b=1;
            --x;
            ++y;
        }
        if(r==1&&b==1) flag=0;
        else if(r+b==0&&tmp) ++cnt;
    }
    if(!flag) printf("0\n");
    else printf("%lld\n",q_pow(2,cnt));
    return 0;
}

#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;

const int maxX = 72, maxPrimes = 20;
bool notPrime[maxX + 1];
vector<lli> primes;

const int maxN = 72;
lli a, b; int n;
int primesMask[maxN + 1];
lli memo[maxN + 1][1LL << maxPrimes];
lli solve(int i = 0, int mask = 0)
{
  if (i == n + 1)
    return 1;
  lli &ans = memo[i][mask];
  if (ans != -1)
    return ans;

  ans = solve(i + 1, mask);
  if (!(mask & primesMask[i]))
    ans += solve(i + 1, mask | primesMask[i]);
  return ans;
}

int main()
{
  notPrime[0] = notPrime[1] = true;
  for (int i = 2; i <= maxX; i ++)
  {
    if (!notPrime[i]) primes.push_back(i);
    for (int j = 0; i*primes[j] <= maxX; j ++)
    {
      notPrime[i*primes[j]] = true;
      if (i % primes[j] == 0) break;
    }
  }

  while (~scanf("%lld %lld", &a, &b))
  {
    n = b - a;
    for (lli i = a; i <= b; i++)
    {
      primesMask[i - a] = 0;
      for (int j = 0; j < primes.size(); j++)
      {
        int p = primes[j];
        if (i % p == 0)
          primesMask[i - a] |= (1LL << j);
      }
    }

    memset(memo, -1, sizeof(memo));
    lli ans = solve();
    printf("%lld\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize ("-O3")
#define int long long
#define ld long double
#define endl "\n"

#define rep(i,begin,end) for (__typeof(end) i=begin-(begin>end); i!=(end)-(begin>end); i+=1-2*(begin>end))
#define umap unordered_map
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fs first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define mii map<int,int>
#define pii pair<int,int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define w(t) int t; cin>>t; rep(tc,0,t)
#define clr(a,x) memset(a,x,sizeof a)

#define chkbit(x,i) ((x)&(1LL<<(i)))
#define setbit(x,i) ((x)|(1LL<<(i)))
#define setbits(x)  __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x
#define print(a, n) rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int mod = 1e9+7;
const int mod2 = 998244353;
const int N = 2e5;
const int inf = 1e18;
const ld PI = 3.141592653589793;

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

template<typename... T>
void in(T&... args) {((cin>>args), ...);}
template<typename... T>
void out(T&&... args) {((cout<<args<<" "), ...);}
template<typename... T>
void outln(T&&... args) {((cout<<args<<" "), ...); cout<<endl;}

int nxt(){int x;cin>>x;return x;}
int add(int a,int b,int mod){int res=(a+b)%mod;return (res<0)?res+mod:res;}
int mul(int a,int b,int mod){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;}
int range(int x,int y,int z) {return (x >= y && x <= z);}

struct customCompare {
    bool operator () (int x, int y) {
        return x>y;
    }
};

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

int calc(int x){
    return ((x*(x+1))/2)%mod2;
}

void solve() {
    int a,b,c;
    in(a,b,c);

    int ans = calc(a);
    ans = mul(ans, calc(b), mod2);
    ans = mul(ans, calc(c), mod2);

    cout <<  ans << endl;
}

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

    solve();
    // int t; cin >> t;
    // for (int tc = 1; tc <= t; tc++) {
    //     cout << "Case #" << tc << ": ";
    //     solve();
    // }

    // printclock;
    return 0;
}

#include"bits/stdc++.h"
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(x) x.begin(),x.end()
#define MOD 1000000007
#define INF INT_MAX
#define FLOAT_ANS setprecision(10)

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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

//最大公約数
template<typename T>
T gcd(T a, T b) {
	if (a % b == 0) {
		return(b);
	}
	else {
		return(gcd(b, a % b));
	}
}

//最小公倍数
template<typename T>
T lcm(T a, T b) {
	return a / gcd(a, b) * b;
}

template <typename X, typename M>
struct SegTreeLazy {
    using FX = function<X(X, X)>;
    using FA = function<X(X, M)>;
    using FM = function<M(M, M)>;
    int n;
    FX fx;
    FA fa;
    FM fm;
    const X ex;
    const M em;
    vector<X> dat;
    vector<M> lazy;
    SegTreeLazy(int n_, FX fx_, FA fa_, FM fm_, X ex_, M em_)
        : n(), fx(fx_), fa(fa_), fm(fm_), ex(ex_), em(em_), dat(n_ * 4, ex), lazy(n_ * 4, em) {
        int x = 1;
        while (n_ > x) x *= 2;
        n = x;
    }

    void set(int i, X x) { dat[i + n - 1] = x; }
    void build() {
        for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
    }

    /* lazy eval */
    void eval(int k) {
        if (lazy[k] == em) return;  // 更新するものが無ければ終了
        if (k < n - 1) {            // 葉でなければ子に伝搬
            lazy[k * 2 + 1] = fm(lazy[k * 2 + 1], lazy[k]);
            lazy[k * 2 + 2] = fm(lazy[k * 2 + 2], lazy[k]);
        }
        // 自身を更新
        dat[k] = fa(dat[k], lazy[k]);
        lazy[k] = em;
    }

    void update(int a, int b, M x, int k, int l, int r) {
        eval(k);
        if (a <= l && r <= b) {  // 完全に内側の時
            lazy[k] = fm(lazy[k], x);
            eval(k);
        } else if (a < r && l < b) {                     // 一部区間が被る時
            update(a, b, x, k * 2 + 1, l, (l + r) / 2);  // 左の子
            update(a, b, x, k * 2 + 2, (l + r) / 2, r);  // 右の子
            dat[k] = fx(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    void update(int a, int b, M x) { update(a, b, x, 0, 0, n); }

    X query_sub(int a, int b, int k, int l, int r) {
        eval(k);
        if (r <= a || b <= l) {  // 完全に外側の時
            return ex;
        } else if (a <= l && r <= b) {  // 完全に内側の時
            return dat[k];
        } else {  // 一部区間が被る時
            X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return fx(vl, vr);
        }
    }
    X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
};

bool isLarger(string s,string t){
	/*SのほうがTより大きければtrue*/
	rep(i,min(s.length(),t.length())){
		if(s[i]>t[i]){
			return true;
		}
		if(s[i]<t[i]){
			return false;
		}
	}

	if(s.length()>t.length()){
		return true;
	}
	else{
		return false;
	}
}

int main(void) {
  ll n;
  cin>>n;
  vector<vector<ll>>v(n,vector<ll>(2));
  rep(i,n){
    cin>>v[i][0]>>v[i][1];
  }

  rep(i,n){
    rep(j,n){
      rep(k,n){
        if(i==j||k==i||j==k){
          continue;
        }
        ll x1=v[i][0]-v[j][0];
        ll y1=v[i][1]-v[j][1];
        ll x2=v[i][0]-v[k][0];
        ll y2=v[i][1]-v[k][1];

        if(x1*y2==x2*y1){
          cout<<"Yes";
          return 0;
        }
      }
    }
  }

  cout<<"No";
}

/*

*/


#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ts to_string
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define dbg(a); cerr<<#a<<"="<<(a)<<endl;
#define dbg2(a,b); cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<endl;
#define dbgarr(a); for(auto elem:a)cerr<<elem<<" ";cerr<<endl;
#define panic(); cerr<<"code executes until here fine"<<endl;
#define out(a); cout<<a<<endl
using namespace std;
int main()
{
	int x;cin>>x;
	if(x<0) {
		cout<<0<<endl;
	}
	else {
		out(x);
	}
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  if (n%2 == 0) {
    cout << "White" << endl;
  } else {
    cout << "Black" << endl;
  }

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i, a, b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)

int main() {
    char S[4];
    cin >> S;
    cout << S[1] << S[2] << S[0] << endl;

    return 0;
}

#include <iostream>
using namespace std;
int main(){
    string s;
    cin>>s;
    cout<<s[1]<<s[2]<<s[0];

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod ((int)1e9+7)
#define lim 1000000000000000007
#define lim1 18446744073709551615  //Unsigned
#define sq(a) ((a)*(a))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mms(v,i) memset(v,i,sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPN(i,a,b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> PL;
typedef pair<ll,int> PLI;
typedef pair<int,ll> PIL;
typedef pair<int,pair<int,int> > pii;
typedef pair<double,double> pdd;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
ll power(ll a,ll b) {
   a %= mod;
   ll res=1;
   while(b){
      if(b%2==1) res=(res*a)%mod;
      a=(a*a)%mod;
      b>>=1;
   }
   return res;
}
ll gcdll(ll a,ll b) {
    if (b==0) return a;
    return gcdll(b,a%b);
}
int gcd(int a,int b) {
    if (b==0) return a;
    return gcd(b,a%b);
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

const int N = (int)2e5+5;
const int M = (int)3e3+5;
const int Q = 301;
const int logN = 19;

bool adj[20][20];
vector<int> temp[1<<9];
int dp[1<<18];
void solve() {
  int n,m,a,b;
  cin >> n >> m;
  if(m==0) {
    cout << n;
    return;
  }
  REP(i,1,m) {
    cin >> a >> b;
    --a, --b;
    adj[a][b] = adj[b][a] = true;
  }
  m = (1<<n);
  REP(i,1,m-1) {
    dp[i] = __builtin_popcount(i);
    if(dp[i]<=n/2) {
      temp[dp[i]].pb(i);
    }
    bool ok = true;
    REP(j,0,n-1) {
      if((i>>j)&1) {
        REP(k,0,n-1) {
          if((i>>k)&1) {
            if(j!=k && !adj[j][k]) {
              ok = false;
              break;
            }
          }
        }
        if(!ok) {
          break;
        }
      }
    }
    if(ok) {
      dp[i] = 1;
    }
  }

  REP(i,1,m-1) {
    int n = __builtin_popcount(i);
    int k = n/2;
    if(dp[i]>1) {
      REP(c,1,k) {
        for(auto j:temp[c]){
          if((i&j)==j) {
            dp[i] = min(dp[i], dp[j] + dp[i-j]);
          }
        }
      }
    }
  }
  cout << dp[m-1];
}
int main() {
    //freopen("output.txt","r",stdin);
    //freopen("output.txt","w",stdout);

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

    int T=1;
    // cin>>T;
    REP(TC,1,T) {
        //cout<<"Case #"<<TC<<": ";
        solve();
        cout<<"\n";
    }
}

// "C:\Users\ianli\Desktop\CP\template\"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define MTP make_tuple
typedef long long int ll;

constexpr int kN = 20;
bitset<kN> graph[kN];
int dp[1 << 18];

int main() {
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	int n, m, tot;
	vector<int> v;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		int a, b;
		scanf("%d%d", &a,  &b);
		a--, b--;
		graph[a][b] = graph[b][a] = true;
	}
	for (int i = 0; i < n; i++) graph[i][i] = true;
	tot = 1 << n;
	for (int i = 1; i < tot; i++) {
		int tmp = i ^ (i & -i);
		bitset<kN> bs = graph[__lg(i & -i)];
		while (tmp) {
			bs &= graph[__lg(tmp & -tmp)];
			tmp ^= tmp & -tmp;
		}
		for (int j = 0; j < n; j++) if (i & (1 << j)) {
			if (!bs[j]) goto Bad;
		}
		v.PB(i);
Bad:
		continue;
	}

	for (int i = 0; i < tot; i++) dp[i] = n;
	for (int i : v) dp[i] = 1;

	for (int i = 0; i < tot; i++)
		for (int j = i; j > 0; j = (j - 1) & (i ^ (i & -i))) dp[i] = min(dp[i], dp[j] + dp[i ^ j]);
	printf("%d\n", dp[tot - 1]);
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define maxn 5005

int main()
{
 int n,m;
 cin>>n>>m;
 vector<int> h(n),w(m);
 for(int i=0;i<n;i++){
    cin>>h[i];
 }
 for(int i=0;i<m;i++){
    cin>>w[i];
 }
 sort(h.begin(),h.end());
 ll pref[n]={0};
 pref[0]=-h[0];
 for(int i=1;i<n;i++){
    if(i%2){
        pref[i]=pref[i-1]+h[i];
    }
    else{
        pref[i]=pref[i-1]-h[i];
    }
 }
 ll suff[n+1]={0};

 for(int i=n-1;i>=0;i--){
    if(i%2){
        suff[i]=suff[i+1]+h[i];
    }
    else{
        suff[i]=suff[i+1]-h[i];
    }
 }

 ll ans=1e18;
 for(int i=0;i<m;i++){
    ll tans=0;
    auto ind=upper_bound(h.begin(),h.end(),w[i]);
    int ind1=upper_bound(h.begin(),h.end(),w[i])-h.begin();
    ind1--;
    if(ind==h.end()){
        if(w[i]<=h[0]){
       tans-=w[i];
       tans-=suff[0];
        }
        else{
            tans+=pref[n-1];
            tans+=w[i];
        }
    }
    else{
    tans=pref[ind1];
   // cout<<ind1<<" "<<pref[ind1]<<" "<<suff[ind1+1]<<endl;
    ind1++;
    if(ind1%2){
        tans+=w[i];
        tans-=suff[ind1];
    }
    else{
        tans-=w[i];
        tans-=suff[ind1];
    }
    }
    ans=min(ans,tans);
    //cout<<tans<<endl;
 }
 cout<<ans<<endl;
}


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

const ll mod = 1000000007;

int main() {
  ll T, N, A, B;
  ll a, b, ans;
  cin >> T;
  ll l, r;

  rep(i, T) {
    cin >> N >> A >> B;
    a = max(A, B);
    b = min(A, B);
    l = (N - a + 1) * (N - a + 1) % mod;
    r = (N - b + 1) * (N - b + 1) % mod;
    ans = l * r % mod;

    l = (N - b + 1) * (N - a + 1) % mod;
    ll x = N - a - b;
    if (x >= 0) {
      r = (x + 1) * (x + 2) % mod;
    } else {
      r = 0;
    }
    ans -= (l - r) * (l - r) % mod;
    ans %= mod;
    if (ans < 0) ans += mod;
    cout << ans << endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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

template <int mod> struct ModInt {
    int x;

    ModInt() : x(0) {}

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

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

    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;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

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

    friend ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, ModInt &a) {
        int64_t t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = ModInt<MOD>;

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll N, A, B;
        cin >> N >> A >> B;
        mint ans = mint(N - A + 1).pow(2) * mint(N - B + 1).pow(2);
        mint tmp = mint(N - A + 1) * (N - B + 1);
        if(N - A - B >= 0) tmp -= mint(N - A - B + 2) * mint(N - A - B + 1);
        ans -= tmp.pow(2);
        cout << ans << "\n";
    }
}


#include <set>
#include <unordered_set>
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <queue>
#include <bitset>

#define INFL 100100100100100100
#define INF 1100100100
using namespace std;

int main() {
	long long b;
	cin >> b;
	long long c;
	cin >> c;
	long long br, bl, brr, bll;
	br = b + (c-2)/2;
	bl = b - c/2;
	brr = -b + (c-1)/2;
	bll = -b - (c-1)/2;
	if (br < brr || bl < bll) {
		swap(br, brr);
		swap(bl, bll);
	}
	cout << br - bl + 1 + brr - bll + 1 - (bl <= brr ? brr - bl + 1: 0) << endl;
	return 0;
}

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

// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using plglg = pair<ll, ll>;
using tlglglg = tuple<ll, ll, ll>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; cin >> x; return x;}
inline ll IN() {ll x; cin >> x; return x;}
inline vs split(const string& t, char c) {vs v; stringstream s(t); string b; while(getline(s, b, c)) v.eb(b); return v;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template <typename T, typename S> inline void print(const pair<T, S>& p) {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << endl;}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v) {for (auto&& p : v) print(p);}
template <typename T> inline void print(const vector<T>& v, string s = " ") {REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {cout << (ok?yes:no) << endl;}

signed main() {
    //my-code
    ll N, D, H; cin >> N >> D >> H;
    vll d(N), h(N); REP(i, N) cin >> d[i] >> h[i];

    double ans = (double) 0.0;

    REP(i, N) {
        double x = 0.0;
        if (d[i] == D) {
            x = h[i];
        } else {
            x = h[i] - (h[i]-H)*d[i]*1.0/(d[i]-D);
        }
        chmax(ans, x);
    }

    cout << fixed << ans << endl;

    return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int         long long
#define mod         1000000007
#define FAST        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define f(i,n)      for(int i=0;i<n;i++)
#define fp(i,k,n)   for(int i=k;i<=n;i++)
#define fr(i,k,n)   for(int i=k;i>=n;i--)
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define dbg(x)      cout << (#x) << " is " << (x) << '\n'
#define F           first
#define S           second
#define sz(x)       (int)(x).size()
#define lb          lower_bound
#define ub          upper_bound
#define mems(x)     memset(x,0,sizeof(x))
#define all(a)      a.begin(),a.end()
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

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

void solve()
{
 int n;
 cin >> n;
 map<string,int> mp,mp1;
 int ok=0;
 string res;
 res="";
 f(i,n)
 {
   string x;
   cin >> x;
   if(x[0]=='!')
   {
     if((int)x.length()==1)
      continue;
     string y=x.substr(1,(int)x.length()-1);
     mp[y]++;
     if(mp[y]>=1 && mp1[y]>=1)
      {res=y;ok=1;}
   }
   else
   {
     mp1[x]++;
     if(mp[x]>=1 && mp1[x]>=1)
      {
        res=x;ok=1;
      }
   }
 }
 if(ok)
  cout << res << '\n';
 else
  cout << "satisfiable\n";
 return;
}

signed main()
{
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  FAST
  int T=1;
  // cin >> T;
  while(T--)
  {
    solve();
  }
  return 0;
}

// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()

#define pll pair<ll,ll>
#define ff first
#define ss second

#define MOD 1000000007

const int N = 100005;

void solve()
{
    ll n;
    cin >> n;
    map<ll,ll> y;
    bool chk = 1;
    for(ll i=0,x;i<n;i++) {
        cin >> x;
        y[x]++;
    }
    for(ll i = 1 ; i <= n ; i++) {
        if(y[i] != 1)  chk = 0;
    }
    if(chk)  cout <<"Yes\n";
    else     cout << "No\n";
}

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

    int t = 1;

    for(int i = 1; i < t + 1; i++) {
        solve();
    }

}


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

int main(){
    int a, b, c;
    cin >> a >> b >> c;
    if(a != b && b != c && c != a){
        cout << 0 << endl;
    }else if(a == b){
        cout << c << endl;
    }else if(b == c){
        cout << a << endl;
    }else if(a == c){
        cout << b << endl;
    }
}

#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int a,b,c; cin >> a >> b >> c;
  if(c%2 == 0){
    a = abs(a);
    b = abs(b);
    if(a == b) cout << "=\n";
    if(a < b) cout << "<\n";
    if(a > b) cout << ">\n";
  }else{
    if(a == b) cout << "=\n";
    if(a < b) cout << "<\n";
    if(a > b) cout << ">\n";
  }
  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#define inp 200005
#define check exit(0)
#define nl cout<<endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(v) for(int i=0;i<v.size();i++) {cout<<v[i]; (i==v.size()-1) ? cout<<"\n":cout<<" "; }
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

// Pick yourself up, 'cause...

int main()
{
    jaldi

    int n;
    cin>>n;

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

    vector<ll> ans(n);
    ordered_set os;

    for(int i=0;i<n;i++)
    {
        ans[0] += v[i] - os.order_of_key(v[i]);
        os.insert(v[i]);
    }

    for(int i=1;i<n;i++)
    {
        ans[i] = ans[i-1] - v[i-1] + (n - 1 - v[i-1]);
    }

    for(ll x:ans) cout<<x<<" \n";

    return 0;
}

// Try till you are out of ideas

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

template<typename T> class segtree {
private:
    int n,sz,h;
    vector<T> node, lazy;
    void eval(int k) {
        if(lazy[k]){
            node[k] += lazy[k];
            if(k < n) {
                lazy[k*2] += lazy[k] / 2, lazy[k*2+1] += lazy[k] / 2;
            }
            lazy[k] = 0;
        }
    }

public:
    segtree(const vector<T>& v) : n(1), sz((int)v.size()), h(0) {
        while(n < sz) n *= 2, h++;
        node.resize(2*n, 0), lazy.resize(2*n, 0);
        for(int i = 0; i < sz; i++) node[i+n] = v[i];
        for(int i = n-1; i >= 1; i--) node[i] = node[2*i] + node[2*i+1];
    }
    void range(int a, int b, T x, int k=1, int l=0, int r=-1){
        if(r < 0) r = n;
        eval(k);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b){
            lazy[k] += (r-l)*x;
            eval(k);
        }else{
            range(a, b, x, 2*k, l, (l+r)/2);
            range(a, b, x, 2*k+1, (l+r)/2, r);
            node[k] = node[2*k] + node[2*k+1];
        }
    }
    T query(int a, int b) {
        a += n, b += n - 1;
        for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
        b++;
        T res1 = 0, res2 = 0;
        while(a < b) {
            if(a & 1) eval(a), res1 += node[a++];
            if(b & 1) eval(--b), res2 += node[b];
            a >>= 1, b >>= 1;
        }
        return res1 + res2;
    }
    void _print(){for(int i = 0; i < sz; i++) cout<<query(i,i+1)<< " ";cout<<endl;}
};

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

    ll n; cin >> n;
    ll a[n]; zep(i, 0, n)cin >> a[i];

    vector<ll> v(n, 0);
    segtree<ll> s(v);

    ll ans = 0;
    ll cnt[n]; memset(cnt, 0, sizeof(cnt));
    zep(i, 0, n){
        ans += s.query(a[i], n);
        cnt[a[i]] = a[i] - s.query(0, a[i]);
        s.range(a[i], a[i] + 1, 1);
    }
    //printa(cnt, 0, n - 1)
    vector<ll> u(n, 0);
    segtree<ll> t(u);

    print(ans)
    zep(i, 0, n - 1){
        ans += n - i - 1 - 2 * cnt[a[i]];
        //print(ans)
        ans += t.query(a[i], n) - t.query(0, a[i]);
        print(ans)
        t.range(a[i], a[i] + 1, 1);
    }

    return 0;
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  vector<int> vec(n);
  vector<ll> re_sum(n);
  rep(i,n) {
    cin >> vec[i];
  }
  sort(vec.begin(), vec.end());
  ll sum = 0, ans = 0;
  for (ll i = 0; i < n; ++i) {
    ans += vec[i]*i - sum;
    sum += vec[i];
  }
  cout << ans << '\n';
}

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

*/
ll N;
bool is_sorted(ll G[])
{
  rep(i, N-1){
    if (G[i] > G[i+1]) return false;
  }
  return true;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  ll T;
  cin >> T;
  rep(i, T){
    cin >> N;
    ll G[N];
    ll cnt = 0;
    vector<ll> ans;
    rep(j, N) cin >> G[j];
    while (!is_sorted(G)){
      ll tmp = -1;
      if (cnt%2 == 0){
        for(ll k=0; k+1<N; k+=2){
          if (G[k] < G[k+1]) continue;
          tmp = k;
        }
        if (tmp == -1) tmp = 0;
      }
      if (cnt%2 == 1){
        for(ll k=1; k+1<N; k+=2){
          if (G[k] < G[k+1]) continue;
          tmp = k;
        }
        if (tmp == -1) tmp = 1;
      }
      swap(G[tmp], G[tmp+1]);
      ans.push_back(tmp+1);
      cnt++;
    }
    ll asz = ans.size();
    cout << asz << endl;
    rep(j, asz){
      cout << ans[j];
      if (j == asz-1) cout << endl;
      else cout << " ";
    }
  }
}


//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int maxN = 1005;
int dist[maxN][maxN];
int a[maxN];
int b[maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
//        int ni = i;
//        if (i & (1 << r)) ni -= (1 << r);
        a[i] = (2 * i) % n;
        b[i] = (2 * i + 1) % n;
        a[i] = max(a[i], 0);
        a[i] = min(a[i], n - 1);
        b[i] = max(b[i], 0);
        b[i] = min(b[i], n - 1);
    }
//    for (int i = 0; i < n; i++) {
//        for (int j = 0; j < n; j++) {
//            dist[i][j] = 500;
//        }
//        dist[i][i] = 0;
//        if (i != a[i]) dist[i][a[i]] = 1;
//        if (i != b[i]) dist[i][b[i]] = 1;
//    }
//    for (int k = 0; k < n; k++) {
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < n; j++) {
//                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
//            }
//        }
//    }
    for (int i = 0; i < n; i++) {
        cout << a[i] + 1 << " " << b[i] + 1 << '\n';
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

struct A {
    int id;
    int l, r;
    A(int id) : id(id) {
        l = r = 0;
    }
};

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

    int n;
    cin >> n;
    vector<A> v;
    for (int i = 1; i <= n; ++i) {
        v.push_back(A(i));
        if (i * 2 <= n) v.back().l = 2 * i;
        if (i * 2 + 1 <= n) v.back().r = 2 * i + 1;
    }
    reverse(v.begin(), v.end());
    for (int i = 0; i < n; ++i) {
        if (v[i].r == 0 && 2 * i + 2 < n) v[i].r = v[2 * i + 2].id;
        if (v[i].l == 0 && 2 * i + 1 < n) v[i].l = v[2 * i + 1].id;
    }
    reverse(v.begin(), v.end());
    for (auto& x : v) {
        if (x.l == 0) x.l = 1;
        if (x.r == 0) x.r = n;
        cout << x.l << ' ' << x.r << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI  = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1;  // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;

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

    ll N; cin >> N;
    VLL A(N),T(N); REP(i,N) cin >> A[i] >> T[i];

    ll Q; cin >> Q;
    VLL X(Q); REP(i,Q) cin >> X[i];

    ll L = -INF, R = INF;
    ll S = 0;  // shift
    REP(i,N) {
        ll a = A[i];
        ll t = T[i];
        if (t == 1) {  // x + a
            S += a;
            L += a;
            R += a;
        } else if (t == 2) {  // max(x, a)
            chmax(L, a);
            if (R < L) R = L;
        } else if (t == 3) {  // min(x, a)
            chmin(R, a);
            if (R < L) L = R;
        }
    }

    // debug(L); debug(R);
    // debug(S);

    for (ll x : X) {
        if (x + S < L) {
            COUT(L);
        } else if (R < x + S) {
            COUT(R);
        } else {
            COUT(x + S);
        }
    }

    // REP(_,L) COUT(X[L] + S);
    // FOR(i,L,R+1) COUT(X[i] + S);
    // REP(_,(Q-1)-R) COUT(X[R] + S);

    return 0;
}

#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007LL
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define SHIFT(n) (1LL<<((ll)n))
#define ALL(a) (a).begin(),(a).end()
#define Max(a) (*max_element(ALL(a)))
#define Min(a) (*min_element(ALL(a)))
using namespace std;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin>>n;
    ll ma = -2*llINF,mi = 2*llINF,plus = 0;
    while(n--){
        ll a,t;cin>>a>>t;
        if(t == 1){
            ma += a;
            mi += a;
            plus += a;
        }else if(t == 2){
            ma = max(ma,a);
            mi = max(mi,a);
        }else{
            ma = min(ma,a);
            mi = min(mi,a);
        }
    }
    ll q;cin>>q;
    while(q--){
        ll a;cin>>a;
        //cout << ma << " " << mi << endl;
        cout << min(mi,max(ma,a+plus)) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

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

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

/* Some Libraries */

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

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    vll a(4);
    rep(i,4) cin>>a[i];
    rep(i,1<<4){
        ll sum=0, rest=0;
        rep(j,4){
            if(i>>j&1){
                sum+=a[j];
            }else{
                rest+=a[j];
            }
        }
        if (sum==rest){
            cout<<"Yes\n";
            return 0;
        }
    }
    cout<<"No\n";
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

    vector<int> A(n + m);
    for (auto &a: A)
        cin >> a;

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

    vector<int> B;
    for (const auto &a: A){
        if (B.size() > 0 and B.back() == a)
            B.pop_back();
        else
            B.push_back(a);
    }

    for (auto &b: B)
        cout << b << " ";
    cout << endl;
}


#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

ll gcd(ll x,ll y){

	x = abs(x);
	y = abs(y);

	if(x < y){
		swap(x,y);
	}
	if(y == 0){
		return x;
	}else{
		return gcd(y,x%y);
	}
}

//最小公倍数
ll lcm(ll x,ll y){

	return (x*y)/gcd(x,y);
}

ll extgcd(ll a,ll b,ll &x,ll &y){
	ll d = a;
	if(b != 0){
		d = extgcd(b,a%b,y,x);
		y -= (a/b)*x;
	}else{
		x = 1;
		y = 0;
	}
	return d;
}

ll mod_inverse(ll a,ll m){
    ll x,y;
    extgcd(a,m,x,y);
    return (m+x%m)%m;
}

void func(){

	ll N,S,K;
	scanf("%lld %lld %lld",&N,&S,&K);

	ll common = gcd(K,N);
	if(S%common != 0){

		printf("-1\n");
		return;
	}

	N /= common;
	S /= common;
	K /= common;

	S = (-S+N)%N;
	ll work = mod_inverse(K,N);

    ll ans = (S*work)%N;

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

int main(){

	int num_case;

	scanf("%d",&num_case);

	for(int loop = 0; loop < num_case; loop++){

		func();
	}

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){
  long long n;
  cin >> n;
  vector<int> a(n);
  for(auto &nx : a){cin >> nx;}
  long long res1=(n*(n-1)/2);

  sort(a.begin(),a.end());
  a.push_back(-1);
  long long cnt=1;
  for(int i=0;i<n;i++){
    if(a[i]!=a[i+1]){
      res1-=((cnt*(cnt-1))/2);
      cnt=1;
    }
    else{cnt++;}
  }
  cout << res1 << '\n';
  //cout << res2 << '\n';
}


#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;

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

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

int main(){
    string s;
    cin >> s;
    rep(i,s.size()){
        if(s[i] == '6') s[i] = '9';
        else if(s[i] == '9') s[i] = '6';
    }
    reverse(all(s));
    cout << s << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i,m,n) for(int i = m - 1; i >= n; i--)
#define ALL(v) v.begin(), v.end()
#define itn int
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define YES() cout << "YES" << endl
#define NO() cout << "NO" << endl
#define println(x) cout << x << endl
#define print(x) cout << x << " "
template<typename T, typename U>
inline bool CMAX(T &m, U x) { if (m < x) { m = x; return true; } return false; }
template<typename T, typename U>
inline bool CMIN(T &m, U x) { if (m > x) { m = x; return true; } return false; }

typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;
const int MOD = 1e9+7;
int main(){
    string s;
    cin >> s;
    string ans;
    REPR(i, s.size()){
        if(s[i] == '0'){
            ans += "0";
        }
        if(s[i] == '1'){
            ans += "1";
        }
        if(s[i] == '6'){
            ans += "9";
        }
        if(s[i] == '8'){
            ans += "8";
        }
        if(s[i] == '9'){
            ans += "6";
        }
    }
    println(ans);
    return 0;
}


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

#define ll long long
#define ii pair <int,int>
#define F first
#define S second
#define ever (;;)

const int N = 100100;

int n,m,x,y,par[N];
ll dis[N],inf=1e18;
vector < pair <int,ii> > adj[N];
priority_queue < pair < ll , int > , vector < pair < ll , int > > , greater < pair < ll , int > > > q;

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

void mrg(int x,int y)
{
    x = get(x);
    y = get(y);

    if( x == y )
        return;

    par[x] = y;
}

void dijkstra(int p)
{
    for(int i=1;i<=n;i++)
        dis[i] = inf;

    dis[p] = 0;
    q.push( { 0 , p } );

    while( !q.empty() )
    {
       // ll w = q.top().F;
        int u = q.top().S;
          ll w = dis [u];
        q.pop();

        //if( dis[u] < w )
            //continue;

        for(auto &X:adj[u])
        {
            int x = X.F;
            int t = X.S.F;
            int k = X.S.S;

            int toreach = (k-w%k)%k;
            if( dis[x] > dis[u] + toreach + t )
            {
                dis[x] = dis[u] + toreach + t;
                q.push( { dis[x] , x } );
            }
        }
    }
}

int main()
{
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=n;i++)
        par[i] = i;
    for(int i=1;i<=m;i++)
    {
        int a,b,t,k;
        scanf("%d%d%d%d",&a,&b,&t,&k);
        adj[a].push_back( { b , { t , k } } );
        adj[b].push_back( { a , { t , k } } );
        mrg(a,b);
    }

    if( get(x) != get(y) )
    {
        printf("-1\n");
        return 0;
    }

    dijkstra(x);

    printf("%lld\n",dis[y]);
}

#include "bits/stdc++.h"
/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
// a new data structure defined. Please refer below
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
//new_data_set p;
//p.find_by_order(3)
//p.order_of_key(6)
*/
//#include <algorithm>
#define ll long long
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define lb lower_bound
#define ub upper_bound
#define endll "\n";
#define all(x) (x).begin(), (x).end()
#define forr(it,x) for(auto it=x.begin();it!=x.end();it++)
#define for1(i,a,b) for(int i=a;i<b;i++)
#define for2(i,a,b) for(int i=a;i>=b;i--)
#define for3(i,a,b) for(long long int i=a;i<b;i++)
#define for4(i,a,b) for(long long int i=a;i>=b;i--)
#define pp pair<int,int>
#define ld long double
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpp vector<pp>
#define vi vector<int>
#define vpll vector<pll>
#define vld vector<ld>
#define vvld vector<vld>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvpp vector<vpp>
#define I insert
#define mem(a,x) memset(a,x,sizeof(a))
#define p_queue(a) priority_queue<a,vector<a>,greater<a>>
using namespace std;
//const int dx[8]={1,0,-1,0,1,1,-1,-1},dy[8]={0,1,0,-1,-1,1,-1,1};
//const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int N=2e5+5;
const int INF = 1e9+5;
/*vector<bool> p(N+1, true);
void sieve()
{
  p[0] = p[1] = false;
  int i,j;
  for (i = 2; i*i <= N; i++)
  {
      if (p[i])
      {
          for (j = i * i; j <=N; j += i)
            {
        p[j] =false;
        }
      }
  }
}*/
/*ll power(ll a1, ll b1){
    ll res = 1;
    while(b1 > 0){
        if(b1 % 2){
            res = 1LL * res * a1 % mod;
        }
        a1 = 1LL * a1 * a1 % mod;
        b1 /= 2;
    }
    return res;
}
ll invmod(ll a1,ll b1){
  return power(a1,b1-2);
}*/
/*ll ncr(int n1,int c1){
  if(n1<c1)
    return 0;
  if(n1==c1||c1==0)
    return 1;
  ll n=n1,c=c1;
  vll fact(n+1,1);
  for1(i,1,n+1){
    fact[i]=fact[i-1];
    fact[i]*=i;
    fact[i]%=mod;
  }
  ll fact1=fact[n];
  ll fact2=fact[c];
  ll fact3=fact[n-c];
  fact1*=invmod(fact2,mod);fact1%=mod;
  fact1*=invmod(fact3,mod);fact1%=mod;
  return fact1;
}*/
/*int gcd(int a,int b){
    if(a<b) swap(a,b);
    if(b==0) return a;
    a%=b;
    return gcd(b,a);
}*/
/*int lcm(int a,int b){
   return (a*1LL*b)/gcd(a,b);
}*/
vll dist,vis;
vector<vector<int>> adj[N];
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,m,x,y,u,v,t1,k1;cin>>n>>m>>x>>y;
    dist.resize(n+1,1e18);vis.resize(n+1,0);
    for1(i,0,m){
      cin>>u>>v>>t1>>k1;
      adj[u].pb({v,k1,t1});
      adj[v].pb({u,k1,t1});
    }
    dist[x]=0;
    p_queue(pll) pq;
    for1(i,1,n+1){
      pq.push({dist[i],i});
    }
    while(!pq.empty()){
      pair<ll,int> src=pq.top();pq.pop();
      //cout<<src.se<<endll;
      if(!vis[src.se]){
        vis[src.se]=1;
        for(auto x:adj[src.se]){
          if(!vis[x[0]]){
            /*ll t2=(src.fi+x[2]+x[1]-1)/(x[1]);
            t2*=x[1];
            if(dist[x[0]]>src.fi+x[1]){
              dist[x[0]]=src.fi+x[1];pq.push({t2,x[0]});
            }
            //cout<<x[0]<<" "<<dist[x[0]]<<" "<<t2<<endll;*/
            ll t2=(src.fi+x[1]-1)/x[1];
            t2*=x[1];t2+=x[2];
            if(dist[x[0]]>t2){
              dist[x[0]]=t2;pq.push({t2,x[0]});
            }
          }
        }
      }
    }
    //for1(i,1,n+1) cout<<dist[i]<<" ";
    //cout<<endll;
    if(dist[y]==1e18){
      cout<<-1<<endll;
    }
    else{
      cout<<dist[y]<<endll;
    }
    return 0;
}

/*

*/


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

typedef long long ll;

const int MAX_P = 3;
int fact[MAX_P];

int extgcd(int a, int b, int& x, int& y) {
    int d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

int mod_inverse(int a, int m) {
    int x, y;
    extgcd(a, m, x, y);
    return (m + x % m) % m;
}

// n!=a p^e としたときのa mod pを求める. O(p)
int mod_fact(int n, int p, int&e) {
    e = 0;
    if (n == 0) return 1;

    // pの倍数の部分を計算
    int res = mod_fact(n / p, p, e);
    e += n / p;

    // (p-1)!=-1なので(p-1)!^(n/p)はn/pの偶奇だけで計算できる
    if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
    return res * fact[n % p] % p;
}

// nCk mod pを求める. O(log_p n)
int mod_comb(int n, int k, int p) {
    if (n < 0 || k < 0 || n < k) return 0;
    int e1, e2, e3;
    int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n - k, p, e3);
    if (e1 > e2 + e3) return 0;
    return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

int main() {
    fact[0] = 1;
    fact[1] = 1;
    fact[2] = 2;
    int N;
    string c;
    cin >> N >> c;
    int s = 0;
    rep(i, N) {
        int x;
        if (c[i] == 'B') x = 0;
        if (c[i] == 'W') x = 1;
        if (c[i] == 'R') x = 2;
        s = (s + x * (2 - (N % 2)) * mod_comb(N - 1, i, 3)) % 3;
        //cout << mod_comb(N - 1, i, 3) << " " << s << endl;
    }
    if (s == 0) printf("B\n");
    if (s == 1) printf("W\n");
    if (s == 2) printf("R\n");
}

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

const int N=0;

void solve(){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	cout << a * d - b * c;
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

int main() {
    int N;
    cin >> N;
    V<int> A(N), B(N);
    rep(i, N) cin >> A[i];
    rep(i, N) cin >> B[i];
    ll ans = 0;
    ll s = 0;
    V<ll> v1, v2;

    rep(i, N) {
        s += A[i];
        if (i & 1) {
            v1.pb(A[i] - B[i]);
        } else {
            v2.pb(A[i] - B[i]);
        }
    }
    sort(ALL(v1));
    sort(ALL(v2));
    ans = s;
    rep(i, N / 2) {
        s -= v1[i] + v2[i];
        chmax(ans, s);
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) ((int) a.size())
#define vi vector<int>
using namespace std;
const int N = 2e5 + 7;
int n, a[N], b[N], r1[N], r2[N];
ll sum;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	L(i, 1, n) cin >> a[i];
	L(i, 1, n) cin >> b[i], a[i] -= b[i], sum += b[i];
	n >>= 1;
	L(i, 1, n) r1[i] = a[i * 2 - 1];
	L(i, 1, n) r2[i] = a[i * 2];
	sort(r1 + 1, r1 + n + 1);
	sort(r2 + 1, r2 + n + 1);
	L(i, 1, n) if(r1[i] + r2[i] > 0) sum += r1[i] + r2[i];
	cout << sum << '\n';
	return 0;
}


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

void TESTCASE(){
    int H, W; cin >> H >> W;
    vector<vector<char>> grid(H+1, vector<char>(W+1, '.'));
    int K; cin >> K;
    for(int q = 1, h, w; q <= K; q++){
        char c;
        cin >> h >> w >> c;
        grid[h][w] = c;
    }
    vector<vector<int>> dp(H+1, vector<int>(W+1, 0));
    vector<vector<int>> ways1(H+1, vector<int>(W+1, 1)), ways2(H + 1, vector<int>(W+1, 1));
    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            if(grid[i][j] == '.') ways1[i][j] = ways1[i][j-1] * 3 % mod;
            else ways1[i][j] = ways1[i][j-1];
        }
    }
    for(int j = 1; j <= W; j++){
        for(int i = 1; i <= H; i++){
            if( grid[i][j] == '.' ) ways2[i][j] = ways2[i-1][j] * 3 % mod;
            else ways2[i][j] = ways2[i-1][j];
        }
    }
    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            if( i == 1 && j == 1 ){
                dp[i][j] = 1;
            }
            else{
                int up = 0, left = 0, sl = 1, su = 1;
                if(i - 1 >= 1 && grid[i-1][j] == 'D'){
                    up = dp[i-1][j];
                }
                else if( i - 1 >= 1 && grid[i-1][j] == 'X'){
                    up = dp[i-1][j];
                }
                else if(i - 1 >= 1 && grid[i-1][j] == '.'){
                    up = dp[i-1][j] * 2; up %= mod;
                }
                if(j - 1 >= 1 && grid[i][j-1] == 'R'){
                    left = dp[i][j-1];
                }
                else if(j - 1 >= 1 && grid[i][j-1] == 'X'){
                    left = dp[i][j-1];
                }
                else if(j - 1 >= 1 && grid[i][j-1] == '.'){
                    left = dp[i][j-1] * 2; left %= mod;
                }
                dp[i][j] = ((up * ways1[i][j-1] % mod) + (left * ways2[i-1][j] % mod) % mod) ;
                dp[i][j] %= mod;
            }
        }
    }

    int c = 1;
    if(grid[H][W] == '.') c = 3;
    else if(grid[H][W] == 'X') c = 2;
    else c = 1;
    cout << ((dp[H][W]*c) % mod) << "\n";
    return;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	TESTCASE();
    return 0;
}

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 998244353
#define LLI long long int
#define REP(i, n) for(int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for(int i = s, i##_l = (e); i < i##_l; i++)
#define _min(a,b) ((a<b)?a:b)
#define _max(a,b) ((a<b)?b:a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a)?1:-1)
#define SORT(v) sort(v.begin(),v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380
LLI power(LLI a, LLI b) { LLI res = 1; while (b > 0) { if (b & 1)res = res * a % MOD; a = a * a % MOD; b >>= 1; }return res; }
int main()
{
	cout << fixed << setprecision(10);

	int h, w, k;
	cin >> h >> w >> k;
	vector<vector<int>> m(h, vector<int>(w, '?'));
	vector<vector<LLI>> dp(h, vector<LLI>(w, 0));

	vector<vector<LLI>> qh(h, vector<LLI>(w + 1));
	vector<vector<LLI>> qw(h + 1, vector<LLI>(w));

	vector<LLI> powlist(_max(w + 1, h + 1));
	powlist[0] = 1;
	FOR(i, 1, powlist.size())
	{
		powlist[i] = (powlist[i - 1] * 3) % MOD;
	}

	REP(i, k)
	{
		int x, y;
		char c;
		cin >> y >> x >> c;
		m[y - 1][x - 1] = c;
	}

	// 区間和
	REP(y, h)
	{
		REP(x, w)
		{
			qh[y][x + 1] += qh[y][x] + (m[y][x] == '?' ? 1 : 0);
		}
	}
	REP(x, w)
	{
		REP(y, h)
		{
			qw[y + 1][x] += qw[y][x] + (m[y][x] == '?' ? 1 : 0);
		}
	}

	dp[0][0] = 1;
	REP(y, h)
	{
		REP(x, w)
		{
			if (0 < y)
			{
				LLI mul = powlist[qh[y][x]];

				if (m[y - 1][x] == 'D' || m[y - 1][x] == 'X')
				{
					dp[y][x] = (((dp[y - 1][x]) % MOD) * mul) % MOD + dp[y][x];
				}
				if (m[y - 1][x] == '?')
				{
					dp[y][x] = (((dp[y - 1][x] * 2) % MOD) * mul) % MOD + dp[y][x];
				}
			}
			dp[y][x] %= MOD;
			if (0 < x)
			{
				LLI mul = powlist[qw[y][x]];
				if (m[y][x - 1] == 'R' || m[y][x - 1] == 'X')
				{
					dp[y][x] = (((dp[y][x - 1]) % MOD) * mul) % MOD + dp[y][x];
				}
				if (m[y][x - 1] == '?')
				{
					dp[y][x] = (((dp[y][x - 1] * 2) % MOD) * mul) % MOD + dp[y][x];
				}
			}
			dp[y][x] %= MOD;
		}
	}

	LLI ans = dp[h - 1][w - 1];
	if (m[h - 1][w - 1] == '?')ans *= 3;
	ans %= MOD;
	cout << ans;
	return 0;
}


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

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

  int n[10] = {0};
  rep(i,s.size()){
    n[s[i]-'0'] ++;
  }

  bool flag = false;

  if(s.size() ==1){
    if(stoi(s)%8 ==0) flag = true;
    else flag = false;
  }

  else if(s.size() == 2){
    if(((s[1]-'0')*10+(s[0]-'0')) %8 == 0 || ((s[0]-'0')*10+(s[1]-'0')) %8 == 0) flag = true;
    else flag = false;
  }

  else{
    rep(i,1000){
      int a[10];
      rep(i,10){
        a[i] = n[i];
      }

      if(i%8 == 0){
        a[i%10] --;
        a[(i/10)%10] --;
        a[(i/100)%10] --;

        flag = true;
        rep(i,10){
          if(a[i] < 0){
            flag = false;
          }
        }
        if(flag) break;
      }
    }
  }

  if(flag) cout << "Yes" << endl;
  else cout << "No" <<  endl;

}

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
#include <cassert>
using namespace std;
#define MOD (long long int)(1e9+7)
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
  else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
  if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
  if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
  while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r){
  if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く？不安.
  if(n < r || r < 0) return 0;
  ll ret = 1;
  if(n <= 1e7){
    ret *= kaijo(n); ret %= MOD;
    ret *= gyaku_kaijo(r); ret %= MOD;
    ret *= gyaku_kaijo(n-r); ret %= MOD;
  }else{
    rep(i,r){
      ret *= n-i; ret %= MOD;
      ret *= mpow(r-i, MOD-2); ret %= MOD;
    }
  }
  return ret;
}

vector<ll> yakusu(ll n){
  vector<ll> ret;
  for(ll p = 1; p*p <= n; p++){
    if(n%p == 0){
      ret.push_back(p);
      if(p*p != n){
        ret.push_back(n/p);
      }
    }
  }
  sort(all(ret));
  return ret;
}

map<ll,ll> soinsu(ll n){
  map<ll,ll> mp;
  for(ll p = 2; p*p <= n; p++){
    if(n%p == 0){
      mp[p]++;
      n /= p;
      p--;
    }
  }
  if(n > 1){
    mp[n]++;
  }
  return mp;
}

int main(void){
  fast_io
  cout<<fixed<<setprecision(15);

  int n;cin>>n;
  vector<ll> A(n);
  rep(i,n){
    cin>>A[i];
  }
  ll ans = 0;
  rep(i,n){
    ans = gcd(ans, A[i]);
  }
  cout<<ans<<endl;
  return 0;

}


/*
  Created by: Amit Kumar at 22:29 on 16 Mar 21
*/
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define db(...)
#endif
using namespace std;
#define ll long long
#define mod 1000000007
#define S(x, n) memset(x, n, sizeof(x));
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define NIL -1
const int MAXN = 1e5 + 1;

long long power(long long x, long long y, long long md)
{
    long long res = 1;
    x = x % md;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % md;
        y = y >> 1;
        x = (x * x) % md;
    }
    return res;
}

void solve()
{
    int n;
    cin >> n;
    string t;
    cin >> t;
    string s;
    cin >> s;

    // true for Takahasi
    vector<vector<bool>> dp(n + 1, vector<bool>(7, 0));
    dp[n][0] = true;
    for (int i = n - 1; i >= 0; i--)
    {
        if (s[i] == 'A')
        {
            for (int j = 0; j < 7; j++)
            {
                dp[i][j] = dp[i + 1][(j * 10) % 7] && dp[i + 1][((j * 10) + t[i] - '0') % 7];
            }
        }
        else
        {
            for (int j = 0; j < 7; j++)
            {
                dp[i][j] = dp[i + 1][(j * 10) % 7] || dp[i + 1][((j * 10) + t[i] - '0') % 7];
            }
        }
    }
    db(dp);
    if (dp[0][0])
        cout << "Takahashi\n";
    else
        cout << "Aoki\n";
}
int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    auto started = std::chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    t = 1;
    //cin >> t;
    for (int I = 1; I <= t; I++)
    {
#ifdef LOCAL
        cerr << "TestCase " << I << "\n";
#endif
        solve();
    }
#ifdef LOCAL
    auto done = std::chrono::high_resolution_clock::now();
    cerr << "Time = " << std::chrono::duration_cast<std::chrono::milliseconds>(done - started).count() << " ms\n";
#endif
    return 0;
}

#include <cstdio>
using namespace std;

const int max_n = 200000;
char s[max_n+1], op[max_n+1];
bool wn[7] = {}, nwn[7];

int main()
{
	int n, cur = 1;
	bool sl1, sl2;

	scanf("%d%s%s", &n, s, op);
	wn[0] = true;

	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = 0; j < 7; j++)
			nwn[j] = wn[j];

		if (op[i] == 'A')
		{
			for (int j = 0; j < 7; j++)
				if (!wn[(j+cur*(s[i]-'0'))%7])
					nwn[j] = false;
		}
		else
		{
			for (int j = 0; j < 7; j++)
				if (wn[(j+cur*(s[i]-'0'))%7])
					nwn[j] = true;
		}

		sl1 = true, sl2 = false;
		for (int j = 0; j < 7; j++)
		{
			wn[j] = nwn[j];
			sl1 &= wn[j], sl2 |= wn[j];
		}

		if (sl1)
		{
			puts("Takahashi");
			return 0;
		}
		else if (!sl2)
		{
			puts("Aoki");
			return 0;
		}

		cur = (cur * 10) % 7;
	}

	if (wn[0])
		puts("Takahashi");
	else
		puts("Aoki");

	return 0;
}


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

int main(){
    long double x,y,r; cin>>x>>y>>r;
    r += 1e-14;
    int lx = ceil(x-r);
    int rx = floor(x+r);
    ll ans = 0;
    for(int i=lx; i<=rx; i++){
        double uy = floor(y + sqrt(r*r - (x-i)*(x-i)));
        double dy = ceil(y - sqrt(r*r - (x-i)*(x-i)));
        ans += uy - dy + 1;
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
	#define ms(a) memset(a,0,sizeof(a))
	#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
	#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
	#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
	#define ce(i,r) i==r?'\n':' '
	#define pb push_back
	#define all(x) x.begin(),x.end()
	#define gmn(a,b) a=min(a,b)
	#define gmx(a,b) a=max(a,b)
	#define fi first
	#define se second
	typedef long long ll;
	typedef unsigned long long ull;
	typedef double db;
	const int infi=2e9;//infi??,????inf????int
	const ll infl=4e18;
	inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
	//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
	inline int qi(){
	    int f=0,fu=1; char c=getchar();
	    while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
	    while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
	    return f*fu;
	}
	inline ll ql(){
		ll f=0;int fu=1; char c=getchar();
	    while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
	    while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
	    return f*fu;
	}
	inline db qd(){
	    char c=getchar();int flag=1;double ans=0;
	    while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
	    if(c=='-') flag=-1,c=getchar();
	    while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
	    if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
	    return ans*flag;
	}
}
namespace Read{
	#define si(a) scanf("%d",&a)
	#define sl(a) scanf("%lld",&a)
	#define sd(a) scanf("%lf",&a)
	#define ss(a) scanf("%s",a)
	#define rai(x,a,b) repi(i,a,b) x[i]=qi()
	#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
	#define pi(x) printf("%d",x)
	#define pl(x) printf("%lld",x)
	#define ps(x) printf("%s",x)
	#define pc(x) printf("%c",x)
	#define pe() puts("")
}
namespace DeBug{
	#define MARK false
	#define DB if(MARK)
	#define pr(x) cout<<#x<<": "<<x<<endl
	#define pra(x,a,b) cout<<#x<<": "<<endl; \
			repi(i,a,b) cout<<x[i]<<" ";	 \
			puts("");
	#define FR(a) freopen(a,"r",stdin)
	#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
typedef long double ldb;
int main()
{
	ldb x,y,r; cin>>x>>y>>r;
	r+=1e-14;
	ll ans=0;
	repi(i,ceil(y-r),floor(y+r)){
		ldb dy=abs(y-i);
		if(dy>r) continue;
		ldb dx=sqrt(r*r-dy*dy);
		ll lx=1ll*ceil(x-dx),rx=1ll*floor(x+dx);
		ans+=(rx-lx+1);
	}
	pl(ans),pe();
	return 0;
}


#include <bits/stdc++.h>
namespace IO{
    char buf[1000000],*p1,*p2;
    inline char getc(){
        if(p1==p2) p2=(p1=buf)+fread(buf,1,1000000,stdin);
        return p1==p2?EOF:*(p1++);
    }
    template<typename tp>inline void r(tp &n){
        n=0;char c=getc();
        while(!isdigit(c)) c=getc();
        while( isdigit(c)) n=n*10+c-48,c=getc();
    }
    template<typename tp,typename...Args>inline void r(tp &n,Args&...args){
        r(n);r(args...);
    }
    template<typename tp>inline void write(tp  n){
        if(n/10) write(n/10);
        putchar(n%10+48);
    }
   template<typename tp>inline void w(tp n,char c='\n'){
       write(n);putchar(c);
    }
};
using namespace IO;
const int N=3e3+5,Mod=998244353;
using namespace std;
int f[N][N];
int solve(int n,int k){
    if(n<=k) return n==k;
    if(k==0) return 0;
    if(f[n][k]!=-1) return f[n][k];
    return f[n][k]=(solve(n-1,k-1)+solve(n,2*k))%Mod;
}
int main(){

   // freopen("1.in","r",stdin);

    int n,k;r(n,k);
    memset(f,-1,sizeof f);
    w(solve(n,k));
    return 0;
}

#include "bits/stdc++.h"

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

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

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

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

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

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

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

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

ll N, K;
vector<llvec> m;

ll f(ll n, ll k){
  //cerr <<  n << " " << k << " ";
  if(k<=0)return 0;
  if(n<=0 or k<=0 or n<k){
    m[n][k] = 0;
    return 0;
  }
  if(n==k)m[n][k]=1;
  if(m[n][k]>=0)return m[n][k];
  ll tmp=0;
  tmp = mod(f(n-1, k-1) + f(n, 2*k), MOD);
  //cerr << tmp << endl;
  m[n][k]=tmp;
  return tmp;
}

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  cin >> N >> K;
  m = vector<llvec>(N+1, llvec(2*(N+1), -1));
  cout << f(N, K);
  return 0;
}


#include <bits/stdc++.h> //Header Files

#define ll long long int
#define mod 1000000007
#define endl "\n"

using namespace std;

template <class T>
void out(int t, T ans)
{
    cout << "Case #" << t << ": " << ans << endl;
}

template <class T>
void out(int t, T ans[])
{
    cout << "Case #" << t << ": ";
    for (T val : ans)
        cout << val << " ";
    cout << endl;
}

template <class T>
void out(int t, vector<T> &ans)
{
    cout << "Case #" << t << ": ";
    for (T val : ans)
        cout << val << " ";
    cout << endl;
}

void runTest(int o)
{
    // Start coding here ...
    int n;
    cin >> n;

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

    sort(a, a + n, greater<ll>());

    ll pre[n];
    pre[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; i--)
        pre[i] = pre[i + 1] + a[i];

    ll sum = 0;
    for (int i = 0; i < n - 1; i++)
    {
        sum += abs(pre[i + 1] - (a[i] * (n - i - 1)));
    }

    cout << sum << endl;
}

int main()
{

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

    int t = 1;
    // cin >> t;

    for (int i = 0; i < t; i++)
        runTest(i + 1);

    return 0;
}

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

int main() {
  ll n; cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  ll ans=0;
  sort(v.begin(), v.end());
  for (ll i = 0; i < n; i++) {
    ans += (2ll*i - n + 1ll) * v[i];
  }
  cout << ans;
  return 0;
}


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

int main() {
    int N, M; cin >> N >> M;
    if(N == 1 && M == 0){
        cout << 1 << " " << 2 << endl;
        return 0;
    }
    if(M < 0){
        cout << -1 << endl;
        return 0;
    } else if(M == N || M == N-1){
        cout << -1 << endl;
        return 0;
    }
    vector<int> L(N), R(N);
    int X = N - 1 - M;
    X--;
    int now = 1;
    rep(i, X){
        L[i] = now;
        now++;
        R[i] = now;
        now++;
    }
    L[X] = now;
    now++;
    R[X] = N * 2;
    for (int i = X+1; i < N; i++){
        L[i] = now;
        now++;
        R[i] = now;
        now++;
    }

    rep(i, N){
        cout << L[i] << " " << R[i] << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<db> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	if(m==0){
		fup(i,1,n,1)printf("%d %d\n",i*2,i*2+1);
		return 0;
	}
	if(m<0 || m>=n-1)return !printf("-1\n");
	fup(i,1,m+1,1){
		printf("%d %d\n",2*i,2*i+1);
	}
	puts("1 500000");
	int rem=n-m-2;
	fup(i,1,rem,1){
		printf("%d %d\n",600000+2*i,600001+2*i);
	}
}


#include <bits/stdc++.h>

#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)

const int mxn = 1e5 + 5;
const int mod = 1e9 + 7;
int f[mxn];
int n, a[mxn];

int main() {
	scanf("%d", &n);
	rep(i, n) scanf("%d", &a[i]);
	f[0] = 1; f[1] = 2;
	for (int i = 1; i < n; ++ i) f[i + 1] = (f[i - 1] + f[i]) % mod;
	int ans = 0;
	rep(i, n) {
		int coef = 1LL * (i ? f[i - 1] : 1) * f[n - 1 - i] % mod;
		if (i) coef = (coef + mod - 1LL * (i - 1 ? f[i - 2] : 1) * (n - 1 - i ? f[n - 2 - i] : 1) % mod) % mod;
		(ans += 1LL * coef * a[i] % mod) %= mod;
	}
	printf("%d\n", ans);
	return 0;
}

#include<ctime>
#include<cstdio>
#include<cctype>
#define ll long long
using namespace std;
const ll N=1e5+7;
const ll p=1e9+7;
ll read(){
	char c;ll x=0,f=1;
	while(!isdigit(c=getchar()))f-=2*(c=='-');
	while(isdigit(c))x=x*10+f*(c-48),c=getchar();
	return x;
}
ll n,res,x,f[N],g[N],h[N];
int main(){
	n=read(),h[1]=0,h[2]=1,f[1]=read();
	for(ll i=3;i<=n;++i)h[i]=(h[i-1]+h[i-2])%p;
	for(ll i=2;i<=n;++i){
		x=read();
		f[i]=(f[i-1]+x*h[i]+(i>2)*(g[i-1]+x*h[i-1]))%p;
		g[i]=((f[i-1]-x*h[i])%p+p)%p;
	}
	res=(f[n]+g[n])%p;printf("%lld",res);
	return 0;
}


// Problem: D - Game in Momotetsu World
// Contest: AtCoder - Mynavi Programming Contest 2021（AtCoder Beginner Contest 201）
// URL: https://atcoder.jp/contests/abc201/tasks/abc201_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii int
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<' ';}cout<<'\n';
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";cout<<'\n';}
int n,m;
vector<vector<short>> a;
vector<vector<int>> dp;
vector<vector<bool>> vis;
int fun(int i,int j){
	if(vis[i][j])return dp[i][j];
	int di[]={1,0};
	int dj[]={0,1};
	int ans=-inf;
	for(int k=0;k<2;k++){
		int I=i+di[k],J=j+dj[k];
		if(I>=n || J>=m)continue;
		ans=max(ans,a[I][J]-fun(I,J));
	}
	vis[i][j]=1;
	if(ans==-inf)ans=0;
	return dp[i][j]=ans;
}
signed main(){
	cin>>n>>m;
	a.resize(n,vector<short>(m,-1));
	dp.resize(n,vector<int>(m));
	vis.resize(n,vector<bool>(m,false));
	char c;
	rep(i,0,n){
		rep(j,0,m){
			cin>>c;
			if(c=='+')a[i][j]=1;
		}
	}
	int ans=fun(0,0);
	/*for(auto z: dp){
		for(auto x: z)cout<<x<<' ';
		cout<<'\n';
	}*/
	if(ans==0)cout<<"Draw\n";
	if(ans>0)cout<<"Takahashi\n";
	if(ans<0)cout<<"Aoki\n";
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}

int f(char a){
	if(a=='+') return 1;
	return -1;
}

//GCJ頑張る
int main() {
	int H,W;
	cin>>H>>W;
	vector<vector<int>> dp(H,vector<int>(W));
	vector<string> S(H);
	rep(i,H) cin>>S[i];
	for(int i=H-1;i>=0;i--) for(int j=W-1;j>=0;j--){
		int a=(i+j)%2;
		a*=-2;
		a+=1;
		if(i+j==H+W-2) continue;
		int b,c;
		if(j!=W-1){
			b=f(S[i][j+1])*a;
			dp[i][j]=b+dp[i][j+1];
		}
		if(i!=H-1){
			c=f(S[i+1][j])*a;
			dp[i][j]=c+dp[i+1][j];
		}
		if(i!=H-1&&j!=W-1){
			/*cout<<a<<" "<<i<<" "<<j<<" "<<b<<" "<<c<<endl;
			cout<<S[i][j+1]<<" "<<S[i+1][j]<<endl;*/
			if(a==1){
				dp[i][j]=max(b+dp[i][j+1],c+dp[i+1][j]);
			}
			else dp[i][j]=min(b+dp[i][j+1],c+dp[i+1][j]);
		}
	}
	if(dp[0][0]>0) cout<<"Takahashi";
	else if(dp[0][0]<0) cout<<"Aoki";
	else cout<<"Draw";
	cout<<"\n";
	/*rep(i,H){
		rep(j,W) cout<<dp[i][j]<<" ";
		cout<<"\n";
	}*/
}


#include<iostream>
#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;}

template<int mod>
struct ModInt {
    long long x;

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

    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 p) const{
        int a = x;
        if (p==0) return 1;
        if (p==1) return ModInt(a);
        if (p%2==1) return (ModInt(a)*ModInt(a)).power(p/2)*ModInt(a);
        else return (ModInt(a)*ModInt(a)).power(p/2);
    }

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

ll n,a,b;

void solve(){
    int V=a+b-2,W=a-1;
    modint ans=n-b+1;
    if(n-a-b>=0){
        ans+=(modint)a*(modint)(n-a);
        ans+=(modint)(b-1)*(modint)(n-a-b+1);
        ans+=(modint)(n-a-b+1)*(modint)(n-a-b)/(modint)2;
    }
    else{
        ans+=(modint)(n-b+1)*(n-a);
    }
    //cout << ans << endl;
    if(a<=n-b){
        ans-=(modint)(n-a-b+1)*(modint)(n-b-a+2)/(modint)2;
    }
    modint all=(modint)(n-a+1)*(modint)(n-a+1)*(modint)(n-b+1)*(modint)(n-b+1);
    //cout << all << " " << ans << endl;
    cout << all-ans*ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    int t;cin >> t;
    rep(i,t){
        cin >> n >> a >> b;
        solve();
    }
}

#include <bits/stdc++.h>
struct __fastio__ { __fastio__ () { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } __fast_io__;
template<class T, class U> bool chmin (T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T, class U> bool chmax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> void print(const T &value) { std::cout << value << '\n'; }
template<class T, class... Args> void print(const T &value, Args... args) { std::cout << value << ' '; print(args...); }
template<class T> using martix = std::vector<std::vector<T>>;
using i32 = std::int_fast32_t;
using u32 = std::uint_fast32_t;
using i64 = std::int_fast64_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

#include <iostream>
#include <cassert>

template<int Modulus>
struct modint {
	using value_type = int;

	static constexpr int mod = Modulus;
	static_assert(mod > 1, "Modulus must be greater than 1");

private :
	value_type x;

	template<class T>
	static constexpr value_type normalize (T value) noexcept {
		if (value < 0) {
			value = (-value) % mod;
			return (value ? value : mod - value);
		}
		return (value % mod);
	}

public :
	explicit constexpr modint () noexcept : x(0) { }

	template<class T>
	constexpr modint (T value) noexcept : x(normalize(value)) { }

	constexpr modint operator+ () const noexcept {
		return modint(*this);
	}

	constexpr modint operator- () noexcept {
		if (x != 0) x = mod - x;
		return modint(*this);
	}

	constexpr modint &operator+= (const modint &p) noexcept {
		if ((x += p.x) >= mod) x -= mod;
		return (*this);
	}

	constexpr modint &operator-= (const modint &p) noexcept {
		if ((x += mod - p.x) >= mod) x -= mod;
		return (*this);
	}

	constexpr modint &operator*= (const modint &p) noexcept {
		x = (int)(1LL * x * p.x % mod);
		return (*this);
	}

	constexpr modint &operator/= (const modint &p) noexcept {
		return ((*this) *= p.inverse());
	}

	constexpr modint operator+ (const modint &p) const noexcept {
		return (modint(*this) += p);
	}

	constexpr modint operator- (const modint &p) const noexcept {
		return (modint(*this) -= p);
	}

	constexpr modint operator* (const modint &p) const noexcept {
		return (modint(*this) *= p);
	}

	constexpr modint operator/ (const modint &p) const noexcept {
		return (modint(*this) /= p);
	}

	constexpr bool operator== (const modint &p) const noexcept {
		return (x == p.x);
	}

	constexpr bool operator!= (const modint &p) const noexcept {
		return (x != p.x);
	}

	constexpr modint pow (long long exp) const noexcept {
		modint value(1), buff(x);
		while (exp) {
			if (exp & 1) value *= buff;
			buff *= buff;
			exp >>= 1;
		}
		return value;
	}

	constexpr modint inverse () const noexcept {
		return pow(mod - 2);
	}

	friend std::ostream &operator<< (std::ostream &os, const modint &p) {
		return (os << p.x);
	}

	friend std::istream &operator>> (std::istream &is, modint &p) {
		long long v;
		is >> v;
		p = modint(v);
		return (is);
	}

};

constexpr std::uint_fast32_t MOD = 1'000'000'007;

void solve () {
	int n, a, b;
	std::cin >> n >> a >> b;
	if (n < a + b) {
		std::cout << 0 << '\n';
		return;
	}

	const int c = n - a - b + 1;
	modint<MOD> cc = c;
	modint<MOD> ans = (cc) * (cc + 1) * 2;
	ans *= modint<MOD>(n - a + 1) * modint<MOD>(n - b + 1);

	modint<MOD> s = cc * (cc + 1) / 2;
	s *= s;
	s *= 4;
	ans -= s;
	std::cout << ans << '\n';
	return;
}

int main() {
	int t;
	std::cin >> t;
	while (t--) solve();
	return 0;
}


#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define PI acos(-1)
typedef  long long ll;
ll MOD=1000000007;

ll gcd(ll x,ll y){
   if(y==0) return x;
   else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
   return x/gcd(x,y)*y;
}

int main(){
   int n;
   cin>>n;
   string s=to_string(n);
   bool f=false;

   for(int i=0;i<=10;i++){
      string r;
      rep(j,i){
         r+='0';
      }
      r+=s;
      string c;
      rep(j,r.size()){
         c+=r[r.size()-j-1];
      }
      bool ff=true;
      rep(j,c.size()){
         if(r[j]!=c[j]){
            ff=false;
            break;
         }
      }
      if(ff){
         f=true;
         break;
      }
   }
   f? cout<<"Yes":cout<<"No";
   return 0;
}

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

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

int getRand(int l, int r)
{
    uniform_int_distribution<int> uid(l, r);
    return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)

const int MOD = 1e9+7;

int mod_pow(int a,int b,int M = MOD)
{
    if(a == 0) return 0;
    b %= (M - 1);  //M must be prime here

    int res = 1;

    while(b > 0)
    {
        if(b&1) res=(res*a)%M;
        a=(a*a)%M;
        b>>=1;
    }

    return res;
}

void solve()
{
   string s;
   cin >> s;

   int res = 0;
   int n = s.length();

   vi fre(26,0);

   for(int i=n-1;i>=0;i--)
   {
       fre[s[i] - 'a']++;

       if(i < n-1 && s[i] == s[i+1])
       {
           f(j,26)
               if(s[i] - 'a' != j)
           {
               res += fre[j];
               fre[s[i] - 'a'] += fre[j];
               fre[j] = 0;
           }
       }
   }

   cout << res;

}

signed main()
{
    fast;

    int t = 1;

//    cin >> t;

    while(t--)

    solve();
}


#include <stdio.h>
#include <map>

int x[100010],y[100010];
int type[100010];
std::map<int,int> check;

int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=a;i++) scanf("%d",&x[i]);
	for(int i=1;i<=a;i++) scanf("%d",&y[i]);
	x[0] = 0, y[0] = 0;
	x[a+1] = b+1, y[a+1] = b+1;
	for(int i=1;i<=a;i++)
	{
		if(x[i]>y[i]) type[i] = 1; // <<
		if(x[i]==y[i]) type[i] = 2; // --
		if(x[i]<y[i]) type[i] = 3; // >>
	}
	for(int i=1;i<a;i++)
	{
		if(type[i]==3&&type[i+1]==1)
		{
			printf("-1");
			return 0;
		}
	}
	// x[j] +(i-j) == y[i]
	long long int ans = 0;
	check[0] = 0;
	for(int i=1;i<=a;i++)
	{
		if(type[i]==1)
		{
			if(check.find(y[i]-i)==check.end())
			{
				printf("-1");
				return 0;
			}
			if(y[i-1]==y[i]-1) ans++;
			else
			{
				int k = check[y[i]-i];
				ans += (i-k);
			}
			check[x[i]-i] = i;
		}
		else if(type[i]==2)
		{
			check.clear();
			check[x[i]-i] = i;
		}
	}
	check.clear();

	//x[j] - (j-i) == y[i]
	check[(b+1)-(a+1)] = a+1;
	for(int i=a;i>=1;i--)
	{
		if(type[i]==3)
		{
			if(check.find(y[i]-i)==check.end())
			{
				printf("-1");
				return 0;
			}
			if(y[i+1]==y[i]+1) ans++;
			else
			{
				int k = check[y[i]-i];
				ans += (k-i);
			}
			check[x[i]-i] = i;
		}
		else if(type[i]==2)
		{
			check.clear();
			check[x[i]-i] = i;
		}
	}
	printf("%lld",ans);
}

#include<bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int h = 0, w = 0;
	cin >> h >> w;
	vector<vector<char> > s(h, vector<char>(w));
	for (auto &i : s)
		for (auto &j : i)
			cin >> j;
	int res = 0;
	auto isIn = [&](int x, int y)
	{
		return (x >= 0 && y >= 0 && x < h && y < w);
	};
	for (int i = 0; i < h; ++i)
		for (int j = 0; j < w; ++j)
		{
			int ct = 0;
			if (isIn(i, j) && s[i][j] == '#')
				++ct;
			if (isIn(i + 1, j) && s[i + 1][j] == '#')
				++ct;
			if (isIn(i, j + 1) && s[i][j + 1] == '#')
				++ct;
			if (isIn(i + 1, j + 1) && s[i + 1][j + 1] == '#')
				++ct;
			if (ct == 1 || ct == 3)
				++res;
		}
	cout << res;
}