#include <bits/stdc++.h>
#include <time.h>
#include <queue>
#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define forr(i,a,n) for(ll i=a;i<n;i++)
#define ford(i,a,n) for(ll i=n-1;i>=a;i--)
#define all(v) v.begin(),v.end()
#define testc  ll ts;\
               cin>>ts;\
               while(ts--)
#define arn ll n;\
            cin>>n;\
            ll a[n];\
            forr(i,0,n)cin>>a[i];\

#define arr(a,n) ll a[n];\
                 forr(i,0,n)cin>>a[i];\

#define sss <<" "<<
#define nnn <<"\n"
#define nl cout<<"\n";
#define prtitr(v) for(auto itr=v.begin(); itr!=v.end() ; itr++)cout<<*itr<<" ";
#define fitr(v) for(auto itr=v.begin(); itr!=v.end() ; itr++)
using namespace std;
ll M=1e9+7;
ll cdiv(ll a,ll b){
    return (a%b==0)?a/b:a/b+1;
}
ll myMod(ll a, ll b){
    ll r = a % b;
    return r < 0 ? r + b : r;
}
ll ModPow(ll a,ll b,ll M)
{
    if(M==1)return 0;
    a%=M;
    ll ans=1,t=1;
    while(t>0&&t<=b)
    {
        if(t&b)
        {
            ans*=a;
            ans%=M;
        }
        t<<=1;
        a*=a;
        a%=M;
    }
    return ans;
}

ll Pow(ll a,ll b)
{
    if(M==1)return 0;
    ll ans=1,t=1;
    while(t>0&&t<=b)
    {
        if(t&b)
        {
            ans*=a;
        }
        t<<=1;
        a*=a;
    }
    return ans;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int countSetBits(int n)
{
    int count = 0;
    while (n > 0)
    {
        count++;
        n &= (n-1);
    }
    return count;
}

// Function that return count of
// flipped number
int FlippedCount(int a, int b)
{
    // Return count of set bits in
    // a XOR b
    return countSetBits(a^b);
}

struct Point{
    int x,y;
};

bool isPrime(ll n){
    if (n <= 1)
        return false;
    for (ll i = 2; i*i <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}
ll modInverse(ll n, ll p)
{
    return ModPow(n, p - 2, p);
}

ll nCrModP(ll n, ll r, ll p)
{
    if (n < r)
        return 0;
    if (r == 0)
        return 1;

    ll fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[n] * modInverse(fac[r], p) % p
            * modInverse(fac[n - r], p) % p)
           % p;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll a[n],b[n];
    forr(i,0,n)cin>>a[i];
    forr(i,0,n)cin>>b[i];
    ll sum=0;
    forr(i,0,n)sum+=a[i]*b[i];
    if(sum==0)cout<<"Yes" nnn;
    else cout<<"No" nnn;

    return 0;
}

// AUTHOR:- Saumya Rangani

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector<pair<int,int>>
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define it(a) for(auto &i:a)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
#define read(v) for(auto &i:v)cin>>i;
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define in cin>>
#define out cout<<
#define default(a,n,b) for(int i=0;i<n;i++)a[i]=b;
#define YNans(poss) cout << (poss ? "YES" : "NO" ) << endl;
#define inarray(a,n) for(int i=0;i<n;i++)cin>>a[i];
#define fast ios_base :: sync_with_stdio(false),cin.tie(NULL);
#define MAXN 2005

int gcd(int a, int b){return (b?gcd(b,a%b):a);}
bool isPrime(int n){if(n==1) return false;if(n==2) return true;for(int i=2;i*i<=n;i++){if(n%i==0)return false;}return true;}
int power(long long x, unsigned int y, int p=mod){int res = 1;x = x % p; if (x == 0) return 0;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1;x = (x*x) % p;}return res;}
int digit(int n){int ans=0;while(n){ans++;n/=10;}return ans;}
int lcm(int a,int b){return a*b/gcd(a,b);}
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}

vector<vector<int>> grp(MAXN);
bool vis[MAXN];
int  dfs(int node){
    int ans=1;
    if(vis[node]){
        return 0;
    }
    vis[node]=1;
    // cout<<node<<endl;
    for(auto i:grp[node]){
        ans+=dfs(i);
    }
    return ans;
}

void solve(){
    int n;in n;
    int m;in m;
    for(int i=0;i<m;i++){
        int x,y;in x>>y;
        grp[x].pb(y);
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(vis,0,sizeof(vis));
        ans+=dfs(i);
    }
    cout<<ans<<endl;
}

