    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    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 rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    using P=pair<ll,ll>;
    const ll INF=1e18;
    const int mod=998244353;

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

      mint inv() const { return pow(mod-2);}
      mint& operator/=(const mint a) { return *this *= a.inv();}
      mint operator/(const mint a) const { return mint(*this) /= a;}
     };
      istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
      ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
      struct combination {
      vector<mint> fact, ifact;
      combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
      }
      mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
      }
     } c(2000005);

    vector<pair<ll,ll>>prime_factorize(ll N) {
      vector<pair<ll,ll>>res;
      for (ll i= 2; i*i<=N;i++) {
        if (N%i!=0){continue;}
        ll cnt=0;
        while(N%i==0){
          cnt++;
          N/=i;
        }
        res.push_back({i, cnt});
      }
      if(N!=1){res.push_back({N, 1});}
      return res;
    }

    int main(){
      int n,m;cin>>n>>m;
      mint ans=0;
      for(int i=1;i<=m;i++){
        auto f=prime_factorize(i);
        mint now=1;
        for(auto p:f){
          now*=c(n-1+p.second,n-1);
        }
        ans+=now;
      }
      cout<<ans<<endl;
      return 0;
    }

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using PLL = pair<pair<long long, long long>, long long>;
using Pxy = pair<long double, long double>;
using Tiib = tuple<int, int, bool>;
using vvl = vector<vector<ll>>;
const int INF = 2001001007;
const int modd = 1000000007;
const long long modl = 1000000007LL;
const long long mod = 998244353LL;
const ll inf = 1e18;
template <typename AT>
void priv(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void privv(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[i].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[i].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){return (a <= x && x < b);}
template <typename SS>
void sor(vector<SS> &ar){sort(ar.begin(),ar.end());}
template <typename SSS>
void rev(vector<SSS> &ar){reverse(ar.begin(),ar.end());}
template <typename SSSS>
void chmin(SSSS &a, const SSSS &b){a = min(a,b);}
template <typename SSSSS>
void chmax(SSSSS &a, const SSSSS &b){a = max(a,b);}

void yes(){cout << "Yes" << endl;}
void no (){cout << "No" << endl;}
void yn (bool t){if(t)yes();else no();}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

ll saiki(vector<vector<ll>> &dp, int n, int k){
    if (dp[n][k] != -1) return dp[n][k];
    if (n == k) return dp[n][k] = 1;
    if (n < k)  return dp[n][k] = 0;
    if (k == 0) return dp[n][k] = 0;
    dp[n][k] = saiki(dp,n-1,k-1) + saiki(dp,n,2*k);
    dp[n][k] %= mod;
    return dp[n][k];
}

int main(){
    int n, k; cin >> n >> k;
    vector<vector<ll>> dp(3001,vector<ll>(6000,-1));
    cout << saiki(dp,n,k) << endl;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef  long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
	if (deg < 0)
		return 0;
	if (!deg)
		return 1 % M;
	if (deg & 1)
		return binpow(val, deg - 1) * val % M;
	ll res = binpow(val, deg >> 1);
	return (res * res) % M;
}
//binomial
ll modinv(ll n) {
	return binpow(n, M - 2);
}
//GCD
ll gcd (ll a, ll b) {
	if (b == 0)
		return a;
	else
		return gcd (b, a % b);
}

int main() {
	// your code goes here
	IOS;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll i, j, t, k, x, y, z,  N;
	cin >> n >> m;
	cout << n / m;

	return 0;

}

#include "bits/stdc++.h"
using namespace std;
#define endl "\n"
using ll = long long;
const int MOD = 1e9 + 7;

void solve() {
	int n, w; cin >> n >> w;
	cout << n / w << endl;
}

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

	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
}


#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define sz(x) int(x.size())
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}

int main(){
  cii(t);

  V<ss> ans;

  while(t--){
    cii(n);
    vvi v(2,vi(2,0));

    V<ss> s(3);
    rep(i,0,3)ci(s[i]);

    rep(i,0,3){
      int fr=s[i][0]-'0';
      int ba=s[i][n*2-1]-'0';

      v[ba][fr]=1;
    }

    ss now;
    ss s0,s1;
    rep(i,0,n){
      s0+="0";
      s1+="1";
    }

    if(v[0][0]==0){
      now=s0+"1"+s0;
    }
    else {
      if(v[1][1]==0){
        now=s1+"0"+s1;
      }
      else {
        if(v[0][1]==0)now=s0+s1+"0";
        else now=s1+s0+"1";
      }
    }

    ans.pb(now);
  }

  for(ss s:ans)co(s);
}

#include<bits/stdc++.h>
#define int long long
#define zmrl(x) (int)((x).size())
#define ahen(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin>>n;
    priority_queue<int, vector<int>, greater<int> > pq;
    for (int i=0; i<=200000; i++) pq.push(i);
    priority_queue<int, vector<int>, greater<int> > ers;
    for (int i=1; i<=n; i++) {
        int a; cin>>a;
        ers.push(a);
        while (zmrl(ers)) {
            if (ers.top() > pq.top()) break;
            while (zmrl(ers) && ers.top() < pq.top()) ers.pop();
            if (zmrl(ers) && ers.top() == pq.top()) {
                ers.pop(); pq.pop();
            }
        }
        cout << pq.top() << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;

const int mod = 998244353;
struct mint {
    ll x;
    mint (ll x = 0) : x((x % mod + mod) % mod) {}
    mint operator -() const {return mint(-x);}
    mint& operator +=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator -=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator *=(const mint a) {(x *= a.x) %= mod; return *this;}
    mint operator +(const mint a) const {return mint(*this) += a;}
    mint operator -(const mint a) const {return mint(*this) -= a;}
    mint operator *(const mint a) const {return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    mint inv() const {return pow(mod - 2);}
    mint operator /=(const mint a) {return *this *= a.inv();}
    mint operator /(const mint a) {return mint(*this) /= a;}
};
istream& operator >>(istream &is, mint &a) {return is >> a.x;}
ostream& operator <<(ostream &os, const mint &a) {return os << a.x;}

mint dp[5005][5005];
bool exist[5005][5005];

int main() {
    int h, w, k; cin >> h >> w >> k;
    map<P,char> mp;
    rep(i,k) {
        int y, x; cin >> y >> x;
        y--; x--;
        exist[y][x] = true;
        char c; cin >> c;
        mp[P(y, x)] = c;
    }
    mint inv_3 = mint(1) / 3;
    dp[0][0] = !exist[0][0] ? inv_3 : 1;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (i == 0 && j == 0) continue;
            if (i) {
                if (exist[i - 1][j]) {
                    char c = mp[P(i - 1, j)];
                    if (c == 'X' || c == 'D') dp[i][j] += dp[i - 1][j];
                } else {
                    dp[i][j] += dp[i - 1][j] * 2;
                }
            }
            if (j) {
                if (exist[i][j - 1]) {
                    char c = mp[P(i, j - 1)];
                    if (c == 'X' || c == 'R') dp[i][j] += dp[i][j - 1];
                } else {
                    dp[i][j] += dp[i][j - 1] * 2;
                }
            }
            if (!exist[i][j]) dp[i][j] *= inv_3;
        }
    }
    mint ans = dp[h - 1][w - 1] * mint(3).pow(h * w - k);
    if (!exist[h - 1][w - 1]) ans *= 3;
    cout << ans << endl;
}

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

long double dp[101][101][101];
long double f(int x, int y, int z)
{
    long double ans = 0;
    if (dp[x][y][z] > 0)
    {
        return dp[x][y][z];
    }
    if (x == 100 || y == 100 || z == 100)
    {
        return 0;
    }
    else
    {
        ans += x * f(x + 1, y, z);
        ans += y * f(x, y + 1, z);
        ans += z * f(x, y, z + 1);
        ans /= (x + y + z);
        ans += 1;
    }
    dp[x][y][z] = ans;
    return ans;
}

int x, y, z;
int main()
{
    cin >> x >> y >> z;
    cout << std::fixed << std::setprecision(15) << f(x, y, z) << endl;
    return 0;
}


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

int gcd (int a, int b) {
	if (b == 0)
		return a;

  return gcd(b, a % b);
}

int lcm (int a, int b) {
    return (a / gcd(a, b)) * b;
}

signed main() {

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

	int n;
	cin >> n;

	int Lcm = 1;
	for (int i = 2; i <= n; i++) {
		Lcm = lcm (Lcm, i);
	}

	cout << Lcm + 1;
	return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef map<ll,ll>::iterator itll;
typedef long double ld;
typedef map<ll,ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define INF 1000000000000000000
#define all(vl) vl.begin(),vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp,it) for(it = mp.begin();it!=mp.end();it++)
#define FOR(i,a,n) for(ll i=a;i<n;i++)
#define FORE(i,a,n) FOR(i,a,n+1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl
#define MIN(x,y) x=min(x,y)
#define MAX(x,y) x=max(x,y)
#define left 0
#define right 1

const double one = 1.0;
const double PI = 3.14159265/*358979323846*/;
const ll maxn = 1e4+10;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll n;
	cin>>n;
	ll an = n;
	n = 1;
	FOR(i,0,an)n*=2;
	vector<ll> vl(n);
	map<ll,ll> pos;
	FOR(i,0,n)
	{
		cin>>vl[i];
		pos[vl[i]] = i;
	}
	ll ans = 0;
	while(1)
	{
		vector<ll> winner;
		if(vl.size() == 2)
		{
			if(vl[0]>vl[1])ans = vl[1];
			else ans = vl[0];
			break;
		}
		for(int i = 0;i<vl.size();i+=2)
		{
			ll a = vl[i],b = vl[i+1];
			if(a<b)winner.pb(b);
			else winner.pb(a);
		}
		vl = winner;
	 }
	cout<<1+pos[ans]<<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 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
typedef pair<int, int> P;
typedef long long ll;
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;}

