//Bismillahir Rahmanir Rahim
//Allahumma Rabbi Jhidni Elma
/*--------Please carefully check--------
    1.Overflow and underflow
    2.Corner case
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long  ull;
typedef long double ld;
const ld PI = 2*acosl(0.0);
const int inf=1e5+7;
const int mxN=3000000;
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define pb push_back
#define reset(a) memset(a,0,sizeof a)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b)*b)
#define abs(a) (a<0?-(a):a)
#define debug1(x)  cout << #x << "=" << x << endl
#define debug2(x, y)  cout << #x << "=" << x << "," << #y << "=" << y << endl
#define digit2(x) floor((log2(x)))
#define digit2(x) floor((log2(x)))
#define sc(a) scanf("%d",&a)
#define pf(a) printf("%d\n",a)
#define  PII  pair<int,int>
#define mk  make_pair

int mark[inf];
struct node
{
    int a,b;
};
vector<PII>tree[inf];
bool cmp(node x,node y)
{
    return x.b<y.b;
}
int prims(int src)
{
    priority_queue<PII ,vector<PII>,greater<PII>>pq;
    pq.push(make_pair(0,src));
    PII x;
    while(!pq.empty())
    {
        x=pq.top();
       int sc=x.second;
       // cout<<x.first<<"  "<<x.second<<endl;
        pq.pop();
        if(mark[sc])
        {
            continue;
        }
         mark[sc]=1;
        // cout<<x.second<<" "<<x.first<<endl;
       for(int i=0;i<tree[sc].size();i++)
       {
           int value=tree[sc][i].second;
          //
           if(mark[value]==0)
           {
              // mark[value]=1;
              // cout<<sc<<" "<<value<<" "<<tree[sc][i].first<<endl;
                pq.push(tree[sc][i]);
           }
       }
    }
    return 0;
}
int write[inf],par[inf],n;
void dfs(int src,int level)
{
   // cout<<level<<" ";
   // cout<<par[src]<<' '<<src<<" "<<write[par[src]]<<" "<<level<<endl;
    if(write[par[src]]==level)
    {
        write[src]=(level%n)+1;
    }
    else
    {
        write[src]=level;
    }
    //ar[src]=level;
    mark[src]=1;

    for(auto x:tree[src])
    {
        //cout<<src<<"-->"<<x.second<<"---"<<x.first<<endl;
        if(mark[x.second]==0)
        {
            par[x.second]=src;
            dfs(x.second,x.first);
        }
    }
   // cout<<src<<" ";
}
int main()
{
    int m,u,v,c,i,temp,j;
    sc(n),sc(m);
    node nd;
    for(i=0;i<m;i++)
    {
        sc(u),sc(v),sc(c);
        //nd.a=v;
       // nd.b=c;
        tree[u].pb(mk(c,v));
        //nd.a=u;
        tree[v].pb(mk(c,u));
    }/*
    for(i=1;i<=n;i++)
    {
        for(j=0;j<tree[i].size();j++)
        {
            cout<<i<<"-->"<<tree[i][j].a<<"-->"<<tree[i][j].b<<endl;
        }
        cout<<endl;
    }*/
    dfs(1,1);
    for(i=1;i<=n;i++)
    {
        cout<<write[i]<<endl;
    }
    cout<<endl;
    //sort(tree.begin(),tree.end(),cmp());
    //prims(1);

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF 0x3f3f3f3f
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)

// const ll mod = 998244353;
// const ll mod = 1e9 + 7;
// const double eps = 1e-6;
// const double PI = acos(-1);
// const double R = 0.57721566490153286060651209;

#define MAXN 205000//点数
#define MAXM 404000//道路数

#define search(i,y) for(int i=head[y];i;i=edge[i].next) //遍历一个点的出边
#define add(u,v,c)  {edge[++cnt]={u,v,c,head[u]};head[u]=cnt;}//加入一条边
int head[MAXN],cnt=1;//0说明没有下一个值
struct EDGE{
    int u,v,c;
    int next;//下一个值的位置;
}edge[MAXM];
int inf = MAXN +1;
int color[MAXN];
bool used[MAXN];
void dfs(int x){
    if(used[x])
        return;
    used[x]=true;
    search(i,x){
        int y=edge[i].v;
        if(used[y])continue;
        if(color[x]==edge[i].c){
            color[y]=edge[i].c-1;
            if(color[y]==0)
                color[y]=edge[i].c+1;
            dfs(y);
        }else{
            color[y]=edge[i].c;
            dfs(y);
        }
    }
    return ;
}

void solve() {
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    color[1]=1;
    dfs(1);
    for(int i=1;i<=n;i++){
        printf("%d\n",color[i]);
    }

}

signed main() {
    ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}


#include<bits/stdc++.h>
#define int ll
#define sz(x) int((x).size())
#define all(x) (x).begin(),(x).end()
#define x first
#define y second
using namespace std;
using ll = long long;
using pi = pair<int,int>;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int minf = 0xc0c0c0c0c0c0c0c0;

int comb[65][65];

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

    comb[0][0] = 1;
    for (int i=1; i<=60; i++) {
        comb[i][0] = 1;
        for (int j=1; j<=i; j++) {
            comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
        }
    }

    int a,b,k; cin>>a>>b>>k;
    string res;
    while (a || b) {
        if (a == 0) {
            res.push_back('b');
            --b;
        }
        else if (b == 0) {
            res.push_back('a');
            --a;
        }
        else {
            if (comb[a+b-1][b] < k) {
                res.push_back('b');
                k -= comb[a+b-1][b];
                --b;
            }
            else {
                res.push_back('a');
                --a;
            }
        }
    }

    cout<<res<<'\n';

    return 0;
}

#include <bits/stdc++.h>

#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;

template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
/**
Read problem statement carefully
**/

LL dp[35][35]; // numbers of ways.

string find_kth(int a, int b, LL k){
    if(a == 0)
        return string(b, 'b');
    if(b == 0)
        return string(a, 'a');
    if(k <= dp[a-1][b]) // put a
        return string("a") + find_kth(a-1, b, k);
    else
        return string("b") + find_kth(a, b-1, k - dp[a-1][b]);
}

void solve(int n){
    int a, b;
    LL k;
    cin >> a >> b >> k;

    dp[0][0] = 1;
    for(int i = 0; i <= a; ++i){
        for(int j = 0; j <= b; ++j){
            if(i) dp[i][j] += dp[i-1][j];
            if(j) dp[i][j] += dp[i][j-1];
        }
    }

    cout << find_kth(a, b, k) << "\n";
}

int main()
{
    Fast;
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // louisfghbvc
        solve(1);
    return 0;
}

/**
Enjoy the problem.
**/


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rrep(i,n) for(int i=((n)-1);i>=0;--i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n,m;
    cin>>n>>m;
    vector<bool> a(1010),b(1010);
    int c;
    rep(i,n){
        cin>>c;
        a[c]=true;
    }
    rep(i,m){
        cin>>c;
        b[c]=true;
    }
    rep(i,1010){
        if((a[i]&&!b[i])||(!a[i]&&b[i])) cout<<i<<" ";
    }
}

