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

using P = pair<int, int>;

//Graph[i][j][k] ==> i 番目のアルファベットが置かれている
//辺のうち、j から出ている k 番目の辺がつなぐ j 以外の頂点

vector<int> Graph[26][1000];

int main()
{
    int N, M;
    cin >> N >> M;
    //作れるどんな回文の大きさよりも大きい数（今回はどんなに長くても N * N * 2 とかだねたぶん）
    int inf = 1 << 30;
    //真ん中に一文字入れたら回文ができるところで使う
    vector<P> edges;
    //辺の情報を入力
    for (int i = 0; i < M; i++)
    {
        int u, v;
        char c;
        cin >> u >> v >> c;
        u--;
        v--;
        int type = c - 'a';
        Graph[type][v].push_back(u);
        Graph[type][u].push_back(v);
        edges.push_back(P(u, v));
        edges.push_back(P(v, u));
    }
    //ここまでで作る回文の最短の長さをいれる配列
    vector<vector<int>> LEN_up_to_here(N, vector<int>(N, inf));
    LEN_up_to_here[0][N - 1] = 0;
    //この queue に次に見ていく始点と終点の組を入れる
    queue<P> que;
    que.push(P(0, N - 1));
    //次に見る始点と終点の組がなくなるまで
    //始点終点両方から幅優先探索をするイメージ
    //（既にみたものは書き換える必要がないね）
    while (!que.empty())
    {
        //queue の先頭を見る
        P now = que.front();
        que.pop();
        auto [start, goal] = now;
        for (int i = 0; i < 26; i++)
        {
            //start と goal からそのアルファベットの辺が生えているときを考える
            if (Graph[i][start].size() == 0 || Graph[i][goal].size() == 0)
            {
                continue;
            }
            for (auto to0 : Graph[i][start])
            {
                for (auto to1 : Graph[i][goal])
                {
                    if (LEN_up_to_here[to0][to1] != inf)
                    {
                        continue;
                    }
                    //回文の長さは 2 づつ増えるよね（両端から作っていくので）
                    LEN_up_to_here[to0][to1] = LEN_up_to_here[start][goal] + 2;
                    que.push(P(to0, to1));
                }
            }
        }
    }
    int ans = inf;
    //回文ができたよ～～うれしいね
    for (int i = 0; i < N; i++)
    {
        ans = min(ans, LEN_up_to_here[i][i]);
    }
    //あと一文字真ん中に入れたら回文になるよ～～
    for (int i = 0; i < 2 * M; i++)
    {
        auto [start, goal] = edges[i];
        ans = min(ans, LEN_up_to_here[start][goal] + 1);
    }
    // ans==inf となっている時は構築できない時と同値だよね～～（作れるときは inf 未満になるようにしたので）
    if (ans != inf)
    {
        cout << ans << endl;
        return 0;
    }
    cout << -1 << endl;
}
//計算量は幅優先探索以外は平気そう
//幅優先探索での計算量は実質的に
//頂点数 N*N, 辺 M*M とみなせるから、
//O(N*N+M*M)になる...タブンネ

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1010
#define M 1010
#define ll long long
int last[N], nxt[M * 2], to[M * 2], ty[M * 2], len = 0;
int dis[N][N], p[N][N];
struct {
	int x, y;
}q[9000010];
void add(int x, int y, int w) {
	to[++len] = y;
	nxt[len] = last[x];
	ty[len] = w;
	last[x] = len;
}
int main() {
	int n, m, K, u, v, w, i, j, k;
	scanf("%d%d", &n, &m);
	memset(dis, 255, sizeof(dis));
	int l = 0, r = 0;
	for(i = 1; i <= m; i++) {
		char ch;
		scanf("%d%d %c", &u, &v, &ch);
		w = ch - 'a' + 1;
		add(u, v, w), add(v, u, w);
		dis[u][v] = dis[v][u] = 1;
		if(!p[u][v]) q[++r].x = u, q[r].y = v, p[u][v] = 1;
		if(!p[v][u]) q[++r].x = v, q[r].y = u, p[v][u] = 1;
	}
	for(i = 1; i <= n; i++) dis[i][i] = 0, p[i][i] = 1, q[++r].x = i, q[r].y = i;;
	while(l != r) {
		l = l % 9000005 + 1;
		u = q[l].x, v = q[l].y;
		for(i = last[u]; i; i = nxt[i]) {
			for(j = last[v]; j; j = nxt[j]) if(ty[j] == ty[i]) {
				if(dis[u][v] + 2 < dis[to[i]][to[j]] || dis[to[i]][to[j]] == -1) {
					dis[to[i]][to[j]] = dis[u][v] + 2;
					if(!p[to[i]][to[j]]) {
						r = r % 9000005 + 1;
						q[r].x = to[i], q[r].y = to[j];
						p[to[i]][to[j]] = 1;
					}

				}
			}
		}
		p[u][v] = 0;
	}
	printf("%d\n", dis[1][n]);
	return 0;
}


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

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 200001;

int A[N], B[N], P[N], IP[N];
vector<pii > v, ans;

int main() {
  int n;
  S(n);
  rep(i,1,n+1) {
    S(A[i]);
    v.pb(mp(A[i], i));
  }
  rep(i,1,n+1) {
    S(B[i]);
  }
  rep(i,1,n+1) {
    S(P[i]);
    IP[P[i]] = i;
  }

  sort(all(v));
  rep(i,0,v.size()) {
    int x = v[i].SS;
    if(IP[x] == x) continue;
    if(B[P[x]] >= A[x]) {
      P(-1);
      return 0;
    }
    ans.pb(mp(x, IP[x]));
    int y = P[x];
    IP[y] = IP[x];
    P[IP[x]] = y;
    IP[x] = x;
    P[x] = x;
  }
  P((int)ans.size());
  rep(i,0,ans.size()) {
    printf("%d %d\n",ans[i].FF, ans[i].SS);
  }
  return 0;
}