int32_t main(){
    fast
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/*
   Author   : MatsuTaku
   Date     : 02/20/21
*/

#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#ifndef ONLINE_JUDGE
#define Assert(cond)      assert(cond)
#else
#define Assert(cond)
#endif
#define rep(i, n)         for (size_t i = 0, i##_len = (n); i < i##_len; i++)
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
void chmax(T& dst, T x) { dst = max(dst, x); }
template<typename T>
void chmin(T& dst, T x) { dst = min(dst, x); }

class Solver {
 public:
  Solver();
  void solve();
};

Solver::Solver() {}

void Solver::solve() {
  string x; cin>>x;
  ulint m; cin>>m;
  ulint d = *max_element(x.begin(), x.end())-'0' + 1;
  auto check = [&](ulint _d) {
    ulint k = 0;
    rep(i, x.size()) {
      ulint v = x[i]-'0';
      if (k > (m - v) / _d)
        return false;
      k = k * _d + v;
      if (k > m)
        return false;
    }
    return k <= m;
  };

  if (x.size() == 1) {
    int ans = check(d);
    cout << ans << endl;
    return;
  }
  if (check(d)) {
    ulint l = d, r = 11e17;
    while (l+1 < r) {
      auto c = (l+r)/2;
      if (check(c)) {
        l = c;
      } else {
        r = c;
      }
    }
    cout << l-d+1 << endl;
  } else {
    cout << 0 << endl;
  }
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(10);
  Solver solver;
  int t = 1;
//  cin>>t;
  while (t--) {
    solver.solve();
  }
  return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vii;
typedef std::pair<int, int> pii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define repi(i, a, b) for(int i = a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define inf 2147483647
#define inf64 9223372036854775807
#define V vector
#define Q queue
#define PQ priority_queue
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;

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

    V<ll> v;

    ll na = 0;
    rep(i, n) {
        ll a, b;
        cin >> a >> b;
        na += a;

        v.pb(a + a + b);
    }

    sort(all(v), greater<ll>());

    ll ans = 0;
    for(auto val : v) {
        na -= val;
        ans++;

        if(na < 0){
            break;
        }
    }

    cout << ans << endl;

}

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


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <set>
#include <iomanip>
#include <stdio.h>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
int main(void){
  long long int n;
  cin>>n;
  vector<pair<long long int,long long int>> hoge(n);
  for(long long int i=0;i<n;i++){
    cin>>hoge.at(i).first>>hoge.at(i).second;
  }
  long long int ans=9999999999999;
  for(long long int i=0;i<n;i++){
    for(long long int j=0;j<n;j++){
      long long int pre;
      if(i==j){
        pre=hoge.at(i).first+hoge.at(i).second;
      }else{
        pre=max(hoge.at(i).first,hoge.at(j).second);
      }
      ans=min(ans,pre);
    }
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#define forr(i,n) for(int i = 0 ; i  < n ; i++)
#define forr1(i,n) for(int i = 1 ; i  <= n ; i++)
#define int long long int
#define pb push_back
#define endl "\n"

const int N = 2e7 + 5 ;
const int inf = 1e9 + 32;

int binary_search(vector<int> &v , int i)
{
    int l = 1 , r = v.size() , ans = 0 ;
    while ( r >= l)
    {
        int mid = (r + l ) / 2;
        cout << 1 << " " << v[mid - 1] << " " << i << "\n";
        cout.flush();
        int z ; cin >> z ;
        if (z != i)
        {
            l = mid + 1;
            ans = l;
        }
        else
        {

            r = mid - 1;
            // ans = mid ;
        }
    }

    return ans;
}
int get_val(int digit)
{
    for (int i = 0 ; i < 3 ; i++)
        digit = digit * 10 + 9;
    return digit;
}
bool mysort(pair<int, int> a , pair<int, int> b )
{
    return a.second < b.second ;
}
int32_t main()
{

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

// #ifndef ONLINE_JUDGE
//     freopen("input.txt" , "r" , stdin);
//     freopen("output.txt" , "w" , stdout);
// #endif

    int n ;
    cin >> n;
    vector<pair<int, int>> p(n);
    int total = INT_MAX , min_a = INT_MAX , min_b = INT_MAX ;
    int index_a = 0 , index_b = 0 ;
    forr(i, n)
    {
        cin >> p[i].first >> p[i].second;
        total = min(total , p[i].first + p[i].second);
        if (p[i].first < min_a)
        {
            min_a = min(min_a, p[i].first) ;
            index_a = i ;
        }
        if (p[i].second < min_b)
        {
            min_b = min(min_b , p[i].second);
            index_b = i ;
        }
    }
    int ans  ;
    if (index_b == index_a)
    {
        sort(p.begin() , p.end());
        int a2 = p[1].first;
        sort(p.begin() , p.end() , mysort);
        int a3 = p[1].second ;
        ans = min({ max(a2 , min_b) , max(a3 , min_a) , total});
    }
    else
    {
        ans = min(total , max(min_a , min_b));
    }

    cout << ans << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define REP(i, n)   for(int i = 0; i < (n); i++)
#define REPS(i, n)  for(int i = 1; i <= (n); i++)
#define RREP(i, n)  for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl

using ll  = long long;
using pi  = pair<int, int>;
using pl  = pair<ll, ll>;
using vi  = vector<int>;
using vl  = vector<ll>;
using vs  = vector<string>;
using vb  = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

//const int MOD = 1e9 + 7;
const int MOD = 998244353;
const int INF = 1e9 + 7;
const ll  INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

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

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

    int N; cin >> N;
    vi x(N), y(N); REP(i, N) cin >> x[i] >> y[i];
    REP(i, N)
    {
      for(int j = i+1; j < N; j++)
      {
        for(int k = j+1; k < N; k++)
        {
          double tx = x[j]-x[i];
          double ty = y[j]-y[i];
          double sx = x[k]-x[i];
          double sy = y[k]-y[i];
          if(tx == 0 && sx != 0) continue;
          if(ty == 0 && sy!= 0) continue;
          if(sx == 0 && tx != 0) continue;
          if(sy == 0 && ty!= 0) continue;
          double a = sx / tx;
          double b = sy / ty;
          if(tx == 0 || ty == 0 || sx == 0 || sy == 0)
          {
            Yes;
            return 0;
          }
          if(abs(a-b) < EPS)
          {
            Yes;
            return 0;
          }
        }
      }
    }
    No;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define V vector
#define P pair<int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using namespace std;
const int INF =1e9+1;

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
// int main() {
//     long long x, y;
//     extGCD(111, 30, x, y);
//     cout << x << ", " << y << endl;
// }

void solve(){
    int n,s,k;cin>>n>>s>>k;
    long long x,y;
    ll g=extGCD(k,n,x,y);
    if(s%g>0){
        cout<<-1<<endl;
        return ;
    }
    s/=g;
    n/=g;
    k/=g;
    ll ans=((-s)*x)%n;
    cout<<(ans+n)%n<<endl;
    return ;
}

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


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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<ll,int> Pair;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

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

struct Edge{
    int from, to, id=-1;
    ll cost=1, k;

    Edge(int from, int to, ll cost=1, ll k=1): from(from), to(to), cost(cost), k(k) {}
    Edge() {}

    friend istream &operator>>(istream  &input, Edge &e ){
        input >> e.from >> e.to >> e.cost >> e.k;
        e.from--; e.to--;
        return input;
    }

    Edge rev(void){
        return Edge(to, from, cost, k);
    }
};

vector<vector<Edge>> edge;
vector<bool> vis;

void Main(){
    int N,M,X,Y; cin >> N >> M >> X >> Y; X--; Y--;
    edge.resize(N);
    REP(_,M){
        Edge e; cin >> e;
        edge[e.from].emplace_back(e);
        edge[e.to].emplace_back(e.rev());
    }

    VL d(N,LINF);
    priority_queue<Pair,vector<Pair>,greater<Pair>> q;
    q.emplace(0,X);
    while(!q.empty()){
        auto [cost, v] = q.top(); q.pop();
        if(!chmin(d[v], cost)) continue;

        for(auto& e : edge[v]){
            ll t = (cost+e.k-1)/e.k * e.k;
            q.emplace(t+e.cost, e.to);
        }
    }

    ll ans = d[Y];
    cout << (ans<LINF ? ans : -1) << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

#include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define hash hash1
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R)
{
     int val = rng() % R;
     assert(val >= 0);
     return val;
}

const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = MOD1;

using namespace std;

//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
     if (s.back() != ',')
     {
          s += ',';
          cerr << "LINE(" << LINE_NUM << "): ";
     }
     int i = s.find(',');
     cerr << s.substr(0, i) << " = " << x;
     s = s.substr(i + 1);
     if (!s.empty())
          cerr << ", ";
     my_debugger(s, LINE_NUM, y...);
}

#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif

void setMod(int mod_val)
{
     MOD = mod_val;
}
//-----------------------------------------------------

void files_init()
{
     freopen("maxflow.in", "r", stdin);
     freopen("maxflow.out", "w", stdout);
}

const int N = 2e5 + 5;
const int LOGN = 20;

int from[N],to[N],cost[N];
vector<int> g[N];
vector<int> vals;
int pow2[65];
int ans=0;

int getOther(int i,int edgeNo)
{
     return from[edgeNo]^to[edgeNo]^i;
}

void dfs(int i,int prev=-1,int cur_xor=0)
{
     vals.push_back(cur_xor);
     for(auto e:g[i])
     {
          if(e==prev)continue;
          int j=getOther(i,e);
          dfs(j,e,cur_xor^cost[e]);
     }
}

int32_t main()
{
     getFaster;
     //files_init();
     int tests = 1;
     //cin >> tests;
     rep(ii, 0, tests)
     {
          setMod(MOD1);
          pow2[0]=1;
          rep(i,1,62)
          {
               pow2[i]=pow2[i-1]*2;
               while(pow2[i]>=MOD)pow2[i]-=MOD;
          }
          int n;
          cin>>n;
          rep(i,1,n)
          {
               cin>>from[i]>>to[i]>>cost[i];
               g[from[i]].push_back(i);
               g[to[i]].push_back(i);
          }

          dfs(1);

          vector<int> cnt0(62),cnt1(62);
          rep(i,0,61)
          {
               for(auto it:vals)
               {
                    if((1LL<<i)&it)
                    cnt1[i]++;
                    else
                    cnt0[i]++;
               }
          }

          for(auto it:vals)
          {
               rev(i,60,0)
               {
                    if((1LL<<i)&it)
                    cnt1[i]--;
                    else
                    cnt0[i]--;
               }

               rev(i,60,0)
               {
                    if((1LL<<i)&it)
                    ans+=pow2[i]*cnt0[i];
                    else
                    ans+=pow2[i]*cnt1[i];
               }
               ans%=MOD;
          }

          cout<<ans<<endl;
     }
     return 0;
}


/* winners never quit and quitters never win.
      #swap                                 */

#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;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

template <typename T>
void make_unique(vector<T>& vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

#define error(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> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)

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

ll nxt() {
    ll TemporaryVariable;
    cin >> TemporaryVariable;
    return TemporaryVariable;
}

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

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

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n=nxt(),m=nxt();
    string s[n];
    rep(a,0,n)
        cin>>s[a];
    ll ans=0;
    rep(a,0,n)
    {
        rep(b,0,m)
        {
            if(a<n-1 and s[a][b]==s[a+1][b] and s[a][b]=='.')
                ans++;
            if(b<m-1 and s[a][b]==s[a][b+1] and s[a][b]=='.')
                ans++;
        }
    }
    cout<<ans<<endl;
	printclock;
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
#include <functional>
#include <fstream>
#include <iostream>
using namespace std; using ll = long long; using ld = long double;  using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.second < secondElof.second;
}
//AtCoder以外ではコメントアウトを忘れずに！
//#include <atcoder/all>
//using namespace atcoder;
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson//
//**AC Libraryリスト**//
//pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint//
//fenwicktree, segtree, lazysegtree, string//
ll i, j, k, l, m; ll N, M, K, H, W, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vll path;
//***********//
int main() {
	string S, T, U;
	cin >> S >> T >> U;
	N = S.length();
	M = T.length();
	ll L = U.length();
	map<char, ll> A;
	for (i = 0; i < N; i++) {
		A[S[i]] += 1;
	}
	for (i = 0; i < M; i++) {
		A[T[i]] += 1;
	}
	for (i = 0; i < L; i++) {
		A[U[i]] += 1;
	}
	if (A.size() > 10) {
		cout << "UNSOLVABLE" << endl;
		exit(0);
	}
	vector<char> B;
	for (auto x : A) {
		B.push_back(x.first);
	}
	while (true) {
		if (B.size() == 10) break;
		B.push_back('*');
	}
	//debug(B);
	sort(B.begin(), B.end());
	//reverse(S.begin(), S.end());
	//reverse(T.begin(), T.end());
	//reverse(U.begin(), U.end());
	do {
		map<char, ll> P;
		vl Q(26);
		//debug(B);
		for (i = 0; i < 10; i++) {
			if(B[i]!='*') Q[B[i]-'a'] = i;
		}
		ll D=0, E=0, F = 0;
		ll valid = 1;
		for (i = 0; i < N; i++) {
			D += Q[S[i]-'a'] * pow(10, N-i-1);
			if (i == 0 && Q[S[i] - 'a'] == 0) {
				valid = 0;
			}
		}
		for (i = 0; i < M; i++) {
			E += Q[T[i]-'a'] * pow(10, M-i-1);
			if (i == 0 && Q[T[i] - 'a'] == 0) {
				valid = 0;
			}
		}
		for (i = 0; i < L; i++) {
			F += Q[U[i]-'a'] * pow(10, L-i-1);
			if (i == 0 && Q[U[i]-'a'] == 0) {
				valid = 0;
			}
		}
		//debug(D, E, F);
		if (D + E == F && valid) {
			for (i = 0; i < N; i++) {
				cout << Q[S[i]-'a'];
			}
			cout << endl;
			for (i = 0; i < M; i++) {
				cout << Q[T[i]-'a'];
			}
			cout << endl;
			for (i = 0; i < L; i++) {
				cout << Q[U[i]-'a'];
			}
			cout << endl;
			exit(0);
		}
	} while (std::next_permutation(B.begin(), B.end()));
	cout << "UNSOLVABLE" << endl;
}


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

void solve(){
    int n,m,Q;
    cin>>n>>m>>Q;
    vector
w(n);
    REP(i,n)cin>>w[i].second>>w[i].first;
    vector<int>x(m);
    REP(i,m)cin>>x[i];
    sort(ALL(w));
    reverse(ALL(w));
    vector
r(Q);
    REP(i,Q){
        cin>>r[i].first>>r[i].second;
        r[i].first--;r[i].second--;
    }
    REP(t,Q){
        vector<int>tmp;
        REP(i,m){
            if(r[t].first<=i&&i<=r[t].second)continue;
            tmp.push_back(x[i]);
        }
        //cout<<SZ(tmp)<<"T"<<endl;
        sort(ALL(tmp));
        map<int,int>mp;
        ll cnt=0;
        if(SZ(tmp)==0){
            OUT(0);
            continue;
        }
        REP(i,n){
            int p=lower_bound(ALL(tmp),w[i].second)-tmp.begin();
            if(p==SZ(tmp))continue;
            while(p<SZ(tmp)&&mp[p]>0)p++;
            if(p==SZ(tmp))continue;
            cnt+=w[i].first;
            mp[p]++;
            //cout<<p<<" ";
        }
        //cout<<endl;
        OUT(cnt);
    }
}
int main(){
    solve();
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <bitset>

using namespace std;

#define MOD 1000000007
#define PI 3.1415926535897932
#define INF 1e9
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, j, n) for (int i = j; i < n; i++)
#define repi(i, n) for (int i = 0; i <= n; i++)
#define repie(i, j, n) for (int i = j; i <= n; i++)
#define all(x) x.begin(), x.end()
#define println() cout << endl
#define P pair<int, int>
#define fi first
#define se second
typedef long long ll;
using Graph = vector<vector<ll>>;

bool bf(P a, P b) {
    if(a.se == b.se) {
        return a.fi < b.fi;
    } else {
        return a.se > b.se;
    }
}

void solve1() {
    ll n, m, q; cin >> n >> m >> q;
    vector<P> wv(n);
    rep(i, n) {
        cin >> wv[i].fi >> wv[i].se;
    }

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

    sort(all(wv), bf);
    vector<ll> anss(q);
    rep(i, q) {
        ll ans = 0;
        ll l, r; cin >> l >> r;

        vector<ll> nw;
        rep(j, m) {
            if(j+1 >= l && j+1 <= r) {
                continue;
            }
            nw.push_back(x[j]);
        }

        sort(all(nw));
        vector<bool> fs(n, true);
        for(int j = 0; j < nw.size(); j++) {
            for(int k = 0; k < n; k++) {
                if(fs[k] && wv[k].fi <= nw[j]) {
                    ans += wv[k].se;
                    fs[k] = false;
                    break;
                }
            }
        }

        anss[i] = ans;
    }

    rep(i, q) {
        cout << anss[i] << endl;
    }
}

int main()
{
    solve1();
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	int a,b,c;
	cin >> a >> b >> c;
	if(a != b && a != c && b != c) {
		cout << "0";
	}
	else if(a == b && b == c &&  a == c) {
		cout << a;
	}
	else if(a == b && a != c) {
		cout << c;
	}
	else if(a == c && a != b) {
		cout << b;
	}
	else if(c == b && a != b) {
		cout << a;
	}
	return 0;
}

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

#define INF 1234567890
#define ll long long

int N;
string s, t;
vector<int> S, T;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cin >> N >> s >> t;
	int sz = 0, so = 0, tz = 0, to = 0;
	for(int i=0; i<N; i++)
	{
		if (s[i] == '0') sz++, S.push_back(i);
		else so++;

		if (t[i] == '0') tz++, T.push_back(i);
		else to++;
	}
	if (sz != tz) { cout << -1 << "\n"; return 0; }
	int Z = sz;

	int res = 0;
	for(int i=0; i<Z; i++)
	{
		if (S[i] != T[i])
			res++;
	}
	cout << res << "\n";
	return 0;
}


#include <bits/stdc++.h>

#define pb push_back
// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ld long double
using namespace std;
const int M = 1000000007;
set<int> qq;
int cnt[4000009];
int mex = 0;

void add(int x) {
  cnt[x]--;
  if (cnt[x] == 0) {
    qq.insert(x);
    mex = min(mex, x);
  }
}

void del(int x) {
  cnt[x]++;
  if (cnt[x] == 1) {
    qq.erase(x);
    if (mex == x) {
      mex = *qq.upper_bound(mex);
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> A(n);
  for (int i = 0; i < n; i++) cin >> A[i];
  for (int i = 0; i <= n; i++) qq.insert(i);
  for (int i = 0; i < m; i++) del(A[i]);
  int ans = mex;
  for (int i = m; i < n; i++) {
    if (A[i] != A[i - m]) {
      add(A[i - m]);
      del(A[i]);
    }
    ans = min(ans, mex);
  }
  cout << ans << '\n';

}

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

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

    int N,M; cin >> N >> M;
    vector<vector<int>> pos(N + 1);
    rep(i,N){
        int A; cin >> A;
        pos[A].push_back(i);
    }

    rep(i,N+1){
        pos[i].push_back(-1);
        pos[i].push_back(N);
        sort(pos[i].begin(), pos[i].end());
    }

    rep(i,N+1){
        rep(j,pos[i].size()){
            if(pos[i][j + 1] - pos[i][j] > M){
                cout << i << endl;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int N = 4e5 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll LL_MAX = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
#define mid (l + r >> 1)
#define ls (o << 1)
#define rs (o << 1 | 1)
#define lson ls, l, mid
#define rson rs, mid + 1, r
inline ll read(){
    ll ans = 0; int f = 1; char c = getchar();
    while(c < '0' || c > '9'){if (c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') {ans = ans * 10 + c - '0'; c = getchar();}
    return ans * f;
}
ll qpow(ll a, ll b){
    ll ans = 1;
    a %= mod;
    while(b){
        if (b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
ll inv(ll x){
    return qpow(x, mod - 2);
}
char a[5003][5003];
int f[5003][5003];
ll n, m, k;

int main(){
    n = read(), m = read(), k = read();
    int h, w;
    char c;
    for (int i = 1; i <= k; ++i){
        h = read(), w = read();
        cin >> a[h][w];
    }
    f[1][1] = qpow(3, n * m - k);

    ll i32 = inv(3) * 2 % mod;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            if (a[i][j] == 'R') f[i][j + 1] += f[i][j], f[i][j + 1] %= mod;
            else if (a[i][j] == 'D') f[i + 1][j] += f[i][j], f[i + 1][j] %= mod;
            else if (a[i][j] == 'X') f[i][j + 1] += f[i][j], f[i + 1][j] += f[i][j],
            f[i][j + 1] %= mod, f[i + 1][j] %= mod;
            else {
                f[i][j + 1] += f[i][j] * i32 % mod, f[i + 1][j] += f[i][j] * i32 % mod,
                f[i][j + 1] %= mod, f[i + 1][j] %= mod;
            }
        }
    }
    cout << f[n][m] << "\n";

    return 0;
}

//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>

#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<pair<int,int>,int>
#define setl pair<LL,LL>

#define M_PI 3.14159265358979323846
#define micro 0.000001

using namespace std;

#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); }
#else
#define AT
#define AE
#define ce(t)
#define __popcnt __builtin_popcount
#define gcd __gcd
#define lcm __lcm
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,1},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
const LL mod = 998244353;

LL cpow(LL x, LL k)
{
	if (!x) return 0;
	LL ret = 1;
	while (k)
	{
		if (k & 1) ret = ret * x % mod;
		x = x * x % mod;
		k >>= 1;
	}

	return ret;
}

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	LL n, m, k; ci(n >> m >> k);
	LL ans = 0;

	if (n == 1 || m == 1)
	{
		co(cpow(k, n + m - 1));
		return 0;
	}

	if (n == 1) swap(n, m);

	fa(i, 1, k + 1)
	{
		ans = (ans + cpow(i, n) * ((cpow(k - i + 1, m) - cpow(k - i, m) + mod) % mod) % mod) % mod;
	}
	co(ans%mod);
	return 0;
}


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

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
    // cout << fixed << setprecision(5);

    std::string S;
    std::cin >> S;

    ll size = S.size();

    ll Maru = 0;
    ll Hatena = 0;

    for (ll i = 0; i < size; i++) {
        if (S.at(i) == 'o') {
            Maru++;

        } else if (S.at(i) == '?') {
            Hatena++;
        }
    }

    //cout << Maru << endl;
    //cout << Hatena << endl;

    if (Maru > 4) {
        cout << 0 << endl;
    }

    else if (Maru == 4) {
        cout << 24 << endl;
    }

    else if (Maru == 3) {
        cout << 36 + 24 * Hatena << endl;
    }

    else if (Maru == 2) {
        cout << 14 + 36 * Hatena + 24 * Hatena * (Hatena - 1)/2 << endl;
    }

    else if (Maru == 1) {
        cout << 1 + 14 * Hatena + 36 * Hatena * (Hatena - 1)/2 + 24 * Hatena * (Hatena - 1) * (Hatena - 2)/6 << endl;
    }

    else if (Maru == 0) {
        cout << Hatena + 14 * Hatena * (Hatena - 1)/2 + 36 * Hatena * (Hatena - 1) * (Hatena - 2)/6 +
                    24 * Hatena * (Hatena - 1) * (Hatena - 2) * (Hatena - 3)/24
             << endl;
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

#define f first
#define s second
#define pb push_back

#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define sz(li) (int) (li).size()
#define sum(a) (accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>

#define endl "\n"

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
template<typename T, typename U> void cmin(T& a, U b){ if (a > b) a = b; }
template<typename T, typename U> void cmax(T& a, U b){ if (a < b) a = b; }

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

  auto check = [&](int n) {
    string num = to_string(n);

    if (sz(num) != 4) {
      reverse(all(num));

      while (sz(num) != 4) {
        num += '0';
      }

      reverse(all(num));
    }

    bool flag1 = true;
    for (int i = 0; i <= 9; i++) {
      if (s[i] == 'o') {
        bool flag2 = false;
        for (char c : num) {
          flag2 |= (c - '0' == i);
        }

        flag1 &= flag2;
      }
    }

    for (char c : num) {
      flag1 &= (s[c - '0'] != 'x');
    }

    return flag1;
  };

  int ans = 0;

  for (int i = 0; i <= 9999; i++) {
    if (check(i)) {
      ans++;
    }
  }

  cout << ans << endl;
}

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

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

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

  // cerr << "Time: " << 1000.0*clock()/CLOCKS_PER_SEC << " ms\n";
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
#define tc int t;cin>>t;while(t--)
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define pb push_back
#define pi pair<int,int>
#define pll pair<lli,lli>
#define vi vector<int>
#define vll vector<lli>
#define vpi vector<pair<int,int> >
#define vpll vector<pair<lli,lli> >
#define vvi vector<vector<int> >
#define vvll vector<vector<lli> >
#define vvpi vector<vector<pair<int,int> > >
#define vvpll vector<vector<pair<lli,lli> > >
#define inf 1000000000
#define maxn 100005
lli mod=1e9+7;

int main(){
    fastio;
    int n;
    cin>>n;
    vll a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    if(n==1){
        cout<<a[0]<<"\n";
        return 0;
    }
    if(n==2){
        lli tot = (2*a[0])%mod;
        cout<<tot<<"\n";
        return 0;
    }
    lli dp[n][2];
    dp[0][1]=dp[0][0]=1;
    for(int i=1;i<n;i++){
        dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
        dp[i][1]=dp[i-1][0];
        //cout<<dp[i][0]<<" "<<dp[i][1]<<"\n";
    }
    lli ans=0;
    for(int i=0;i<n;i++){
        int nl = i;
        int nr = n-1-i;
        if(i==0){
            ans+= ( a[i]*(dp[nr-1][0]+dp[nr-1][1]) )%mod;
            ans%=mod;
            continue;
        }
        nl--;
        if(nl and nr){
            ans+=(a[i]*( ((dp[nl-1][0]+dp[nl-1][1])*(dp[nr-1][0]+dp[nr-1][1]))%mod ) )%mod;
            ans%=mod;
            ans-=( a[i]*((dp[nl-1][0]*dp[nr-1][0])%mod) )%mod;
            ans=(ans+mod)%mod;
        }
        else{
            int cr = max(nl,nr);
            ans+=(a[i]*(dp[cr-1][0]+dp[cr-1][1]))%mod;
            ans%=mod;
            ans-= ( a[i]*dp[cr-1][0] )%mod;
            ans=(ans+mod)%mod;
        }
    }
    cout<<ans<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node {
	int to,next;
} a[400005];
int aa[200005],b[200005],suma,sumb;
int h[200005],cnt,use[200005],st[200005],top;
void add(int x,int y) {
	cnt++;
	a[cnt].to=y;
	a[cnt].next=h[x];
	h[x]=cnt;
}
void dfs(int v) {
	st[++top]=v;
	use[v]=1;
	for(int i=h[v]; i; i=a[i].next) {
		if(!use[a[i].to]) {
			dfs(a[i].to);
		}
	}
	return ;
}
signed main() {
	int n,m,f=1;
	scanf("%lld%lld",&n,&m);
	for(int i=1; i<=n; i++) {
		scanf("%lld",&aa[i]);
		suma+=aa[i];
	}
	for(int i=1; i<=n; i++) {
		scanf("%lld",&b[i]);
		sumb+=b[i];
	}
	for(int i=1; i<=m; i++) {
		int x,y;
		scanf("%lld%lld",&x,&y);
		add(x,y);
		add(y,x);
	}
	for(int i=1; i<=n; i++) {
		if(aa[i]!=b[i]) {
			f=0;
		}
	}
	if(f==1) {
		printf("Yes");
		return 0;
	}
	if(suma!=sumb) {
		printf("No");
		return 0;
	}
	for(int i=1; i<=n; i++) {
		if(!use[i]) {
			top=0;
			dfs(i);
			int sa=0,sb=0;
			for(int j=1; j<=top; j++) {
				sa+=aa[st[j]];
				sb+=b[st[j]];
			}
			if(sa!=sb) {
				printf("No");
				return 0;
			}
		}
	}
	printf("Yes");
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define vi vector<int>
#define TP template <class o>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 210, size = 1 << 20, mod = 998244353, inf = 2e9;

// char buf[size],*p1=buf,*p2=buf;
template <class o> void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
template <class o> void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
template <class o> void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
template <class o> void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

int n, a[N], b[N], p[N];
bool vis[N], f[N];

void ex() {
    puts("No");
    exit(0);
}

void add(int x, int y) {
    if (~x) {
        if (vis[x])
            ex();
        vis[x] = 1;
        p[x] = y;
    }
}

bool pd(int l, int r) {
    int len = (r - l + 1) / 2, mid = l + len;
    for (int i = l; i < mid; i++) {
        if (vis[i]) {
            if (~p[i]) {
                if (p[i] != i + len)
                    return 0;
            } else if (vis[i + len])
                return 0;
        } else {
            if (~p[i + len])
                return 0;
        }
    }
    return 1;
}

int main() {
    qr(n);
    memset(p, -1, sizeof p);
    for (int i = 1; i <= n; i++) {
        qr(a[i]);
        qr(b[i]);
        add(a[i], b[i]);
        add(b[i], a[i]);
        if (~a[i] && ~b[i] && a[i] >= b[i])
            ex();
    }
    n *= 2;
    f[0] = 1;
    for (int i = 2; i <= n; i += 2)
        for (int j = 0; j < i; j += 2)
            f[i] |= f[j] && pd(j + 1, i);
    puts(f[n] ? "Yes" : "No");
    return 0;
}


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

using namespace std;

#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define i64 long long
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2e5+5;

int d[MAXN];
int t_in[MAXN];
int t_out[MAXN];
int timer = 0;
vi edges[MAXN];

void dfs(int v, int depth) {
	d[v] = depth;
	t_in[v] = timer++;
	for (int u : edges[v]) {
		dfs(u, depth + 1);
	}
	t_out[v] = timer++;
}

int main() {
	int n;
	scanf("%d", &n);
	fore(i, 2, n) {
		int x;
		scanf("%d", &x);
		edges[x].pb(i);
	}
	dfs(1, 0);
	vector<vi> lst(n + 1);
	fore(i, 1, n) {
		lst[d[i]].pb(t_in[i]);
	}
	forn(i, n) {
		sort(lst[i].begin(), lst[i].end());
	}
	int queries;
	scanf("%d", &queries);
	forn(q, queries) {
		int u, d;
		scanf("%d%d", &u, &d);
		if (lst[d].empty()) {
			printf("0\n");
			continue;
		}
		auto beg = lower_bound(lst[d].begin(), lst[d].end(), t_in[u]);
		auto end = upper_bound(lst[d].begin(), lst[d].end(), t_out[u]);
		printf("%d\n", end - beg);
	}
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define N 214514
#define all(x) (x).begin(), (x).end()
const ll mod = 1000000007;
// const ll mod = 998244353;
const ll inf = 8000000000000000000;
void dfs(ll from, vector<vector<ll>>& v, vector<P>& pos, ll* cnt, ll hei,
         vector<vector<ll>>& num, vector<ll>& h) {
    pos[from].fi = *cnt;
    (*cnt)++;
    num[hei].push_back(pos[from].fi);
    h[from] = hei;
    for (ll x : v[from]) {
        hei++;
        dfs(x, v, pos, &(*cnt), hei, num, h);
        hei--;
    }
    pos[from].se = *cnt;
    (*cnt)++;
}
void solve() {
    ll n;
    cin >> n;
    vector<vector<ll>> v(n + 1);
    for (int i = 2; i <= n; i++) {
        ll x;
        cin >> x;
        v[x].push_back(i);
    }
    vector<vector<ll>> num(n + 1);
    ll cnt = 0, hei = 0;
    vector<P> pos(n + 1);
    vector<ll> h(n + 1);
    dfs(1, v, pos, &cnt, hei, num, h);
    ll q;
    cin >> q;
    while (q--) {
        ll u, d;
        cin >> u >> d;
        auto itl = lower_bound(all(num[d]), pos[u].fi),
             itr = lower_bound(all(num[d]), pos[u].se);
        cout << distance(num[d].begin(), itr) - distance(num[d].begin(), itl)
             << endl;
    }
}

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


#include<bits/stdc++.h>
using namespace std;
#define int 		long long
#define pb 			push_back
#define all(a) 		a.begin(),a.end()
#define pii 		pair<int,int>
#define fi 			first
#define se 			second
#define rep(i,k,n) 	for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
const int M=1e9+7,N=205001;

int mm(int x,int y){x%=M,y%=M;return (x*y)%M;}//Modular Multiply
int po(int x,int y){ if(!y)return 1;int a=po(x,y/2)%M;if(y%2)return mm(a,mm(a,x));return mm(a,a);}//(x**y)%M

signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	double pi,th,n,x0,y0,xn,yn,x1,y1,xm,ym;
	cin>>n>>x0>>y0>>xn>>yn;
	pi=2.0*acos(0.0);
	th=(2*pi)/n;
	xm=(x0+xn)/2;
	ym=(y0+yn)/2;
	x0-=xm;y0-=ym;
	x1=x0*cos(th)-y0*sin(th)+xm;
	y1=x0*sin(th)+y0*cos(th)+ym;
	cout<<fixed << setprecision(12)<<x1<<" "<<y1;
}

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>

// #include <atcoder/all>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&&  ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a)        \
  {                    \
    cout << a << endl; \
    return 0;          \
  }
typedef long long ll;

class dstream : public ostream {};

template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
  cout << "\033[1;31m" << v << "\033[0m";
#endif
  return os;
}

ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
  out << std::endl;
#endif
  return out;
}
dstream dout;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (auto const &x : v) {
    os << x;
    if (&x != &v.back()) {
      os << " : ";
    }
  }
  os << " ]" << endl;
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  os << "[" << endl;
  for (auto const &x : v) {
    os << " ";
    os << x;
  }
  os << "]" << endl;
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  os << "[ " << v.first << " " << v.second << " ]" << endl;
  return os;
}

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

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

  ll N;
  cin >> N;
  long double ax, ay, bx, by, cx, cy, v1x, v1y, v2x, v2y;
  cin >> ax >> ay;
  cin >> bx >> by;
  cx = (ax + bx) / 2.0;
  cy = (ay + by) / 2.0;

  v1x = ax - cx;
  v1y = ay - cy;

  long double c = cosf64x(2.0 * 3.1415926 / (long double)N);
  long double s = sinf64x(2.0 * 3.1415926 / (long double)N);
  v2x = c * v1x - s * v1y;
  v2y = s * v1x + c * v1y;

  long double dx = cx + v2x;
  long double dy = cy + v2y;

  cout << std::fixed << std::setprecision(15) << dx << " " << dy << endl;
  // cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 101;
map<int,int>f;
int n, a[N];
set<int>s;
set<int>::iterator it;
signed main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n; i++) scanf("%d",&a[i]);
	sort(a + 1,a + n + 1);
	int ans = 1;
	for(int i = 1;i <= n; i++)
		for(int j = 1;j * j <= a[i]; j++)
		{
			if(a[i] % j == 0)
			{
				if(!f[j] && j < a[1]) f[j] = a[i];
				if(j < a[1]) f[j] = __gcd(f[j],a[i]),s.insert(j);
				if(j * j == a[i]) continue;
				if(!f[a[i]/j] && a[i]/j < a[1]) f[a[i]/j] = a[i];
				if((a[i]/j) < a[1]) f[a[i]/j] = __gcd(a[i],f[a[i]/j]),s.insert(a[i]/j);
			}
		}
	for(it = s.begin();it != s.end(); it++)
	    if(f[*it] == *it) ++ ans;
 	cout << ans;
}

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;

const int inf = 2e9;

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

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

    set<int> facs;

    for (int x: a) {
	for (int i=1; i*i<=x; i++) {
	    if (x%i==0) {
		facs.insert(i);
		facs.insert(x/i);
	    }
	}
    }

    int res = 0;
    for (int f: facs) {
	int cur = 0;
	int lo = inf;
	for (int x: a) {
	    if (x%f==0) {
		cur = __gcd(cur, x);
	    } else {
		lo = min(lo, x);
	    }
	}
	//cout<<f<<": "<<cur<<endl;
	if (f <= lo && cur==f) {
	    res++;
	}
    }

    out(res);

    return 0;
}


// Template begins
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stdio.h>
#include <stack>
#include<queue>
#include <map>
#include <cmath>
#include<cstring>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define fo(i,a,b) for(i=a;i<=b;i++)
#define all(v) (v).begin(),(v).end()
#define sort0(v) sort(all(v))
#define lbb(a,b,arr,x) lower_bound(arr+a,arr+b+1,x)-arr
#define ubb(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-arr
#define freq(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-lower_bound(arr+a,arr+b+1,x)
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define d0(x) cout<<(x)<<" "
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl
#define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl
#define max3(a,b,c) max(max((a),(b)),(c))
#define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define printp(x) cout << x.ff << " " << x.ss << endl
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
    for(auto v : V) os << v << " ";
    return cout << "";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
    for(auto s:S) os<<s<<" ";
    return cout << "";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << P.first << " " << P.second;
}
const ll mod=998244353;
#define inf 9999999999999

ll inv(ll i){if(i==1) return 1;return (mod-((mod/i)*inv(mod%i))%mod)%mod;}

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

ll pwr(ll a, ll b) {a %= mod; ll res = 1;while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;}return res;}
//Template ends

int main(){
    fio;
    ll a,b,c;
    cin>>a>>b>>c;
    ll ans=1;
    ans*=a;
    ans=(ans*b)%mod;
    ans=(ans*c)%mod;
    ans=(ans*(a+1))%mod;
    ans=(ans*(b+1))%mod;
    ans=(ans*(c+1))%mod;
    ans=(ans*inv(8))%mod;
    d1(ans);
}

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll r=998244353;
int main() {
	// your code goes here
	ll a,b,c;
	cin>>a>>b>>c;
	ll a2=(a*(a+1))/2;

	ll b2=(b*(b+1))/2;

	ll c2=(c*(c+1))/2;;

	ll ab=((a2%r)*(b2%r))%r;
	ll abc=((ab%r)*(c2%r))%r;

	cout<<abc;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
	string res = "{";
	for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
	return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
	int val;
	Mint() { val = 0; }
	Mint(const ll& x) {
		val = (-MOD <= x && x < MOD) ? x : x % MOD;
		if (val < 0) val += MOD;
	}
	template <typename U>
	explicit operator U() const { return (U)val; }
	friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
	friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
	friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
	Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
	friend Mint modex(Mint a, ll p) {
		assert(p >= 0);
		Mint ans = 1;
		for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
	Mint& operator++() { return *this += 1; }
	Mint& operator--() { return *this -= 1; }
	Mint operator++(int) { Mint result(*this); *this += 1; return result; }
	Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
	Mint operator-() const { return Mint(-val); }
	friend Mint operator+(Mint a, const Mint& b) { return a += b; }
	friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
	friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
	friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
	friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
	friend string to_string(const Mint& b) { return to_string(b.val); }
};
int main() {
	int n;
	scanf("%d", &n);
	vector<string> C(2, string(2, '_'));
	for (int i = 0; i < 2; ++i)
		for (int j = 0; j < 2; ++j)
			scanf(" %c", &C[i][j]);
	if (C[0][1] == 'B') {
		auto D = C;
		int sum = (int)'A' + 'B';
		D[0][0] = sum - C[1][1];
		D[1][1] = sum - C[0][0];
		D[0][1] = sum - C[0][1];
		D[1][0] = sum - C[1][0];
		swap(D, C);
	}
	assert(C[0][1] == 'A');
	Mint ans = 0;
	if (n == 2) ans = 1;
	else if (C[0][0] == 'A') ans = 1;
	else if (C[1][0] == 'B') ans = modex(Mint(2), n - 3);
	else {
		Mint a = 1, b = 0;
		for (int i = 1; i + 1 < n; ++i) {
			Mint na = a + b;
			Mint nb = a;
			swap(na, a);
			swap(nb, b);
		}
		ans += a;
	}
	printf("%d\n", ans.val);
}

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

#define mod 1000000007

int a[3][3],dp[1005][3],n;
int x = -1;

int power(int p)
{
    int ans = 1;
    while(p)
    {
        ans = ((ans%mod) * (2%mod))%mod;
        p--;
    }
    return ans;
}

int solve(int i,int last)
{

    if(i == n)
    {
        if(x==1 && last == 1) return 0;
        else return 1;
    }
    if(dp[i][last] != -1) return dp[i][last];
    int ans = solve(i+1,(last^1));
    if(last != x) ans = (ans%mod + solve(i+1,last)%mod)%mod;
    return dp[i][last] = ans;

}

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

    int i,j;
    char c;
    cin >> n;
    for(i=0;i<=n;i++) dp[i][0] = dp[i][1] = -1;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            cin >> c;
            a[i][j] = c - 'A';
        }
    }
    if(n<=3)
    {
        cout << 1 << endl;
        return 0;
    }
    if(a[0][1] == 0)
    {
        if(a[0][0]==0)
        {
            cout << 1 << endl;
        }
        else
        {
            if(a[1][0] == 1)
            {
                cout << power(n-3) << endl;
            }
            else
            {
                x = 1;
                n -= 2;
                int ans = (solve(1,0)%mod + solve(1,1)%mod)%mod;
                cout << ans << endl;
            }

        }
    }
    else
    {
        if(a[1][1] == 1)
        {
            cout << 1 << endl;
        }
        else
        {
            if(a[1][0] == 0)
            {
                cout << power(n-3) << endl;
            }
            else
            {
                x = 0;
                n -= 2;
                int ans =  solve(1,1)%mod;
                cout << ans << endl;

            }
        }
    }
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <string>
#include <cassert>

using namespace std;

typedef long long llint;

const int INF = 0x3f3f3f3f;
const llint INFLL = 0x3f3f3f3f3f3f3f3fLL;

void print() { cout << "\n"; }

template <typename...T, typename X>
void print(X&& x, T... args) { cout << x << " "; print(args...); }

int input() { return 0; }

template <typename...T, typename X>
int input(X& x, T&... args) {
    if (!(cin >> x)) return 0;
    return input(args...) + 1;
}

const llint MOD = 998244353;

const int N = 5555;

int n, m, k;

char grid[N][N];
int dp[2][N];

int xs[N][N];
int ys[N][N];

llint mypow(llint a, llint b, llint c) {
    llint res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
            res %= c;
        }
        b >>= 1;
        a = (a * a) % c;
    }
    return res;
}