// Murabito-B 21/04/17
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        mp[a[i]]++;
    }
    vector<int> ans;
    for (int i = 0, x; i < m; ++i) {
        cin >> x;
        if (mp[x] == 0) ans.push_back(x);
        mp[x]++;
    }
    for (int i = 0; i < n; ++i) {
        if (mp[a[i]] == 1) ans.push_back(a[i]);
    }
    sort(ans.begin(), ans.end());
    for (int x : ans) cout << x << " ";
}
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
    #define ms(a) memset(a,0,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
    inline int qi(){
        int f=0,fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline ll ql(){
        ll f=0;int fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline db qd(){
        char c=getchar();int flag=1;double ans=0;
        while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
        if(c=='-') flag=-1,c=getchar();
        while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
        if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
        return ans*flag;
    }
}
namespace Read{
    #define si(a) scanf("%d",&a)
    #define sl(a) scanf("%lld",&a)
    #define sd(a) scanf("%lf",&a)
    #define ss(a) scanf("%s",a)
    #define rai(x,a,b) repi(i,a,b) x[i]=qi()
    #define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
    #define pi(x) printf("%d",x)
    #define pl(x) printf("%lld",x)
    #define ps(x) printf("%s",x)
    #define pc(x) printf("%c",x)
    #define pe() puts("")
}
namespace DeBug{
    #define MARK false
    #define DB if(MARK)
    #define pr(x) cout<<#x<<": "<<x<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
const int MAX_N=2e5+5;
int n;
char s[MAX_N],x[MAX_N];
bool dp[MAX_N][7];
int main()
{
    n=qi(),ss(s+1),ss(x+1);
    dp[n][0]=true;
    repd(i,n,1){
        if(x[i]=='T'){
            repi(j,0,6)if(dp[i][(10*j)%7]||dp[i][(10*j+s[i]-'0')%7]) dp[i-1][j]=true;
        }
        else{
            repi(j,0,6)if(dp[i][(10*j)%7]&&dp[i][(10*j+s[i]-'0')%7]) dp[i-1][j]=true;
        }
    }
    puts(dp[0][0]?"Takahashi":"Aoki");
    return 0;
}

    #include <bits/stdc++.h>

    // #include <ext/pb_ds/assoc_container.hpp>
    // #include <ext/rope>
    // #include <ext/pb_ds/tree_policy.hpp>
    // #include <ext/pb_ds/trie_policy.hpp>
    // using namespace __gnu_pbds;
    // using namespace __gnu_cxx;
    // typedef tree<long long, null_type, std::less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds;
    // typedef trie<std::string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>Trie;

    using namespace std;
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=n-1;i>=a;i--)
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    #define sz(x) ((int)(x).size())
    #define rdv(x) long long x; cin>>x;
    #define deb(x) cout<<#x<<"="<<x<<endl;
    #define inf 1e18
    #define endl "\n"

    typedef vector<long long> vll;
    typedef long long ll;
    typedef pair<long long,long long> pii;
    typedef long double ld;
    mt19937 mrand(random_device{}());
    const ll mod=100000000000007;
    int rnd(int x) { return mrand() % x;}
    ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
    bool prime[200001];
    void SieveOfEratosthenes()
    {
        memset(prime, true, sizeof(prime));
        prime[1]=false;
        for (int p=2; p*p<=200000; p++)
        {
            if (prime[p] == true)
            {
                for (int i=p*p; i<=200000; i += p)
                    prime[i] = false;
            }
        }
        return;
    }

    ll ceil(ll x,ll y)
    {
        ll ans=x/y;
        if(x%y)
            ans++;

        return ans;
    }

    void file_i_o(){
        #ifndef ONLINE_JUDGE
            freopen("input.txt","r",stdin);
            freopen("output.txt","w",stdout);
        #endif
    }

    void YN(bool flag)
    {
        if(flag)
            cout<<"YES";
        else
            cout<<"NO";
    }
    #define pi 3.1415926535
    // head

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

        map<ll,vll>mp;

        rep(i,0,n)
        {
            ll x,c;
            cin>>x>>c;

            mp[c].pb(x);
        }

        ll l=0,r=0,left=0,right=0;

        for(auto it:mp)
        {
            ll nl=inf,nr=-inf;

            for(auto i:it.se)
            {
                nl=min(nl,i);
                nr=max(nr,i);
            }

            ll ldi=inf,rdi=inf;

            //L->L2->R2
            rdi=min(rdi,abs(l-nl)+abs(nr-nl)+left);
            ldi=min(ldi,abs(l-nr)+abs(nr-nl)+left);
            ldi=min(ldi,abs(r-nr)+abs(nr-nl)+right);
            rdi=min(rdi,abs(r-nl)+abs(nr-nl)+right);

            l=nl;
            r=nr;
            left=ldi;
            right=rdi;
        }

        cout<<min(left+abs(l),right+abs(r));
        cout<<"\n";
    }
    int main()
    {
        ios_base::sync_with_stdio(false); 			//Used to reduce the time
        cin.tie(NULL);
        cout.tie(NULL);

        file_i_o();

        ll T=1;
        //cin>>T;
        //ll temp=T;
        while(T--)
        {
            //cout<<"Case #"<<temp-T<<": ";
            solve();
        }
        cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<" ms\n";
    }


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

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> A(M);
    vector<int> B(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i];
    }
    int K;
    cin >> K;
    vector<int> C(K + 1);
    vector<int> D(K + 1);
    for (int i = 0; i < K; i++) {
        cin >> C[i] >> D[i];
    }
    int ans = -1;
    for (int i = 0; i < (1 << K); i++) {
        vector<int> save(N + 1);
        for (int j = 0; j < K; j++) {
            if (i & (1 << j)) save.at(C.at(j))++;
            else save.at(D.at(j))++;
        }
        int cnt = 0;
        for (int j = 0; j < M; j++) {
            if (save.at(A.at(j)) > 0 && save.at(B.at(j)) > 0) cnt++;
        }
        ans = max(ans, cnt);
    }
    cout << ans << endl;
}

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

struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(x!=a.x) return x<a.x;
    //if(y!=a.y)
    return x>a.x;
  };
};

int a,b,c,d,i,n,m,e,k,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[211111];
int l[201011],ck[201010];
ll dp[2];
int ss[201011];
int ans[1];

long long x,y,z;
const long long mod=1000000007,hf=(mod+1)/2;

//string r;
char r;
vector<int> v[201011];
P u[201011];
queue<P> q;

//multiset<int> s;
stack<int> s;
unordered_map<int,int> p[1];
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//rng()
bool as(P a,P b)
{
  //if(a.x!=b.x)
  //return a.x<b.x;
  //if(a.y!=b.y) return a.y>b.y;
  return min(a.x,a.y)<min(b.x,b.y);
}

int main()
{
  scanf("%d %d",&a,&b);
  for(int t=1;t<a;t++)
  {
    scanf("%d %d",&n,&m);
    v[n].push_back(m);
    v[m].push_back(n);
  }
  n=1,m=a;
  for(;n<m;)
  {
    int k=(n+m)/2;
    e=0;
    queue<int> q;
    memset(ss,0,sizeof(ss));
    memset(l,-1,sizeof(l));
    memset(o,0,sizeof(o));
    memset(ck,0,sizeof(ck));
    for(int t=1;t<=a;t++)
    if(v[t].size()==1)
    {
      l[t]=-1;
      //ck[t]=1;
      q.push(t);
    }
    int r=0;
    for(;q.size();q.pop())
    {
      int p=q.front();
      //printf("#%d\n",p);
      ck[p]=1;
      r++;
      if(-l[p]>o[p])
      {
        if(-l[p]>k)
        {
          //printf("**%d\n",p);
          e++;
          for(int h=0;h<v[p].size();h++)
          if(ck[v[p][h]]==0)
          {
            ss[v[p][h]]++;
            o[v[p][h]]=max(o[v[p][h]],k);
            if(ss[v[p][h]]+1==v[v[p][h]].size())
            {
              //ck[v[p][h]]=1;
              q.push(v[p][h]);
            }
          }
        }
        else if(r==a) e++;
        else
        {
          for(int h=0;h<v[p].size();h++)
          if(ck[v[p][h]]==0)
          {
            //printf("^^%d\n",v[p][h]);
            ss[v[p][h]]++;
            l[v[p][h]]=min(l[v[p][h]],l[p]-1);
            if(ss[v[p][h]]+1==v[v[p][h]].size())
            {
              //ck[v[p][h]]=1;
              q.push(v[p][h]);
            }
          }
        }
      }
      else
      {
        for(int h=0;h<v[p].size();h++)
          if(ck[v[p][h]]==0)
          {
            ss[v[p][h]]++;
            if(o[p]>0) o[v[p][h]]=max(o[v[p][h]],o[p]-1);
            //else l[v[p][h]]=min(l[v[p][h]],-1);
            if(ss[v[p][h]]+1==v[v[p][h]].size())
            {
              //ck[v[p][h]]=1;
              q.push(v[p][h]);
            }
          }
      }
    }

    if(e<=b) m=k;
    else n=k+1;

  }
  //printf("%d %d %d\n",n,m,e);
  //for(int t=1;t<=a;t++)
    //printf("#%d %d\n",l[t],o[t]);
  printf("%d",n);
}
/*
7 2
1 2
2 3
3 4
4 5
5 6
6 7
 */


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

typedef long long ll;
typedef unsigned long long ull;

#define mp make_pair
#define fr first
#define sc second
template<int MOD=1000000007>
struct modint{
	/*static modint inv[2005];
	static void init(){
		for(int i=0;i<2005;i++)inv[i]=modpow(modint(i),MOD-2);
	}*/
	ull val;
	modint(ull x){ val=x%MOD; }
	modint(){}
	friend modint modpow(modint x,ull k){
		modint ret(1ULL);
		while(k>0){
			if(k&1ULL)ret*=x;
			x*=x;
			k>>=1;
		}
		return ret;
	}
	modint& operator +=(const modint& rhs){
		this->val+=rhs.val;
		if(this->val>=MOD)this->val-=MOD;
		return *this;
	}
	friend modint operator+(modint lhs, const modint& rhs){
		lhs+=rhs;
		return lhs;
	}
	modint& operator -=(const modint& rhs){
		this->val+=MOD-rhs.val;
		if(this->val>=MOD)this->val-=MOD;
		return *this;
	}
	friend modint operator-(modint lhs, const modint& rhs){
		lhs-=rhs;
		return lhs;
	}
	modint& operator *=(const modint& rhs){
		this->val*=rhs.val;
		this->val%=MOD;
		return *this;
	}
	friend modint operator*(modint lhs, const modint& rhs){
		lhs*=rhs;
		return lhs;
	}
	modint& operator /=(const modint& rhs){
		(*this)*=modpow(rhs,MOD-2);
		//(*this)*=inv[rhs.val];
		return *this;
	}
	friend modint operator/(modint lhs, const modint& rhs){
		lhs/=rhs;
		return lhs;
	}
};
//template<int MOD>
//modint<MOD> modint<MOD>::inv[2005];

const int MOD=998244353;
typedef modint<MOD> mi;

int main(){
	int n,m,k;
	cin>>n>>m>>k;
	if(n>m)swap(n,m);
	if(n==1){
		cout<<modpow(mi(k),m).val<<endl;
		return 0;
	}

	mi ret=0;
	for(int i=1;i<=k;i++){
		mi l=modpow(mi(i),n)-modpow(mi(i-1),n);
		mi r=modpow(mi(k-i+1),m);
		ret+=l*r;
	}
	cout<<ret.val<<endl;
}

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

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
vector<pair<int,ll>> E[201010];
const ll mo=1000000007;
ll pat;
ll C[2];

void dfs(int cur,int pre,int id,int d) {
	C[id]++;
	FORR2(e,c,E[cur]) if(e!=pre) {
		if(c&(1LL<<d)) {
			dfs(e,cur,id^1,d);
		}
		else {
			dfs(e,cur,id,d);
		}

	}
}