#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
// using namespace atcoder;
using lint = long long;
using ulint = unsigned long long;
template<class T>using graph = vector<vector<T>>;
#define endl '\n'
int const INF = 1<<30;
lint const INF64 = 1LL<<61;
lint const mod = 1e9+7;
//long const mod = 998244353;
lint ceilDiv(lint x, lint y){if(x >= 0){return (x+y-1)/y;}else{return x/y;}}
lint floorDiv(lint x, lint y){if(x >= 0){return x/y;}else{return (x-y+1)/y;}}
lint Sqrt(lint x){lint upper = 1e9;lint lower = 0;while(upper - lower > 0){lint mid = (1+upper + lower)/2;if(mid * mid > x){upper = mid-1;}else{lower = mid;}}return upper;}
lint gcd(lint a,lint b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
lint lcm(lint a,lint b){return (a / gcd(a,b)) * b;}
lint chmin(vector<lint>&v){lint ans = INF64;for(lint i:v){ans = min(ans, i);}return ans;}
lint chmax(vector<lint>&v){lint ans = -INF64;for(lint i:v){ans = max(ans, i);}return ans;}
double dist(double x1, double y1, double x2, double y2){return sqrt(pow(x1-x2, 2) + pow(y1-y2,2));}
vector<lint>prime;void makePrime(lint n){prime.push_back(2);for(lint i=3;i<=n;i+=2){bool chk = true;for(lint j=0;j<prime.size() && prime[j]*prime[j] <= i;j++){if(i % prime[j]==0){chk=false;break;}}if(chk)prime.push_back(i);}}

int main(){
    int n;
    cin >> n;
    int a[n];
    int b[n];
    int p[n];
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    for(int i=0;i<n;i++){
        cin >> b[i];
    }
    for(int i=0;i<n;i++){
        cin >> p[i];
        p[i]--;
        if(i != p[i] && a[i] <= b[p[i]]){
            cout << -1 << endl;
            return 0;
        }
    }
    vector<pair<int,int>>ans;
    vector<int>invP(n);  //荷物iを誰が持ってるか
    for(int i=0;i<n;i++){
        invP[p[i]] = i;
    }
    priority_queue<pair<lint,int>>que;
    for(int i=0;i<n;i++){
        que.push({b[i], i});
    }
    while(!que.empty()){
        auto f = que.top();
        que.pop();
        if(f.second == invP[f.second])continue;
        int nimotu1 = f.second;
        int hito1 = invP[f.second];
        int hito2 = nimotu1;
        int nimotu2 = p[hito2];
        ans.push_back({hito1+1, 1+hito2});
        p[hito1] = nimotu2;
        p[hito2] = nimotu1;
        invP[nimotu1] = hito2;
        invP[nimotu2] = hito1;
    }
    cout << ans.size() << endl;
    for(auto p: ans){
        cout << p.first << " " << p.second<<endl;
    }

}


#include "bits/stdc++.h"
#define int long long
#define pb push_back
using namespace std;
const int MOD = 1e9 + 7;
int power(int z){
    if(z==0)return 1;
    return power(z-1)*10;
}
signed main(){
    int K;
    cin >> K;
    string S,T;
    cin >> S >> T;
    int cnt[10];
    for(int i=1;i<=9;i++)cnt[i]=K;
    int to=9*K-8;
    for(int i=0;i<4;i++){
        --cnt[S[i]-'0'];
        --cnt[T[i]-'0'];
    }
    double ans=0;
    for(int i=1;i<=9;i++){
        for(int j=1;j<=9;j++){
            double p_cards=cnt[i]*1.0/to;
            if(i==j) p_cards*=(cnt[j]-1)*1.0/(to-1);
            else p_cards*=cnt[j]*1.0/(to-1);
            int tcnt[10], acnt[10];
            for(int k=1;k<=9;k++) tcnt[k]=acnt[k]=0;
            for(int k=0;k<4;k++){
                ++tcnt[S[k]-'0'];
                ++acnt[T[k]-'0'];
            }
            ++tcnt[i];
            ++acnt[j];
            int t_score=0, a_score=0;
            for(int k=1;k<=9;k++){
                t_score+=k*power(tcnt[k]);
                a_score+=k*power(acnt[k]);
            }
            ans+=(t_score>a_score)*p_cards;
        }
    }
    cout << fixed << setprecision(12) << ans << "\n";
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
#include <set>
#include <functional>
#include <map>
#include <unordered_map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long ll;
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define all(u) u.begin(),u.end()
using Interval = pair<ll,ll>;
using Graph = vector<vector<ll> >;
#define fi first
#define se second

struct Edge{
    ll to;
    ll w;
    Edge(ll to, ll w):to(to),w(w){}
};

using wGraph = vector<vector<Edge> >;

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

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

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

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

const ll INF = 1LL << 60;

ll k;
string S,T;

ll MOD = 1000000007;

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

int main(){
    cin >> k;
    cin >> S >> T;
    vector<ll>num(10,k);
    vector<ll> tnum(10,0);
    vector<ll> anum(10,0);

    num[0] = 0;

    rep(i,4){
        ll s = S[i]-'0';
        ll t = T[i]-'0';
        tnum[s] += 1;
        anum[t] += 1;
        num[s] -= 1;
        num[t] -= 1;
    }

    double sum = 0;
    repi(i,1,10) sum+=num[i];
    double ok = 0;

    repi(i,1,10){
        repi(j,1,10){
            if (i == j){
                if (num[i] <= 1) continue;
            }
            else{
                if (num[i] <= 0 || num[j] <= 0) continue;
            }
            ll tc = 0;
            ll ac = 0;
            tnum[i] += 1;
            anum[j] += 1;
            rep(k,10){
                tc += k*modpow(10,tnum[k],MOD);
                ac += k*modpow(10,anum[k],MOD);
            }
            if (tc > ac){
               if (i == j) ok += num[i]*(num[i]-1);
               else ok += num[i]*num[j];
            }
            tnum[i] -= 1;
            anum[j] -= 1;
        }
    }

    cout << fixed << setprecision(15) << ok/(sum*(sum-1)) << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1e9+7;

int main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll t, n;
  cin>>t>>n;
  auto f = [&](auto x){
    return (ll)floor((100+t)*x*0.01);
  };
  ll ok = 0, ng = 100*n;
  while (abs(ok-ng) > 1) {
    ll mid = (ok + ng) / 2;
    if (f(mid) - mid < n) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << f(ok) + 1 << "\n";
}

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

int t,N,hah[160];

vector<int> g;

template<typename Tp>
void read(Tp &x)
{
	x=0;int f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=f;
}

int main()
{
	read(t),read(N);
	for(int i=1;i<=100;i++)
	{
		int now=(100+t)*i/100;
		hah[now]=1;
	}
	for(int i=1;i<=100+t;i++) if(!hah[i]) g.push_back(i);
	int sz=g.size();
	int d=N/sz;
	if(N%sz==0) d--;
	N%=sz;
	if(!N) N=sz;
	N--;
	printf("%lld\n",(long long)(100+t)*d+g[N]);
	return 0;
}


#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(){
	const int m=1000000007;
	int n, p;
	cin>>n>>p;

	int ans=p-1;
	int c=p-2;
	for(int d=n-1; d>0; d>>=1){
		if(d&1) ans=(ll)ans*c%m;
		c=(ll)c*c%m;
	}

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

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>

using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
//const ll mod=1000000007;
const ll mod=998244353;
const ll INF=10e12;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}

int main(void){
    ll N;
    cin>>N;
    vector<ll> A(N);
    rep(i,N) cin>>A.at(i);
    sort(ALLOF(A));

    ll ans=0;
    rep(i,N) ans=(ans+A.at(i)*A.at(i))%mod;

    ll rui=A.at(N-1);
    for(ll i=N-2;i>=0;i--){
        ans=(ans+A.at(i)*rui)%mod;
        rui=(2*rui+A.at(i))%mod;
    }
    cout<<ans%mod<<endl;
    return 0;
}


/*
    I love the sound you make when you shut up.
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
/*-------typedefs------*/
template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
using ll = long long;
using pi = pair<int, int>;
/*-----in and out--------*/
#define pf(a) cout << a << endl
#define forIn(arr, num) for(int i = 0; i < num; i++) cin >> arr[i];
#define vpnt(ans) for(int i = 0; i < int(ans.size()); i++) cout << ans[i] << (i + 1 < int(ans.size()) ? ' ' : '\n');
/*---useful defines------*/
#define sz(x) (int)(x).size()
#define pb push_back
#define mem(a, b) memset(a,(b), sizeof(a))
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
/*----- the binary answer of life-----*/
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
/*---checking and pushing-----*/
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
/*---- FAST I/O and file read ---*/
void go() {
    ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#endif
}
/*-------- test-case stuff--------------*/
#define ssolve solve();
#define msolve int T;cin >> T;while(T--) {solve();}
#define mcsolve int T;cin >> T;for(int tt = 1;tt <= T;tt++) {cout << "Case #" << tt << ": ";solve();}
/*-------- movement in a 2D array ------*/
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
/*----------------------------------------------------------------*/
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
/*-------------- Push your limits here ---------------------------*/
int n, a[30], ans = 2e9;
void dfs(int x, int s1, int s2) {
    if(x == n + 1) {
        ckmin(ans, s1 ^ s2);
        return;
    }

    dfs(x + 1, s1 | a[x], s2);
    dfs(x + 1, a[x], s1 ^ s2);
}

void solve() {
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    dfs(1, 0, 0);
    pf(ans);
}

int main() {
    go();
    ssolve
    return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    ll res = 2000000000;
    for (int bit = 0; bit < (1<<N); bit++) {
        stack<ll> tmp;
        tmp.push(A[0]);
        for (int i = 1; i < N; i++) {
           if (bit & (1<<i)) tmp.top() |= A[i];
           else tmp.push(A[i]);
        }
        ll ans = tmp.top();
        tmp.pop();
        while(!tmp.empty()) {
            ans ^= tmp.top();
            tmp.pop();
        }
        res = min(res, ans);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define read freopen ("in.txt","r",stdin);
#define out freopen ("out.txt","w",stdout);
#define sz(x) int((x).size())
#define rep0(i,k) for (int i=0 ; i<k ; i++)
#define rep(i,k)  for (int i=1 ; i<=k ; i++)
#define reset(a,b)   memset(a, (b), sizeof(a))
#define sortv(k)     sort(k.begin(),k.end())
#define sortg(k)     sort(k.begin(),k.end(),greater<int>())
#define rev(k)       reverse(k.begin(),k.end())
#define umin(a,b)    a=min(a,b)
#define umax(a,b)    a=max(a,b)
#define min3(a,b,c)  min(a, min(b,c))
#define pyes         cout<<"YES"<<endl
#define pno          cout<<"NO"<<endl;
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mpr make_pair
#define pi acos(-1.0)
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
constexpr ll imax=1e9;
constexpr ll imin=-1e9;
constexpr ll MOD=1e9+7;
const int limit=1000001;

void solution(){

    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    if(d*c-b<=0) cout<<-1;
    else{

        ll x=d*c-b;
        ll y=a/x;
        if(a%x) y++;
        cout<<y;
    }
    return;
}
int main()
{
    fast;
    //read;
    //out;
    int tc=1;
    //cin>>tc;
    while(tc--) solution();
    return 0;
}

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

using lli = int64_t;
using ld = long double;

lli g = 10000;

lli dist(lli x1, lli y1, lli x2, lli y2)
{
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

lli bsearch(lli x, lli y, lli r, lli y1, lli l, lli h, bool fl)
{
    lli ans = LLONG_MAX;

    while (l <= h)
    {
        lli mid = l + (h - l) / 2;
        mid -= mid % g;

        if (dist(x, y, mid, y1) <= r * r)
        {
            ans = mid;

            (fl ? l : h) = mid + (fl ? g : -g);
        }
        else
            (fl ? h : l) = mid + (fl ? -g : g);
    }

    return ans;
}

lli calc(lli x, lli y, lli r, lli y1)
{
    lli rx = x;
    rx -= rx % g;

    lli mid = LLONG_MAX;

    if (dist(x, y, rx, y1) <= r * r)
        mid = rx;
    else
    if (dist(x, y, rx + g, y1) <= r * r)
        mid = rx + g;

    if (mid == LLONG_MAX) return 0;

    lli rl = x - r;
    lli rr = x + r;

    rl -= rl % g;
    rr -= rr % g;
    rr += g;

    lli l = bsearch(x, y, r, y1, rl, mid, false);
    lli h = bsearch(x, y, r, y1, mid, rr, true);

    return (h - l) / g + 1;
}

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

    ld xd, yd, rd; cin >> xd >> yd >> rd;

    lli x = llroundl(xd * ld(g));
    lli y = llroundl(yd * ld(g));
    lli r = llroundl(rd * ld(g));

    lli ans = 0;

    for (lli y1 = y - y % g; y - y1 <= r; y1 -= g)
        ans += calc(x, y, r, y1);

    for (lli y1 = y - (y % g) + g; y1 - y <= r; y1 += g)
        ans += calc(x, y, r, y1);

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


#include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
  ll modpow(ll x, ll b){
    ll res = 1;
    while(b){
      if(b&1)res = res * x % MOD;
      x = x * x % MOD;
      b>>=1;
    }
    return res;
  }

  ll modinv(ll x){
    return modpow(x, MOD-2);
  }

  bool was_output = false;
  template<class t>
  void output(t a){
    if(was_output)cout << " ";
    cout << a;
    was_output = true;
  }
  void outendl(){
    was_output = false;
    cout << endl;
  }
  ll in(){
    ll res;
    scanf("%lld", &res);
    return res;
  }

  template<class t>
  istream& operator>>(istream&is, vector<t>&x){
    for(auto &i:x)is >> i;
    return is;
  }

  template<class t, class u>
  istream& operator>>(istream&is, pair<t, u>&x){
    is >> x.first >> x.second;
    return is;
  }

  template<class t>
  t in(){
    t res; cin >> res; return res;
  }

  template<class t>
  void out(t x){
    cout << x;
  }

  template<class t>
  vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
    sort(line.begin(),line.end(),comp);
    return line;
  }

  template<class t>
  vector<t> reversed(vector<t> line){
    reverse(line.begin(),line.end());
    return line;
  }
}

using namespace templates;

ll func(){
    int n = in();
    int m = in();
    vector<int> weights(n);
    foreach(i,weights)i=in();
    sort(all(weights));

    vector<pll> bridge(m);
    foreach(i,bridge)i=in<pll>();

    ll res = INFLL;

    foreach(i,weights){
        foreach(bri,bridge){
            if(i > bri.second){
                return -1;
            }
        }
    }

    vector<ll> mins(bit(n));
    rep(i,bit(n)){
        ll sum = 0;
        rep(j,n){
            if(bit(j) & i){
                sum += weights[j];
            }
        }
        ll min_dis = 0;
        foreach(bri,bridge){
            if(sum > bri.second)chmax(min_dis,bri.first);
        }
        mins[i] = min_dis;
    }

    vector<int> order;
    rep(i,n)order.emplace_back(i);
    do{
        vector<ll> pos(n);
        rep(i,n){
            ll min_pos = 0;
            ll sum = bit(order[i]);
            per(j,i){
                sum += bit(order[j]);
                chmax(min_pos,mins[sum]+pos[j]);
            }
            pos[i] = min_pos;
        }
        chmin(res,pos.back());
    }while(next_permutation(all(order)));

    if(res==INFLL)return -1;
    return res;
}

int main(){
    cout << func() << endl;
  return 0;
}

using namespace std;

#define visual

#ifdef visual
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <assert.h>
#include <functional>
#include <math.h>
#include <string>
#include <ctime>
#endif
#ifndef visual
#include <bits/stdc++.h>
#endif

typedef long long ll;

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

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

    vector<long long> w;
    long long mxw = 0;
    for (long long i = 0; i < n; i++) {
        long long x;
        cin >> x;

        mxw = max(mxw, x);
        w.push_back(x);
    }

    vector<pair<long long, long long>> v;
    v.push_back({ 0, 0 });
    for (long long i = 0; i < m; i++) {
        long long x, y;
        cin >> x >> y;
        if (y < mxw) {
            cout << -1 << endl;
            return 0;
        }
        v.push_back({y, x});
        //camels with weight more than first must have length at least second
    }
    sort(v.begin(), v.end());
    vector<pair<long long, long long> > u;
    long long cur = -1;
    for (long long i = 0; i < v.size(); i++) {

        if (v[i].second > cur) {
            u.push_back(v[i]);
            //cout << v[i].first << " " << v[i].second << endl;
            cur = v[i].second;
        }
    } //u now has increasing firsts, increasing seconds

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

    ll ans = -1;
    do {
        vector<long long> loc(n);
        for (long long i = 0; i < n; i++) {
            long long bst = 0;

            for (long long j = 0; j < i; j++) {
                long long a;
                long long tot = 0;
                for (long long k = j; k <= i; k++) {
                    tot += w[k];
                }
                pair<long long, long long> p = { tot, -1 };
                auto i = lower_bound(u.begin(), u.end(), p);
                i--;
                a = (*i).second;
                //a should be minimum distance for weight of tot
                bst = max(bst, loc[j] + a);
            }
            loc[i] = bst;
        }
        //cout << loc[n-1] << endl;
        if (ans == -1 || loc[n-1] < ans) {
            ans = loc[n-1];
        }
    } while (next_permutation(w.begin(), w.end()));

    cout << ans << endl;

}


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

#define M 1000000007
#define int long long

int32_t main() {
    int n, m;
    cin >> n >> m;
    if (m < 0) {
        cout << -1 << endl;
        return 0;
    }
    else if (m > 0 && n-m < 2) {
        cout << -1 << endl;
        return 0;
    }
    vector<vector<int>> res;
    int st = 1;
    int mx = n-m-2;
    if (m == 0)
        mx = n-1;
    for (int i = 0; i < mx; i++) {
        res.push_back({st, st+1});
        st += 2;
    }
    int en = st+2;
    for (int i = 0; i <= m; i++) {
        res.push_back({en-1, en});
        en += 2;
    }
    if (m > 0)
        res.push_back({st, en});
    for (auto v: res)
        cout << v[0] << " " << v[1] << endl;
}

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

#ifndef aa
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define double long double
#define pii pair<int,int>
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define deb(x) cout << x << endl
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;

const int N = 2e5+5;

void solve(){

	int n, m;
	cin>>n>>m;
        if(n==1 && m==0)
	   done("1 2");
	if(m<0 || m>=n-1)
		done(-1);

	vpii v;
	v.pb({1,2*m+4});
	fr(i,1,m+1)
		v.pb({2*i, 2*i+1});
	int cnt = 2*m+5;
	while(sz(v)<n)
		v.pb({cnt++,cnt++});

	trav(p, v)
		cout << p.fi << " " << p.se << endl;

}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout << fixed << setprecision(15);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
#ifdef aa
    cout << endl << endl << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
    return 0;
}

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

int divide(int a, int b) {
    return (a % mod) * powm(b, mod - 2) % mod;
}

int norm(int a) {
    while (a >= mod) a -= mod;
    while (a < 0) a += mod;
    return a;
}

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}