int main() {
    input(n, m, k);

    memset(grid, 0, sizeof(grid));

    int a, b;
    char c[10];
    for (int i = 0; i < k; i++) {
        scanf("%d%d%s", &a, &b, c);
        grid[a][b] = c[0];
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            xs[i][j] += xs[i][j - 1] + (grid[i][j] == '\0'? 1: 0);
        }
    }

    for (int j = 1; j <= m; j++) {
        for (int i = 1; i <= n; i++) {
            ys[i][j] += ys[i - 1][j] + (grid[i][j] == '\0'? 1: 0);
        }
    }

    int p = 1;
    memset(dp, 0, sizeof(dp));
    dp[1][1] = 1;
    for (int i = 1; i <= n; i++) {
        int q = p ^ 1;
        for (int j = 1; j <= m; j++) {
            if (i == 1 && j == 1) {
                continue;
            }
            llint cur = 0;
            if (i - 1 > 0) {
                llint a = 0;
                if (grid[i - 1][j] == 'D' || grid[i - 1][j] == 'X') {
                    a += dp[q][j];
                } else if (grid[i - 1][j] == '\0') {
                    a += dp[q][j] * 2;
                }
                a *= mypow(3, xs[i][j - 1], MOD);
                cur += a;
            }
            if (j - 1 > 0) {
                llint b = 0;
                if (grid[i][j - 1] == 'R' || grid[i][j - 1] == 'X') {
                    b += dp[p][j - 1];
                } else if (grid[i][j - 1] == '\0') {
                    b += dp[p][j - 1] * 2;
                }
                b *= mypow(3, ys[i - 1][j], MOD);
                cur += b;
            }
            dp[p][j] = cur % MOD;
        }
        /*
        for (int j = 1; j <= m; j++) {
            printf("%lld ", dp[p][j]);
        }
        puts("");
        */
        p = q;
    }

    llint res = dp[p ^ 1][m];
    if (grid[n][m] == '\0') {
        res = res * 3 % MOD;
    }
    print(res);

    return 0;
}