void solve() {
	int i,j,k,l,r,x,y; string s;

	cin>>N;
	FOR(i,N-1) {
		ll a;
		cin>>x>>y>>a;
		E[x-1].push_back({y-1,a});
		E[y-1].push_back({x-1,a});
	}

	ll ret=0;
	FOR(i,60) {
		ZERO(C);
		dfs(0,0,0,i);

		(ret+=C[0]*C[1]%mo*((1LL<<i)%mo))%=mo;

	}
	cout<<ret<<endl;
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


/* Lucky_Glass */
#include <cstdio>
#include <cstring>
#include <algorithm>

const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n;
char str[N];
int dp[N], mndp[30];

int main() {
  scanf("%d%s", &n, str + 1);
  memset(mndp, 0x3f, sizeof mndp);

  mndp[str[1] - 'a'] = 0;
  for (int i = 1; i <= n; ++i) {
    int &now = dp[i] = INF;
    for (int c = 0; c < 26; ++c)
      if (c != str[i] - 'a')
        now = std::min(now, mndp[c] + 1);
    if (i < n)
      mndp[str[i + 1] - 'a'] = std::min(mndp[str[i + 1] - 'a'], now);
  }
  printf("%d\n", dp[n] == INF ? -1 : dp[n]);
  return 0;
}

//  by pakhomovee
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <queue>
#include <unordered_set>

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define dbg(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FEL(a, x) for (auto& a : x)
#define REV reverse
#define asrt assert
#define unique1(x) x.resize(unique(all(x)) - x.begin())

const int mod = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    if (s[0] != s.back()) {
        cout << 1;
    } else {
        int df = 0;
        for (int i = 0; i + 1 < s.size(); ++i) {
            if (s[i] != s[0] && s[i + 1] != s[0]) {
                cout << 2;
                return;
            }
        }
        cout << -1;
    }
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    t = 1;
    while (t--) {
        solve();
    }
}


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

#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define max Max
#define min Min
#define abs Abs

using namespace std;

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

template<typename T,typename TT> inline T Max(T a,TT b){return a>b?a:b;}
template<typename T,typename TT> inline T Min(T a,TT b){return a>b?b:a;}
template<typename T> inline T Abs(T x){return x<0?-x:x;}

int a,b,x,y;

signed main()
{
	a=read();b=read();x=read();y=read();
	int ans=0;
	if(a>b) ans=min((abs(a-b)-1)*y+x,(abs(a-b)*2-1)*x);
	else if(a<b) ans=min(abs(a-b)*y+x,(abs(a-b)*2+1)*x);
	else if(a==b) ans=x;
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ll long long
int f(int n){
    return n%10 + n%100/10 + n/100;
}
int main(){
    int a, b;
    cin >> a >> b;
    cout << max(f(a), f(b)) << endl;
}


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

#define all(v) (v).begin(),(v).end()
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
using vll = vector<ll>;

constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;

int main(){
    int n;
    cin>>n;
    vll v(n);
    ll k=0;
    rep(i,n){
        ll a,b;cin>>a>>b;
        v[i]=2*a+b;
        k+=a;
    }
    sort(all(v),greater<ll>());
    int ans=0;
    rep(i,n){
        if(k<0LL)break;
        k-=v[i];
        ans++;
    }
    cout<<ans;
}

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
using namespace std;
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using lli = long long;
using vi = vc<int>;
using ii = pair<int,int>;

lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);}
lli lcm(lli a, lli b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
int popcount(lli x) { return __builtin_popcountll(x); }
lli poww(lli a, lli b){
	lli res = 1;
	while(b){ if(b&1) res = res*a; a=a*a; b>>=1;}
	return res;
}

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}

template<class t,class u>bool mmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool mmin(t&a,u b){if(b<a)a=b;return b<a;}
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
// ---- しゃけ ツナマヨ ('-')7
typedef struct node *pn;
struct node{
	array<lli,2>a;
	pn l,r;
	node(){a[0]=a[1]=0; l = r = 0;}
};

lli g0(pn u){
	if(u)return u->a[0];
	return 0;
}

lli g1(pn u){
	if(u)return u->a[1];
	return 0;
}

array<lli,2> SUM(array<lli,2> a, array<lli,2> b){
	return {a[0]+b[0],a[1]+b[1]};
}

void upd(pn &u, int l, int r, int val, int side){
	if(l>r)return;
	if(l==r){
		u->a[0]+=side;
		u->a[1]=(u->a[0])*val;
		return;
	}
	int mid = (l+r)/2;
	if(val<=mid){
		if(!u->l)u->l = new node();
		upd(u->l,l,mid,val,side);
	}
	else{
		if(!u->r)u->r = new node();
		upd(u->r,mid+1,r,val,side);
	}
	u->a[0]=g0(u->l)+g0(u->r);
	u->a[1]=g1(u->l)+g1(u->r);
}

array<lli,2> find(pn &u, int l, int r, int ll, int rr){
	if(!u or l>r or l>rr or r<ll)return {0,0};
	if(ll<=l and r<=rr)return {g0(u),g1(u)};
	int mid = (l+r)/2;
	return SUM(find(u->l,l,mid,ll,rr), find(u->r,mid+1,r,ll,rr));
}

int main(){_

	auto solve=[&](){
		int L = 0, R = 1e8+5;
		int n,m,q; cin>>n>>m>>q;
		pn A = new node(), B = new node();
		upd(A,L,R,0,n);
		upd(B,L,R,0,m);
		vi aa(n,0), bb(m,0);
		lli sum = 0;
		auto g=[&](pn &a, pn &b, int &x, int y){
			auto tot = find(a,L,R,L,R);
			auto aux = find(a,L,R,L,x);
			sum-= (tot[1]-aux[1])+aux[0]*x;
			upd(b,L,R,x,-1);
			x = y;
			upd(b,L,R,x,+1);
			aux = find(a,L,R,L,x);
			sum+=(tot[1]-aux[1])+aux[0]*x;
		};
		fore(i,0,q){
			int t,x,y; cin>>t>>x>>y; x--;
			if(t==2)g(A,B,bb[x],y);
			else g(B,A,aa[x],y);
			cout<<sum<<ENDL;
		}

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

}


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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)

//#define	INF (1<<30)
#define	INF (2147483647)
// 2^31 -1
//= 1,073,741,824 *2 -1
//=   536,870,912 *4 -1

#define	MOD 1000000007

#define	Rep0(i, n)	for (auto i=0; i<n; i++)
#define	Rep1(i, n)	for (auto i=1; i<=n; i++)
#define Sort(P)	sort(P.begin(), P.end())
#define Rev(P)	reverse(P.begin(), P.end())

int	main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	ll N;
	cin >>N;
	vector<ll> A(N, 0);
	Rep0(i, N)
		cin >>A.at(i);
	ll ANS=0;
	ll D=0;
	ll DMax=0;
	ll X=0;
	Rep0(i, N) {
		D +=A.at(i);
		DMax=max(DMax, D);
		ANS=max(ANS, X+DMax);
		X +=D;
	}
	cout <<ANS <<endl;
	return 0;
}

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

template<typename T>
struct BIT{
    int n;
    vector<T> d;
    BIT(int n=0): n(n), d(n+1) {}
    void add(int i, T x=1){//場所i(0-indexed)にxを足す
        for(i++; i<=n; i+= i&-i){
            d[i] += x;
        }
    }
    T sum(int i){//場所i以下(0-indexed)の和
        T x = 0;
        for(i++; i; i -= i&-i){
            x += d[i];
        }
        return x;
    }
};

int main(){
    int h, w, m;
    cin >> h >> w >> m;
    vi left(h, w);
    vi  top(w, h);
    vv wall(h);
    //vector<set<int>> yoko(h);
    //vector<set<int>> tate(w);
    rep(i,m){
        int x, y;
        cin >> x >> y;
        x--; y--;
        left[x] = min(left[x], y);
        top[y]  = min( top[y], x);
        wall[x].push_back(y);
        //yoko[x].insert(y);
        //tate[y].insert(x);
    }
    //rep(i,h)yoko[i].insert(w);
    //rep(j,w)tate[j].insert(h);

    //1手で動けるマス数
    ll ans = left[0] + top[0] -1;

    //横縦の順で動けるマス数
    for(int j=1; j<left[0]; j++){
        ans += top[j]-1;
    }

    //横縦の順で動けなかったが縦横でなら動ける(縦からは影になっている)マス数
    BIT<int> bit(w);
    for(int j0 = left[0]; j0<w; j0++)
        bit.add(j0, 1);

    for(int i=1; i<top[0]; i++){
        ans += bit.sum(left[i]-1);
        auto itr = wall[i].begin();
        while(itr != wall[i].end()){
            if(bit.sum(*itr) == bit.sum(*itr-1))
                bit.add(*itr, 1);
            itr++;
        }
    }

    cout << ans << endl;

    return 0;
}


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

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

  int H,W;
  cin>>H>>W;
  vector<string> S(H);
  rep(i,H) cin>>S[i];

  vector<vector<pair<int,int>>> v(H+W);
  rep(i,H) rep(j,W) v[i+j].push_back(mkp(i,j));

  ll ans=1;
  for(int k=0;k<H+W-1;k++){
    ll r=0,b=0;
    ll c=0;
    for(auto [i,j]:v[k]){
      if(S[i][j]=='R') r++;
      else if(S[i][j]=='B') b++;
      else c++;
    }

    if(r>0&&b>0) ans=0;
    else if(r>0||b>0);
    else{
      ans=ans*2%MOD;
    }
  }

  cout<<ans<<endl;

  return 0;
}

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

typedef long long LL;

const int N=500,mod=998244353;

int n,m;
char s[N+9][N+9];

void into(){
  scanf("%d%d",&n,&m);
  for (int i=1;i<=n;++i)
	scanf("%s",s[i]+1);
}

int cnt[N*2+9][3],ans;

void Get_ans(){
  for (int i=1;i<=n;++i)
	for (int j=1;j<=m;++j) ++cnt[i+j][s[i][j]=='R'?2:s[i][j]=='B'];
  ans=1;
  for (int i=2;i<=n+m;++i){
	if (cnt[i][1]&&cnt[i][2]) ans=0;
	if (!cnt[i][1]&&!cnt[i][2]) ans=1LL*ans*2%mod;
  }
  printf("%d\n",ans);
}

void work(){
  Get_ans();
}

void outo(){
}