#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;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int N, P;
const int MOD = 1e9 + 7;

int exp_mod(int a, int b) {
	int r = 1ll;
	while (b) {
		if (b & 1ll) r = r * a % MOD;
		a = a * a % MOD;
		b >>= 1ll;
	}
	return r;
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N >> P;
	cout << (P - 1) * exp_mod(P - 2, N - 1) % MOD << '\n';
}

#ifndef _MSC_VER
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#endif
#pragma region
using llint = long long;
constexpr std::pair<int, int> dxy[] = { {1,0}, {0,1}, {-1,0}, {0,-1} };
#pragma endregion

namespace mtl {
  template <class T, class U>
  constexpr T Pow(T A, U B) noexcept {
    T res = 1;
    while (B) {
      if (B & 1) { res *= A; }
      A *= A; B >>= 1;
    }
    return res;
  }

  template <class M = int>
  constexpr long long modpow(long long A, long long B, const M MOD = 1000000007) noexcept {
    A %= MOD;
    long long res = 1;
    while (B > 0) {
      if (B & 1) { res *= A; res %= MOD; }
      A *= A; A %= MOD;
      B >>= 1;
    }
    return res;
  }

  template <class T, class M = int>
  constexpr T inverse(T A, const M MOD = 1000000007) noexcept {
    T B = MOD, U = 1, V = 0;
    while (B) {
      T t = A / B;
      A -= t * B; std::swap(A, B);
      U -= t * V; std::swap(U, V);
    }
    U %= MOD;
    return U < 0 ? U += MOD, U : U;
  }

  inline std::tuple<long long, long long, long long> extgcd(const long long a, const long long b) {
    if (b == 0) return { a,1,0 };
    long long g, x, y;
    std::tie(g, x, y) = extgcd(b, a % b);
    return std::make_tuple(g, y, x - a / b * y);
  }

  inline std::pair<long long, long long> Chinese_Rem(const std::vector<long long>& b, const std::vector<long long>& m) {
    long long r = 0, M = 1;
    for (int i = 0; i < (int)b.size(); ++i) {
      long long p, q, d;
      std::tie(d, p, q) = extgcd(M, m[i]);
      if ((b[i] - r) % d != 0) return std::make_pair(0, -1);
      long long tmp = (b[i] - r) / d * p % (m[i] / d);
      r += M * tmp;
      M *= m[i] / d;
    }
    return std::make_pair((r % M + M) % M, M);
  }
}

constexpr llint mod = 1000000007ll;

int main() {
  llint N, P; std::cin >> N >> P;
  llint S = mtl::modpow(P - 1, N, mod);
  llint V = mtl::modpow(mtl::inverse(P - 1, mod), N - 1, mod);
  llint U = mtl::modpow(P - 2, N - 1, mod);
  std::cout << (S * V % mod * U) % 1000000007ll << std::endl;
}


/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define F(i,a,b) for(i=a;i<b;i++)
#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
using namespace __gnu_pbds; // for pbds
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e16;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
  ll n;
  cin>>n;
  cout<<100-(n%100);
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#*/
ll t,test=1;
//cin>>test;
F(t,1,test+1)
{
    //cout<<"Case #"<<t<<": ";
    solve();
    nl
}
return 0;
}

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
	ll X;
	cin>>X;
	ll Y=X;
	Y/=100;
	Y+=1;
	Y*=100;
	ll ans=Y-X;
	cout<<ans<<endl;

}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353
#define INF LONG_MAX/3
#define bit(n) (1LL<<(n))

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

char ban[5005][5005];
ll dp[5005][5005];

long long modpow(long long ori, long long po){
	long long res = 1;
	while(po > 0){
		if(po&1){
			res *= ori;
			res %= DIV;
		}
		ori *= ori;
		ori %= DIV;
		po >>= 1;
	}
	return res;
}

int main(){
	ll H, W, K;
	cin >> H >> W >> K;
	rep(y, H) rep(x, W) ban[y][x] = '.';

	rep(i, K) {
		ll h, w;
		char c;
		cin >> h >> w >> c;
		h--;w--;
		ban[h][w] = c;
	}
	dp[0][0] = modpow(3, H * W - K);

	ll memo = modpow(3, DIV-2);
	rep(h, H) rep(w, W) {
		if(ban[h][w] == 'R') {
			dp[h][w+1] += dp[h][w];
			dp[h][w+1] %= DIV;
		} else if(ban[h][w] == 'D') {
			dp[h+1][w] += dp[h][w];
			dp[h+1][w] %= DIV;
		} else if(ban[h][w] == 'X') {
			dp[h][w+1] += dp[h][w];
			dp[h][w+1] %= DIV;

			dp[h+1][w] += dp[h][w];
			dp[h+1][w] %= DIV;
		} else {
			//R
			dp[h][w+1] += dp[h][w] * memo;
			dp[h][w+1] %= DIV;
			//D
			dp[h+1][w] += dp[h][w] * memo;
			dp[h+1][w] %= DIV;

			//X
			dp[h][w+1] += dp[h][w] * memo;
			dp[h][w+1] %= DIV;

			dp[h+1][w] += dp[h][w] * memo;
			dp[h+1][w] %= DIV;
		}
	}
	cout << dp[H-1][W-1] << endl;
}

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second

ll MOD=998244353;
int h, w, k;
char g[5005][5005];
ll dp[5005][5005], emp[5005][5005], po3[200005], inv;

ll modpow(ll x, ll e){
	if(x==0) return 0;
	if(e==0) return 1;
	ll y=modpow(x, e/2);
	if(e%2==0) return (y*y)%MOD;
	return ((y*y)%MOD*x)%MOD;
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>h>>w>>k;
	po3[0]=1;
	for(int i=1;i<200005;i++){
		po3[i]=(po3[i-1]*3)%MOD;
	}
	for(int i=0;i<=h;i++){
		for(int j=0;j<=w;j++){
			g[i][j]='.';
		}
	}
	for(int i=0;i<k;i++){
		int x, y;
		char c;
		cin>>x>>y>>c;
		g[x][y]=c;
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			emp[i][j]=emp[i-1][j]+emp[i][j-1]-emp[i-1][j-1]+(g[i][j]=='.');
		}
	}
	if(g[1][1]=='.') dp[1][1]=3;
	else dp[1][1]=1;
	inv=modpow(3, MOD-2);
	for(int x=1;x<=h;x++){
		for(int y=1;y<=w;y++){
			if(x+y==2) continue;
			ll c=po3[emp[x][y]-emp[x][y-1]], r=po3[emp[x][y]-emp[x-1][y]];
			if(g[x][y-1]=='R' || g[x][y-1]=='X'){
				dp[x][y]=(dp[x][y]+dp[x][y-1]*c)%MOD;
			}
			if(g[x-1][y]=='D' || g[x-1][y]=='X'){
				dp[x][y]=(dp[x][y]+dp[x-1][y]*r)%MOD;
			}
			if(g[x][y-1]=='.'){
				dp[x][y]+=(((2*inv*dp[x][y-1])%MOD)*c)%MOD;
				dp[x][y]%=MOD;
			}
			if(g[x-1][y]=='.'){
				dp[x][y]+=(((2*inv*dp[x-1][y])%MOD)*r)%MOD;
				dp[x][y]%=MOD;
			}
		}
	}
	cout<<dp[h][w]<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define next Cry_For_theMoon