/*
^^^TEST^^^
2 2 3
1 1 X
2 1 R
2 2 R
---
5
$$$TEST$$$

^^^TEST^^^
3 3 5
2 3 D
1 3 D
2 1 D
1 2 X
3 1 R
-----
150
$$$TEST$$$

^^^TEST^^^
5000 5000 10
585 1323 R
2633 3788 X
1222 4989 D
1456 4841 X
2115 3191 R
2120 4450 X
4325 2864 X
222 3205 D
2134 2388 X
2262 3565 R
----
139923295
$$$TEST$$$

*/

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <sstream>
#include <map>
#define INF 100000000
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using p = pair<int, int>;
vector<vector<string>> m(5004,vector<string>(5004,"b"));
vector<vector<ll>> dp(5004,vector<ll>(5004,0));

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

int main() {

    ll h,w,k;
    ll mod = 998244353;

    cin >> h >> w >> k;

    rep(i,k){
        ll a, b;
        string c;
        cin >> a >> b >> c;
        a--; b--;
        m[a][b] = c;
    }
    ll f = 1;
    ll mod3 = modinv(3, mod);
    for(ll i = 0; i < h*w-k;i++) f = (f * 3)%mod;
    dp[0][0] = f%mod;
    rep(i,h){
        rep(j,w){
            if(m[i][j] == "b"){
                dp[i][j+1] = (dp[i][j+1] + dp[i][j]*mod3) % mod;
                dp[i+1][j] = (dp[i+1][j] + dp[i][j]*mod3) % mod;
                dp[i][j+1] = (dp[i][j+1] + dp[i][j]*mod3) % mod;
                dp[i+1][j] = (dp[i+1][j] + dp[i][j]*mod3) % mod;
            }else if(m[i][j] == "D"){
                dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
            }else if(m[i][j] == "X"){
                dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % mod;
                dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
            }else{
                dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % mod;
            }
        }
    }
    cout << dp[h-1][w-1] << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define range(i, a, b) for (ll i = (a); i < (b); i++)
#define rrange(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using pri_s = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pri_b = priority_queue<T>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T> void er(T a) { cout << a << '\n'; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }
template<class T, class U> ostream &operator << (ostream &s, const pair<T, U> &p) { s << p.first << ' ' << p.second; return s; }

struct fastio {
	fastio() {
		cin.tie(0); cout.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(20);
		cerr << fixed << setprecision(20);
	}
}fastio_;

const int N = ten(5) + 10;
vector<vector<int>> graph(N, vector<int>());
vector<int> sz(N, 1), dp(N, 1);

void dfs(int n) {
	vector<P> p;
	for (int i : graph[n]) {
		dfs(i);
		sz[n] += sz[i];
		p.pb({ sz[i] - 2 * dp[i],sz[i] % 2 });
		dp[n] += sz[i] - dp[i];
	}
	sort(all(p), greater<P>());
	int t = 0; int sum = 0;
	for (auto q : p) {
		if (q.first > 0 && q.second == 0) {
			dp[n] -= q.first;
		}
		if (q.second == 1) {
			if (t == 0) {
				dp[n] -= q.first;
			}
			t ^= 1;
		}
		if (q.first < 0 && q.second == 0) {
			sum += q.first;
		}
	}
	if (t == 0) dp[n] -= sum;
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i < n; i++) {
		int v;
		cin >> v;
		v--;
		graph[v].pb(i);
	}
	dfs(0);
	cout << dp[0] << '\n';
}