int main(){
  int T=1;
  //scanf("%d",&T);
  for (;T--;){
	into();
	work();
	outo();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define dup(x,y) (((x)+(y)-1)/(y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
template<typename T> bool chmax(T &a, T b) {if(a<b){a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, T b) {if(a>b){a=b; return 1; } return 0; }
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
ll mpow(ll x, ll p) {
	if (p==0) return 1;
	ll res=mpow(x,p/2); res=res*res%MOD;
	return p%2?res*x%MOD:res;
}
ll lpow(ll x, ll p) {
	if (p==0) return 1;
	ll res=lpow(x,p/2); res*=res;
	return p%2?res*x:res;
}

int main() {
	int x;
	cin >> x;
	x %= 100;
	if (x == 0) cout << 100 << endl;
	else cout << 100 - x << endl;
}

#include <iostream>
using namespace std;
int main() {
  int n;
  while(cin>>n){
    int ans=100-n%100;
    cout<<ans<<endl;
  }
}


#include<bits/stdc++.h>

using namespace std;

#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              f
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()

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

const ll   N     =  3005;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;

ll n, k, dp[N][N];

ll mem(int i, int j){
	if(i < j) return 0;
	if(i == 0) return j == 0;
	if(j == 0) return i == 0;
	if(i == j) return 1;
	if(dp[i][j] != -1) return dp[i][j];
	dp[i][j] = (mem(i - 1, j - 1) + mem(i, 2 * j)) % mod;
	return dp[i][j];
}

void solve(){
	mset(dp, -1);
	cin >> n >> k;
	deb1(mem(n, k));
}

int main(){
	GODSPEED;
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; i++){
		solve();
	}
	#ifndef ONLINE_JUDGE
	cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
	#endif
}

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

template <const int32_t MOD> struct ModInt {
  long long x;
  ModInt() : x(0) {}
  ModInt(long long u) : x(u) {
    x %= MOD;
    if (x < 0)
      x += MOD;
  }
  friend bool operator==(const ModInt &a, const ModInt &b) {
    return a.x == b.x;
  }
  friend bool operator!=(const ModInt &a, const ModInt &b) {
    return a.x != b.x;
  }
  friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
  friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
  friend bool operator<=(const ModInt &a, const ModInt &b) {
    return a.x <= b.x;
  }
  friend bool operator>=(const ModInt &a, const ModInt &b) {
    return a.x >= b.x;
  }
  static ModInt sign(long long k) {
    return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
  }

  ModInt &operator+=(const ModInt &m) {
    x += m.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &m) {
    x -= m.x;
    if (x < 0LL)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &m) {
    x = (1LL * x * m.x) % MOD;
    return *this;
  }

  friend ModInt operator-(const ModInt &a) {
    ModInt res(a);
    if (res.x)
      res.x = MOD - res.x;
    return res;
  }
  friend ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += ModInt(b);
  }
  friend ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= ModInt(b);
  }
  friend ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b);
  }

  static long long fp(long long u, long long k) {
    long long res = 1LL;
    while (k > 0LL) {
      if (k & 1LL)
        res = (res * u) % MOD;
      u = (u * u) % MOD;
      k /= 2LL;
    }
    return res;
  }

  static constexpr int mod() { return MOD; }

  ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
  ModInt inv() {
    assert(x);
    return ModInt(fp(x, MOD - 2));
  }
  ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
  friend ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b).inv();
  }

  friend ostream &operator<<(ostream &out, const ModInt &a) {
    return out << a.x;
  }
  friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};

const int MOD = 998244353;
using Mint = ModInt<MOD>;

const int MAXN = 3001;

Mint dp[MAXN][MAXN];

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

  int N, K;
  cin >> N >> K;
  dp[0][0] = 1;
  for (int res(1); res < MAXN; ++res)
    for (int nec(MAXN - 1); nec >= 1; --nec) {
      dp[res][nec] =
          dp[res - 1][nec - 1] + (2 * nec < MAXN ? dp[res][2 * nec] : 0);
    }
  cout << dp[N][K] << endl;
}


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

typedef vector <int> vi;
typedef pair <int, int> pii;
typedef vector <long long> vll;
typedef pair <long long, long long> pll;
#define pb push_back
#define all(c) c.begin(), c.end()
#define For(i, a, b) for (long long i = a; i < b; ++i)
#define Forr(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define ll long long
#define endl "\n"
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) {cout << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) {const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

const int N = 2010;
vector <pii> graph[N];
bool visited[N];
int dist[N];

int dijkstra(int org) {
	priority_queue <pii, vector <pii>, greater <pii>> pq;
	For(i,0,N) dist[i] = INT_MAX;
	memset(visited, 0, sizeof(visited));
	pq.push({0, org});
	bool f = 0;
	while (!pq.empty()) {
		int source = pq.top().S, d = pq.top().F;
		pq.pop();
		if (f and source == org) return d;
		f = 1;
		if (visited[source]) continue;
		visited[source] = 1;
		for (pii a : graph[source]) {
			if (dist[a.F] < d + a.S) continue;
			dist[a.F] = d + a.S;
			pq.push({d + a.S, a.F});
		}
	}
	return -1;
}

void solve() {
	int n,m; cin >> n >> m;
	For(i,0,m) {
		int u,v,c; cin >> u >> v >> c;
		graph[u].pb({v,c});
	}
	For(i,1,n+1) {
		cout << dijkstra(i) << endl;
	}
}

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

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
#define int long long int
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define deb(x) cout<< ">" << #x << '=' << x << endl;
#define read(v) for(auto &it:v) cin>>it;
const int  N = 2e5 + 5;
void solve() {
	int n, m, t;
	cin >> n >> m >> t;
	int prev = 0;
	int charge = n;
	int last;
	for (int i = 0; i < m; i++) {
		int l, r;
		cin >> l >> r;
		charge -= (l - prev);
		if (charge <= 0) {
			cout << "No" << endl;
			return;
		}
		charge += (r - l);
		charge = min(charge, n);
		prev = r;
	}
	charge -= (t - prev);
	if (charge > 0) {
		cout << "Yes" << endl;
		return;
	}
	else {
		cout << "No" << endl;
	}

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


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<iostream>

#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define N 500005

using namespace std;

int n, a[N], id[N], vis[N], Count[N];

int cmp(int x, int y)
{
	return a[x] > a[y];
}

int main()
{
	scanf("%d", &n); n <<= 1;
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), id[i] = i;
	sort(id + 1, id + n + 1, cmp);
	for (int i = 1; i <= n / 2; ++i) vis[id[i]] = 1;
	for (int i = 1; i <= n; ++i)
	{
		if (!Count[vis[i] ^ 1]) printf("("), Count[vis[i]] ++;
		else Count[vis[i] ^ 1] --, printf(")");
	}
}

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

ll mod = 998244353;

int main() {
    ll N; cin >> N;
    vector<Pr> seq;
    rep(i,2*N){
        ll a; cin >> a;
        seq.push_back(make_pair(a,i));
    }
    sort(seq.begin(),seq.end());
    int small[2*N];
    rep(i,2*N) small[i] = 0;
    rep(i,N){
        int k = seq[i].second; small[k] = 1;
    }
    ll cnt[2];
    rep(i,2) cnt[i] = 0;
    rep(i,2*N){
        if(cnt[small[i]]<cnt[(small[i]^1)]) cout << ")";
        else cout << "(";
        cnt[small[i]]++;
    }
    cout << endl;
    //cout << ans << endl;
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

	int N, M;
	cin >> N >> M;

	int A[1001], B[1001];
	char C[1001];
	rep(i, M) {
		cin >> A[i] >> B[i] >> C[i];
		A[i]--;
		B[i]--;
	}
	vector<int> E[1000010];
	rep(i, M) rep(j, M) {
		if (C[i] != C[j]) continue;
		if (i == j) continue;

		E[A[i] * N + A[j]].pb(B[i] * N + B[j]);
		E[A[i] * N + B[j]].pb(B[i] * N + A[j]);
		E[B[i] * N + A[j]].pb(A[i] * N + B[j]);
		E[B[i] * N + B[j]].pb(A[i] * N + A[j]);
	}

	int D[1000100] = {};
	rep(i, N* N) D[i] = 1e9;

	queue<int> que;
	que.push(N - 1);
	D[N - 1] = 0;
	while (que.size()) {
		int u = que.front();
		que.pop();

		for (auto to : E[u]) {
			if (D[to] < 1e8) continue;
			chmin(D[to], D[u] + 1);
			que.push(to);
		}
	}

	//if (D[(N - 1) * N] > 1e8) co(-1);
	//else co(D[(N - 1) * N]);

	int kotae = 1e9;
	rep(i, N) chmin(kotae, D[i * N + i] * 2);
	rep(i, M) chmin(kotae, D[A[i] * N + B[i]] * 2 + 1);
	rep(i, M) chmin(kotae, D[B[i] * N + A[i]] * 2 + 1);

	if (kotae > 1e8) co(-1);
	else co(kotae);

	Would you please return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	string t = "";
	cin >> s;
	int f = 1;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 'R') f *= -1;
		else if (f == 1) {
			if (*t.end() == s[i]) t.pop_back();
			else t.push_back(s[i]);
		}
		else if (f == -1) {
			if (*t.begin() == s[i]) t.erase(0, 1);
			else t = s[i] + t;
		}
	}
	if (f == -1) reverse(t.begin(), t.end());
	for (int i = 0; i < t.size() - 1; i++) {
		if (t.size() == 0) break;
		while (t[i] == t[i + 1] && t.size()) {
			t.erase(i, 2);
			if (i > 0) i--;
		}
	}
	cout << t << endl;
}


#include <bits/stdc++.h>
#define LL long long
#define st first
#define nd second

const LL LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;

template <class T> T read(T &a) {
    a=0;char x=getchar();bool f=0;
    for(;x<'0'||x>'9';x=getchar())f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
    if(f)a=-a;
    return a;
}
template <class T> void write(T a) {
    static char s[40];int len=-1;
    if(a<0)putchar('-'),a=-a;
    if(a==0)putchar('0');
    for (int i=0;i<40;++i)s[i]=0;
    for(;a!=0;s[++len]=a%10+'0',a/=10);
    for(int i=len;i>=0;--i)putchar(s[i]);
}
template <class T> void writeln(T a) {
    write(a);
    puts("");
}