using namespace std;
const int MAXN=25,LIM=1<<19;
int n,m,g[MAXN][MAXN];
int f[LIM],d[LIM],ans,vis[MAXN];
inline int Min(int a,int b){
	return (a<b)?a:b;
}
int main(){
	scanf("%d%d",&n,&m);
	ans=n;
	for(int i=1;i<=n;i++){
		g[i][i]=1;
	}
	for(int i=1,u,v;i<=m;i++){
		scanf("%d%d",&u,&v);
		g[u][v]=g[v][u]=1;
	}
	for(int i=0;i<n;i++){
		f[1<<i]=1;
	}
	for(int i=1;i<(1<<n);i++){
		if(f[i])continue;
		memset(vis,0,sizeof vis);
		f[i]=1;
		for(int j=0;j<n;j++){
			if(i&(1<<j)){
				vis[j+1]=1;
			}
		}
		for(int j=1;j<=n;j++){
			if(!vis[j])continue;
			for(int k=j+1;k<=n;k++){
				if(!vis[k])continue;
				if(!g[j][k]){
					f[i]=0;
					break;
				}
			}
			if(!f[i])break;
		}
	}
	for(int i=1;i<(1<<n);i++){
		d[i]=1000;
		for(int j=i;j;j=(j-1)&i){
			if(!f[j])continue;
			d[i]=Min(d[i],1+d[j xor i]);
		}
	}
	printf("%d",d[(1<<n)-1]);
	return 0;
}

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

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef tuple<int,int,int> ti;
typedef tuple<ll,ll,ll> tl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<ti> vti;
typedef vector<tl> vtl;

typedef priority_queue<int> pq;
typedef priority_queue<ti,vector<ti>,greater<ti>> pqg;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

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

	int n,m;
	cin>>n>>m;

	vector<vpi> adj(n);
	for(int i=0; i<m; i++){
		int a,b;
		char c;
		cin>>a>>b>>c;
		a--; b--;
		adj[a].pb(mp(b,c-'a'));
		adj[b].pb(mp(a,c-'a'));
	}

	vector<vector<bool>> vis(n, vector<bool>(n,false));
	vis[0][n-1]=true;
	pqg q;
	q.push(mt(0,0,n-1));

	while(!q.empty()){
		int a,b,s;
		tie(s,a,b)=q.top(); q.pop();
		if(a==b){
			cout<<s;
			return 0;
		}
		for(auto u:adj[a]){
			if(u.f==b){
				vis[b][b]=true;
				q.push(mt(s+1,b,b));
			}
		}
		for(auto u:adj[a]){
			for(auto v:adj[b]){
				if(vis[u.f][v.f]) continue;
				if(u.s==v.s){
					vis[u.f][v.f]=true;
					q.push(mt(s+2,u.f,v.f));
				}
			}
		}
	}

	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>

#define Mod 998244353

using namespace std;

typedef long long ll;

inline int read() {
	int out = 0;
	bool flag = false;
	register char cc = getchar();
	while (cc < '0' || cc > '9') {
		if (cc == '-') flag = true;
		cc = getchar();
	}
	while (cc >= '0' && cc <= '9') {
		out = (out << 3) + (out << 1) + (cc ^ 48);
		cc = getchar();
	}
	return flag ? -out : out;
}

inline void write(int x, char ch) {
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) putchar('0');
	else {
		int num = 0;
		char cc[22];
		while (x) cc[++num] = x % 10 + 48, x /= 10;
		while (num) putchar(cc[num--]);
	}
	putchar(ch);
}

int fpow(int x, int y) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % Mod;
		x = 1ll * x * x % Mod;
		y >>= 1;
	}
	return res;
}

int n, a[2510];

int main() {
	n = read();
	if (n <= 1668) {
		a[1] = 6, a[n - 1] = 10, a[n] = 15;
		for (int i = 1; i < n - 1; i++) a[i] = 6 * i;
	}
	else {
		int cnt = 0;
		for (int i = 1; i <= 1666; i++) if (i % 5 != 0) a[++cnt] = 6 * i;
		for (int i = cnt + 1; i <= min(cnt + 1000, n - 1); i++) a[i] = 10 * (i - cnt);
		cnt += 1000; if (cnt > n - 1) cnt = n - 1;
		for (int i = cnt + 1, t = 1; i <= n; i++, t += 2) a[i] = 15 * t;
	}
	for (int i = 1; i <= n; i++) write(a[i], ' ');
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
//const long nPrime = 1000000007;
//const long nPrime = 998244353;
int main() {
    int n;
    cin >> n;
    vector<long> viAns = {6,10,15};
    for(long i = 2; 6*i < 10000; i++){
        viAns.push_back(6*i);
    }
    for(long i = 2; 10*i < 10000; i++){
        viAns.push_back(10*i);
    }
    for(long i = 2; 15*i < 10000; i++){
        viAns.push_back(15*i);
    }

    vector<int> viCheck(10000,0);
    int i = 0;
    while(n>0){
        if(viCheck[viAns[i]] == 0){
            cout << viAns[i] << " ";
            n--;
            viCheck[viAns[i]] = 1;
        }
        i++;
    }
    return 0;
}


//GIVE ME AC!!!!!!!!!!!!!!!!!
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
using P=pair<ll,ll>;
ll dp[2500][2500];
ll add(char c){
  if(c=='+'){
    return 1;
  }
  return -1;
}
int main(){
  ll h,w;
  cin>>h>>w;
  char c[h][w];
  rep(i,0,h){
    rep(j,0,w){
      cin>>c[i][j];
    }
  }
  dp[h-1][w-1]=0;
  for(ll i=h-1;i>=0;i--){
    for(ll j=w-1;j>=0;j--){
      if(i==h-1&&j==w-1){
        continue;
      }
      dp[i][j]=-inf;
      if(i<h-1){
        dp[i][j]=max(dp[i][j],add(c[i+1][j])-dp[i+1][j]);
      }
      if(j<w-1){
        dp[i][j]=max(dp[i][j],add(c[i][j+1])-dp[i][j+1]);
      }
    }
  }
  if(dp[0][0]>0){
    cout<<"Takahashi"<<endl;
  }
  if(dp[0][0]==0){
    cout<<"Draw"<<endl;
  }
  if(dp[0][0]<0){
    cout<<"Aoki"<<endl;
  }
}

#include <algorithm>
#include <bitset>
//#include <cmath>
//#include <complex>
#include <cctype>
#include <cstdio>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <climits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_set>
#include <unordered_map>

using namespace std;

int64_t INF = LLONG_MAX;

void mod(int64_t &n){
    n %= 998244353;
}

int64_t factorial(int64_t n){
    if(n == 1 or n == 0){
        return 1;
    }else{
        int64_t tmp = n*factorial(n-1);
        mod(tmp);
        return tmp;
    }
}

//UnionFind木
class UnionFind {
private:
    //xの親はparent.at(x)．x==parent.at(x)ならxは根．
    vector<int> parent;

public:
    //コンストラクタ．parentの要素数をNで初期化．
    UnionFind(int N) : parent(N, -1){
    }

    //再帰的にxの根を探索．この時，経路圧縮を同時に行う．
    int root(int x){
        if(parent.at(x) < 0){
            return x;
        }else{
            parent.at(x) = root(parent.at(x));
            return parent.at(x);
        }
    }

    //xとyを接続．xをyの根に接続する．
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx == ry){
            return;
        }else if(-(parent.at(rx)) > -(parent.at(ry))){
            swap(rx, ry);
        }

        //rxのサイズ <= ryのサイズ
        parent.at(ry) += parent.at(rx);
        parent.at(rx) = ry;
        return;
    }

    //xとyが同じ木に属するか判定．xの根とyの根が同じならtrue．
    bool isSame(int x, int y){
        return root(x) == root(y);
    }

    //xの属する木のサイズを取得．
    int size(int x){
        int rx = root(x);
        return parent.at(rx) * (-1);
    }

    void calc(int64_t &n){
        for(int i = 0; i < parent.size(); i++){
            if(parent.at(i) < 0){
                n *= factorial(-parent.at(i));
                mod(n);
            }
        }
    }
};

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

    int N, K;

    cin >> N >> K;

    vector<vector<int>> a(N, vector<int>(N));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            cin >> a.at(i).at(j);
        }
    }

    int64_t ans = 1;

    UnionFind uf1(N);

    //行スワップ
    for(int i = 0; i < N; i++){
        for(int j = i+1; j < N; j++){
            bool flag = true;
            for(int k = 0; k < N; k++){
                if(a.at(i).at(k) + a.at(j).at(k) > K){
                    flag = false;
                    break;
                }
            }

            if(flag){
                uf1.unite(i, j);
            }
        }
    }

    uf1.calc(ans);

    UnionFind uf2(N);
    //列スワップ
    for(int i = 0; i < N; i++){
        for(int j = i+1; j < N; j++){
            bool flag = true;
            for(int k = 0; k < N; k++){
                if(a.at(k).at(i) + a.at(k).at(j) > K){
                    flag = false;
                    break;
                }
            }
            if(flag){
                uf2.unite(i, j);
            }
        }
    }

    uf2.calc(ans);

    cout << ans << endl;

    return 0;
}


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

typedef long long ll;
ll i,j,k,a,b,c;
ll T,m;
ll t[10005],v[10005],f[100005];
int main()
{
	cin>>a>>b;
	if(b%a==0)
	{
		cout<<"Yes";
		return 0;
	}
	else cout<<"No";
	return 0;
}

#include <bits/stdc++.h>

#define ig  cin.ignore(numeric_limits<streamsize>::max(), '\n');
using namespace std;

int binarySearch(long long int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }

    return -1;
}

void solve()
{
    int a,b,sum=0;
    cin>>a>>b;
    for(int i=1;i<=a;i++)
        for(int j=1;j<=b;j++)
            sum+=(i*100)+j;
    cout<<sum<<endl;
}

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


#include<bits/stdc++.h>

using namespace std;
using int64 = long long;
// const int mod = 1e9 + 7;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

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

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

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

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

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

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