//#include <bits/stdc++.h>
//using namespace std;
//typedef long long ll;
//typedef pair<int, int> pi;
//typedef pair<ll, ll> pll;
//
//vector<int> V[100001], odd[100001];
//
//int f[100001], sz[100001];
//
//void dfs(int pos) {
//	f[pos] = -1, sz[pos] = 1;
//
//	int sum = 0;
//	for (int w : V[pos]) {
//		dfs(w); sz[pos] += sz[w];
//		if (sz[w] & 1) odd[pos].push_back(w);
//		else {
//			if (f[w] >= 0) f[pos] += f[w];
//			else sum += f[w];
//		}
//	}
//	sort(odd[pos].begin(), odd[pos].end(), [&](int a, int b) -> bool {
//		return f[a] > f[b];
//		});
//	bool aoki = false;
//	for (int w : odd[pos]) {
//		f[pos] += (aoki ? -1 : 1) * f[w];
//		aoki = !aoki;
//	}
//
//	if (odd[pos].size() & 1) f[pos] -= sum;
//	else f[pos] += sum;
//}
//
//int main() {
//	ios::sync_with_stdio(0);
//	cin.tie(0), cout.tie(0);
//
//	int n; cin >> n;
//	for (int i = 2; i <= n; i++) {
//		int a; cin >> a;
//		V[a].push_back(i);
//	}
//	cout << (n - f[1]) / 2;
//
//	return 0;
//}

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