int n, k;

int main() {
    read(n), read(k);
    if (n == 1 && k == 0) {
        puts("1 2");
        return 0;
    }
    if (k < 0 || k > n - 2) {
        puts("-1");
        return 0;
    }
    printf("%d %d\n", 1, (k + 2) * 2);
    for (int i = 1; i <= k + 1; ++i) {
        printf("%d %d\n", i * 2, i * 2 + 1);
    }
    for (int i = k + 3; i <= n; ++i) {
        printf("%d %d\n", i * 2, i * 2 + 1);
    }
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define SZ(x) (int)x.size()
#define pb push_back
#define FULL(x, y) memset(x, y, sizeof(x))
using namespace std;

const int N = 1000005, M = 1005;
int n, m;
vector<int> c[26], g[N];
int f[M][M], vis[N], dis[N];

void add(int a, int b, int u, int v) {
	int x = a*n + u, y = b*n + v;
	g[x].pb(y), g[y].pb(x);
	x = b*n + u, y = a*n + v;
	g[x].pb(y), g[y].pb(x);
	x = b*n + v, y = a*n + u;
	g[x].pb(y), g[y].pb(x);
	x = a*n + v, y = b*n + u;
	g[x].pb(y), g[y].pb(x);
}

int main() {
	cin>> n >> m;
	int a, b, u, v;
	char ch;
	for(int i=1; i<=m; ++i) {
		cin>> a >> b >> ch;
		u = a - 1, v = b - 1;
		f[u][v] = 1, f[v][u] =1;
		c[ch-'a'].pb(u*n+v);
	}
	for(int i=0; i<26; ++i) {
		int len = SZ(c[i]);
		for(int j=0; j<len; ++j) {
			for(int k=j+1; k<len; ++k) {
				a = c[i][j]/n, b = c[i][j] - a*n;
				u = c[i][k]/n, v = c[i][k] - u*n;
				add(a, b, u, v), add(u, v, a, b);
			}
		}
	}
	queue<int> q;
	q.push(n-1);
	vis[n-1] = 1;
	dis[n-1] = 0;
	while(!q.empty()) {
		int fr = q.front();
		q.pop();
		for(int i=0; i<SZ(g[fr]); ++i) {
			v = g[fr][i];
			if (!vis[v]) {
				dis[v] = dis[fr] + 1;
				vis[v] = 1;
				q.push(v);
			}
		}
	}
	int ans = -1;
	for(int i=0; i<n; ++i) {
		v = i*n + i;
		if (vis[v] && (ans == -1 || (dis[v]*2 < ans))) {
			ans = dis[v] * 2;
		}
	}
	for(int i=0; i<n ;++i) {
		for(int j=0; j<n; ++j) {
			v = i*n + j;
			if (f[i][j] && vis[v] && (ans == -1 || (dis[v]*2 + 1 < ans))) {
				ans = dis[v]*2 + 1;
			}
		}
	}
	cout<< ans << endl;
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<pll> vpll;
template<typename T> void makeunique(vector<T> &vx) {sort(vx.begin(),vx.end());auto it=unique(vx.begin(),vx.end());vx.resize(std::distance(vx.begin(),it));}

//If w[L,R] >= v[i], then dist[L,R] >= len[i]

int f[1<<8];
#define FORSSI(sub, full) for(int sub = 0 ;sub!=(full)  ; sub = (sub - (full)) & (full))

int hquery(map<int,int> &h, int key) {
  //Queries the largest (k,v) pair where k<=key
  auto it=h.upper_bound(key);
  if (it==h.begin()) {
    return 0;
  }
  --it;
  return it->snd;
}

void hins(map<int,int> &h, int key, ll val) {
  //Inserts (key,val) into h.
  //Maintains the invariant that if key1<key2, then val1<val2.
  auto it=h.upper_bound(key);
  if (it==h.begin()) {
    it=h.insert(MP(key,val)).fst;
  }
  else {
    --it;
    if (it->snd >= val) return;
    if (it->fst==key) it->snd = val;
    else it=h.insert(MP(key,val)).fst;
  }
  ++it;
  while(it!=h.end()) {
    if (it->snd <= val) it=h.erase(it);
    else return;
  }
}

ll solve(long long N, long long M, std::vector<long long> w, std::vector<long long> vlen, std::vector<long long> v){
  {
    ll wmax=0; for (auto &x:w) chkmax(wmax,x);
    ll vmin=LLONG_MAX; for (auto &x:v) chkmin(vmin,x);
    if (wmax>vmin) return -1;
  }
  map<int,int> h;
  for (int i=0;i<M;i++) hins(h,v[i],vlen[i]);
  vi perm; for (int i=0;i<N;i++) perm.PB(i);
  int final=INT_MAX;
  do {
    memset(f,0,sizeof f);
    for (int l=0;l<N;l++) {
      int sumw=w[perm[l]];
      int sig=0;
      for (int r=l+1;r<N;r++) {
        sumw+=w[perm[r]];
        int dist=hquery(h,sumw-1);
        sig|=1<<(r-1);
        //if(dist) printf("l:%d r:%d sig:%d dist:%d\n",l,r,sig,dist);
        f[sig]=dist;
      }
    }
    int lim=1<<N;
    for (int x=3;x<lim;x++) {
      int best=f[x];
      FORSSI(y,x) {
        chkmax(best,f[y]+f[x^y]);
      }
      //if(best) printf("x:%d best:%d\n",x,best);
      f[x]=best;
    }
    chkmin(final,f[lim-1]);
  } while(next_permutation(perm.begin(),perm.end()));
  return final;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> w(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&w[i]);
    }
    std::vector<long long> l(M);
    std::vector<long long> v(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&l[i]);
        scanf("%lld",&v[i]);
    }
    ll ans=solve(N, M, std::move(w), std::move(l), std::move(v));
    printf("%lld\n",ans);
    return 0;
}

// RioTian 21/03/24
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int n;
    cin >> n;
    vector<pair<ll, ll>> towns;
    ll sa = 0, sb = 0;  // T,A
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        towns.emplace_back(a, b);
        sa += a;
    }
    sort(towns.begin(), towns.end(), [](pair<ll, ll>& p, pair<ll, ll>& q) {
        return p.first * 2 + p.second > q.first * 2 + q.second;
    });
    for (int i = 0; i < n; ++i) {
        if (sa < sb) {
            cout << i << "\n";
            return 0;
        }
        sa -= towns[i].first, sb += towns[i].first + towns[i].second;
    }
    cout << n << "\n";
    return 0;
}


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

int main(){

  long long N;
  cin >> N;

  long long ans = 0;

  for(int i = 1; i <= 5; i++){
    if(N >= pow(10, 3*i)){
      ans += (N-pow(10, 3*i)+1);
    }
  }

  cout << ans << endl;

}

// clang-format off
#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;

#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;

#define INF 1000000000000000000
#define inf 1000000000
// #define MOD  1000000007
#define MOD 998244353
#define PI 3.1415926535897932385

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }

ld modp(ld b, ll e) {
    ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }

void solve()
{
    // START

    string str;
    cin>>str;
    ll ans=0;
    for(int i=0;i<=str.length()-4;i++){
        string x = str.substr(i,4);
        if(x=="ZONe")ans++;
    }
    cout<<ans<<"\n";

    // END
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);   cout<<fixed;
    #ifdef godfather
        cin.exceptions(cin.failbit);
        freopen("little.in", "r", stdin);
        freopen("little.out", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    fr(i,1,t)
    {
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    end_routine();
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
int to_int(char c) {
	if(c == 'R') return 0;
	if(c == 'W') return 1;
	if(c == 'B') return 2;
	assert(false);
}
char to_char(int x) {
	if(x == 0) return 'R';
	if(x == 1) return 'W';
	if(x == 2) return 'B';
	assert(false);
}
int C(int x, int y) {
	int ans = 1;
	while(x > 0) {
		if(x%3 < y%3) return 0;
		if(y%3 == 1 && x%3 == 2) ans = ans * 2 % 3;
		x /= 3;
		y /= 3;
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int n;
	string s;
	cin >> n >> s;
	ll ans = 0, pw = 1;
	for(int i = 1; i < n; ++i) {
		pw = pw * 2 % 3;
	}
	for(int i = 0; i < n; ++i) {
		ans += C(n-1, i) * to_int(s[i]) * pw;
		ans %= 3;
	}
	cout << to_char(ans) << endl;
}

#include <bits/stdc++.h>
#define rep(i, a, b)   for(int i = a; i <= b; ++i)
#define repr(i,a,b)    for(int i = a ; i >= b; --i)
#define NAME           "NAME"
#define pb             push_back
#define mp             make_pair
#define EL             cout << '\n'
#define cqtshadow      int main
#define fillchar(a,x)  memset(a, x, sizeof (a))
#define faster         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
typedef int64_t        ll;
typedef vector<int>    vi;
typedef vector<ll>     vl;
typedef pair<int, int> ii;
typedef pair<ll, ll>   iil;
const ll  oo  =  1e18;
const int inf =  1e9;
const int mod =  3;
const int N   =  4e5 + 7;
const int M   =  1e6 + 7;
void file() {freopen ( NAME ".inp", "r", stdin ); freopen ( NAME ".out", "w", stdout ); freopen ( NAME ".err", "w", stderr);}

int n, c[N], C_[5][5];
string s;

void cal() {
	rep(i, 0, 3)
	rep(j, 0, i)
		if(i == 0 || j == 0) C_[i][j] = 1;
		else C_[i][j] = (C_[i - 1][j]%mod + C_[i - 1][j - 1]%mod)%mod;
}

int C(int k, int n) {
	if(k < mod && n < mod) return C_[n][k];
	return (C(k/mod, n/mod)%mod * C_[n%mod][k%mod])%mod;
}

cqtshadow () {
	faster
	cin >> n >> s;
	rep(i, 1, n) {
		if(s[i - 1] == 'W') c[i] = 0;
		if(s[i - 1] == 'R') c[i] = 1;
		if(s[i - 1] == 'B') c[i] = 2;
	}
	cal();
	int res = 0;
	rep(i, 1, n) {
		res = (res + (c[i] * C(i - 1, n - 1)%mod)%mod)%mod;
	}
	if(n%2 == 0) res *= -1;
	res = (res%mod + mod*1000000)%mod;
	if(res == 0) cout << "W";
	if(res == 1) cout << 'R';
	if(res == 2) cout << "B";
}


#include<bits/stdc++.h>

using namespace std;

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

#define mp make_pair
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(v) (v).begin(), (v).end()
#define endl "\n"
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#define files(name) ifstream fin(name".in"); ofstream fout(name".out");
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }

const int mod = 1e9 + 7;
const int mxN = 101;

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

    int n;
    cin >> n;
    int ans=n/100;
    if(ans*100<n) ++ans;
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define w(x) int  x; cin>>x; while(x--)
void basics()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

}
int32_t main()
{
	basics();
	int year;
	cin >> year;
	float ans = (float)year / 100;
	int yr = (int)ans;
	// cout << ans;
	if ((float)yr == ans)
		cout << yr;
	else
		cout << yr + 1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
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) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 2e5+10;

int k, factorial[17], h[maxn][17], p[17];
int tot = 0, nCr[17][17];
vi dp(16, 0);

void add(int &ans, int add){
	add %= MOD;
	ans += add;
	if(ans >= MOD) ans -= MOD;
	if(ans < 0) ans += MOD;
}

void calc(){
	p[0] = 0;
	fo(i, 1, 16){
		p[i] = i;
	}
	h[0][0] = 1;
	fo(n, 1, maxn - 1){
		fo(x, 0, k - 1){
			if(n < x) continue;
			int &ans = h[n][x];
			for (int i = 0; i <= x; ++i)
			{
				int mul = 1;
				if(i % 2) mul = -1;
				add(ans, mul * nCr[x][i] * p[k - i]);
			}
		}
		fo(i, 1, 16){
			p[i] = (p[i] * i) % MOD;
		}
	}
}

int find(int n, int x){
	if(x < 0) return 0;
	if(n == 0){
		if(x == 0) return 1;
		return 0;
	}
	assert(x <= k);
	int ans = nCr[16 + x - k][x];
	// show(n, x, ans, h[n][x]);
	ans =(ans * h[n][x]) % MOD;
	return ans;
}

void increase(int id){
	dp[id]++;
	if(dp[id] == 1) tot++;
}

void decrease(int id){
	dp[id]--;
	if(dp[id] == 0) tot--;
}

int32_t main(){
    fast_io;
    string st;
    cin >> st >> k;
    factorial[0] = 1;
    fo(i, 1, 16) factorial[i] = (factorial[i - 1] * i);
    for (int i = 0; i < 17; ++i)
    {
    	for (int j = 0; j < i + 1; ++j)
    	{
    		nCr[i][j] = (factorial[i]) / (factorial[j] * factorial[i - j]);
    		nCr[i][j] %= MOD;
    	}
    }
    calc();
    vi v;
    for(const auto &it: st){
    	if(it >= '0' and it <= '9') v.emplace_back(it - '0');
    	else v.emplace_back(10 + it - 'A');
    }
    int ans = 0, rem = v.size();
    for(auto it: v){
    	if(tot > k) break;
    	rem--;
    	for (int i = 0; i < it; ++i)
    	{
    		increase(i);
    		if(tot == 1 and dp[0] > 0){
    		}
    		else add(ans, find(rem, k - tot));
    		decrease(i);
    	}
    	increase(it);
    }
    if(tot == k) add(ans, 1);
    // show(ans);
    rem = v.size() - 1;
    fo(i, 1, v.size() - 1){
    	rem--;
    	add(ans, 15 * find(rem, k - 1));
    }
    show(ans);
    return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1) res *= x;
  res %= mod;
  return res;
}

inline ll mod(ll a, ll m) {
  return (a % m + m) % m;
}

ll ext_gcd(ll a, ll b, ll& p, ll& q) {
  if (b == 0) {
    p = 1;
    q = 0;
    return a;
  }
  ll d = ext_gcd(b, a % b, q, p);
  q -= a / b * p;
  return d;
}

P crt(ll b1, ll m1, ll b2, ll m2) {
  ll p, q;
  ll d = ext_gcd(m1, m2, p, q);
  if ((b2 - b1) % d != 0) return {0, -1};
  ll m = lcm(m1, m2);
  ll tmp = (b2 - b1) / d * p % (m2 / d);
  ll r = mod(b1 + m1 * tmp, m);
  return make_pair(r, m);
}

signed main() {
  init();
  ll T;
  cin >> T;
  while (T--) {
    ll N;
    cin >> N;
    vs S(3);
    cin >> S;
    cout << 1;
    rep(i, N) cout << 0;
    rep(i, N) cout << 1;
    ENDL;
  }

  return 0;
}


// Problem: C - Swappable
// Contest: AtCoder - AtCoder Beginner Contest 206（Sponsored by Panasonic）
// URL: https://atcoder.jp/contests/abc206/tasks/abc206_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

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

#define ll long long
#define ld long double
#define fr(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define mp make_pair
#define vl vector<ll>
#define mll map<ll,ll>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int,int>
#define tc int t;cin>>t;while(t--)
#define FILE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inp(a) long long a;cin>>a;
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
const ll mod=1e9+7;
const ll N=4e5+5;

#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

void solve(){
	ll n; cin>>n;
	map<ll,ll> m;

	fr(i,n){
		ll x; cin>>x;
		m[x]++;
	}
	ll ans=0, temp=n;
	for(auto i:m){
		ll j=i.second;
		temp-=j;
		ans+=j*temp;
	}
	cout<<ans;
}

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

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

int main() {
    int N;
    scanf("%d",&N);
    map<int,int> M;
    static int A[(int)3e5+16];
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
        M[A[i]]++;
    }
    ll result=0;
    for(int i=0;i<N;i++){
        result+=N-M[A[i]];
    }
    printf("%lld",result/2);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define mp(x,y) make_pair(x,y)

// input

void read(ll a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%ll", &a[i]);
	}
}

void read(int a[], int s, int n) {
	for (int i = 0 + s; i < n + s; ++i) {
		scanf("%d", &a[i]);
	}
}

// fenwick

ll getSum(ll bits[], int index)
{
	ll sum = 0;
	while (index > 0)
	{
		sum += bits[index];
		index -= index & (-index);
	}
	return sum;
}

void updateAt(ll bits[], int n, int index, ll val)
{
	while (index <= n)
	{
		bits[index] += val;
		index += index & (-index);
	}
}

static const int MX_VAL = 1e6 + 2;
ll bits[MX_VAL + 1] = {};

// inversions

ll getInvCnt(ll a[], int n)
{
	ll cnt = 0;
	memset(bits, 0, sizeof(bits));
	for (int i = n - 1; i >= 0; i--)
	{
		cnt += getSum(bits, a[i] - 1);
		updateAt(bits, MX_VAL, a[i], 1);
	}

	return cnt;
}

// MODFIY N!!!
const int N = 1 * 1e6 + 2;

// MODFIY MOD!!!
const int MOD = 1e9 + 7;

long long fac[N], rfac[N];

long long mul(long long a, long long b) { return 1LL * a * b % MOD; }

long long fpow(long long a, long long b) {
	if (b == 0) return 1;
	long long re = fpow(a, b / 2);
	if (b % 2 == 1) return mul(a, mul(re, re));
	return mul(re, re);
}

long long inv(long long x) {
	return fpow(x, MOD - 2);
}

int nck(int x, int y) {
	return mul(fac[x], mul(rfac[y], rfac[x - y]));
}

void prepareFac()
{
	fac[0] = 1;
	rfac[0] = inv(1);
	for (int i = 1; i < N; ++i) {
		fac[i] = i * fac[i - 1] % MOD, rfac[i] = inv(fac[i]);
	}
}

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

int a[N], b[N];
string s[2000];
vector<pii> v[256];
int vis[2000 * 2000];
int visa[256];
int ind(pii pii) {
	return pii.first * 2000 + pii.second;
}

int n, m, cost; queue<pii> q;
void go(int x, int y) {
	if (x < 0 || x == n)
		return;
	if (y < 0 || y == m)
		return;
	auto c = mp(x, y);
	if (vis[ind(c)])
		return;
	if (s[x][y] == '#')
		return;
	q.push(c);
	vis[ind(c)] = cost;
}

void solve_case()
{

	cin >> n >> m;
	pii start, finish;

	for (int i = 0; i < n; ++i) {
		cin >> s[i];
		for (int j = 0; j < s[i].length(); ++j)
			if (s[i][j] == 'S')
				start = mp(i, j);
			else if (s[i][j] == 'G')
				finish = mp(i, j);
			else if (s[i][j] >= 'a' && s[i][j] <= 'z')
				v[s[i][j]].push_back(mp(i, j));
	}

	q.push(start);
	vis[ind(start)] = 1;
	while (!q.empty()) {
		auto c = q.front(); q.pop();
		if (c == finish) {
			cout << vis[ind(c)] - 1;
			return;
		}
		cost = vis[ind(c)] + 1;
		go(c.first - 1, c.second);
		go(c.first + 1, c.second);
		go(c.first , c.second + 1);
		go(c.first, c.second - 1);
		int x = c.first;
		int y = c.second;
		if (s[x][y] >= 'a' && s[x][y] <= 'z' && !visa[s[x][y]]) {
			visa[s[x][y]] = true;
			for (auto p : v[s[x][y]]) {
				if (!vis[ind(p)]) {
					vis[ind(p)] = cost;
					q.push(p);
				}
			}
		}

	}
	cout << -1;
	cout << endl;
}

void prepare()
{
}

int main()
{
#ifdef __OLIMP__
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int T = 1;
	// scanf("%d", &T);
	//prepare();
	for (int tc = 1; tc <= T; tc++)
		solve_case();

	return 0;
}