int main() {
  int N, M;
  cin >> N >> M;
  int x = 0;
  for(int i = 0; i < N; i++) {
    string s;
    cin >> s;
    x += count(begin(s), end(s), '1') % 2;
  }
  cout << 1LL * (N - x) * x << "\n";
}

#include <iostream>
#include <string>

int main() {
  long long n,m;
  std::cin >> n >> m;
  long long p = 0;
  for (int i = 0; i < n; i++) {
    std::string s;
    std::cin >> s;
    int c = 0;
    for (int k = 0; k < m; k++) {
      if (s[k] == '1') c++;
    }
    if (c & 1) p++;
  }
  std::cout << p * (n - p) << std::endl;
  return 0;
}


 #include<bits/stdc++.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<time.h>
#include <cstdio>
#include <iostream>
#include <vector>
#define ll long long
#define int long long
#define inf 0x3f3f3f3f
#define mods 1000000007
#define modd 998244353
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
 using namespace std;
 ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;return b==0?a:gcd(b,a%b);}
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll qp(ll a,ll b,ll mod){ll ans=1;if(b==0){return ans%mod;}while(b){if(b%2==1){b--;ans=ans*a%mod;}a=a*a%mod;b=b/2;}return ans%mod;}//快速幂%
ll qpn(ll a,ll b, ll p){ll ans = 1;a%=p;while(b){if(b&1){ans = (ans*a)%p;--b;}a =(a*a)%p;b >>= 1;}return ans%p;}//逆元   (分子*qp(分母,mod-2,mod))%mod;

ll a[11111];
ll last[11111];
ll pre[11111];
signed main(){
 ll n;
 read(n);
 ll mi=1e9;
 for(int i=1;i<=n;i++){
 read(a[i]);
 mi=min(mi,a[i]);
 last[i]=n;
 pre[i]=1;
 }

 ll ans=0;
 for(int i=1;i<=n;i++){
 for(int j=i-1;j>=1;j--){
 if(a[j]<a[i]){
    pre[i]=j+1;
    break;
 }

 }

 for(int j=i+1;j<=n;j++){
        //i=L
 if(a[j]<a[i]){
    last[i]=j-1;
    break;
 }

 }

 }

 for(int i=1;i<=n;i++){
 ll len=last[i]-pre[i]+1;
 ans=max(ans,len*a[i]);
 }
printf("%lld",ans);

}

#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back

using namespace std;

void test(){
	ll n;
	cin>>n;
	ll a[n];
	f(i,n)cin>>a[i];
	ll res = a[0];
	f(i,n){
		ll c = 0, s = a[i];
		for(int j=i;j<n;j++){
			c++;
			s = min(s,a[j]);
			res = max(res,s*c);
		}
	}
	cout<<res<<"\n";

}

int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	// cin>>tests;
	while(tests--){
		test();
	}
}


#include <iostream>
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while(T--)
  {
    int n;
    cin >> n;
    string s[3];
    cin >> s[0] >> s[1] >> s[2];
    int ones = 0, zeros = 0;
    cout << 1;
    for(int i = 1; i <= n; i++) cout << 0;
    for(int i = 1; i <= n; i++) cout << 1;
    cout << endl;
  }
  return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define N 200005
using namespace std;
int n,a[N],b[N],p[N],ans,sx[N],sy[N],z[N],zz,mx,mxid;
bool vis[N];
void dfs(int x)
{
	if(vis[x])return;
	z[++zz]=x;
	vis[x]=1;
	if(a[x]>mx)
	{
		mx=a[x];
		mxid=x;
	}
	dfs(p[x]);
}
int main()
{
#ifdef lovelyG
Sfile(1);
#endif
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)scanf("%d",&b[i]);
	for(int i=1;i<=n;++i)scanf("%d",&p[i]);
	for(int x,i=1;i<=n;++i)
	{
		if(vis[i])continue;
		zz=mx=0;
		dfs(i);
		x=mxid;
		while(p[x]!=x)
		{
			if(b[p[x]]>=a[x]||b[p[p[x]]]>=a[p[x]])
			{
				puts("-1");
				return 0;
			}
			sx[++ans]=x;
			sy[ans]=p[x];
			swap(p[x],p[p[x]]);
		}
	}
	printf("%d\n",ans);
	for(int i=1;i<=ans;++i)
	printf("%d %d\n",sx[i],sy[i]);
	return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
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; }
constexpr int dy[4]={ -1,0,1,0 };
constexpr int dx[4]={ 0,-1,0,1 };

ll a1, b1, a2, b2;

signed main(){
    cin >> a1 >> b1 >> a2 >> b2;
    if((a1+b1)%2 == (a2+b2)%2){
        if(a1 == a2 && b1 == b2){
            cout << 0 << endl;
        }
        else if(a1+b1 == a2+b2 || a1-b1 == a2-b2 || abs(a1-a2)+abs(b1-b2) <= 3){
            cout << 1 << endl;
        }
        else{
            cout << 2 << endl;
        }
    }
    else{
        if(abs(a1-a2)+abs(b1-b2) <= 3){
            cout << 1 << endl;
        }
        else if(abs((a1+b1)-(a2+b2)) <= 3 || abs((a1-b1)-(a2-b2)) <= 3 || abs(a1-a2)+abs(b1-b2) <= 6){
            cout << 2 << endl;
        }
        else{
            cout << 3 << endl;
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pp=pair<int,int>;
const ll INF=1e9;
const ll mod=1e9+7;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int x=c-a, y=d-b;
  int s;
  if (x==0 && y==0) {s=0; goto OUT;}
  else if (abs(x)==abs(y) || (abs(x)<=2 && abs(y)<=2) || (abs(x)==3 && y==0) || (abs(y)==3 && x==0)) {
    s=1;
    goto OUT;
  }
  else if (x%2==y%2) {s=2; goto OUT;}
  else {
    queue<pp> Q;
    for (int i=-2; i<=2; i++) {
      for (int j=-2; j<=2; j++) {
        Q.push(pp(x+i,y+j));
      }
    }
    Q.push(pp(x,y+3));
    Q.push(pp(x,y-3));
    Q.push(pp(x+3,y));
    Q.push(pp(x-3,y));
    while (Q.size()>0) {
      pp q=Q.front();
      Q.pop();
      x=q.first;
      y=q.second;
      if (abs(x)==abs(y) || (abs(x)<=2 && abs(y)<=2) || (abs(x)==3 && y==0) || (abs(y)==3 && x==0)) {
        s=2;
        goto OUT;
      }
    }
  }
  s=3;
OUT:
  cout << s << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
static const ll mod=1000000007;
ll H,W;
char f[2005][2005];
ll modpow(ll x,ll y){
  if(y==0)
    return 1;
  else if(y%2==0){
  ll z=modpow(x,y/2);
    return (z*z)%mod;
  }else{
   ll z=modpow(x,y/2);
    return (x*((z*z)%mod))%mod;
  }
}
ll sum[2005][2005];
int main(){
cin>>H>>W;ll K=0;
  for(ll i=0;i<H;i++){
  string s;cin>>s;
    for(ll j=0;j<W;j++){
      f[i][j]=s[j];
    if(f[i][j]=='.')K++;
    }
  }
  for(ll i=0;i<H;i++){
  ll x=0;
    for(ll j=0;j<W;j++){
    if(f[i][j]=='#')
      x=0;
      else{
      x++;sum[i][j]+=x;
      }
     }
   } for(ll j=0;j<W;j++){
  ll x=0;
    for(ll i=H-1;0<=i;i--){
    if(f[i][j]=='#')
      x=0;
      else{
      x++;sum[i][j]+=x;
      }
     }
   } for(ll i=0;i<H;i++){
  ll x=0;
    for(ll j=W-1;0<=j;j--){
    if(f[i][j]=='#')
      x=0;
      else{
      x++;sum[i][j]+=x;
      }
     }
   } for(ll j=0;j<W;j++){
  ll x=0;
    for(ll i=0;i<H;i++){
    if(f[i][j]=='#')
      x=0;
      else{
      x++;sum[i][j]+=x;
      }
     }
   }ll ans=0;ll k=modpow(2,K);
  for(ll i=0;i<H;i++)
    for(ll j=0;j<W;j++)
      if(f[i][j]!='#'){
        ll y=sum[i][j]-3;
      ll x=(k-modpow(2,K-y)+mod)%mod;
      ans=(ans+x)%mod;
      }cout<<ans<<endl;
      return 0;
}

#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 1000000007
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 main() {
    int H, W; cin >> H >> W;
    vector<string> S(H);
    REP(i, 0, H) cin >> S[i];

    int tot = 0;
    vector<vector<int>> R(H, vector<int>(W, 0)), C(H, vector<int>(W, 0));
    REP(i, 0, H) REP(j, 0, W) {
        if (S[i][j] == '#') continue;
        R[i][j] = 1 + ((j > 0) ? R[i][j - 1] : 0);
        C[i][j] = 1 + ((i > 0) ? C[i - 1][j] : 0);
        tot++;
    }
    REP(i, 0, H) for (int j = W - 1; j > 0; j--) if (R[i][j] && R[i][j - 1]) R[i][j - 1] = R[i][j];
    REP(j, 0, W) for (int i = H - 1; i > 0; i--) if (C[i][j] && C[i - 1][j]) C[i - 1][j] = C[i][j];

    mint ans = 0;
    REP(i, 0, H) REP(j, 0, W) {
        if (S[i][j] == '#') continue;
        // cout << "#(" << i << "," << j << ") : " << R[i][j] << ", " << C[i][j] << endl;
        int cnt = R[i][j] + C[i][j] - (R[i][j] && C[i][j]);
        ans += (mint(2).pow(cnt) - 1) * mint(2).pow(tot - cnt);
    }
    cout << ans << endl;

    return 0;
}


/**
 *   @FileName	d.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.10 22:08:23
**/

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

template< int MOD >
struct mint {
public:
    long long x;
    mint(long long x = 0) :x((x%MOD+MOD)%MOD) {}
    mint(std::string &s) {
        long long z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= MOD;
        }
        this->x = z;
    }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint& operator/=(const mint &a) {
        long long n = MOD - 2;
        mint u = 1, b = a;
        while (n > 0) {
            if (n & 1) {
                u *= b;
            }
            b *= b;
            n >>= 1;
        }
        return *this *= u;
    }
    mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-() const {return mint() -= *this; }
    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    friend std::istream &operator>>(std::istream &is, mint &n) {
        long long x;
        is >> x;
        n = mint(x);
        return is;
    }
    bool operator==(const mint &a) const {
        return this->x == a.x;
    }
    mint pow(long long k) const {
        mint ret = 1;
        mint p = this->x;
        while (k > 0) {
            if (k & 1) {
                ret *= p;
            }
            p *= p;
            k >>= 1;
        }
        return ret;
    }
};
constexpr int MOD = 1e9 + 7;
int main() {
    int t;cin >> t;
    while(t--) {
        ll n,a,b;cin >> n >> a >> b;
        if (b > a) swap(a,b);
        ll m = min(n-a,b-1);
        auto k = (mint<MOD>(2) * (n-a+1)*m-mint<MOD>(m)*(m+1)+mint<MOD>(a-b+1)*(n-a+1));
        auto p = mint<MOD>(n-a+1)*(n-b+1);
        cout << p * p - k * k << endl;
    }
    return 0;
}

#include <cstdio>
#include <cctype>
#define rr register
using namespace std;
const int N=111;
int f[N],siz[N],x[N],y[N],n;
inline signed iut(){
	rr int ans=0,f=1; rr char c=getchar();
	while (!isdigit(c)) f=(c=='-')?-f:f,c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans*f;
}
inline signed getf(int u){return f[u]==u?u:f[u]=getf(f[u]);}
inline void uni(int x,int y){
	rr int fa=getf(x),fb=getf(y);
	if (fa==fb) return;
	if (siz[fa]>siz[fb]) fa^=fb,fb^=fa,fa^=fb;
	siz[fb]+=siz[fa],f[fa]=fb;
}
inline bool check(double mid){
	for (rr int i=1;i<n+3;++i) f[i]=i,siz[i]=1;
	for (rr int i=1;i<=n;++i){
		if (mid*2>100-y[i]) uni(i,n+1);
		if (y[i]+100<2*mid) uni(i,n+2);
		for (rr int j=i+1;j<=n;++j)
		if ((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])<mid*mid*4)
		    uni(i,j);
	}
	return getf(n+1)==getf(n+2);
}
signed main(){
	n=iut();
	for (rr int i=1;i<=n;++i)
	    x[i]=iut(),y[i]=iut();
	rr double l=0,r=100;
	while (l+1e-6<r){
		rr double mid=(l+r)/2;
		if (check(mid)) r=mid;
		    else l=mid;
	}
	return !printf("%lf",l);
}


#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;

const int mod = 1e9+7;
ll ans = 0;

array<int, 61> dfs(auto &g, int u, int par, auto &pref, auto &sz)
{
    sz[u] = 1;
    array<int, 61> ret{};
    int tot = 0;
    for (auto [v, w] : g[u]) {
        if (v == par) continue;
        pref[v] = pref[u] ^ w;
        auto child = dfs(g, v, u, pref, sz);
        sz[u] += sz[v];
        for (int i = 0; i < 61; ++i) {
            ll mul = 1LL << i; mul %= mod;
            ll ct = 1LL*child[i]*(tot-ret[i]) + 1LL*(sz[v]-child[i])*ret[i];
            ct %= mod;
            ans += mul*ct; ans %= mod;
            ret[i] += child[i];
        }
        tot += sz[v];
    }
    for (int i = 0; i < 61; ++i) {
        ll mul = 1LL << i; mul %= mod;
        ll ct = 0;
        if ((pref[u]>>i)&1) {
            ct = sz[u]-1-ret[i];
            ++ret[i];
        }
        else {
            ct = ret[i];
        }
        ct %= mod;
        ans += mul*ct; ans %= mod;
    }
    return ret;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

    int n; cin >> n;
    vector<vector<array<ll, 2>>> g(n);
    for (int i = 0; i < n-1; ++i) {
        int u, v; cin >> u >> v;
        ll w; cin >> w;
        g[--u].push_back({--v, w});
        g[v].push_back({u, w});
    }
    vector<ll> pref(n);
    vector<int> sz(n);
    dfs(g, 0, 0, pref, sz);
    cout << ans;
}

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,dis[1001001],mod=1e9+7,u,v,w,num,ans;
struct node
{
	int v,w;
}t;
vector<node>g[1001001];
void dfs(int u,int fa)
{
	for(int i=0;i<g[u].size();i++)
	{
		int v=g[u][i].v;
		if(g[u][i].v==fa)
			continue;
		dis[v]=dis[u]^g[u][i].w;
		dfs(v,u);
	}
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<+n;i++)
	{
		scanf("%lld%lld%lld",&u,&v,&w);
		t.v=v;
		t.w=w;
		g[u].push_back(t);
		t.v=u;
		g[v].push_back(t);
	}
	dfs(1,0);
	for(int j=60;j>=0;j--)
	{
		num=0;
		for(int i=1;i<=n;i++)
			if((dis[i]&(1ll<<j))>0)
				num++;
		ans=(ans+(((1ll<<j)%mod)*num%mod)*(n-num)%mod)%mod;
	}
	cout<<ans;
    return 0;
}