const int N = 1e5 + 10;

int n, fa[N], siz[N], f[N];
vector <int> g[N];

void dfs_(int u) {
	f[u] = -1, siz[u] = 1;
	int tot = 0;
	vector<int> odd;
	for (int v : g[u]) {
		dfs_(v), siz[u] += siz[v];
		if (siz[v] & 1) odd.push_back(v);
		else {
			if (f[v] >= 0) f[u] += f[v];
			else tot += f[v];
		}
	}
	sort(odd.begin(), odd.end(), [&](int a, int b) -> bool {
		return f[a] > f[b];
		});

	bool aoki = false;
	for (int w : odd) {
		f[u] += (aoki ? -1 : 1) * f[w];
		aoki = !aoki;
	}

	if (odd.size() & 1)
		f[u] -= tot;
	else
		f[u] += tot;
}

int main() {
	scanf("%d", &n);
	for (int i = 2; i <= n; i++)
		scanf("%d", &fa[i]), g[fa[i]].push_back(i);
	dfs_(1);
	printf("%d\n", (n - f[1]) / 2);
}


#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 55, MOD = 998244353;
int dsu[N], sz[N];

int find(int u){
	return dsu[u]==u ? u : dsu[u] = find(dsu[u]);
}

void unite(int u, int v){
	u = find(u), v = find(v);
	if(u==v) return;
	if(sz[u] > sz[v]) swap(u,v);
	dsu[u] = v;
	sz[v] += sz[u];
}

signed main(){
	fastio
	int n,k;
	cin >> n >> k;
	iota(dsu,dsu+N,0);
	fill(sz,sz+N,1);
	int arr[n][n];
	for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) cin >> arr[i][j];
	int ans = 1;
	for(int i = 0;i < n;i++){
		for(int j = i+1;j < n;j++){
			bool ok = true;
			for(int l = 0;l < n;l++){
				if(arr[i][l]+arr[j][l] > k) ok = false;
			}
			if(ok){
				unite(i,j);
			}
		}
	}
	for(int i = 0;i < n;i++) if(dsu[i] == i) for(int j = 1;j <= sz[i];j++) ans = ans*j%MOD;
	iota(dsu,dsu+N,0);
	fill(sz,sz+N,1);
	for(int i = 0;i < n;i++){
		for(int j = i+1;j < n;j++){
			bool ok = true;
			for(int l = 0;l < n;l++){
				if(arr[l][i]+arr[l][j] > k) ok = false;
			}
			if(ok) unite(i,j);
		}
	}
	for(int i = 0;i < n;i++) if(dsu[i] == i) for(int j = 1;j <= sz[i];j++) ans = ans*j%MOD;
	cout << ans << "\n";
}

#include <bits/stdc++.h>

using namespace std;

#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;

constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;

using Array = vector<ll>;
using Matrix = vector<Array>;

/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/

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

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

struct Edge {
    ll to, cap, rev;

    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll) G[to].size()));
    if (revFlag)
        G[to].push_back(Edge(from, revCap, (ll) G[from].size() - 1));
}

map<ll, ll> mp;

ll rec(ll x) {
    if (mp.find(x) != mp.end()) return mp[x];
    ll ret = INF;
    if ((ll) 2e9 + 10 >= x) chmin(ret, x * 1);
    chmin(ret, rec(x / 2) + 1 + x % 2 * 1);
    if ((x + 1) / 2 != x) chmin(ret, rec((x + 1) / 2) + 1 + x % 2 * 1);
    return mp[x] = ret;
}