#include <bits/stdc++.h>

#define pb push_back
#define db1(x) cout << #x << " = " << x << endl
#define db2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define paging cout << "-----------------------" << endl

using namespace std;
void localFileInput() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
const ll mod = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 2010;
int n, m;
char s[N][N];
bool vis[N][N];
bool use[30];
int f[N][N];
vector<pii> v[30];
pii st;
bool check(int x, int y) {
    return (x>=0&&x<n) && (y>=0&&y<m) && !vis[x][y] && s[x][y] != '#';
}
int bfs() {
    int x = st.first;
    int y = st.second;
    vis[x][y] = true;
    f[x][y] = 0;
    queue<pii> que; que.push(st);
    while(!que.empty()) {
        pii p = que.front(); que.pop();
        x = p.first;
        y = p.second;
        if(s[x][y] == 'G') return f[x][y];
        if(islower(s[x][y])) {
            int t = s[x][y] - 'a';
            if(!use[t]) {
                use[t] = true;
                for (pii val : v[t]) if(!vis[val.first][val.second]) {
                    vis[val.first][val.second] = true;
                    f[val.first][val.second] = f[x][y] + 1;
                    que.push(val);
                }
            }
        }
        for (int i = 0; i < 4; ++ i) {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if(check(nx, ny)) {
                vis[nx][ny] = true;
                f[nx][ny] = f[x][y] + 1;
                que.push({nx, ny});
            }
        }
    }
    return -1;
}
void sol() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++ i) scanf("%s", s[i]);
    for (int i = 0; i < n; ++ i) for (int j = 0; j < m; ++ j) {
        if(s[i][j] == 'S') st = {i, j};
        if(islower(s[i][j])) {
            v[s[i][j]-'a'].pb({i, j});
        }
    }
    printf("%d\n", bfs());
}
int main()
{
    localFileInput();
    // int _; scanf("%d", &_); while(_ --)
    sol();
    return 0;
}


//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll int
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);

int n,p;
int ans=0;
void rec(vector<int> a)
{
    if(a.size()==n)
    {
        bool ok=true;
        int sum=0;
        fi(0,n)
        {
            sum+=a[i];
            if(sum%p==0) ok=false;
        }
        if(ok)
        {
            ans++;
            for(auto x:a)
            {
                cout<<x<<' ';
            }
            cout<<'\n';
        }
        return ;
    }
    fi(1,p)
    {
        a.pb(i);
        rec(a);
        a.pop_back();
    }
}

const int mod=1e9+7;
const int mxn=1e6+3;

ll binpow(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1){
            res=((res%mod)*(a%mod))%mod;
        }
        a=((a%mod)*(a%mod))%mod;
        b=b>>1;
    }
    return res%mod;
}

void solve()
{
    cin>>n>>p;
    // rec({});
    // cout<<ans<<'\n';
    cout<<((p-1)*binpow((p-2),n-1))%mod<<'\n';

}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;
    while(tt--)
    {
        solve();
    }
}

//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1).
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
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; }

long long pow(long long x, long long n, long long MM) {
    long long ret = 1;
    while (n > 0) {
        if (n & 1) ret = ret * x % MM;  // n の最下位bitが 1 ならば x^(2^i) をかける
        x = x * x % MM;
        n >>= 1;  // n を1bit 左にずらす
    }
    return ret;
}

int main(){
  ll N,P;
  cin>>N>>P;
  cout << (P-1)*pow(P-2,N-1,MOD)%MOD << endl;
  return 0;

}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std;
int main()
{
   ll i,j,k,T,c=0,n;
   cin>>n>>T;
   ll arr[n];
   for(i=0;i<n;i++)
   {
       cin>>arr[i];
   }

  vector<ll>vec1,vec2,vec3;

   for(i=0;i<n/2;i++)
   {
       vec1.pb(arr[i]);
   }

   for(i=n/2;i<n;i++)
   {
       vec2.pb(arr[i]);
   }

   n=vec1.size();

   ll ans=0;

   for(i=0;i<(1LL<<n);i++)
   {
      c=0;
      for(j=0;j<n;j++)
      {
        if(i&(1LL<<j))
        {
           c+=vec1[j];
        }
      }

      if(c<=T)
      {
       vec3.pb(c);
      }
   }

   sort(vec3.begin(),vec3.end());
   ans=vec3[vec3.size()-1];
   c=0;

   n=vec2.size();

   for(i=0;i<(1LL<<n);i++)
   {
      c=0;
      for(j=0;j<n;j++)
      {
        if(i&(1LL<<j))
        {
           c+=vec2[j];
        }
      }

        ll id=lower_bound(vec3.begin(),vec3.end(),T-c)-vec3.begin();
          if(c<=T)
          ans=max(ans,c);

           if(id==vec3.size())
            ans=max(ans,c+vec3[id-1]);
          else if(vec3[id]==T-c)
            ans=max(ans,T);
          else if(id>0)
            ans=max(ans,c+vec3[id-1]);
      }

   cout<<ans;

return 0;

}

#include<bits/stdc++.h>
#define re register
#define ll long long
#define dl double
#define LL inline ll
#define I inline int
#define V inline void
#define B inline bool
#define FOR(i,a,b) for(re int i=(a),i##i=(b) ; i<=i##i ; ++i)
#define ROF(i,a,b) for(re int i=(a),i##i=(b) ; i>=i##i ; --i)
#define gc getchar()
//#define gc (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++
using namespace std;
const int N=4e5+10,inf=1e9+7;
char *fs,*ft,buf[1<<18];
LL read(){
    ll p=0; bool w=0; char ch=gc;
    while(!isdigit(ch)) w=ch=='-'?1:0,ch=gc;
    while(isdigit(ch)) p=p*10+ch-'0',ch=gc;
    return w?-p:p;
}
ll n,us1=3,us2=5,as1,as2;
map<ll,int>mp;
int main(){
	n=read();
	FOR(i,1,50){
		mp[us1]=i,us1*=3;
		if(us1>n) break;
	}
	FOR(i,1,50){
		if(mp[n-us2]){
			cout<<mp[n-us2]<<' '<<i;
			return 0;
		}
		us2=us2*5;
		if(us2>n) break;
	}
	cout<<-1;
	return 0;
}


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

#define inf 1001000009
#define infmx 1e18ll
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define all(x) x.begin(),x.end()
///debug
#define wh(x)   cerr<<#x<<" is "<<x<<endl;
#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){cout<<endl;}
template<class H,class... T>void err(istream_iterator<string>it,H a,T... t){cerr<<*it<<" = "<<a<<" | ";err(++it,t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class T>auto&operator<<(ostream& os,vector<T>_v){bool a=1;for(auto x:_v){os<<(a?"":" ")<<x;a=0;}return os;}
///
typedef pair<int,int>pi;
typedef pair<long long,long long>pll;
typedef pair<pi,int>ppi;
typedef pair<int,pi>pip;
typedef vector<int>vi;
typedef vector<pi>vpi;
const int mod=1e9+7;
ll power(ll a,ll p){ll r=1,y=a;while(p){if(p&1)r=(1LL*r*y)%mod;p>>=1;y=(1LL*y*y)%mod;}return r;}

const int N=2100000;
int ara[N];

int main()
{
    IOS;
    double tol=0.0000001;
    double sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    if(sx>gx)
    {
        swap(sx,gx);
        swap(sy,gy);
    }
    double l=sx,r=gx;
    while(l<=r){
        double mid=(l+r)/2;

        if((sy/(mid-sx))>=(gy/(gx-mid)))l=mid+tol;

        else r=mid-tol;
    }
    cout<<setprecision(8)<<r<<endl;
}

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 998244353
using namespace std;
using ll = long long;

template<int M>
class ModInt {
   public:
    ll value;
    constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; }
    constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; }
    constexpr ModInt inv() const { return this->pow(M - 2); }
    constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; };
    constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; };
    constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; };
    constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; };
    constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; };
    constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value <  0) value += M; return *this; };
    constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; };
    constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); };
    constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; }
    constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; }
    friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; }
};

using mint = ModInt<MOD>;

int dx[] = {1, 0};
int dy[] = {0, 1};

vector<vector<char>> G(5000, vector<char>(5000, '*'));
vector<vector<mint>> dp(5001, vector<mint>(5001, 0));

int main() {
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int H, W, K; cin >> H >> W >> K;

    REP(i, 0, K) {
        int h, w; char c; cin >> h >> w >> c;
        G[--h][--w] = c;
    }

    dp[0][0] = mint(3).pow(H * W - K);
    mint inv = mint(3).inv();
    REP(i, 0, H) {
        REP(j, 0, W) {
            REP(d, 0, 2) {
                if (d == 0 && G[i][j] == 'R') continue;
                if (d == 1 && G[i][j] == 'D') continue;
                int ni = i + dx[d];
                int nj = j + dy[d];
                if (G[i][j] == '*') {
                    dp[ni][nj] += (dp[i][j] * 2 * inv);
                } else {
                    dp[ni][nj] += dp[i][j];
                }
            }
        }
    }
    cout << dp[H - 1][W - 1] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, M;
  cin >> N >> M;
  vector<P> B(M);
  REP(i, M) cin >> B[i].first >> B[i].second;
  sort(ALL(B));
  set<int> st;
  set<int> ins;
  set<int> del;
  st.insert(N);
  int lastx = 0;
  REP(i, M) {
    int x = B[i].first;
    int y = B[i].second;
    if(x != lastx) {
      for(int z : del) st.erase(z);
      for(int z : ins) st.insert(z);
      del.clear();
      ins.clear();
      lastx = x;
    }
    if(st.find(y) != st.end()) del.insert(y);
    if(st.find(y+1) != st.end() || st.find(y-1) != st.end()) ins.insert(y);
  }
  for(int z : del) st.erase(z);
  for(int z : ins) st.insert(z);
  cout << st.size() << endl;
}

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, t;
    cin >> n >> t;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    vector<int> b, c;
    rep(i,n/2) b.push_back(a[i]);
    for(int i = n/2; i < n; i++) c.push_back(a[i]);
    vector<ll> sum1, sum2;
    int n2 = n/2;
    rep(i,1<<n2){
        ll tmp = 0;
        rep(j,n2) if(i>>j&1) tmp += b[j];
        sum1.push_back(tmp);
    }
    rep(i,1<<(n-n2)){
        ll tmp = 0;
        rep(j,n-n2) if(i>>j&1) tmp += c[j];
        sum2.push_back(tmp);
    }
    ll ans = 0;
    sort(sum1.begin(), sum1.end());
    sort(sum2.begin(), sum2.end());
    rep(i,sum1.size()){
        int id = upper_bound(sum2.begin(), sum2.end(), t-sum1[i]) - sum2.begin();
        if(id == 0) continue;
        ll tmp = sum1[i] + sum2[id-1];
        ans = max(tmp, ans);
    }

    cout << ans << endl;
}


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