// Author: Pawan Raj
// Problem: D - Shipping Center
// Contest: AtCoder - Panasonic Programming Contest (AtCoder Beginner Contest 195)
// URL: https://atcoder.jp/contests/abc195/tasks/abc195_d

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
#define show(A) for (auto i: A) cout << i << " "; cout << '\n';
#define show_(A, n) F(i, n) { show(A[i]) }

using namespace std;
using ld = long double;
using vi = vector < int > ;
using mi = map < int, int > ;
using pi = pair < int, int > ;

const int N = 100005;
const int MOD = 1e9 + 7;
const int inf = 1e18 + 1;
/***************************code begins here*****************************/

void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	pi a[n];
	int b[m];
	F(i,n) cin >> a[i].fi >> a[i].se;
	F(i,m) cin >> b[i];
	while(q--){
		int ans = 0;
		vi aa;
		vi vis(n,0);
		int l,r; cin >> l >> r;
		for(int i=0;i<m;i++){
			if(i >= (l-1) && i<r) continue;
			aa.pb(b[i]);
		}
		sort(all(aa));
		// show(aa);
		for(int x : aa){
			int c = -1;
			F(i,n){
				if(vis[i]) continue;
				if(a[i].fi <= x){
					if(c==-1) c = i;
					if(a[c].se < a[i].se) c = i;
				}
			}
			// cout << c << '\n';
			if(c == -1) continue;
			ans += a[c].se;
			vis[c] = 1;
		}
		cout << ans << '\n';
	}
}

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

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

    return 0;
}

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

int h,w,a,b;
int used[16][16];

int dfs(int x, int y, int n){
    if(y==h) return n==0;
    if(x==w) return dfs(0,y+1,n);
    if(used[y][x]) return dfs(x+1,y,n);
    int res = 0;
    if(y+1<h and !used[y+1][x] and 0<n){
        used[y][x] = used[y+1][x] = 1;
        res += dfs(x+1,y,n-1);
        used[y][x] = used[y+1][x] = 0;
    }
    if(x+1<w and !used[y][x+1] and 0<n){
        used[y][x] = used[y][x+1] = 1;
        res += dfs(x+1,y,n-1);
        used[y][x] = used[y][x+1] = 0;
    }
    res += dfs(x+1,y,n);
    return res;
}

int main() {
    fio(); cin>>h>>w>>a>>b;
    int ans = dfs(0,0,a);
    print(ans);
}