int main()
{
	ll n, C; cin >> n >> C;
	vector<pair<ll, ll>> p;
	rep(i, n)
	{
		ll a, b, c; cin >> a >> b >> c;
		p.eb(a, c);
		p.eb(b+1, -c);
	}
	sort(all(p));

	ll ans = 0;
	ll x = 0;
	ll y = 0;
	for (auto [i,j] : p)
	{
		ans += (i-x) * min(y, C);
		x = i;
		y += j;
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  void solve() {
    int a, b, c;
    vector<vector<vector<double>>> memo(101, vector<vector<double>>(101, vector<double>(101, -1)));
    cin >> a >> b >> c;
    cout << calc(a, b, c, memo) << endl;
  }
  double calc(int a, int b, int c, vector<vector<vector<double>>> &memo) {
    if (a >= 100 || b >= 100 || c >= 100) {
      return 0;
    } else if (memo[a][b][c] >= 0) {
      return memo[a][b][c];
    } else {
      double ret = 1.0;
      ret += (double)(a) / (double)(a + b + c) * calc(a + 1, b, c, memo);
      ret += (double)(b) / (double)(a + b + c) * calc(a, b + 1, c, memo);
      ret += (double)(c) / (double)(a + b + c) * calc(a, b, c + 1, memo);
      return memo[a][b][c] = ret;
    }
  }
};

}  // namespace Problem

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

  Problem::Solver sol;
  sol.solve();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int h,w,a[110][110],maxx=0,minn=1010;
	long long sum=0;
	cin>>h>>w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>a[i][j];
			minn=min(minn,a[i][j]);
			maxx=max(maxx,a[i][j]);
		}
	}
	if(maxx==minn) {
		cout<<0;
		return 0;
	}
	else {
		for(int i=1;i<=h;i++){
			for(int j=1;j<=w;j++)
			{
				sum+=a[i][j]-minn;
			}
		}
	}
	cout<<sum;
	return 0;
}

//インクルードなど
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

//イテレーション
#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--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl;

//aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a, ll b) {return (a + (b - 1)) / b;}
ll myfloor(ll a, ll b) {return a / b;}

int main() {
	//input
	ll N, Q; cin >> N >> Q;

	vector<ll> A(N);
	vector<ll> B(N);
	REP(i, N) {
		cin >> A[i];
		B[i] = A[i] - i;
	}

	vector<ll> K(Q);
	REP(i, Q) cin >> K[i];

	//calculation
	REP(i, Q) {
		ll k = K[i];

		if (k >= B[N - 1]) {
			cout << A[N - 1] + 1 + k - B[N - 1] << endl;
			continue;
		}

		REP(j, N) {
			if (B[j] > k) {
				if (j == 0) cout << k << endl;
				else cout << A[j - 1] + 1 + k - B[j - 1] << endl;
				break;
			}
		}

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;
//constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)

class graph
{
public:
  struct edge
  {
    ll to, cost;
    edge(ll to, ll cost) : to(to), cost(cost) {}

    bool operator<(const edge &right) const
    {
      return cost < right.cost;
    }

    bool operator>(const edge &right) const
    {
      return cost > right.cost;
    }
  };

  ll N;
  vector<vector<edge>> G;

  graph(ll n)
  {
    N = n;
    G.resize(N);
  }

  void connect(ll sv, ll gv, ll c)
  {
    G.at(sv).push_back(edge(gv, c));
  }

  void connect2(ll v0, ll v1, ll c)
  {
    connect(v0, v1, c), connect(v1, v0, c);
  }

  void dijkstra(ll sv, vector<ll> &costs)
  {
    costs = vector<ll>(N, INF);
    costs.at(sv) = 0;
    priority_queue<pll, vector<pll>, greater<pll>> pque;
    for (auto e : G.at(sv))
    {
      pque.push(make_pair(e.cost, e.to));
    }

    while (!pque.empty())
    {
      pll cv = pque.top();
      ll c = cv.first, v = cv.second;
      pque.pop();

      if (costs.at(v) != INF)
        continue;
      costs.at(v) = c;

      for (auto e : G.at(v))
      {
        if (costs.at(e.to) != INF)
          continue;

        pque.push(make_pair(c + e.cost, e.to));
      }
    }
  }
};

ll R, C;

ll tptol(ll r, ll c, ll b)
{
  return 2 * (R * c + r) + b;
}

int main()
{
  cin >> R >> C;
  vector<vector<ll>> A(R, vector<ll>(C - 1));
  for (ll i = 0; i < R; i++)
  {
    for (ll j = 0; j < C - 1; j++)
    {
      cin >> A.at(i).at(j);
    }
  }
  vector<vector<ll>> B(R - 1, vector<ll>(C));
  for (ll i = 0; i < R - 1; i++)
  {
    for (ll j = 0; j < C; j++)
    {
      cin >> B.at(i).at(j);
    }
  }

  graph gr(2 * R * C);
  for (ll r = 0; r < R; r++)
  {
    for (ll c = 0; c < C - 1; c++)
    {
      gr.connect(tptol(r, c, 0), tptol(r, c + 1, 0), A.at(r).at(c));
      //cerr << r << " " << c << " " << r << " " << c + 1 << " " << A.at(r).at(c) << endl;
    }
  }
  for (ll r = 0; r < R; r++)
  {
    for (ll c = 1; c < C; c++)
    {
      gr.connect(tptol(r, c, 0), tptol(r, c - 1, 0), A.at(r).at(c - 1));
      //cerr << r << " " << c << " " << r << " " << c - 1 << " " << A.at(r).at(c - 1) << endl;
    }
  }
  for (ll r = 0; r < R - 1; r++)
  {
    for (ll c = 0; c < C; c++)
    {
      gr.connect(tptol(r, c, 0), tptol(r + 1, c, 0), B.at(r).at(c));
      //cerr << r << " " << c << " " << r + 1 << " " << c << " " << B.at(r).at(c) << endl;
    }
  }

  for (ll r = 0; r < R; r++)
  {
    for (ll c = 0; c < C; c++)
    {
      gr.connect(tptol(r, c, 0), tptol(r, c, 1), 0);
      gr.connect(tptol(r, c, 1), tptol(r, c, 0), 1);
    }
  }
  for (ll r = 1; r < R; r++)
  {
    for (ll c = 0; c < C; c++)
    {
      gr.connect(tptol(r, c, 1), tptol(r - 1, c, 1), 1);
    }
  }

  vector<ll> costs;
  gr.dijkstra(0, costs);

  ll ans = costs.at(tptol(R - 1, C - 1, 0));
  cout << ans << endl;
}

#include<cstdio>
#include<queue>
#define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i)
using namespace std;
const int N=505,INF=0x3fffffff;
template<typename T>void read(T &x)
{
	bool neg=false;
	unsigned char c=getchar();
	for(;(c^48)>9;c=getchar())if(c=='-')neg=true;
	for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48);
	if(neg)x=-x;
}
struct node
{
	int x,y,d;
	bool operator<(node b)const{return b.d<d;}
}n1,n2;
int n,m,a[N][N],b[N][N],dis[N][N];
priority_queue<node> q;
int main()
{
	read(n);read(m);
	F(i,0,n)F(j,0,m-1)read(a[i][j]);
	F(i,0,n-1)F(j,0,m)read(b[i][j]);
	F(i,0,n)F(j,0,m)dis[i][j]=INF;
	dis[0][0]=0;
	n1.x=n1.y=n1.d=0;
	q.push(n1);
	while(!q.empty())
	{
		int x,y,d;
		n1=q.top();q.pop();
		if(dis[x=n1.x][y=n1.y]!=(d=n1.d))continue;
		if(x<n)if(dis[x+1][y]>dis[x][y]+b[x][y])
		{
			dis[x+1][y]=dis[x][y]+b[x][y];
			n2.x=x+1;n2.y=y;n2.d=dis[x+1][y];
			q.push(n2);
		}
		F(i,0,x)if(dis[i][y]>dis[x][y]+x-i+1)
		{
			dis[i][y]=dis[x][y]+x-i+1;
			n2.x=i;n2.y=y;n2.d=dis[i][y];
			q.push(n2);
		}
		if(y&&dis[x][y-1]>dis[x][y]+a[x][y-1])
		{
			dis[x][y-1]=dis[x][y]+a[x][y-1];
			n2.x=x;n2.y=y-1;n2.d=dis[x][y-1];
			q.push(n2);
		}
		if(y<m&&dis[x][y+1]>dis[x][y]+a[x][y])
		{
			dis[x][y+1]=dis[x][y]+a[x][y];
			n2.x=x;n2.y=y+1;n2.d=dis[x][y+1];
			q.push(n2);
		}
	}
	printf("%d\n",dis[n-1][m-1]);
	return 0;
}


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

int main() {
    int H, W;
    cin >> H >> W;
    vector<vector<int>> grid(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            char c;
            cin >> c;
            if (c == '+') grid[i][j] = 1;
            else grid[i][j] = -1;
        }
    }
    vector<vector<int>> dp(H, vector<int>(W, 0));
    for (int i = H-1; i >= 0; i--) {
        for (int j = W-1; j >= 0; j--) {
            if (i == H-1 && j == W-1) continue;
            if ((i+j)%2 == 0) {
                dp[i][j] = -1e9;
                if (i < H-1) dp[i][j] = max(dp[i][j], dp[i+1][j]+grid[i+1][j]);
                if (j < W-1) dp[i][j] = max(dp[i][j], dp[i][j+1]+grid[i][j+1]);
            }
            if ((i+j)%2 == 1) {
                dp[i][j] = 1e9;
                if (i < H-1) dp[i][j] = min(dp[i][j], dp[i+1][j]-grid[i+1][j]);
                if (j < W-1) dp[i][j] = min(dp[i][j], dp[i][j+1]-grid[i][j+1]);
            }
        }
    }
    if (dp[0][0] > 0) cout << "Takahashi\n";
    else if (dp[0][0] == 0) cout << "Draw\n";
    else cout << "Aoki\n";
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> T;
typedef pair<ll, vector<ll>> Pd;

const ll INF = 1e18;
const ll fact_table = 3200008;

priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←

/*
#define endl "\n"
#ifdef ENJAPMA
#undef endl
#endif
*/

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)