#define ll long long

int main() {
	string X;      //原串
	ll M;		   //最大
	cin >> X >> M;
	int n = X.size();
	vector<ll> b(n);   //n个向量(已经定型) 每个都是long long  b数组

	ll m = 0;
	for (int i = 0; i < n; i++) {
		b[i] = X[i] - '0';	  //转数字
		m = max(m, b[i]);     //原串最大的数字
	}

	auto test = [&](ll N, ll k) {     //把N转k进制
		vector<ll> a(0);      //向量口袋，虽是0个，但是向量可以扩大
		while (N > 0) {       //模拟短除(转进制)
			a.push_back((N % k));
			N /= k;
		}
		reverse(a.begin(), a.end());  //正过来
		if (a.size() < X.size())      //比原串还短
			return false;             //滚
		else if (a.size() > X.size()) //阔以
			return true;
		else {
			int i = 0;
			while (i<n&& a[i] == b[i]) {        //有多少个相同的数字
				++i;
			}
			if (i == n)        //全相同
				return true;
			else if (a[i] < b[i])	  //小于原串
				return false;
			else
				return true;

		}
	};

	if (n == 1)		//串长度为1
		cout << (b[0] <= M) ? '1': '0' ;
	else if (!(test(M, m + 1)))
		cout << 0 << endl;
	else {			//二分
		ll ok = m + 1;  	//左
		ll ng = M + 1; 		//右
		ll mid;
		while (ng - ok > 1) {
			mid = (ok + ng) / 2;
			if (test(M, mid))
				ok = mid;
			else
				ng = mid;
		}
		cout << ok - m << endl;

		return 0;
	}
}

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

using namespace std;
//using namespace atcoder;

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

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

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

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

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

// edit

string X;
ull M;

bool check(ull d) {
    ull val = 0;
    for (auto c : X) {
        if (1. * val * d > 1e18) {
            return false; // too large
        }

        val *= d;
        val += (c - '0');

    }

    return val <= M;
}

void solve() {
    cin >> X >> M;

    ull lb = 1; // ok
    ull ub = M + 1; // ng
    ull tapu;

    if (X.size() == 1) {
        ll val = stoll(X);
        if (val <= M) {
            cout << 1 << endl;
        } else {
            cout << 0 << endl;
        }

        return;
    }

    for (auto c : X) {
        chmax(lb, (ull) c - '0' + 1);
    }

    tapu = lb;
    if (!check(lb)) {
        cout << 0 << endl;
        return;
    }

    while (ub - lb > 1) {
        ull md = (lb + ub) / 2;
        if (check(md)) {
            lb = md;
        } else {
            ub = md;
        }
    }

    ull ans = ub - tapu;
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
    #define ms(a) memset(a,0,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
    inline int qi(){
        int f=0,fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline ll ql(){
        ll f=0;int fu=1; char c=getchar();
        while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
        while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
        return f*fu;
    }
    inline db qd(){
        char c=getchar();int flag=1;double ans=0;
        while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
        if(c=='-') flag=-1,c=getchar();
        while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
        if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
        return ans*flag;
    }
}
namespace Read{
    #define si(a) scanf("%d",&a)
    #define sl(a) scanf("%lld",&a)
    #define sd(a) scanf("%lf",&a)
    #define ss(a) scanf("%s",a)
    #define rai(x,a,b) repi(i,a,b) x[i]=qi()
    #define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
    #define pi(x) printf("%d",x)
    #define pl(x) printf("%lld",x)
    #define ps(x) printf("%s",x)
    #define pc(x) printf("%c",x)
    #define pe() puts("")
}
namespace DeBug{
    #define MARK false
    #define DB if(MARK)
    #define pr(x) cout<<#x<<": "<<x<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
typedef long double ld;//%Lf
const db pi=acos(-1.0);
const db eps=1e-8;
inline int sgn(db x){ return fabs(x)<eps?0:(x<0?-1:1); }
struct Point{
    #define Vec Point
    db x,y;
    Point(){}
    Point(db _x,db _y){x=_x,y=_y;}
    void input(){sd(x),sd(y);}
    bool operator ==(Point b)const{return sgn(x-b.x)==0&&sgn(y-b.y)==0;}
    Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);}
    Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
    Point operator /(const db b)const{return Point(x/b,y/b);}
    Point operator *(const db b)const{return Point(x*b,y*b);}
    db operator ^(const Point &b)const{return x*b.y-y*b.x;}//叉乘
    db operator *(const Point &b)const{return x*b.x+y*b.y;}//点乘
    Point transXY(Point b,db B){//绕点b 逆时针 旋转角度B(弧度制)后的坐标
        db tx=x-b.x,ty=y-b.y;
        return Point(tx*cos(B)-ty*sin(B)+b.x,tx*sin(B)+ty*cos(B)+b.y);
    }
    db dis(Point p){ return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }//hypot(x-p.x,y-p.y)
};
int main()
{
    int n=qi();
    int x0=qi(),y0=qi(),xn2=qi(),yn2=qi();
    Point center=Point(1.0*(x0+xn2)/2,1.0*(y0+yn2)/2);
    db arc=2*pi/n;
    Point res=Point(1.0*x0,1.0*y0);
    res=res.transXY(center,arc);
    printf("%.10lf %.10lf\n",res.x,res.y);
    return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cstdio>
#include <numeric>
#include <queue>
#include <list>
#include <random>
#include <iomanip>

using namespace std;
using ll=long long;

const double PI=3.14159265358979323846;

int main()
{
  ll N;
  double x0, xN2, y0, yN2;
  cin >> N >> x0 >> y0 >> xN2 >> yN2;

  double xc, yc;
  xc = (x0+xN2)/2.0;
  yc = (y0+yN2)/2.0;

  double tx0 = x0 - xc;
  double ty0 = y0 - yc;

  double r = 360.0/N*(PI/180.0);

  double tx1 = tx0*cos(r)-ty0*sin(r);
  double ty1 = tx0*sin(r)+ty0*cos(r);

  double x1 = tx1 + xc;
  double y1 = ty1 + yc;

  cout << std::fixed << std::setprecision(8) << x1 << " " << y1 << endl;

  return 0;
}


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

// #define NDEBUG
#include<cassert>

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

const int maxn=3e5;

int a[maxn+5], n;
map<int, int>m;

signed main(){
    n=readin(1);
    rep(i, 1, n){
        a[i]=readin(1);
        ++m[a[i]];
    }
    ll ans=0;
    rep(i, 1, n){
        ans+=(n-m[a[i]]);
    }
    writc(ans>>1);
    return 0;
}

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

int main(){
    long n, g = 0;
    cin >> n;
    long a[n], b[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        g += a[i];
    }
    for(int i = 0; i < n; i++){
        cin >> b[i];
        g += b[i];
    }
    priority_queue<long, vector<long>, greater<long>> q;
    for(int i = 0; i < n; i++){
        q.push(a[n - 1 - i]);
        q.push(b[i]);
        g -= q.top();
        q.pop();
    }
    cout << g << endl;
}


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

int main() {
  long long n;
  cin >> n;
  unordered_map<int,bool> x;
  int count = 0;
  for (long long i = 2; i <= 100000 && i <= n; i++) {
    if (x.count(i) > 0) {
      continue;
    }
    long long t = i * i;
    while (t <= n) {
      if (t <= 100000) x[t] = true;
      count++;
      t *= i;
    }
  }
  cout << (n - count) << endl;
  return 0;
}

#include<iostream>
using namespace std;
int main()
{
    int N,p;
    cin>>N;
    if(1<=N && 300>=N)
    {
       p=N*1.08;
       if(p<206)
       {
           cout<<"Yay!";
       }
       else if(p==206)
       {
           cout<<"so-so";
       }
       if(p>206)
       {
           cout<<":(" ;

        }
    }

    return 0;
}


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

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

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

int main(){

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

	int n;
	cin >> n;
	string s;
	cin >> s;

	if(n<3) cout<<n;
	else{
		int temp=n;
		stack<char> st;
		for(int i=0; i<n; i++){
			if(s[i]=='x'){
				char c1='.',c2='.';
				if(!st.empty()){
					c1 = st.top();st.pop();
				}
				if(!st.empty()){
					c2 = st.top();st.pop();
				}
				// debug(i,c1,c2);
				if(c1=='o' && c2=='f'){
					temp-=3;
				}else{
					st.push(c2);
					st.push(c1);
					st.push('x');
				}
			}else{
				st.push(s[i]);
			}
		}
		cout<<temp;
	}

	return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
stack<char> st;
string s;
int n;
signed main(){
	cin>>n>>s;
	for(int i=0;i<n;i++){
		st.push(s[i]);
		if(st.size()<3) continue;
		string cur="";
		for(int j=0;j<3;j++) cur+=st.top(),st.pop();
		//cout<<cur<<"\n";
		if(cur!="xof") st.push(cur[2]),st.push(cur[1]),st.push(cur[0]);
	}
	cout<<st.size();
	return 0;
}