#include <bits/stdc++.h>
using namespace std;
struct __INIT
{
    __INIT()
    {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
//#define INF (1<<30)
#define LINF (lint)(1LL << 56)
#define endl "\n"
#define rep(i, n) for (lint(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (lint(i) = (n - 1); (i) >= 0; (i)--)
#define flc(x) __builtin_popcountll(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define vec vector<lint>
#define nep(x) next_permutation(all(x))
typedef long long lint;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int MAX_N = 3e5 + 5;
constexpr int MOD = 1000000007;
typedef __int128_t llint;

int main(void)
{
    int N, M, Q;
    cin >> N >> M >> Q;
    pint itm[N];
    rep(i, N) cin >> itm[i].se >> itm[i].fi;
    sort(itm, itm + N, greater<pint>());
    int X[M];
    rep(i, M) cin >> X[i];
    while (Q--)
    {
        int L, R;
        cin >> L >> R;
        L--, R--;
        vector<int> box;
        rep(i, M) if (i < L || i > R) box.push_back(X[i]);
        sort(all(box));
        int ans = 0;
        rep(i, N)
        {
            rep(j, box.size())
            {
                if (itm[i].se <= box[j])
                {
                    ans += itm[i].fi, box[j] = 0;
                    break;
                }
            }
        }
        cout << ans << endl;
    }
}

#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
// #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
const int yuzu=2e5;
typedef int fuko[yuzu|10];
fuko a,b;
int main() {
  int i,n,m,c1,c2,j;
  read(n),read(m);
  for (i=1;i<=n;++i) a[i]=read();
  for (i=1;i<=m;++i) b[i]=read();
  for (i=1;i<=1e3;++i) {
    c1=c2=0;
    for (j=1;j<=n;++j) c1+=a[j]==i;
    for (j=1;j<=m;++j) c2+=b[j]==i;
    !c1&&c2||!c2&&c1?printf("%d ",i):0;
  }
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SZ = 2e5 + 100, INF = 1e9 * 1e9 + 10, MOD = 998244353;
vector<ll> vec1, vec2;
vector<pair<ll, ll>> v1, v2;

ll fnd(int j) {
	int l = 0, r = v1.size();
	while (r - l > 1) {
		int mid = (l + r) / 2;
		int newps = v1[mid].first - abs(j - mid);
		if (newps >= v1[j].second) {
			r = mid;
		}
		else {
			l = mid;
		}
	}

	return r;
}

ll get() {
	vector<ll> c1, c2;
	v1.clear();

	for (int i = 0; i < vec1.size() - 1; i++) {
		ll dir = 0, dir2 = 0;
		if (vec1[i] < vec2[i]) dir = 1;
		if (vec1[i] > vec2[i]) dir = 2;

		if (vec1[i + 1] < vec2[i + 1]) dir2 = 1;
		if (vec1[i + 1] > vec2[i + 1]) dir2 = 2;

		if (dir == 1 || dir == 0) {
			v1.push_back({ vec1[i], vec2[i] });
		}

		if (dir == 1 && dir2 == 2) {
			cout << "-1";
			exit(0);
		}
	}

	v1.push_back({ vec1.back(), vec2.back() });
	ll ans = 0;

	ll upd = -1;

	for (int i = 0; i < v1.size(); i++) {
		if (upd != -1) {
			v1[i].first = v1[upd].first - abs(upd - i);
			if (upd == i) upd = -1;
		}
		if (v1[i].first == v1[i].second) continue;

		auto it = lower_bound(v1.begin(), v1.end(), v1[fnd(i)]);

		if (upd == -1 || (it - v1.begin()) >= upd) {
			upd = (it - v1.begin());
			if (v1[i].second != v1[(it - v1.begin())].first - abs(i - (it - v1.begin()))) {
				cout << "-1";
				exit(0);
			}

			ans += abs(i - (it - v1.begin()));
		}
		else {
			ll j = (it - v1.begin());

			v1[j].first = v1[upd].first - abs(upd - j);
			if (v1[i].second != v1[(it - v1.begin())].first - abs(i - (it - v1.begin()))) {
				cout << "-1";
				exit(0);
			}

			ans += abs(i - (it - v1.begin()));
		}
	}

	return ans;
}

int main()
{
	fastInp;

	ll n, l;
	cin >> n >> l;

	vec1.resize(n);
	vec2.resize(n);

	for (auto &cur : vec1) cin >> cur;
	for (auto &cur : vec2) cin >> cur;
	vec1.insert(vec1.begin(), 0);
	vec2.insert(vec2.begin(), 0);
	vec1.push_back(l + 1);
	vec2.push_back(l + 1);

	ll q = get();

	for (int i = 0; i < vec1.size(); i++) {
		vec1[i] = l + 1 - vec1[i];
		vec2[i] = l + 1 - vec2[i];
	}

	reverse(vec1.begin(), vec1.end());
	reverse(vec2.begin(), vec2.end());

	ll k = q + get();

	cout << k;

	return 0;
}

#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
#define ll long long
//#define file
using namespace std;

int a[100002],b[100002],n,L,i,j,k,l,sum;
ll ans;

void Exit() {printf("-1\n");exit(0);}

int main()
{
	#ifdef file
	freopen("c.in","r",stdin);
	#endif

	scanf("%d%d",&n,&L);
	fo(i,1,n) scanf("%d",&a[i]);
	fo(i,1,n) scanf("%d",&b[i]);

	a[0]=0,a[n+1]=L+1;
	b[0]=0,b[n+1]=L+1;
	l=0;
	fo(i,0,n)
	if (b[i]+1<b[i+1])
	{
		sum=0;
		while (l<=n && a[l]+1==a[l+1]) ++l;
		k=min(l,i);

		while (l<=n && sum<b[i+1]-b[i]-1)
		sum+=a[l+1]-a[l]-1,++l;

		if (sum>b[i+1]-b[i]-1) Exit();
		ans+=max(l,i+1)-k-1;
	}
	printf("%lld\n",ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1);
const int maxn = 2e6 + 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ull base = 131;
const ull mod1 = 1e9 + 9;
const ull mod2 = 1610612741;

ll x, y, a, b;

int main() {
    scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
    ll ans = 0;
    ll k = b/(a-1);
    while (x <= k && x*a < y) x *= a, ++ans;
    if (x < y) ans += (y-x-1)/b;
    printf("%lld\n", ans);

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i = 0; i < (long long)(n); i++)
typedef long long  ll;
const ll mod=1e9+7;
typedef pair<int,int> P;
const int INF=1<<29;
int main(){
    double a,b;
    cin>>a>>b;
    cout<<fixed<<setprecision(10);
    cout<<a*b/100<<endl;
}


#include <iostream>
#include <vector>
#include <utility>
#include<algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#include <tuple>
#include <set>
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main(){
  ll t,n;
  cin >> t;
  cin >> n;
  ll ans = n-1+(100*n)/t;
  if((100*n)%t != 0){
    ans ++;
  }
  cout << ans << endl;
  return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include <cfenv>
#include <cmath>

using namespace std;

int main() {
	double t, N;
	cin >> t >> N;

	double A = ceil(N * 100 / t);

	double B = floor(t * (A - 1.0) / 100);

	long long C = (long long)A + (long long)B;

	cout << C << endl;

	return 0;
}


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

int main() {
    int n,w;

    cin >> n >> w;

    cout << n/w << endl;

    return 0;
}

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

#define stp(var, init, end) for (auto var = init; var < end; ++var)
#define stpe(var, init, end) for (auto var = init; var <= end; ++var)
#define ll long long

int main(void)
{
    string a, b;
    cin >> a >> b;
    int sa = 0, sb = 0;
    stp(i, 0, 3)
    {
        sa += a[i] - '0';
        sb += b[i] - '0';
    }
    cout << max(sa, sb);
    return 0;
}


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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

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

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

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

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

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

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

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

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

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

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll n=nxt();
    string s;
    cin>>s;
    string s1="110";
    ll ans=0;
    rep(a,0,3)
    {
        bool flag=true;
        rep(b,a,a+n)
        {
            if(s1[b%3]!=s[b-a])
                flag=false;
        }
        if(flag)
        {
            ll tot=(a+n+2)/3;
            ans+=10000000000-tot+1;
        }
    }
    cout<<ans<<endl;
	printclock;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1000000007;

int main() {
    int n;
    cin >> n;
    string t;
    cin >> t;
    if (t == "0") {
        cout << 10000000000 << endl;
        return 0;
    }
    if (t == "1") {
        cout << 20000000000 << endl;
        return 0;
    }
    if (t == "10") {
        cout << 10000000000 << endl;
        return 0;
    }
    if (t == "01") {
        cout << 9999999999 << endl;
        return 0;
    }
    if (t == "11") {
        cout << 10000000000 << endl;
        return 0;
    }
    ll zero = 0;
    if (t[0] == '0') {
        zero = 0;
    } else if (t[1] == '0') {
        zero = 1;
    } else if (t[2] == '0') {
        zero = 2;
    } else {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i < n; ++i) {
        if (i % 3 == zero && t[i] == '0') continue;
        if (i % 3 != zero && t[i] == '1') continue;
        cout << 0 << endl;
        return 0;
    }
    ll m = (n + 2 - zero + 2) / 3;
    cout << 10000000000 - m + 1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for(int I = A; I < B; I++)
#define RFOR(I, A, B) for(int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

ll PD[205][205];

ll comb(ll k, ll n){
	if(k == 0) return 1;
	if(k == n) return 1;
	ll &mem = PD[k][n];
	if(mem != -1) return mem;

	return mem = comb(k-1, n-1) + comb(k, n-1);
}

int main(){
	ll l;
	cin >> l;
	memset(PD, -1, sizeof PD);
	cout << comb(11, l-1) << endl;
	return 0;
}

//Code by Mukul Totla
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
void sublime(){
    #ifndef ONLINE_JUDGE
    freopen ("input.txt", "r", stdin);
    freopen ("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
      }

int main()
{
   sublime();
   string s;
   ll i;
   cin>>s;
   if(s.length()==1)
   {
    if(s=="8")
      cout<<"Yes";
    else
      cout<<"No";
   }
   else if(s.length()==2)
   {
    map<char,ll>mp;
    for(i=0; i<s.length(); i++)
      mp[s[i]]++;
    int f=0;
    for(i=16; i<=100; i+=8)
    {
      map<char,ll>m;
      string str=to_string(i);
      m[str[0]]++;
      m[str[1]]++;
      if(mp[str[0]]>=m[str[0]] && mp[str[1]]>=m[str[1]])
      {
        f=1;
        break;
      }
    }
    if(f==1)
      cout<<"Yes";
    else
      cout<<"No";
   }
   else
   {
    map<char,ll>mp;
    for(i=0; i<s.length(); i++)
      mp[s[i]]++;
    int f=0;
    for(i=104; i<1000; i+=8)
    {
      map<char,ll>m;
      string str=to_string(i);
      m[str[0]]++;
      m[str[1]]++;
      m[str[2]]++;
      if(mp[str[0]]>=m[str[0]] && mp[str[1]]>=m[str[1]]
        && mp[str[2]]>=m[str[2]])
      {
        f=1;
        break;
      }
    }
    if(f==1)
      cout<<"Yes";
    else
      cout<<"No";
   }
  return 0;
}


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

int main(void){
  int64_t a,b;
  cin >> a >> b;
  cout << max(2*a+100-b,0L) << endl;
  return 0;
}

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

#define NL '\n'
#define sz(x) x.size()
#define INF LONG_MAX
#define IINF LLONG_MAX
#define PI (2.0*acos(0.0))

#define sfi(x) scanf("%d",&x)
#define sfi2(x,y) scanf("%d %d",&x,&y)
#define sfi3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sfl(x) scanf("%lld",&x)
#define sfl2(x,y) scanf("%lld %lld",&x,&y)
#define sfl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sfd(x) scanf("%lf",&x);
#define sfd2(x,y) scanf("%lf %lf",&x,&y);

#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(max3(a,b,c),d)
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d) min(a,min3(b,c,d))

#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define For(i, j, k) for(int i = j; i <= k; i++)
#define Rof(i, j, k) for(int i = j; i >= k; i--)
#define all(x) x.begin(),x.end()
#define asort(x) sort(all(x));
#define dsort(x) sort(all(x), greater<int>())
#define unq(x) x.erase(unique(x.begin(),x.end()),x.end())

#define mem(ara,val) memset(ara,val,sizeof(ara))
#define READ freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout)
#define IOS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0)

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair <int,int> PII;
typedef pair <LL,LL> PLL;

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

int main()
{

//    int t; sfi(t); For(tt,1,t){
//
//
//    }

        int a, b;
        sfi2(a,b);
        printf("%d\n", max(((2*a)+100)-b,0));
//    main();
}


#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
constexpr long long MOD = 998244353;
constexpr long long MOD32 = 665496236;

int main(){
    long H=0, W=0, K=0;
    char c=getchar_unlocked();
    while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();}
    c=getchar_unlocked();
    while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();}
    char f[H][W];
    for(int i = 0;i < H;++i) for(int j = 0;j < W;++j) f[i][j] = '\0';

    long long y = H * W - K;
    while(--K>=0){
        long h=0, w=0;
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
        c=getchar_unlocked();
        while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
        f[h-1][w-1] = getchar_unlocked();
        getchar_unlocked();
    }
    --H; --W;
    long long dp[W+1] = {0};
    dp[0] = 1;
    for(int i = 0; i < H; ++i){
        for (int j = 0; j < W; ++j){
            switch(f[i][j]) {
                case 'R':
                    dp[j + 1] += dp[j];
                    dp[j] = 0;
                    break;
                case '\0':
                    dp[j] = dp[j] * MOD32 % MOD;
                case 'X':
                    dp[j + 1] += dp[j];
            }
        }
            switch(f[i][W]) {
                case 'R':
                    dp[W] = 0;
                    break;
                case '\0':
                    dp[W] = dp[W] * MOD32 % MOD;
            }
    }
    for (int j = 0; j < W; ++j){
        switch(f[H][j]) {
            case '\0':
                dp[j] = dp[j] * MOD32 % MOD;
            case 'R':
            case 'X':
                dp[j + 1] += dp[j];
        }
    }

    long long ans = dp[W] % MOD;
    long long x = 3;
    while(y) {
      if(y & 1) ans = ans * x % MOD;
      x = x * x % MOD;
      y >>= 1;
    }
    printf("%d", ans);

    return 0;
}


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

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

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

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

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

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

string to_string(bool b) {
  return (b ? "true" : "false");
}

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

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

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

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

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

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

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}
const int maxn = 10;
int N,P,A[maxn];
//int res;
//void dfs(int pos){
	//if(pos >= N){
		//int act = 0;
		//bool can = true;
		//forn(i,0,N){
			//act += A[i];
			//if(act%P == 0){
				//can = false;
				//break;
			//}
		//}
		//res += (can == true);
		//return;
	//}
	//forn(i,1,P){
		//A[pos] = i;
		//dfs(pos+1);
	//}
//}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> P;
	//dfs(0);
	//cout << res << '\n';
	int res = mul(P-1,power(P-2,N-1));
	cout << res << '\n';

	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/

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

int main() {

    int N;
    string S;
    cin >> N >> S;

    string V;

    for (int i = 0; i < N; i++) {
        V.push_back(S.at(i));
        if (V.size() >= 3) {
            int index = V.size() - 1;
            if (V.at(index) == 'x' && V.at(index - 1) == 'o' && V.at(index - 2) == 'f') {
                V.erase(index - 2, 3);
            }
        }
    }

    //cout << V << endl;
    cout << V.size() << endl;

}


#include <iostream>
#include <vector>
#include <algorithm> //sort,二分探査など
//#include<bitset>
//#include<cmath>//pow,logなど
//#include<deque>
//#include<functional>
//#include<iterator>
//#include<map>
//#include<numeric>//gcd,lcm
//#include<queue>
//#include<set>
//#include<stack>
//#include<string>
//#include<utility>//pair

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

//浮動小数点処理高速化
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using i64 = int64_t;
using f80 = __float80;
using u64 = uint_fast64_t;

constexpr i64 INF = 1010000000000000017LL;
constexpr u64 MOD = 1000000007LL;
constexpr f80 PI = 3.14159265358979323846;

#define _overload3(_1, _2, _3, name, ...) name
#define repi(i, start, end) for (int i = (int)(start); i < (int)(end); ++i)
#define _rep(i, n) repi(i, 0, n)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, start, end) for (int i = (int)(start); i > (int)(end); --i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

#define v(T) vector<T>
#define vv(T) v(v(T))
#define vvv(T) v(v(v(T)))
typedef v(int) vi;

#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>

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

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
  rep(i, v.size()) os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
  for (T &in : v)
    is >> in;
  return is;
}

//sort(rng(P),[](const vi &a, const vi &b){return a[1]<b[1];});

void Main()
{
  i64 A, B, C;
  cin >> A >> B >> C;
  i64 ans = A * (A + 1) / 2;
  i64 m = 998244353;
  ans %= m;

  ans *= (B * (B + 1) / 2) % m;
  ans %= m;

  ans *= (C * (C + 1) / 2) % m;
  ans %= m;

  cout << ans << "\n";
}

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

#include <iostream>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>

// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>

typedef long long LL;
//typedef long double LD;
using namespace std;

//#define MOD 1000000007
#define MOD 998244353
//#define MAX 100100
//#define NIL -1
//#define INFTY 1000000000000000000

int main(){
    LL a;
    LL b;
    LL c;
    LL ans;
    cin >> a >> b >> c;
    ans=(a*(a+1)/2)%MOD;
    ans=(ans*((b*(b+1)/2)%MOD))%MOD;
    ans=(ans*((c*(c+1)/2)%MOD))%MOD;

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long n;
    cin>>n;

    long long tmp=n;
    long long num=0;
    long long len=1;
    bool flag=true;
    while(tmp>0){
        if(flag&&tmp%10==0) len*=10;
        else flag=false;
        num=num*10+tmp%10;
        tmp/=10;
    }
    num*=len;

    cout<<(num==n?"Yes":"No")<<endl;

    return 0;
}

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long
#define all(x) (x).begin(),(x).end()
using namespace std;
template<typename KeyType, typename ValueType>
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}
const int mod=1e9+7;
void solve()
{
	string s;
	cin>>s;
	string temp=s;
	reverse(temp.begin(),temp.end());
	stringstream ss(temp);
	int g=0;
	ss>>g;
	string ans=to_string(g);
	string rev=ans;
	reverse(rev.begin(),rev.end());
	if(rev==ans)
	{
		cout<<"Yes";
	}
	else
	{
		cout<<"No";
	}
	return;

}
int main()
{
	solve();
}


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

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

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

typedef long long ll;
typedef pair <int,ll> Pair;

const int maxn=2e5+5,mod=1e9+7;

int n,ans;
ll dep[maxn];
vector <Pair> g[maxn];

void dfs(int u,int fa) {
	for(int i=0;i<g[u].size();++i) {
		int v=g[u][i].first;
		if(v==fa) continue;
		dep[v]=(dep[u]^g[u][i].second);
		dfs(v,u);
	}
}

int main() {
	n=read(9);
	rep(i,1,n-1) {
		int u=read(9),v=read(9); ll w=read(9ll);
		g[u].push_back(make_pair(v,w)),g[v].push_back(make_pair(u,w));
	}
	dfs(1,0);
	rep(i,0,60) {
		int cnt=0;
		rep(j,1,n) if(dep[j]>>i&1) ++cnt;
		ans=(ans+1ll*(1ll<<i)%mod*cnt%mod*(n-cnt)%mod)%mod;
	}
	print(ans,'\n');
	return 0;
}

#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 100000000000000000LL
#define ll  long long
#define debug(x) cout << #x << " "<< x <<endl;
#define debug_vector(v) cout << #v << " "; _print(v);
const ll inf =1e9+44;
const int MAX=3e5+9;
const ll MOD= 1e9+7;
const double eps=1e-10;
double const PI=3.1415926535897931;
using namespace std;
int dx[4] = {1 ,0 , - 1, 0};
int dy[4] = {0 ,1 ,  0 , -1};
void _print(vector<int> v){
	cout << "[ ";
	for ( auto u : v)
		cout << u << " ";
	cout << " ]"<< endl;
}
ll x[MAX];
vector < pair<int , ll >  >adj[MAX];
ll arr[MAX];
void dfs(int u , int  p ){

	for(auto v : adj[u]){
		if(v.first == p)
			continue;
		x[v.first] = x[u]^v.second;
		dfs(v.first , u);
	}
}
int main(){
	int n;
	cin >> n;
	for(int i = 0 ; i <n-1 ;i++){
		int x, y;
		ll w;
		cin >> x >> y >> w;
		adj[x].push_back(  { y , w});
		adj[y].push_back( { x, w });
	}
	dfs( 1 , 0 );

	ll ans = 0;
	for ( ll j = 0 ; j <=60 ; j++){
		ll o = 0 , z = 0;
		for (int i = 1 ; i <= n ; i++){
			 if (x[i]&(1ll<<j))
			 	o ++;
			 else
			 	z ++;
			// cout << x[i] << endl;
		}
		//cout << z <<" " <<o << endl;
		 z  = (z*o+MOD)%MOD;
		 ans = ans  + (1ll<<j)%MOD*z%MOD+MOD;
		 ans = ans%MOD;
	}
	cout << ans;

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Test int T;cin>>T;while(T--)
#define ls lower_bound
#define up upper_bound
#define bs binary_search
#define S second
#define F first
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvi vector<vi>
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define m_n(v) *min_element(all(v))
#define m_x(v) *max_element(all(v))
#define _(a) for(auto &i:a)
#define nl cout<<"\n";
#define momo(k) memset(k,0,sizeof(k))
#define momi(k) memset(k,-1,sizeof(k))
// #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;

int power(int x, int y,int mod){
    int res = 1;
    while (y){
        if (y & 1)
        res = (res*x)%mod;
        y = y>>1;
        x = (x*x)%mod;
    }
    return res;
}
int modInverse(int a, int m){
    int m0 = m;
    int y = 0, x = 1;
    if (m == 1)  return 0;
    while (a > 1){
        int q = a / m;
        int t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0) x += m0;
    return x;
}
int binomial(int n,int k){
    int res = 1;
    if ( k > n - k ) k = n - k;
    for (int i = 0; i < k; ++i){
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

// vector<int> prime;
// void sieve(){
//     bool arr[30000]={0};
//     prime.pb(2);
//     for(int i=3;i<30000;i+=2){
//         if(arr[i])continue;
//         for(int j=i*i;j<30000;j+=i) arr[j]=1;
//         prime.pb(i);
//     }
// }
// const int N=1e18+2;
const int mod = 998244353;
const int N = 2e5+9;

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

int32_t main(){
    // doper();
    IOS;
    int n;
    cin>>n;
    string s;
    cin>>s;
    stack<char> st;
    for(auto i:s){
        if(i == 'x' and st.size()>0){
            if(st.top() == 'o' and st.size()>1){
                bool flag = 0;
                st.pop();
                if(st.top() == 'f' ) {
                    st.pop();
                    flag = 1;
                }
                if(flag == 0){
                    st.push('o');
                    st.push(i);
                }
            }else st.push(i);
        }else st.push(i);
    }
    cout<<st.size();
    // while(!st.empty()){
    //     cout<<st.top();nl;
    //     st.pop();
    // }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define d(x) cout<<x<<" "
#define nl cout<<endl
#define rep(var,init_val,final_val) for(int var = init_val; var < final_val; ++var)
#define tt ll t;cin>>t;while(t--)
#define show(a,b) rep(i,0,b) d(a[i]);nl;
#define mod 1000000007
#define vi vector<int>
#define pii pair<int,int>
const int d4i[] = {-1,0,1,0};
const int d4j[] = {0,1,0,-1};

const int d8i[] = {-1,-1,-1,0,1,1,1,0};
const int d8j[] = {-1,0,1,1,1,0,-1,-1};
// #define ONLINE_JUDGE true

void solve(){
	//cout<<"HELLO\n";
	int n;
	string s ,ans;
	cin>>n>>s;
	ans=s;
	int j = 0;
	rep(i,0,n){
		//cout<<i<<endl;
		ans[j]=s[i];
		j++;
		if(j<3) continue;
		if(ans.substr(j-3,3)=="fox") j-=3;
		//cout<<ans<<" "<<j<<endl;

	}
	cout<<j<<endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	solve();
	return 0;
}