void solve() {
    ll x, y;
    cin >> x >> y;
    if (x >= y) {
        cout << x - y << en;
    }else{
        priority_queue<P, vec<P>, greater<P>> que;
        que.push({0, y});
        ll ans = INF;
        map<ll, ll> mp;
        mp[y]++;
        while (que.size()) {
            auto[num,val] = que.top();
            que.pop();
            if (num > ans) continue;
            if(val<=x){
                chmin(ans, num+x-val);
            }else{
                chmin(ans,num+val-x);
            }
            if (val % 2 == 1) {
                if (!mp.count(val + 1) and ans > num + 1) {
                    mp[val + 1]++;
                    que.push({num + 1, val + 1});
                }
                if (!mp.count(val - 1) and ans > num + 1) {
                    mp[val - 1]++;
                    que.push({num + 1, val - 1});
                }
            }else{
                if(mp.count(val/2) or ans <= num+1) continue;
                que.push({num+1,val/2});
            }
        }
        cout<<ans<<en;
    }

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    REP(i, t - 1) {
        solve();
    }*/

    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;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define vl vector<ll>
#define vs vector<string>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define sl set<ll>
#define pql priority_queue<ll>

#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,n) for(i=n-1;i>=0;i--)
#define REP(i,a,b) for(i=a;i<b;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define vsort(a) sort(all(a))
#define rvsort(a) sort(rall(a))

#define F first
#define S second
#define pi acos(-1)
#define inf 10000000000000000
#define mod 1000000007
#define MA LLONG_MAX
#define MI LLONG_MIN

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

long modInverse(long long n)
{
    return power(n, mod - 2);
}

long long ncr(long long n, ll r)
{
    if (n < r)
        return 0;
    if (r == 0)
        return 1;
    long long fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % mod;

    return (fac[n] * modInverse(fac[r]) % mod*modInverse(fac[n - r])% mod)% mod;
}

void solve()
{
    ll n,i,ans=0;
    cin>>n;
    ll sum=0;
    vl a(n);
    rep(i,n)
    cin>>a[i];
    ordered_set s;
    for(i=0;i<n;i++)
    {
        ans+=s.size()-s.order_of_key(a[i]);
        s.insert(a[i]);
    }
    cout<<ans<<endl;
    for(i=0;i<n-1;i++)
    {
        ans+=(n-a[i]-1);
        ans-=a[i];
        cout<<ans<<endl;
    }
}

int32_t main() {
	ios_base::sync_with_stdio(false);
   cin.tie(NULL); cout.tie(NULL);
// #ifdef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

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

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

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

#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vpi vetor<pii>
#define vpl vector<pll>
#define pb push_back
#define all(v) v.begin(),v.end()
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL);

void cout_vector(vi v){

	for (ll unsigned i=0; i<v.size(); i++) cout << v[i] << " ";
	cout<<endl;
}

int sum_vector(vi v){
	return accumulate(all(v),0);
}

void sort_vector(vi&v){
	sort(all(v));
}

void sort_comp(vi &v, bool func(int,int)){
	sort(all(v),func);
}

bool comp(array<int,2> a,array<int,2> b){
	return a[0]+a[1]<b[0]+b[1];
}

ll gcd(ll a, ll b){
	while(a){
		ll temp =a;
		a=b%a;
		b=temp;
	}
	return b;
}

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

string binary(ll num){
	string ans="";
	do{
		ans=to_string(num%2)+ans;
		num/=2;
	}while(num);
	return ans;
}

const int mxn = 2e3+7;
const ll mod = 1e9+7;

ll pwr(ll num, ll p, ll modi){
	ll res=1;
	while(p>0){
		if(p&1){res*=num; res%=modi;}
		num*=num;
		num%=modi;
		p/=2;
	}
	return res;
}

ll inverse(ll num){
	return pwr(num,mod-2,mod);
}

void solve(){
	int n;
	cin>>n;
	string a,b;
	cin>>a;
	for(int i=0; i<n;i++){
		if(b.size()<=1) b+=a.at(i);
		else{
			int bs=b.size();
			if(b.at(bs-2)=='f' && b.at(bs-1)=='o' && a.at(i)=='x'){
				b.pop_back();
				b.pop_back();
			}
			else{
				b+=a.at(i);
			}
		}
	}
	cout<<b.size();
}

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

	return 0;
}


/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <time.h> //clock
#define endl "\n";
using namespace std;