typedef vector<ll> vec;
typedef vector<vector<ll>> mat;

// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言

// const ll mod = 998244353ll;
const ll mod = 1000000007ll;

ll mypow(ll a, ll b, ll m = mod) {
    ll x = 1;
    while (b) {
        while (!(b & 1)) {
            (a *= a) %= m;
            b >>= 1;
        }
        (x *= a) %= m;
        b--;
    }
    return x;
}
vec rv(ll read) {
    vec res(read);
    for (int i = 0; i < read; i++) {
        cin >> res[i];
    }
    return res;
}
/*
ll fact[fact_table + 5], rfact[fact_table + 5];

void c3_init() {
    fact[0] = rfact[0] = 1;
    for (ll i = 1; i <= fact_table; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
    for (ll i = fact_table; i >= 1; i--) {
        rfact[i - 1] = rfact[i] * i;
        rfact[i - 1] %= mod;
    }
    return;
}
ll c3(ll n, ll r) {
    return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/
bool icpc = false;
bool multicase = false;

ll n, m, k, q;
string s;
ll dp[1050000];
vector<P> ban[20];

bool solve() {
    cin >> n >> m;
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        ban[a].pb(P(b, c));
    }
    for (int i = 1; i <= n; i++) {
        sort(all(ban[i]));
    }
    dp[0] = 1;
    ll bit = (1ll << n);
    for (ll set = 0; set < bit; set++) {
        // set -> newset (= set + (1ll << j))
        for (int j = 0; j < n; j++) {
            if ((set >> j) & 1) continue;
            ll newset = set + (1ll << j);
            ll newcnt = __builtin_popcount(newset);
            bool dame = false;
            for (auto [maxi, cnt] : ban[newcnt]) {
                ll r = 0;
                for (int k = 0; k < n; k++) {
                    if ((k < maxi) && (newset >> k) & 1ll) r++;
                }
                if (r > cnt) dame = true;
            }
            if (!dame) dp[newset] += dp[set];
        }
    }
    p(dp[(1ll << n) - 1]);
    return true;
}

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

    if (icpc) {
        while (solve())
            ;
        return 0;
    }
    ll q, testcase = 1;
    if (multicase) {
        cin >> q;
    } else {
        q = 1;
    }
    while (q--) {
        // cout << "Case #" << testcase << ": ";
        solve();
        testcase++;
    }

    // solve();
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;

pll operator+(pll a,pll b){
  return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
  return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
  return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
  a=abs(a);
  b=abs(b);
  if(a==0)return b;
  if(b==0)return a;
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
  if(a<b){
    a=b;
    return true;
  }
  return false;
}
template<class T>
bool chmin(T& a,const T& b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
  os << "[ ";
  REP(a.size()){
    os<< a[i] << " ";
  }
  os<< " ]";
  return os;
}

const long long MOD = 1000000007;
long long modpow(long long a,long long b,unsigned long long MOD){
  long long base=a,ans=(1LL);
  while(b){
    if(b&1){
      ans=ans*base%MOD;
    }
    base=base*base%MOD;
    b>>=1;
  }
  return ans;
}
void solve(long long N, long long P){
  cout<<(P-1)*modpow(P-2,N-1,MOD)%MOD<<endl;
}

int main(){
  cout<<setprecision(1000);
  long long N;
    scanf("%lld",&N);
    long long P;
    scanf("%lld",&P);
  solve(N, P);
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
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-'0');
		ch=getchar();
	}
	return x*f;
}
const int maxn=2e5+5;
ll n,k,a[maxn],cnt[15];
ll f(ll x) {
	for(int i=0; i<=9; i++) cnt[i]=0;
	while(x) {
		cnt[x%10]++;
		x/=10;
	}
	ll g1=0,g2=0;
	for(int i=9; i>=0; i--) {
		for(int j=0; j<cnt[i]; j++) {
			g1=g1*10+i;
		}
	}
	for(int i=0; i<=9; i++) {
		for(int j=0; j<cnt[i]; j++) {
			g2=g2*10+i;
		}
	}
	return g1-g2;
}
int main() {
	n=read(),k=read();
	a[0]=n;
	for(int i=1; i<=k; i++) {
		a[i]=f(a[i-1]);
	}
	printf("%lld\n",a[k]);
}


#include <bits/stdc++.h>
using namespace std;using ll=long long;using vi=vector<int>;using vvi=vector<vi>;using vl=vector<ll>;using vvl=vector<vl>;using P=pair<int,int>;using PL=pair<ll,ll>;using vp=vector<P>;using vpl=vector<PL>;

#define all(v)(v).begin(),(v).end()
#define rall(v)(v).rbegin(),(v).rend()

// cin and cout of a vector
template<class T>class iterable{static false_type c(string v);template<class U>static auto c(U v)->decltype(all(v),true_type());static false_type c(...);public:const static bool value=decltype(c(declval<T>()))::value;};
template<class T,enable_if_t<iterable<T>::value,int> =0>ostream&operator<<(ostream&o,const T&v){for(auto&&i:v)o<<i<<' ';return o;}
template<class T>istream&operator>>(istream&i,vector<T>&v){for(T&j:v)i>>j;return i;}template<class T>vector<T>&operator<<(vector<T>&v,const T&t){v.push_back(t);return v;}

// for loop
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define revv(i, a, b) for (int i = (int)(b-1); i >= (int)(a); i--)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; 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++)

// Yes/no
#define cho(n,a,b) cout<< ((n)?a:b)<<endl;
void YES(int n){cho(n,"YES","NO");}void Yes(int n){cho(n,"Yes","No");}void Poss(int n){cho(n,"Possible","Impossible");}

#define pb push_back
#define revsort(v) sort((v).rbegin(), (v).rend())
#define sortv(v) sort((v).begin(), (v).end())

// max/min
template<class T, class U>bool chmax(T&a,const U&b){bool x=a<b;x?a=b:b;return x;}template<class T, class U>bool chmin(T&a,const U&b){bool x=a>b;x?a=b:b;return x;}
template<class T, class U>auto max(const T&a,const U&b){return a<b?b:a;}template<class T, class U>auto min(const T&a,const U&b){return a<b?a:b;}

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

template<class T>auto minv(const T&a){return *min_element(all(a));}
template<class T>auto maxv(const T&a){return *max_element(all(a));}
template<class T>auto sumv(const T&a){ll sum = 0; for(auto x: a) sum+= x; return sum;}
template<class T>void pr(const T&a){cout << a << endl;}
template<class T>void pr_precise(const T&a){cout.precision(10); cout << fixed << a << endl;}

int check(ll a, ll b){
    if (a == 0 && b == 0) return 0;
    if (a == b || a + b <= 3) return 1;

    if ((a-b)%2 == 0) return 2; // long + long
    if (a + b <= 6) return 2; // short + short. miss this case;
    if ( abs(a - b) <= 3) return 2; // long + short

    return 3;
}

int main() {
    int cases = 1;
    // cin >> cases;
    rep(CASE, cases){
        ll a, b, c;
        cin >> a >> b >> c;
        double dp[101][101][101] = {{{0.}}};
        rev(i,100){
            rev(j,100){
                rev(k, 100){
                    dp[i][j][k] = (i*dp[i+1][j][k] + j*dp[i][j+1][k] + k*dp[i][j][k+1])/(i+j+k) + 1.;
                }
            }
        }
        pr_precise(dp[a][b][c]);
    }
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  double a, b, c;
  cin >> a >> b >> c;
  vector<vector<vector<double>>> dp(
      110, vector<vector<double>>(110, vector<double>(110, 0.0)));
  dp[a][b][c] = 1.0;
  for(int i = 0; i <= 100; i++) {
    for(int j = 0; j <= 100; j++) {
      for(int k = 0; k <= 100; k++) {
        if(i == 0 && j == 0 && k == 0) {
          continue;
        }
        dp[i + 1][j][k] += dp[i][j][k] * (double)i / (i + j + k);
        dp[i][j + 1][k] += dp[i][j][k] * (double)j / (i + j + k);
        dp[i][j][k + 1] += dp[i][j][k] * (double)k / (i + j + k);
      }
    }
  }
  double ans = 0;
  for(int i = 0; i < 100; i++) {
    for(int j = 0; j < 100; j++) {
      ans += dp[i][j][100] * (100 - c);
      ans += dp[i][100][j] * (100 - b);
      ans += dp[100][i][j] * (100 - a);
    }
  }
  printf("%.10f\n", ans);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define endl "\n"
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// const ll mod = 1e9 + 7;
const ll mod = 998244353;
const ll sz = 1e6 + 5;
vector<ll> fac(sz, 1);
vector<bool> prime(sz, true);
vector<ll> fib(sz, 0);

ll powmod(ll a, ll b) { ll res=1; a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1) res=res*a%mod; a=a*a%mod; } return res; }
ll nCr(ll n, ll r) { return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r], mod-2) ) % mod ); }
ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a,b)) * b; }

// * Always PASS arrays, strings etc to functions BY REFERENCE

void solve() {
    ll n, x;
    cin >> n >> x;

    string s;
    cin >> s;

    for (auto c: s) {
        if (c == 'o')
            x += 1;
        else
            x = max(0ll, x-1);
    }

    cout << x << endl;
}

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

//     fib[1] = 1;
//     for (int i = 2; i < sz; i++)
//         fib[i] = (fib[i-1] + fib[i-2]) % mod;

//     for (ll i = 2; i < sz; i++)
//         fac[i] = (fac[i-1] * i) % mod;

//     prime[0] = prime[1] = false;
//     for (ll i = 2; i*i < sz; i++) {
//         if (!prime[i])
//             continue;

//         for (ll j = i*i; j < sz; j += i)
//             prime[j] = false;
//     }

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

    while(t--) {
        solve();
    }

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

  int n, x;
  string s;
  cin >> n >> x >> s;
  for (auto &c : s) {
    if (c == 'o') {
      x++;
    } else {
      x--;
    }
    if (x < 0) {
      x = 0;
    }
  }
  cout << x << "\n";
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//~ while (clock()<=69*CLOCKS_PER_SEC)

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
    while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 10, M=310;
const int base = 1 << 18;

int gcd(int a, int b, int& x, int& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

void solve() {
    int n, s, k;
    int res = 0;
    cin >> n >> s  >> k;
    if (__gcd(n, k) != 1) {
        int tmp = __gcd(n, k);
        if (s % tmp) {
            cout << "-1\n";
            return;
        }
    }
    int tmp = (n - s) / k;
    res += tmp;
    s += tmp *  k;
    s %= n;
    if (s == 0) {
        cout << res << "\n";
        return;
    }
    int x = 1, y = 1;
    gcd(n, k, x, y);
    int nww = ((n * k) / __gcd(n, k));
    while( y < 0 ) {
        x -= nww / n;
        y += nww / k;
    }
    res += (n - s) / __gcd(n, k) * y;
    s = 0;

    int cofaj = nww / k;
    res %= cofaj;

    cout << res << "\n";
}

int32_t main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    cout.tie(0);
    cerr.tie(0);
    cout << setprecision(9) << fixed;
    cerr << setprecision(20) << fixed;
    int test = 1, f;
    cin >> test;
    F(_test, 0, test) {
        //cout<<"Case #"<<_test + 1<<": ";
        solve();
//		if(_test == 1)
//            return 0;
    }
}
/*

 */

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <queue>

#define	INF   1010101010LL
#define INFLL 1010101010101010101LL

using namespace std;

int mod = 1000000007;
//int mod = 998244353;

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

	return d;
}

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		long long n, s, k;
		cin >> n >> s >> k;
		long long x, y;
		long long d = extgcd(k, n, x, y);
		if (s % d != 0) {
			cout << -1 << endl;
			continue;
		}

		n /= d;
		s /= d;
		k /= d;
		long long ans = ((x * -s) % n + n) % n;
		cout << ans << endl;
	}

	return 0;
}


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

#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define SP << " " <<

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<P> pvec;
typedef vector<vector<int>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<int> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<int,vector<int>,greater<int>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;

const vector<int> DX = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> DY = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const ll INF = (1LL << 60); // 1152921504606846976
const double PI = (3.141592653589794);
const double EPS = (0.0000000001); // 10^(-10)

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline T ceil(T a, T b) {return T((a + b - 1) / b);}
template<class T> inline T round(T a, T b) {return T(a / b);}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }
template <class T> inline void out(T &a) { bool flag=true; for(auto&x:a){if(flag) {flag=false;} else{ cout << ' '; } cout<<x; } cout << endl; }

//----------------------------------------------------------------
int nmax=200000; // 2*(10^5)
vvec G(nmax);

void solve4ts()
{

  string s;
  cin>>s;
  int n=s.size();

  rep(i,n){
    if(s[i]=='.') break;
    cout<<s[i];
  }

  cout<<endl;

}
//-----------------------------------------------------------------

signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15);
  int repeat = 1;
  // cin >> repeat;
  while(repeat--) solve4ts();
}

/*

g++ -std=c++1z code.cpp

./a.out

python3 expander.py code.cpp

*/

#include<bits/stdc++.h>
#define st string
#define sz(x) (x).size()
#define pb push_back
#define pp pop_back
using namespace std;
int main()
{
  st s;
    cin >> s;
    while (s[sz(s) - 1] == '0')s.pp();
    for (int i = 0; i <sz(s) / 2; i++) {
        if(s[i] != s[sz(s) -1 -i]){
        cout << "No";
        return 0;
    }
}
cout << "Yes";
return 0;
}


#define _DEBUG
#include "bits/stdc++.h"
//#include <atcoder/all>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,a5,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug_6(x1,x2,x3,x4,x5,x6) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<", "#x6<<": "<<x6<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_6,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define prind(argument) cout<<std::fixed<<setprecision(13)<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define PI acosl(-1)
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba emplace_back
#define pubamp(a,b) emplace_back(mp(a,b))
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
	int N;
	int x1[101],y1[101],x2[101],y2[101];
	scan(N);
	rep(i,N){
		scan(x1[i]>>y1[i]);
	}
	rep(i,N){
		scan(x2[i]>>y2[i]);
	}
	if(N==1){
		prin("Yes");
		return 0;
	}
	int a01x=x1[1]-x1[0],a01y=y1[1]-y1[0];
	map<pint,int> acount;
	rep(i,N){
		int atox=x1[i]-x1[0],atoy=y1[i]-y1[0];
		int naiseki=a01x*atox+a01y*atoy;
		int gaiseki=a01x*atoy-atox*a01y;
		acount[mp(naiseki,gaiseki)]++;
	}
	rep(ii,N){
		rep(jj,N){
			if(ii==jj) continue;
			int b01x=x2[ii]-x2[jj],b01y=y2[ii]-y2[jj];
			map<pint,int> bcount;
			rep(i,N){
				int btox=x2[i]-x2[jj],btoy=y2[i]-y2[jj];
				int naiseki=b01x*btox+b01y*btoy;
				int gaiseki=b01x*btoy-btox*b01y;
				bcount[mp(naiseki,gaiseki)]++;
			}
			bool ok=1;
			for(const auto& fs:acount){
				if(bcount[fs.first]!=fs.second){
					ok=0;
				}
			}
			if(ok){
				prin("Yes");
				return 0;
			}
		}
	}
	prin("No");
	return 0;
}

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

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

void cout_line(vector<int> &a){
    for(int i=0;i<a.size();i++){
        if(i<a.size()-1){
            cout<<a.at(i)<<" ";
        }else{
            cout<<a.at(i)<<endl;
        }
    }
}