using PLL = pair<long long,long long>;
using P = pair<int,int>;
const long long INF = 3000000000000000001;
const int inf = 1001001001;
long long MOD = 1000000007;

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long mod) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % mod, M / 2,mod);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % mod;
    return res %= mod;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
    if(res < 0) res += MOD;
    return res % MOD;
}

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

    int N;cin >> N;
    string S,T;cin >> S >> T;
    set<int> index1;
    for(int i = 0;i<N;i++){
        if(S.at(i) == '1') index1.insert(i);
    }
    long long ans = 0;
    for(int i = 0;i<N;i++){
        if(S.at(i) != T.at(i)){
            auto itr = index1.upper_bound(i);
            if(itr == index1.end()){
                cout << -1 << endl;
                return 0;
            }
            int near1 = *index1.upper_bound(i);
            S.at(near1) = '0';
            S.at(i) = T.at(i);
            ans += near1-i;
            //cout << i << " " << near1 << " " << ans << endl;
            index1.erase(near1);
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int N, A, B;
    cin >> N >> A >> B;
    cout << N - A + B << endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<stack>
using namespace std;
int solvea(){

  long long int ans=0;
  long long int s;
  cin>>s;
  for(long long int i=1LL;i<=s;i++){
    long long int hoge=0LL;
    for(long long int j=1LL;j*j*i<=s;j++){
      long long int temp=(s/(i*j));
      hoge+=temp;
    }
    long long int sq=sqrt(s/i);
    ans+=(2LL*hoge-sq*sq);
  }
  cout<<ans<<endl;
  return 0;
}

int solveb(){
  long long int a,b,c;
  cin>>a>>b>>c;
  int table[10][4]=
    {{0,0,0,0},
     {1,1,1,1},
     {6,2,4,8},
     {1,3,9,7},
     {6,4,6,4},
     {5,5,5,5},
     {6,6,6,6},
     {1,7,9,3},
     {6,8,4,2},
     {1,9,1,9}};
  a=a%10LL;
  b=b%4LL;
  if(b==0LL){
    cout<<table[a][0]<<endl;
  }
  else if(b==2LL){
    if(c>1){
      cout<<table[a][0]<<endl;
    }
    else{
      cout<<table[a][2]<<endl;
    }
  }
  else{
    if(c%2==0LL){
      cout<<table[a][1]<<endl;
    }
    else{
      cout<<table[a][b]<<endl;
    }
  }
  return 0;
}

class letter{
public:
  long long int itr;
  char ch;
  void print(){
    cout<<itr<<" "<<ch<<endl;
  }
};

int main(){
  string text;
  cin>>text;
  stack<letter> hoge;
  long long int ruiseki[26][200002]={};
  ruiseki[int(text[0]-97)][0]++;
  for(int i=0;i<26;i++){
    for(int j=1;j<text.size();j++){
      if(int(text[j])==i+97){
        ruiseki[i][j]=ruiseki[i][j-1]+1;
      }
      else{
        ruiseki[i][j]=ruiseki[i][j-1];
      }
    }
    ruiseki[i][text.size()]=ruiseki[i][text.size()-1];
  }

  for(int i=0;i<text.size()-1;i++){
    if(text[i]==text[i+1]){
      letter temp;
      temp.itr=i;
      temp.ch=text[i];
      hoge.push(temp);
    }
  }

  letter before;
  before.itr=text.size();
  before.ch='9';
  long long int ans=0LL;
  while(!hoge.empty()){
    letter now=hoge.top();
    hoge.pop();
    //now.print();
    //cout<<"ans="<<ans<<endl;
    if(before.itr!=now.itr+1){
      if(now.ch!=before.ch){
        ans+=(text.size()-before.itr);
      }
      ans+=((before.itr-now.itr-1)-(ruiseki[int(now.ch)-97][before.itr-1]-ruiseki[int(now.ch)-97][now.itr]));
    }
    before=now;
  }
  cout<<ans<<endl;
  return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
void YES(int a){
  if(a) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}
void Yes(int a){
  if(a) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}

//#include <atcoder/all>

//using namespace atcoder;
//typedef modint998244353 mint;

int main(){
  string S;
  cin>>S;
  int N=S.size();
  VVI X(26,VI(N+1));
  reverse(all(S));
  rep(i,N) X[S[i]-'a'][i+1]++;
  rep(i,26) rep(j,N+1){
    if(j) X[i][j]+=X[i][j-1];
  }
  reverse(all(S));
  VPI P(0);
  rep(i,N-1){
    if(S[i]==S[i+1]) P.pb(mp(S[i]-'a',i));
  }
  int tmp=-1;
  VPI Q(0);
  rep(i,P.size()){
    if(tmp!=P[i].fi){
      Q.pb(P[i]);
      tmp=P[i].fi;
    }
  }
  reverse(all(Q));
  ll ans=0;
  //for(auto e: Q) cout<<e.fi<<" "<<e.se<<endl;
  rep(i,Q.size()){
    if(!i){
      ans+=N-2-Q[i].se;
      ans+=2-(X[Q[i].fi][N-Q[i].se]-X[Q[i].fi][0]);
    }
    else{
      ans+=N-2-Q[i].se;
      //cout<<ans<<endl;
      ans+=2-(X[Q[i].fi][N-Q[i].se]-X[Q[i].fi][N-Q[i-1].se]);
    }
    //cout<<(X[Q[i].fi][N-Q[i].se]-X[Q[i-1].fi][0])<<" "<<ans<<endl;
  }
  cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
const int maxn = 1000 + 10;
const ll mod = 1e9 + 7;
char s[4][10];
int tr[4];
ll f[maxn], g[maxn];
ll powMod(ll a, ll k)
{
	ll ret = 1;
	while(k > 0)
	{
		if(k & 1) ret = ret * a % mod;
		a = a * a % mod; k >>= 1;
	}
	return ret;
}
int main()
{
	int n;
	scanf("%d", &n);
	g[1] = g[0] = f[0] = 1;
	for(int i = 2; i <= n; ++i)
	{
		f[i] = g[i - 2];
		g[i] = (g[i - 1] + f[i]) % mod;
	}
	for(int i = 0; i < 4; ++i)
	{
		scanf("%s", s[i]);
		tr[i] = s[i][0] - 'A';
	}
	if(tr[1] == 0)
	{
		if(tr[0] == 0) puts("1");
		else
		{
			if(tr[2] == 0)
				printf("%lld\n", f[n]);
			else
				printf("%lld\n", powMod(2, n - 3));
		}
	}
	else
	{
		if(tr[3] == 1) puts("1");
		else
		{
			if(tr[2] == 1)
				printf("%lld\n", f[n]);
			else
				printf("%lld\n", powMod(2, n - 3));
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;
int main(){
    int N; cin >> N;
    vector<ll> A(N), sum(N+1);
    for(int i=0; i<N; i++){
        cin >> A[i];
        sum[i+1] = sum[i]+A[i];
    }
    vector<vector<int>> dp(N+2,vector<int>(N+2));
    dp[1][0]++;
    int ans = 0;
    for(int i=0; i<N; i++){
        for(int j=N; 1<=j; j--){
            dp[j+1][sum[i+1]%(j+1)] += dp[j][sum[i+1]%j];
            dp[j+1][sum[i+1]%(j+1)] %= mod;
            if(i == N-1){
                ans += dp[j][sum[i+1]%j];
                ans %= mod;
            }
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/priority_queue.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef gp_hash_table<int, int> hashmap;
//typedef tree<pii, null_type, std::less<pii>, splay_tree_tag, tree_order_statistics_node_update> splaytree;
//typedef tree<pii, null_type, std::less<pii>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::binary_heap_tag> binheap;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::pairing_heap_tag> pairingheap;

template <typename T>
inline void read(T &x)
{
    T data = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        data = (data << 3) + (data << 1) + ch - '0';
        ch = getchar();
    }
    x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args)
{
    read(t);
    read(args...);
}

//mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 9;
const ll mod = 998244353;

signed main()
{
    //freopen("in.txt","r",stdin);
    //freopen("data.txt","w",stdout);
    //std::ios::sync_with_stdio(false);
    //std::cin.tie(0);
    //std::cout.tie(0);
    ll n;
    cin >> n;
    cout << (100 - (n % 100)) << endl;
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define sp(x) cout<< setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf INT64_MAX*0.99
#define pi 3.141592653589793238;
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

typedef pair<int, int> P;

signed main(){
    int n; cin>>n;
    int cnt = 0;
    REP(i,n){
        int in;
        cin>>in;
        if(in>10) cnt+=in-10;
    }
    cout<<cnt<<endl;
    return 0;
}


// Problem: A - Three Dice
// Contest: AtCoder - AISing Programming Contest 2021（AtCoder Beginner Contest 202）
// URL: https://atcoder.jp/contests/abc202/tasks/abc202_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Author: abhidot

#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
using namespace std;
const long long N=200005, INF=2000000000000000000;

/*------------- Modular exponentiation -------------*/
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

/*------------- Sieve --------------*/
vector <int> prime;
bool is_composite[N];

void sieve (int n) {
	fill (is_composite, false);
	for (int i = 2; i < n; ++i) {
		if (!is_composite[i]) prime.push_back (i);
		for (int j = 0; j < prime.size () && i * prime[j] < n; ++j) {
			is_composite[i * prime[j]] = true;
			if (i % prime[j] == 0) break;
		}
	}
}

/*------------ Utitlity function ---------------*/
void print(bool n){
    if(n){
        cout<<"YES";
    }else{
        cout<<"NO";
    }
}

int32_t main()
{
    IOS;
    int a,b,c;
    cin>>a>>b>>c;
    cout<<21-a-b-c;
}

// Written by abhidot

#include <bits/stdc++.h>

using namespace std;
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = uint64_t;

//define
#define int long long
#define ll int
#define trav(i,v)   for(auto i: v)
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v)  v.begin(), v.end()
#define itn int
#define sz(x) (int)x.size()

//typedef
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< std::vector<int> > vvi;
typedef std::vector< std::pair < pii, int > > vpp;

// const long long MOD = 1000000007;
const long long MOD = 998244353;
const long double PI = 3.141592653589793238;
const long long pi = 31415926;
const long long inf = 1000000000000000000;
const long long small_inf = INT_MAX;

int modpow(int x, int n, int mod = MOD){ int res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%MOD; n>>=1;} return res;}
int power(int x, int n){ int res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res;}

void init(){

}

void solve()
{
/*Don't hurry, nothing good comes if you rush*/
    int a, b, c;
    cin>>a>>b>>c;
    a-=7, b-=7, c-=7;
    cout<<abs(a+b+c)<<endl;
}

signed main(){
    // #ifndef ONLINE_JUDGE
    //    freopen("./input.txt", "r", stdin);
    //    freopen("./output.txt", "w", stdout);
    // #endif
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t23 = 1, tt23 = 1;
    // cin>>t23;
    init();
    while(tt23<=t23)
    {
        // cout<<"Case #"<<tt23<<": ";
        solve();
        tt23++;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}


#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
	x=0;char ch=getchar();bool f=false;
	while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template <typename T>
inline void write(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
#define ll long long
const int N=2e5+5,M=1e8;
int n,m,q;
ll a[N],b[N];
struct SGTree{
	int cur,root;
	ll sum[N*80];
	int num[N*80],ls[N*80],rs[N*80];
	void Modify(int &x,int l,int r,int pos,int v,int tp){
		if(!x) x=++cur;
		int mid=l+r>>1;sum[x]+=v*tp;num[x]+=tp;
		if(l==r) return ;
		if(pos<=mid) Modify(ls[x],l,mid,pos,v,tp);
		else Modify(rs[x],mid+1,r,pos,v,tp);
		return ;
	}
	ll QuerySum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return sum[x];
		int mid=l+r>>1;ll res=0;
		if(ql<=mid) res+=QuerySum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QuerySum(rs[x],mid+1,r,ql,qr);
		return res;
	}
	int QueryNum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return num[x];
		int mid=l+r>>1,res=0;
		if(ql<=mid) res+=QueryNum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QueryNum(rs[x],mid+1,r,ql,qr);
		return res;
	}
}t1,t2;
ll Ans;
int main(){
	read(n),read(m),read(q);
	for(int i=1;i<=n;i++) t1.Modify(t1.root,0,M,a[i],a[i],1);
	for(int i=1;i<=m;i++) t2.Modify(t2.root,0,M,b[i],b[i],1);
//	for(int i=1;i<=n;i++){
//		Ans+=1ll*t2.QueryNum(t2.root,0,M,1,a[i])*a[i];
//		Ans+=1ll*t2.QuerySum(t2.root,0,M,a[i]+1,M);
//	}
	for(int i=1;i<=q;i++){
		int op,pos;ll v;
		read(op),read(pos),read(v);
		if(op==1){
			Ans-=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans-=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],-1);

			a[pos]=v;
			Ans+=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans+=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],1);
		}
		else{
			Ans-=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans-=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],-1);
			b[pos]=v;
			Ans+=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans+=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],1);
		}
		write(Ans),putchar('\n');
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

template <typename T>
class fenwick {
private:
	vector <T> sum;
	vector <T> cnt;
	int size;

public:
	fenwick(int _n) : size(_n) {
		sum.assign(size + 1, 0);
		cnt.assign(size + 1, 0);
	}

	void upd_sum(int i, T v) {
		for(; i <= size; i += i&-i) {
			sum[i] += v;
		}
	}

	void upd_cnt(int i, T v) {
		for(; i <= size; i += i&-i) {
			cnt[i] += v;
		}
	}

	T qry_sum(int i) {
		T curr = 0;
		for(; i; i -= i&-i) {
			curr += sum[i];
		}
		return curr;
	}

	T qry_cnt(int i) {
		T curr = 0;
		for(; i; i -= i&-i) {
			curr += cnt[i];
		}
		return curr;
	}
};

const int maxN = 2e5 + 5;

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m, q;
	cin >> n >> m >> q;

	vector <tuple<int, int, int>> queries;
	vector <int> order = {0};

	for(int i = 0; i < q; i++) {
		int t, x, y;
		cin >> t >> x >> y; --x;
		queries.emplace_back(t, x, y);
		order.push_back(y);
	}
	order.erase(unique(order.begin(), order.end()), order.end());
	sort(order.begin(), order.end());

	vector <int> A(n, 0), B(m, 0);
	fenwick <ll> ft_A(maxN), ft_B(maxN);
	ft_A.upd_cnt(1, n); ft_B.upd_cnt(1, m);

	ll ans = 0;
	for(auto &[t, x, y] : queries) {
		int pos = lower_bound(order.begin(), order.end(), y) - order.begin() + 1;
		assert(order[pos - 1] == y);

		if(t == 1) {
			int prev = lower_bound(order.begin(), order.end(), A[x]) - order.begin() + 1;
			assert(order[prev - 1] == A[x]);

			ans -= 1LL * ft_B.qry_cnt(prev - 1) * A[x];
			ans -= (ft_B.qry_sum(pos - 1) - ft_B.qry_sum(prev - 1));
			ans += 1LL * ft_B.qry_cnt(pos - 1) * y;

			ft_A.upd_cnt(prev, -1); ft_A.upd_cnt(pos, 1);
			ft_A.upd_sum(prev, -A[x]); ft_A.upd_sum(pos, y);
			A[x] = y;
		}
		else {
			int prev = lower_bound(order.begin(), order.end(), B[x]) - order.begin() + 1;
			assert(order[prev - 1] == B[x]);

			ans -= 1LL * ft_A.qry_cnt(prev - 1) * B[x];
			ans -= (ft_A.qry_sum(pos - 1) - ft_A.qry_sum(prev - 1));
			ans += 1LL * ft_A.qry_cnt(pos - 1) * y;

			ft_B.upd_cnt(prev, -1); ft_B.upd_cnt(pos, 1);
			ft_B.upd_sum(prev, -B[x]); ft_B.upd_sum(pos, y);
			B[x] = y;
		}
		cout << ans << '\n';
	}
}