signed main(){
    int N; cin>>N;
    int M; cin>>M;
    vec_int A(M);
    vec_int B(M);
    vec_int C(M);
    vec_int D(M);
    rep(i,M)cin>>A.at(i)>>B.at(i)>>C.at(i)>>D.at(i);

    vector<vector<T>> G(N+1);
    rep(i,M){
        G.at(A.at(i)).push_back(make_tuple(B.at(i), C.at(i), D.at(i)));
        G.at(B.at(i)).push_back(make_tuple(A.at(i), C.at(i), D.at(i)));
    }
    vec_int visited(N+1, -1);
    priority_queue<P, vector<P>, greater<P>> pq;
    pq.emplace(0, 1);

    while(!pq.empty()){
        int cost, pos; tie(cost, pos) = pq.top(); pq.pop();
        if(visited.at(pos)>=0)continue;
        visited.at(pos) = cost;
        if(pos==N){
            cout<<cost<<endl;
            return 0;
        }

        for(auto temp: G.at(pos)){
            int next, c, d; tie(next, c, d) = temp;
            if(visited.at(next)>=0)continue;
            int t = (sqrt((double)(1+4*d)) -1 )/2 + 1;
            int next_time;
            if(t+5<cost){
                next_time = cost+c+(int)(d/(cost+1));
            }else{
                next_time = (int)pow(10,18);
                for(int i=t-5;i<=t+5;i++){
                    if(i<cost)continue;
                    int temp = cost+c+(int)(d/(i+1))+(i-cost);
                    next_time = min(temp, next_time);
                }
            }
            pq.emplace(next_time, next);
        }
    }
    cout<<-1<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T>
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

ll n;
vector<ll>c;
Graph g;

multiset<ll>st;
vector<ll>ans;

void dfs(ll v,ll p){
	auto it = st.find(c[v]);
	if(it==st.end()){
		ans.push_back(v);
	}
	st.insert(c[v]);
	for(auto x:g[v]){
		if(x==p)continue;
		dfs(x,v);
	}
	st.erase(st.find(c[v]));
	return;
}

int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	cin >> n;
	c.resize(n);
	rep(i,n)cin>>c[i];
	g.resize(n);
	rep(i,n-1){
		ll a,b;
		cin >> a >> b;
		a--;b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(0,-1);
	sort(ans.begin(),ans.end());
	rep(i,ans.size()){
		cout << ans[i]+1 << endl;
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int arr[100005];
bool vis[100005];
vector<int>vec;
multiset<int>ms;
void dfs(int node,vector<vector<int>>&v)
{
    vis[node]=1;
    if(!ms.count(arr[node-1]))
        vec.push_back(node);
    ms.insert(arr[node-1]);
    for(int i=0;i<v[node].size();i++){
        if(!vis[v[node][i]])
            dfs(v[node][i],v);
    }
    ms.erase(ms.find(arr[node-1]));
}
int main()
{
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>arr[i];
    vector<vector<int>>v(n+5);
    for(int i=0; i<n-1; i++) {
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1,v);
    sort(vec.begin(),vec.end());
    for(auto it:vec)
        cout<<it<<"\n";
}


//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber

#include <bits/stdc++.h>

using namespace std;

// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;

// constants...
const double PI = acos(-1);
const int MOD = 1e9+7; // 998244353;
const int MXS = 2e5+5;
const int MXI = 1e9+5;
const ll MXL = 1e15+5;

// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

// chess moves...
// int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right
// int dy[] = {+1, -1, +0, +0};
// int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
// int dx[] = {-2, -2, -1, -1,  1,  1,  2,  2}; ///Knight's Move
// int dy[] = {-1,  1, -2,  2, -2,  2, -1,  1};

// structs...
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

// functions...
ll gcd(ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll lcm(ll a, ll b)
{
    return (a/gcd(a, b)*b);
}
ll comb(ll a, ll b)
{
    ll x = max(a-b, b), ans=1;

    for(ll K=a, L=1; K>=x+1; K--, L++){
        ans *= K;
        ans /= L;
    }

    return ans;
}

ll ans = 0;
vi ara;
void solve(ll i, ll num, ll cnt)
{
	if(num%3==0){
		ans = max(ans, cnt);
	}
	if(i==sz(ara)) return;

	solve(i+1, num*10+ara[i], cnt+1);
	solve(i+1, num, cnt);
}
void task()
{
	// Code here...

	ll n;

	cin >> n;

	while(n){
		ara.PB(n%10);
		n /= 10;
	}
	reverse(all(ara));

	solve(0, 0, 0);

	if(0==ans) cout << "-1\n";
	else cout << sz(ara)-ans << "\n";
}

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

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

	task();

    return 0;
}

// #pragma GCC optimise ("O1")
#include<bits/stdc++.h>
using namespace std;
#define ll           int
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define _rep(i,n)   for(ll i=n-1;i>=1;i--)
#define scn(a)       scanf("%lld",&a)
#define scns(a,b)    scanf("%lld %lld",&a,&b)
#define print(a)     printf("%lld",a)
#define vec          vector<ll>
#define pb           push_back
#define pairs        pair<ll,ll>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define srt(v)       sort(v.begin(),v.end())
#define read(a)       for(auto &it:a) cin>>it;
#define write(a)      for(auto it:a) cout<<it<<" ";
#define yeS(GOOD)      GOOD ? cout<<"YES\n" : cout<<"NO\n"
#define mem(a,b)     memset(a,b,sizeof(a))
#define nl           printf("\n")
#define inf          1e9
#define zer          INT_MIN
#define fast         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N=20000;
long long n;
vec v;
ll dp[20][20][N],k;
ll f(ll i, ll j, ll sum=0)
{
// cout<<sum<<endl;
	if(i==(int)v.size() && j<=k-1)
	{
		// cout<<sum<<endl;
		if(sum%3==0 && sum)
		return 0;

		return 1e9;
	}

	if(i==(int)v.size() || j>k-1)
	return 1e9;

	if(dp[i][j][sum]!=-1)
	return dp[i][j][sum];

	ll ans=0;

	ans=min(f(i+1,j+1,sum)+1,f(i+1,j,sum+v[i]));

	return dp[i][j][sum]=ans;

}

void solve()
{
	cin>>n;
	// cout<<n<<endl;

	v.clear();
	mem(dp,-1);
	while(n)
	{
		int dig=(int)(n%10);
		v.pb(dig);
		n/=10;
		k++;
	}

	ll ans=f(0,0);
	if(ans>=1e9) ans=-1;
	cout<<ans<<endl;
}

int main()
{
	  int queries=1;
	  // cin>>queries;
	  while(queries--)
	  {
	    solve();
	  }
}


/*
Author: QAQAutomaton
Lang: C++
Code: C.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
	__INIT__(){
		fill((unsigned char *)(&inf),(unsigned char *)(&inf)+sizeof(inf),0x3f);
	}
}__INIT___;
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	template<typename A>
	inline bool read (A &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}
	inline bool read (char &x) {
		while((x=gc())==' '||x=='\n' || x=='\r');
		return x!=EOF;
	}
	inline bool read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r');
		if(*x==EOF)return 0;
		while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
		*x=0;
		return 1;
	}
	template<typename A,typename ...B>
	inline bool read(A &x,B &...y){
		return read(x)&&read(y...);
	}
	template<typename A>
	inline bool write (A x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
		return 0;
	}
	inline bool write (char x) {
		putc(x);
		return 0;
	}
	inline bool write(const char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	inline bool write(char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	template<typename A,typename ...B>
	inline bool write(A x,B ...y){
		return write(x)||write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int s[1<<8|5],atl[1<<8|5];
pii e[100005];
int g[9][9];
int dis[9][9],p[9];
int n,m;
signed main(){
#ifdef QAQAutoMaton
	freopen("C.in","r",stdin);
	freopen("C.out","w",stdout);
#endif
	read(n,m);
	int mx=0;
	for(int i=0;i<n;++i){
		read(s[1<<i]);
		chkmax(mx,s[1<<i]);
	}
	for(int i=1;i<(1<<n);++i)if(i&(i-1))s[i]=s[i&(i-1)]+s[i&(-i)];
	for(int i=1;i<=m;++i){
		read(e[i].y,e[i].x);
		if(mx>e[i].x)return write("-1\n");
	}
	sort(e+1,e+m+1);
	for(int i=1;i<=m;++i)chkmax(e[i].y,e[i-1].y);
	for(int i=0;i<(1<<n);++i){
		atl[i]=(lower_bound(e+1,e+m+1,make_pair(s[i],-1))-1)->y;
	}
	for(int i=1;i<=n;++i)p[i]=i-1;
	int ans=inf;
	do{
		for(int i=1;i<=n;++i){
			int w=0;
			for(int j=i;j;--j){
				w|=1<<p[j];
				dis[j][i]=-atl[w];
			}
			for(int j=i+1;j<=n;++j)dis[j][i]=inf;
		}
		for(int k=1;k<=n;++k)
			for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)chkmin(dis[i][j],dis[i][k]+dis[k][j]);
		chkmin(ans,-dis[1][n]);
	}while(next_permutation(p+1,p+n+1));
	write(ans,'\n');
	return 0;
}

// Pratiyush Mishra

#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

void mainSolve()
{
  get(n);
  get(m);
  getvec(wt, n);
  sort(all(wt));
  vector<ll> sum(1 << n, 0), dis(1 << n, 0);
  for (int i = 0; i < (1 << n); i++)
  {
    int x = 0;
    for (int j = 0; j < n; j++)
      if (i >> j & 1)
        x += wt[j];
    sum[i] = x;
  }
  int need = *max_element(all(wt));
  bool flag = true;
  // for (int i = 0; i < m; i++)
  // {
  //   int l, v;
  //   cin >> l >> v;
  //   if (v < need)
  //   {
  //     flag = false;
  //   }
  //   for (int j = 0; j < (1 << n); j++)
  //   {
  //     if (sum[j] > v)
  //       dis[j] = max(dis[j], l);
  //   }
  // }
  for (int j = 0; j < m; j++)
  {
    get(l);
    get(w);
    if (w < need)
      flag = false;
    for (int i = 0; i < (1 << n); i++)
    {
      if (sum[i] > w)
        dis[i] = max(dis[i], l);
    }
  }
  if (!flag)
  {
    p1(-1);
    return;
  }
  ll ans = 1e15;
  vl per(n);
  iota(all(per), 0);
  do
  {
    vl pos(n, 0);
    for (int i = 1; i < n; i++)
    {
      ll s = (1 << per[i]);
      for (int j = i - 1; j >= 0; j--)
      {
        s |= (1 << per[j]);
        pos[i] = max(pos[i], pos[j] + dis[s]);
      }
    }
    ans = min(ans, pos[n - 1]);
  } while (next_permutation(all(per)));
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  //get(t);
  ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <local/debug.h>
#else
#define debug(...) (void)0
#endif

struct DSU {
  vector<int> r;
  DSU(int n) : r(n, -1) {}
  int get(int x) { return r[x] < 0 ? x : r[x] = get(r[x]); }
  int size(int x) { return -r[get(x)]; }
  bool join(int x, int y) {
    x = get(x), y = get(y);
    if (x == y)
      return false;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return true;
  }
};

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

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i];

  int const maxa = 200001;
  DSU dsu(maxa);
  rep(i, 0, n / 2) dsu.join(a[i], a[n - i - 1]);

  ll res = 0;
  rep(i, 0, maxa) if (i == dsu.get(i)) res += dsu.size(i) - 1;
  cout << res << "\n";
}


#include<bits/stdc++.h>
#define w(x)            int x; cin>>x; for(int tc=1;tc<=x;tc++)
#define trace(x) 		cerr<<#x<<": "<<x<<" "<<endl;
#define trace1(x,y)		cerr<<#x<<": "<<x<<" "<<endl;cerr<<#y<<": "<<y<<" "<<endl;
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define inp(x) cin>>x;
#define f first
#define s second
#define inf 1e18

using namespace std;
void solve(int tc) {
	int a, b, c;
	cin >> a >> b >> c;
	cout << a - b + c << endl;

}

int32_t main() {
	FIO
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	// w(x) {
	solve(1);
	// }
	return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h> // -_-bruh u copying mah code?!
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
#define lli long long int
#define i int
#define cr char
#define f float
#define uns unsigned
#define input(x) cin >>x
#define output(x) cout <<x
#define space() cout <<endl
#define gc getchar_unlocked
#define vi(x) vector<x>
#define loop(I,N) for(lli I=0;I<N;I++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626
const i mod = 1000000007;
#define Fill(a,v) memset(a,v,sizeof(a))
#define you -> Noob
//written by Aden Frost
void fastaf(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
void solve(void)
{
    lli N,a,b;
    input(N)>>a>>b;
    output(N-a+b);
}
int main()
{
    fastaf();
   // i T;
    //input (T);
    //while(T--)
    //{
       solve();
       space();
    //}
    return 0;
}


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

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
#define SZ(x) ((int)(x).size())
#define YESNO(bool)            \
    if (bool) {                \
        cout << "YES" << endl; \
    } else {                   \
        cout << "NO" << endl;  \
    }
#define yesno(bool)            \
    if (bool) {                \
        cout << "yes" << endl; \
    } else {                   \
        cout << "no" << endl;  \
    }
#define YesNo(bool)            \
    if (bool) {                \
        cout << "Yes" << endl; \
    } else {                   \
        cout << "No" << endl;  \
    }
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    } else
        return false;
}
template <typename T>
inline void print(const T &x) {
    cout << x << "\n";
}
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");
}
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;

ll solve(ll a, ll b, ll c) {
    // TODO: edit here
    if (a == b) return c;
    if (b == c) return a;
    if (c == a) return b;
    return 0;
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll a, b, c;
    cin >> a >> b >> c;
    auto ans = solve(a, b, c);
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;

typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> ) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
   (( cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
   ((cout << args << ' '), ...);
   cout << '\n';
}
//  mp.max_load_factor(0.25);
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>;
const int MOD = 1e9 + 7, MOD1 = 998244353, MAX = 5e3 + 5;
const char nl = '\n';
const ll INF = 2e18 + 5;
int N, M;
int dp[MAX][MAX];
int inv_[MAX];
int pot_[MAX];
ll pot(ll x, ll y){
   if(y == 0) return 1;
   if(y & 1) return (pot(x, y - 1) * x) % MOD1;
   ll k = pot(x, y / 2);
   return (k * k) % MOD1;
}
ll inv(ll x){
   return pot(x, MOD1 - 2);
}
void add_self(int& x, int y){
   x += y;
   if(x >= MOD1){
      x -= MOD1;
   }
}
int op(int m, int x){
   x = m - x;
   int xd1 = pot_[m];
   int xd2 = pot_[x];
   int gu = (xd1 - xd2);
   if(gu < 0) gu += MOD1;
   gu = int(((ll)gu * inv_[m - x]) % MOD1);
   int th = pot_[m];
   th -= gu;
   if(th < 0){
      th += MOD1;
   }
   return th;
}
void solve(){
   cin >> N >> M;
   for(int i = 1; i <= M; i++){
      dp[1][i] = 1;
      pot_[i] = 1;
   }
   for(int i = 2; i <= N; i++){
      int aux = 0;
      for(int j = 1; j <= M; j++){
         pot_[j] = int((ll(pot_[j]) * j) % MOD1);
         add_self(aux, dp[i - 1][j]);
      }
      for(int j = 1; j <= M; j++){
         add_self(dp[i][j], aux);
         add_self(dp[i][j], op(M, j));
      }
   }
   int ans = 0;
   for(int i = 1; i <= M; i++){
      add_self(ans, dp[N][i]);
   }
   cout << ans << "\n";
}
int main(){
   cpu();
   for(int i = 1; i < MAX; i++){
      inv_[i] = int(inv(i));
   }
   solve();

   return 0;
}

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

template <typename T>
void print(const vector<vector<T>>& matrix, const string& name = "") {
  if (!name.empty()) cout << name << ": " << endl;
  for (const auto& row : matrix) {
    for (const auto& element : row) {
      cout << element << " ";
    }
    cout << endl;
  }
}

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

  // dp(i, j) = number of ways to cut prefix [:i] int j pieces
  const int64_t INFTY = 1e18;
  vector<vector<int64_t>> dpv(L, vector<int64_t>(13, 0));
  dpv[0][1] = 1;
  for (int i = 1; i < L; ++i) {
    for (int j = 1; j < dpv[i].size(); ++j) {
      // dpv[2][2] += dpv[1][2]

      // We don't cut.
      dpv[i][j] += dpv[i - 1][j];

      // We cut.
      dpv[i][j] += dpv[i - 1][j - 1];
    }
  }

  cout << dpv.back().back() << endl;
}


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

int main(){
	ll n,k;
	cin>>n>>k;
	vector<ll> dp2(3*n+1);
	for (ll s2=2;s2<=2*n;++s2){
		dp2[s2]=min(s2-1,n)-max(s2-n,1LL)+1;
	}
	vector<ll> dp3(3*n+1);
	ll acu2=0;
	for (int i=0;i<=3*n;++i){
		dp3[i]=acu2;
		acu2+=dp2[i];
		if (i>=n) acu2-=dp2[i-n];
	}
	ll s=0;
	ll acu=0;
	while (true){
		if (acu>=k) break;
		s+=1;
		acu+=dp3[s];
	}
	ll a,b;
	// phase beauty
	k-=acu-dp3[s]+1;
	acu=0;
	for (a=max(1LL,s-2*n);a<=min(n,s-2);++a){
		ll sumbc=s-a;
		ll nxt=min(sumbc-1,n)-max(sumbc-n,1LL)+1;
		if (acu+nxt>k) break;
		acu+=nxt;
	}

	// phase taste
	k-=acu;
	acu=0;
	for (b=max(1LL,s-a-n);b<=min(n,s-a-1);++b){
		if (acu+1>k) break;
		acu+=1;
	}
	cout<<a<<' '<<b<<' '<<s-a-b<<'\n';
	return 0;

}

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

using namespace std;

struct Complex {
  double r, i;

  Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}
};

Complex operator + (const Complex &a, const Complex &b) {
  return Complex(a.r + b.r, a.i + b.i);
}

Complex operator - (const Complex &a, const Complex &b) {
  return Complex(a.r - b.r, a.i - b.i);
}

Complex operator * (const Complex &a, const Complex &b) {
  return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}

Complex operator / (const Complex &a, const Complex &b) {
  return Complex((a.r * b.r + a.i * b.i) / (b.r * b.r + b.i * b.i), (a.i * b.r - a.r * b.i) / (b.r * b.r + b.i * b.i));
}

Complex operator * (const Complex &a, const double &b) {
  return Complex(a.r * b, a.i * b);
}

const int N = 5e6 + 10;

int r[N];

void get_r(int lim) {
  for (int i = 0; i < lim; i++) {
    r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);
  }
}

void FFT(Complex *f, int lim, int rev) {
  for (int i = 0; i < lim; i++) {
    if (i < r[i]) {
      swap(f[i], f[r[i]]);
    }
  }
  const double pi = acos(-1.0);
  for (int mid = 1; mid < lim; mid <<= 1) {
    Complex wn = Complex(cos(pi / mid), rev * sin(pi / mid));
    for (int len = mid << 1, cur = 0; cur < lim; cur += len) {
      Complex w = Complex(1, 0);
      for (int k = 0; k < mid; k++, w = w * wn) {
        Complex x = f[cur + k], y = w * f[cur + mid + k];
        f[cur + k] = x + y, f[cur + mid + k] = x - y;
      }
    }
  }
  if (rev == -1) {
    for (int i = 0; i < lim; i++) {
      f[i].r /= lim;
    }
  }
}

long long a[N], b[N], n, k;

Complex A[N], B[N];

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    A[i] = Complex(1.0, 0);
  }
  int lim = 1;
  while (lim <= 3 * n) {
    lim <<= 1;
  }
  get_r(lim);
  FFT(A, lim, 1);
  for (int i = 0; i < lim; i++) {
    B[i] = A[i] * A[i] * A[i];
    A[i] = A[i] * A[i];
  }
  FFT(A, lim, -1);
  FFT(B, lim, -1);
  for (int i = 3; i <= 3 * n; i++) {
    b[i] = (long long)(B[i].r + 0.5);
  }
  for (int i = 2; i <= 2 * n; i++) {
    a[i] = (long long)(A[i].r + 0.5);
  }
  // for (int i = 1; i <= 3 * n; i++) {
  //   printf("%lld ", a[i]);
  // }
  // puts("");
  // for (int i = 1; i <= 3 * n; i++) {
  //   printf("%lld ", b[i]);
  // }
  // puts("");
  long long sum;
  for (int i = 3; i <= 3 * n; i++) {
    if (k > b[i]) {
      k -= b[i];
    }
    else {
      sum = i;
      break;
    }
  }
  // cout << k << "\n";
  for (int i = 1; i <= n; i++) {
    if (k > a[sum - i]) {
      k -= a[sum - i];
    }
    else {
      for (int j = 1; j <= n; j++) {
        if (i + j < sum && sum - i - j >= 1 && sum - i - j <= n) {
          k--;
          if (k == 0) {
            printf("%d %d %d\n", i, j, sum - i - j);
            return 0;
          }
        }
      }
      break;
    }
  }
  return 0;
}


#include <iostream>
using namespace std;
int H, W, A, B, ans = 0;
void dfs(int i, int bit, int A, int B){
    if(i == H * W) return (void)ans++;
    if(bit & 1 << i) return dfs(i + 1, bit, A, B);
    if(B) dfs(i + 1, bit | 1 << i, A, B - 1);
    if(A){
        if(i % W != W - 1 && ~bit & 1 << (i + 1)) dfs(i + 1, bit | 1 << i | 1 << (i + 1), A - 1, B);
        if(i + W < H * W) dfs(i + 1, bit | 1 << i | 1 << (i + W), A - 1, B);
    }
}
int main(){
    cin >> H >> W >> A >> B;
    dfs(0, 0, A, B);
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N, M; RI(N); RI(M);
  vector<string> S(M);
  i64 L_min = 100, L_max = 0;
  REP(0, M, i) {
    RS(S[i]);
    L_min = min(L_min, sz(S[i]));
    L_max = max(L_max, sz(S[i]));
  }
  i64 L_avg = (L_min + L_max) / 2;
  // cerr << L_min << " " << L_max << endl;

  unordered_map<i64, vi> L;
  REP(0, M, i) {
    L[sz(S[i])].pb(i);
  }

  vector<string> ans(N, string(N, '.'));

  // horizontal
  i64 N_h = 0, lh;
  i64 Ch;
  REP(L_min, L_max+1, lh) {
    N_h += sz(L[lh]);
    Ch = N / lh; // how many on each row
    if(Ch * N <= N_h) {
      break;
    }
  }
  // cerr << Ch << " " << lh << endl;

  i64 l_cur = L_min;
  REP(0, N, i) {
    i64 pos = 0;
    REP(0, Ch, j) {
      i64 idx = L[l_cur].back();
      L[l_cur].pop_back();
      for(auto &c: S[idx]) {
        ans[i][pos] = c;
        ++pos;
      }
      if(L[l_cur].empty()) {
        L.erase(L.find(l_cur));
        ++l_cur;
      }
    }
    assert(l_cur <= lh);
    assert(pos <= Ch * lh);
  }

  i64 C_begin = Ch * lh;
  // vertical
  i64 N_c = 0, lc;
  i64 Cc;
  REP(l_cur, L_max+1, lc) {
    N_c += sz(L[lc]);
    Cc = N / lc; // how many on each column
    if(Cc * (N - C_begin) <= N_c) {
      break;
    }
  }
  //cerr << Cc << " " << lc << endl;

  REP(C_begin, N, i) {
    i64 pos = 0;
    REP(0, Cc, j) {
      i64 idx = L[l_cur].back();
      L[l_cur].pop_back();
      for(auto &c: S[idx]) {
        assert(ans[pos][i] == '.');
        ans[pos][i] = c;
        ++pos;
      }
      if(L[l_cur].empty()) {
        L.erase(L.find(l_cur));
        ++l_cur;
      }
    }
    assert(l_cur <= lc);
    assert(pos <= Cc * lc);
  }

  REP(0, N, i) {
    WS(ans[i]);
  }

  return 0;
}

//Bismillahir Rahmanir Raheem
#include<bits/stdc++.h>
typedef long long ll;
const ll INFLL = 1e18;
const ll INF =1e9;
using namespace std;
void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll n,a[300005],t,k;
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        a[t]++;
    }
    ll ans=0;
    for(int i=0;i<=n;i++)
    {
        if(a[i]<k)
        {
            ans=ans+i*(k-a[i]);
            k=a[i];
        }
    }
    ans=ans+(n+1)*k;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
#ifndef LOCAL
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    long long k, n, m;
    cin >> k >> n >> m;

    vector<int> arr(k);
    for (int &it : arr) {
        cin >> it;
    }

    int left = m;
    vector<int> brr(k);

    priority_queue<pair<long long, int>> pq;

    for (int i = 0; i < k; i++) {
        int b = (int)(arr[i] * m / n);
        left -= b;
        brr[i] = b;
        pq.push({-abs((b + 1) * n - arr[i] * m) + abs(b * n - arr[i] * m), i});
    }

    for (int i = 0; i < left; i++) {
        auto v = pq.top(); pq.pop();
        int j = v.second;
        brr[j]++;
        pq.push({-abs((brr[j] + 1) * n - arr[j] * m) + abs(brr[j] * n - arr[j] * m), j});
    }

    for (int it : brr) {
        cout << it << " ";
    }
    cout << '\n';
}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
constexpr int INF = 1001001001;

constexpr bool is_multiple = false;

i64 choose2(i64 n) {
    return n * (n-1);
}

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

    int score_s = 0, score_tt = 0;
    map<int, int> map_s, map_tt;
    rep(i, 4) {
        map_s[s[i] - '0']++;
        map_tt[t[i] - '0']++;
    }

    long double rem = 9 * k - 8;

    long double res = 0;

    auto score_calc_s = [&](int i) {
        int ret = 0;

        map_s[i]++;

        for (int j = 1; j <= 9; j++) {
            int tmp = j;
            rep(_, map_s[j]) tmp *= 10;
            ret += tmp;
        }

        map_s[i]--;

        return ret;
    };

    auto score_calc_tt = [&](int i) {
        int ret = 0;

        map_tt[i]++;

        for (int j = 1; j <= 9; j++) {
            int tmp = j;
            rep(_, map_tt[j]) tmp *= 10;
            ret += tmp;
        }

        map_tt[i]--;

        return ret;
    };

    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            if (score_calc_s(i) > score_calc_tt(j)) {
                if (i != j) {
                    res += ((k - map_s[i] - map_tt[i]) / rem) * ((k - map_s[j] - map_tt[j]) / (rem - 1));
                }
                else {
                    res += ((k - map_s[i] - map_tt[i]) / rem) * (max(k - map_s[j] - map_tt[j] - 1, 0) / (rem - 1));
                }
            }
        }
    }

    cout << fixed << setprecision(10) << res << endl;
}

int main() {
    int t = 1;
    if (is_multiple) cin >> t;
    while (t--) solve();
    return 0;
}


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

const int N = 2005;

int n, m, fa[N], ans1, ans2, siz[N];
char a[N][N];

int find_(int x) {
	return x == fa[x] ? x : fa[x] = find_(fa[x]);
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n + m; i++)
		fa[i] = i, siz[i] = 1;
	for (int i = 1; i <= n; i++)
		scanf("%s", a[i] + 1);
	a[1][1] = a[n][1] = a[1][m] = a[n][m] = '#';
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (a[i][j] == '#') {
				int x = find_(i), y = find_(j + n);
				if (x^y)
					fa[x] = y, siz[y] += siz[x];
			}
	for (int i = 1; i <= n + m; i++)
		if (find_(i) == i)
			ans1 += (i <= n)|(siz[i] >= 2), ans2 += (i > n)|(siz[i] >= 2);
	printf("%d\n", min(ans1, ans2) - 1);
}

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

using namespace std;
using namespace __gnu_pbds;

#define all(x)  x.begin(), x.end()
#define sz(x)   (int)x.size()
#define pb      push_back

#define ll      long long
#define int     long long

template <typename T>
using ordered_set = tree<T, null_type,
      less<T>, rb_tree_tag,
      tree_order_statistics_node_update>;

const int MOD1 = 1000000007;
const int MOD2 = 998244353;
const int N = 2e6 + 10;

#ifdef LOCAL
	#include "debug.h"
#else
	#define trace(...) 420;
#endif

struct DSU
{
	int32_t _parent_[N], _rank_[N], _size_[N], comp = 0;

	void make_set(int32_t v) {
		_parent_[v] = v;
		_rank_[v] = 0;
		_size_[v] = 1;
		comp++;
	}
	void init(int32_t n) {
		for (int32_t i = 0; i < n; ++i) {
			make_set(i);
		}
	}
	int32_t find_set(int32_t v) {
		if (v == _parent_[v]) {
			return v;
		}
		return _parent_[v] = find_set(_parent_[v]);
	}
	void merge(int32_t a, int32_t b) {
		a = find_set(a);
		b = find_set(b);
		if (a != b) {
			if (_rank_[a] < _rank_[b]) {      //merge by height
				swap(a, b);
			}
			_parent_[b] = a;
			if (_rank_[a] == _rank_[b]) {
				_rank_[a]++;
			}

			// if (_size_[a] < _size_[b])            //merge by size
			//     swap(a, b);
			// _parent_[b] = a;
			_size_[a] += _size_[b];
			comp--;
		}
	}
} dsu;

vector<int> R[1001], C[1001];
vector<int> out;

void solve() {
	int h, w;
	cin >> h >> w;

	if (h == 2 || w == 2) {
		cout << 0;
		return;
	}

	string rink[h];
	dsu.init(h * w);
	int useless = 0;
	for (int i = 0; i < h; ++i) {
		cin >> rink[i];
		for (int j = 0; j < w; ++j) {
			if (rink[i][j] == '#') {
				R[i].pb(i * w + j);
				C[j].pb(i * w + j);
				if (i == 0 || i == h - 1) {
					out.pb(i * w + j);
				}
			} else {
				useless++;
			}
		}
		if (rink[i][0] == '#') { out.pb(i * w); }
		if (rink[i][w - 1] == '#') { out.pb(i * w + w - 1); }

	}

	int er = 0;
	for (int i = 0; i < h; ++i) {
		if (i && i != h - 1) { er += R[i].empty(); }
		for (int t : R[i]) {
			dsu.merge(t, R[i][0]);
		}
	}
	int ec = 0;
	for (int i = 0; i < w; ++i) {
		if (i && i != w - 1) { ec += C[i].empty(); }
		for (int t : C[i]) {
			dsu.merge(t, C[i][0]);
		}
	}
	for (int t : out) {
		dsu.merge(t, out[0]);
	}
	int x = min(er, ec);
	if (sz(out) == 0) { if (x == 0) { x = 1; } dsu.comp++; }
	cout << x + max(0LL, dsu.comp - useless - 1);
	// trace(er, ec, dsu.comp - useless);

}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
		cout << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define P pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout<<fixed<<setprecision(20) ;

inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-1) ;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

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

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

int main(){
  int a[4];
  for(int i=0;i<4;i++){
    cin>>a[i];
  }
  int min=1000;
  for(int i=0;i<4;i++){
    if(a[i]<min)min=a[i];
  }
  cout<<min;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>

typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;

#define rip(i, n, _s) for (int i = (_s);i < (int)( n ); i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename T>
using MaxHeap = std::priority_queue<T>;
template<typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

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

# ifdef LOCAL_DEBUG
template<typename T>
void vdeb(const std::vector<T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename T>
void vdeb(const std::vector<std::vector<T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        std::cout << i << ' ';
        vdeb(bb[i]);
    }
    std::cout << '\n';
}
# endif

using namespace std;

map<ll, ll> mp;

ll rec(const ll x, const ll y) {
    ll ret = abs(x-y);
    if(y == 1) return ret;
    if(mp.count(y)) return mp[y];
    if(y&1) {
        chmin(ret, min(rec(x, (y+1)>>1), rec(x, (y-1)>>1))+2);
    }
    else {
        chmin(ret, rec(x, y>>1)+1);
    }
    mp[y] = ret;
    return ret;
}

int main() {
    ll x, y; cin >> x >> y;
    // if(x > y) cout << abs(x-y) << endl;
    cout << rec(x,y) << endl;
}

#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>

using namespace std;

int main(){
    long x,y;
    scanf("%ld %ld", &x, &y);
    long nAns = abs(x-y);
    long nTmp = 0;
    while (y>x) {
        if (y%2 == 0) {
            y /= 2;
            nTmp++;
        } else {
            nAns = min(nAns, nTmp+2+abs((2*x-y)/2));
            y = (y+1)/4;
            nTmp += 3;
        }
        nAns = min(nAns, nTmp+abs(x-y));
    }

    printf("%ld", nAns);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const double PI=acos(-1);
const double eps=1e-5;
int main(){
	int n,a,b,c,d;
	scanf("%d",&n);
	scanf("%d%d%d%d",&a,&b,&c,&d);
	double x=1.0*(a+c)/2.0,y=1.0*(b+d)/2.0;
	double len=sqrt(1.0*(a-c)*(a-c)+1.0*(b-d)*(b-d))/2;
	double al=atan2(1.0*(d-b),1.0*(c-a));//用atan()会WA,
	double bl=2.0*PI/n;
	double cl=al+bl;
	double ax=x-len*cos(cl),ay=y-len*sin(cl);
	if(fabs(ay)<eps) ay=0;//-0的情况
	if(fabs(ax)<eps) ax=0;
	printf("%.5f %.5f\n",ax,ay);
	return 0;
}

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

ll solve() {
    ll N;
    cin >> N;
    double x, y;
    cin >> x >> y;
    complex<double> z0(x,y);
    cin >> x >> y;
    complex<double> zh(x,y), zo = (z0+zh);
    zo /= 2;
    complex<double> a(cos(2*M_PI/N),sin(2*M_PI/N));
    complex<double> ans;
    ans = zo + (z0-zo)*a;
    cout << ans.real() << " " << ans.imag() << "\n";
    return 0;
}

int main() {
    cout.setf(ios::fixed);
    cout.precision(16);
    solve();
    return 0;
}


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

int main(){
    int n, D, H;
    cin >> n >> D>> H;
    double ans = 0;
    rep(i, n){
        int d, h;
        cin >> d >> h;
        double now = H - double (H-h) * D/ (D-d);
        ans = max(ans, now);
    }
    printf("%.10f\n", ans);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
// using ll = long long;
#define int long long

void solve(){
	int n, d, h;
	cin>>n>>d>>h;
	vector<pair<int,int>> a(n);
	for(int i=0;i<n;i++){
		cin>>a[i].first>>a[i].second;
	}
	sort(a.begin(), a.end());
	double ans = 0;
	for(int i=0;i<n;i++){
		double now = double(a[i].second - h)/ double(a[i].first - d);
		ans = max(ans, a[i].second - now*a[i].first);
	}
	cout<<fixed<<setprecision(10);
	cout<<ans<<"\n";
}
int32_t main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	int t;
	// cin>>t;
	t = 1;
	int _=1;
	while(t--){
		// cout<<"Case #"<<_++<<": ";
		solve();
	}
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class DSumOfDifference {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<int> A(N);
        REP(i, N) cin >> A[i];
        sort(A.begin(), A.end());
        int sum = A[0];
        int res = 0;
        FOR(i, 1, N-1) {
            res += A[i] * i - sum;
            sum += A[i];
        }
        cout << res << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    DSumOfDifference solver;
    solver.solve(in, out);
    return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define endl "\n";
using namespace std;

const int INFINT = 2e9;
const ll INFLL = 2e18;
const ll mod = 1000000007;

template<ll mod> class modint{
public:
    ll val=0;
    //コンストラクタ
    modint(ll x=0){while(x<0)x+=mod;val=x%mod;}
    //コピーコンストラクタ
    modint(const modint &r){val=r.val;}
    //算術演算子
    modint operator -(){return modint(-val);} //単項
    modint operator +(const modint &r){return modint(*this)+=r;}
    modint operator -(const modint &r){return modint(*this)-=r;}
    modint operator *(const modint &r){return modint(*this)*=r;}
    modint operator /(const modint &r){return modint(*this)/=r;}
    //代入演算子
    modint &operator +=(const modint &r){
        val+=r.val;
        if(val>=mod)val-=mod;
        return *this;
    }
    modint &operator -=(const modint &r){
        if(val<r.val)val+=mod;
        val-=r.val;
        return *this;
    }
    modint &operator *=(const modint &r){
        val=val*r.val%mod;
        return *this;
    }
    modint &operator /=(const modint &r){
        ll a=r.val,b=mod,u=1,v=0;
        while(b){
            ll t=a/b;
            a-=t*b;swap(a,b);
            u-=t*v;swap(u,v);
        }
        val=val*u%mod;
        if(val<0)val+=mod;
        return *this;
    }
    //等価比較演算子
    bool operator ==(const modint& r){return this->val==r.val;}
    bool operator <(const modint& r){return this->val<r.val;}
    bool operator !=(const modint& r){return this->val!=r.val;}
};

using mint = modint<mod>;

//入出力ストリーム
istream &operator >>(istream &is,mint& x){//xにconst付けない
    ll t;is >> t;
    x=t;
    return (is);
}
ostream &operator <<(ostream &os,const mint& x){
    return os<<x.val;
}

//累乗
mint modpow(const mint &a,ll n){
    if(n==0)return 1;
    mint t=modpow(a,n/2);
    t=t*t;
    if(n&1)t=t*a;
    return t;
}

void solve() {
    int N;
    ll M;
    cin >> N >> M;
    int s = N;
    for (int i=0; i<N; i++) {
        int a; cin >> a;
        s += a;
    }
    mint ans = 1;
    for (int i=1; i<=s; i++) {
        ans *= (N+M-i+1);
        ans /= i;
    }

    cout << ans << endl;

    return;
}

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


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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

int dp[3000000];

int main(){
  int n;cin>>n;
  vector<int> v(n);
  REP(i,n)cin>>v[i];
  int sum=0;
  REP(i,n)sum+=v[i];
  dp[0]=1;
  REP(i,n){
    for(int j=sum;j>=0;j--){
      if(dp[j])dp[j+v[i]]=1;
    }
  }
  int ans=1e9;
  for(int i=0;i<=sum;i++)if(dp[i])ans=min(ans,max(sum-i,i));
  cout<<ans<<endl;
}

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

typedef long long int ll;
constexpr int kN = int(1E5 + 10);

int a[kN], b[kN], y[kN], x[kN];

int main() {
	int n, l, idx = -1, now = 0, lst = 0;
	ll ans = 0;
	scanf("%d%d", &n, &l);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
	a[0] = b[0] = 0, a[n + 1] = b[n + 1] = l + 1;
	for (int i = 0; i <= n; i++) x[i] = a[i + 1] - a[i] - 1;
	for (int i = 0; i <= n; i++) y[i] = b[i + 1] - b[i] - 1;
	for (int i = 0; i <= n; i++) if (y[i]) {
		now = 0;
		while (x[lst] == 0) lst++;
		while (now < y[i]) now += x[++idx];
		if (now > y[i]) {
			printf("-1\n");
			return 0;
		}
		ans += max(i - lst, 0) + max(idx - i, 0);
		lst = idx + 1;
	}
	printf("%lld\n", ans);
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
using std::cin;using std::cerr;
using std::max;using std::min;
#define N 100005
#define ll long long
#define db double
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
int a,b,c,flag,ans;
int Pow(int x,int y,int mod,int &f)
{
	int res=1;
	for(;y;y>>=1,f|=(ll)x*x>=mod,x=(ll)x*x%mod)
		if(y&1) f|=(ll)res*x>=mod,res=(ll)res*x%mod;
	return res;
}
int main()
{
	scanf("%d%d%d",&a,&b,&c);
	b=Pow(b,c,4,flag);
	if(flag) b+=4,flag=0;
	ans=Pow(a,b,10,flag);
	printf("%d\n",ans);
	return 0;
}

#include <bits/stdc++.h>

#define FAST_IO ios::sync_with_stdio(false); cin.tie(0), cout << setprecision(15);
#define ll long long int
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, k, n) for (ll i = k; i>=n; i--)
#define repp(i, k, n) for (ll i = k; i < (n); i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define vc vector<ll>
using namespace std;
using namespace std::chrono;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds Milliseconds;

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

ll fact[1000005];
ll mod = 998244353;

void initialize() {
    ll n = 1000005;
    fact[0] = 1;
    fact[1] = 1;
    repp(i, 2, n) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
}

ll ncr(ll a, ll b) {
    ll k = (fact[b] * fact[a - b]) % mod;
    ll g = mPow(k, mod - 2, mod);
    return (fact[a] * g) % mod;
}

void dd() {
    ll n, m, k;
    cin >> n >> m >> k;
    ll ans = 0;
    ll q = mPow(k, n, mod);
    repp(i, 1, n + 1) {
        ans += ((i) * (ncr(n - 1, i - 1) * ((k * (mPow(k - 1, i - 1, mod))) % mod)) % mod) % mod;
        cout << "Segements: " << i << " NCR " << ncr(n - 1, i - 1) << " WAYS " << k * (mPow(k - 1, i - 1, mod)) << "\n";
        ans %= mod;
    }
    q = mPow(q, mod - 2, mod);
    cout << "Inverse Q " << q << "\n";
    cout << "P " << ans << "\n";
    ans *= q;
    ans %= mod;
    cout << ans << "\n";
}

void d() {
    ll n, m, k;
    cin >> n >> m >> k;
    ll ans = 0;
    ll q = pow(k, n);
    repp(i, 1, n + 1) {
        ans += (i) * (ncr(n - 1, i - 1) * k * (mPow(k - 1, i - 1, mod)));
        ans %= mod;
    }
    q = mPow(q, mod - 2, mod);
    ans *= q;
    ans %= mod;
    cout << ans << "\n";
}

void atcoder() {
    ll n; cin>>n;
    ll a[n], b[n];
    rep(i, n) cin>>a[i];
    rep(i, n) cin>>b[i];
    ll ans = 0;
    rep(i, n){
        ans+=a[i]*b[i];
    }
    if(ans == 0) cout<<"Yes";
    else cout<<"No";
}

int main() {
    FAST_IO
    ll test = 1;
//    initialize();
//    cin >> test;
    while (test-- > 0) {
        atcoder();
    }
}