// AtCoder Templete

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define lloop(i, r, n) for (int i = (r) - 1; i >= 0; i--)
#define all(in) in.begin(),in.end()
#define pb push_back
#define fi first
#define se second
#define re resize
#define as assign
#define cl clear

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

using ui = unsigned int;
using ll = long long;
using ull = unsigned long long;
using dd = double;
using ldd = long double;

using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<dd,dd>;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

using vb = vector<bool>;
using vs = vector<string>;
using vdd = vector<dd>;
using vldd = vector<ldd>;

using pq = priority_queue<int, vector<int>, less<int>>; // 降順に取り出す
using pqq = priority_queue<int, vector<int>, greater<int>>; // 昇順に取り出す

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

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const int INF = 1e9;
const ll inf = 4e18;

const int MAX = 300300;
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1; // 階乗
    finv[0] = finv[1] = 1; // 階乗の逆元
    inv[1] = 1; // 逆元
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = ( MOD - inv[MOD%i] * (MOD/i) % MOD ) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
long long COM(int n, int k){
    if (n < 0 || k < 0 || n < k) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long PER(int n, int k) {
    if (n < 0 || k < 0 || n < k) return 0;
    return fac[n] * finv[n - k] % MOD;
}

ll modpow(ll a, ll n, ll p) {
    ll res = 1;
    while (n) {
        if (n & 1) res = res * a % p;
        a = a * a % p;
        n >>= 1;
    }
    return res;
}
ll modinv(ll a, ll p) { return modpow(a, p-2, p); }

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

int n;
vvi g;
vi path;
vi check;
vi ans;
int num;

pii dfs1(int v, int p) {
    pii res = pii(0,v);
    for ( auto nv : g[v] ) if ( nv != p ) {
        auto ch = dfs1(nv, v);
        ch.fi++;
        chmax( res, ch );
    }
    return res;
}

bool dfs2(int v, int p, int t) {
    bool ch = false;
    if ( v == t ) ch = true;
    for ( auto nv : g[v] ) if ( nv != p && dfs2(nv, v, t) ) ch = true;
    if ( ch || v == t ) path.pb(v);
    return ch;
}

void dfs3(int v, int p) {
    ans[v] = ++num;
    for ( auto nv : g[v] ) if ( nv != p && nv != check[v] ) dfs3(nv, v);
    if ( check[v] >= 0 ) dfs3(check[v], v);
    ++num;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // cout << fixed << setprecision(10) <<  << endl;
    COMinit();

    /* write code under here */
    cin >> n;

    g.re(n);
    rep(i,n-1) {
        int s, t; cin >> s >> t;
        s--; t--;
        g[s].pb(t);
        g[t].pb(s);
    }

    auto p = dfs1(0, -1);
    auto q = dfs1(p.se, -1);

    dfs2(p.se, -1, q.se);
    reverse(all(path));

    check.as(n,-1);
    rep(i,(int)path.size()) if ( i ) check[ path[i-1] ] = path[i];

    ans.as(n,-1);
    num = 0;
    dfs3(p.se, -1);

    for ( auto v : ans ) cout << v << " ";
    cout << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int O = 2e5 + 1;

int t = 1;

vector <int> c, dp, g[O], Max;

int dfs(int u, int par = 0){
    Max[u] = 1;
    for (int v : g[u]){
        if (v == par) continue;
        dp[v] = dp[u] + 1;
        Max[u] = max(Max[u], dfs(v, u) + 1);
    }
    return Max[u];
}

void dfs1(int u, int par = 0){
    c[u] = t;
    vector <pair <int, int>> m;
    for (int v : g[u]){
        if (v == par) continue;
        m.push_back({Max[v], v});
    }
    sort(m.begin(), m.end());
    for (pair <int, int> v : m){
        t += 1;
        dfs1(v.second, u);
        t += 1;
    }
}

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n; cin >> n;
    c.resize(n + 1);
    dp.resize(n + 1);
    Max.resize(n + 1);
    for (int i = 1; i < n; ++ i){
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1);
    int compare = 0;
    int root = 1;
    for (int i = 1; i <= n; ++ i){
        if (dp[i] > compare){
            compare = dp[i];
            root = i;
        }
    }
    dfs(root);
    dfs1(root);
    for (int i = 1; i <= n; ++ i) cout << c[i] << " ";
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)

int N, M;
vector<pair<int, int> > G[100011];

VI que;
bool vis[100011];
int ans[100011];

void MAIN() {
    scanf("%d%d", &N, &M);
    REP (i, M) {
	int x, y, c;
	scanf("%d%d%d", &x, &y, &c);
	x--; y--;
	G[x].emplace_back(y, c);
	G[y].emplace_back(x, c);
    }

    que.push_back(0);
    vis[0] = true;
    ans[0] = 1;
    REP (i, N) {
	int v = que[i];
	EACH (e, G[v]) if (!vis[e->first]) {
	    int w = e->first;
	    vis[w] = true;
	    que.push_back(w);

	    if (ans[v] == e->second) {
		ans[w] = ans[v] + 1;
		if (ans[w] > N) ans[w] = 1;
	    } else {
		ans[w] = e->second;
	    }
	}
    }

    REP (i, N) printf("%d\n", ans[i]);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i = 0;i < N;++i)
#define rep1(i,N) for(int i = 1;i < N;++i)
#define co(x) cout << x << endl
#define cosp(x) cout << x << " "
using ll = long long;
using ull = unsigned long long;
int sub(int N){
    vector<char>n;
    string c = to_string(N);
    for(int i = 0;i < c.length();i++) n.push_back(c[i]);
    sort(n.begin(),n.end());
    int a = 0,b = 0;
    for(int i = 0;i < n.size();i++){
        a += (int)n[i] - '0';
        if(i != n.size()-1) a *= 10;
    }
    reverse(n.begin(),n.end());
    for(int i = 0;i < n.size();i++){
        b += (int)n[i] - '0';
        if(i != n.size()-1) b *= 10;
    }
    a = b-a;
    return a;
}
int main(){
int N;
int K;
cin >> N >> K;
int a = N;
rep(i,K){
    a = sub(a);
}
co(a);
}


/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

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

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

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

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

ll conbinationMemo[201][12];

void cmemoInit(){
    rep(i, 201){
        rep(j, 12){
            conbinationMemo[i][j] = -1;
        }
    }
}

ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------

int main(void){
    ld n;
    cin >> n;
    ld x0, y0, xh, yh;
    cin >> x0 >> y0 >> xh >> yh;
    pair<ld, ld> cent = {(x0 + xh) / 2.0, (y0 + yh) / 2.0};
    ld xz, yz;
    xz = x0 - cent.first;
    yz = y0 - cent.second;

    ld deg = atan2(yz, xz);
    deg += (2.0 * M_PI) / n;
    ld d = sqrt(xz * xz + yz * yz);
    pair<ld,ld> ans = {cos(deg) * d, sin(deg) * d};

    ans.first += cent.first;
    ans.second += cent.second;

    cout << fixed << setprecision(10) << ans.first << " " << ans.second << endl;
}

#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<ll,ll>;
using vi = vector<int>;
const ll INF = 1009009009009009009;

int n;
double PI = acos(-1);

int main(){
    cin >> n;

    double x,y;
    cin >> x >> y;
    complex<double> c0(x,y);
    cin >> x >> y;
    complex<double> c2(x,y);

    complex<double> center = (c0+c2)/2.0;

    complex<double> rot(cos(2*PI/n),sin(2*PI/n));

    complex<double> r = ((c0-c2)/2.0) * rot;

    complex<double> ans = center + r;

    printf("%.12f %.12f\n",ans.real(),ans.imag());

}


#include <bits/stdc++.h>
/*
// using ASL (use make)
#include <atcoder/all>
using namespace atcoder;
*/
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
// 1次元 vector print
template <class T>
ostream &operator<<(ostream &s, vector<T> vec) {
    for (int i = 0; i < (int)vec.size(); ++i) {
        if (i > 0) {
            s << " ";
        }
        s << vec[i];
    }
    return s << endl;
}
// 2次元 vector print
template <class T>
ostream &operator<<(ostream &s, vector<vector<T>> vec2) {
    for (int i = 0; i < (int)vec2.size(); ++i) {
        s << vec2[i];
    }
    return s;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;

int main() {
    int x;
    cin >> x;
    cout << max(x, 0) << endl;
}

//Author : Prakhar Asaiya
#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(ll i = a; i < b; ++i)
#define REPN(i,a,b) for(ll i = a; i <= b; ++i)
#define REPV(i,a,b) for(ll i = a; i >= b; --i)
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define with_tests int t; cin>>t; while(t--){ solve(); }
#define without_tests solve();
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 100000000
#define ff first
#define ss second
#define max(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
#define min(a, b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
const int M=1e9+7;

void solve()
{
	int n;
	cin>>n;
	if(n < 0)
	cout<<"0";
	else
	cout<<n;

}

int main()
{/*
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#*/
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  //with_tests;
  without_tests;
  return 0;
}


/*
"An anomaly, I'm Muhammad Ali
  Cause I know one day I'm gonna be the"
                - Greatest, Eminem
*/
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#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;
typedef long long int ll;
#define ff first
#define Shazam ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define ss second
#define all(c) c.begin(),c.end()
#define endl "\n"
#define test() int t; cin>>t; while(t--)
#define fl(i,a,b) for(int i = a ; i <b ;i++)
#define get(a) fl(i,0,a.size())  cin>>a[i];
#define pra(a) fl(i,0,a.size()) cout<<a[i]<<" ";	cout<<endl;
#define pr(a,n) fl(i,0,n) cout<<a[i]<<" ";	cout<<endl;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll INF = 2e18;
const int inf = 2e9;
const int mod1 = 1e9 + 7;

int main(){
    Shazam;
    int n; cin >> n;
    vector<ll> a(n);
    get(a);
    unordered_map<ll, ll> dp;
    int lo = *min_element(all(a));
    for(ll x : a){
        for(ll i = 1; i * i <= x && i < lo; i++){
            if(x % i == 0){
                dp[i] = __gcd(dp[i], x);
                if(x / i != i && x / i < lo){
                    dp[x / i] = __gcd(dp[x / i], x);
                }
            }
        }
    }
    int ans = 1;
    for(auto pp : dp){
        ans += (pp.ff == pp.ss);
    }
    cout << ans << endl;
    return 0;
}

// Problem: A - Long Common Subsequence
// Contest: AtCoder - AtCoder Grand Contest 052
// URL: https://atcoder.jp/contests/agc052/tasks/agc052_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

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

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

void solve()
{
    int n;
    cin>>n;
    string a,b,c;
    cin>>a>>b>>c;
    for(int i=0;i<n;i++)
    {
    	cout<<"0";
    }
    for(int i=0;i<n;i++)
    {
    	cout<<"1";
    }
    cout<<"0"<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    cin>>t;
    while(t--)
        solve();
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
#define ll long long
#define VV(t) vector<vector<t>>
using namespace std;
using Graph = VV(int);
const int INF = 1<<29;

int factorial(int n) {
   if (n<=1) return 1;
   else return n*factorial(n-1);
}

int main() {
   int N; cin >> N;
   Graph G(N, vector<int>(N, INF));
   rep(i, N) {
      rep(j, N) {
         char c; cin >> c;
         if (c == '1') G[i][j] = 1;
      }
      G[i][i] = 0;
   }
   rep(k, N) {
      rep(i, N) {
         rep(j, N) {
            G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
         }
      }
   }
   double v = 0;
   rep(i, N) {
      int nonInf = 0;
      rep(j, N) if (G[j][i] < INF/2) nonInf += 1;
      v += (double)1 / nonInf;
   }
   cout << fixed << setprecision(20) << v << endl;
}

#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")
#define int long long int
#define f first
#define s second
#define pb push_back
#define endl '\n'
#define T long double

vector<vector<int>>adj;
bool vis[105];
int cnt[105];

void dfs(int v){
	vis[v]=1;
	cnt[v]++;
	for(auto u:adj[v]){
		if(vis[u]){
			continue;
		}
		dfs(u);
	}
}

main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	char arr[n][n];
	adj.resize(n+1);
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			cin>>arr[i][j];
			if(arr[i][j]=='1'){
				adj[i+1].pb(j+1);
			}
		}
	}
	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			vis[j]=0;
		}
		dfs(i);

	}
	T ans=0;
	for(int i=1; i<=n; i++){
		ans=ans+(1.0/cnt[i]);
	}
	cout<<setprecision(20)<<ans;
	return 0;
}


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

int main() {

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

	int n;
	cin >> n;

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

	sort(a, a+n);

	ll ans=0, fs=0;
	for(int i=1; i<n; i++) {
		fs += abs(a[0] - a[i]);
	}
	ans = fs;
	for(int i=1; i < n; i++) {
		ll d = abs(a[i] - a[i-1]);
		fs = fs - (d * (n-i));
		ans += fs;
	}

	cout << ans << endl;
}

#include <bits/stdc++.h>
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
constexpr int P = 998244353;
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int N;
	std::cin >> N;
	std::vector<int> a(N);
	for (int i = 0; i < N; i++)
		std::cin >> a[i];
	std::sort(a.begin(), a.end());
	int ans = 0, sum = 0;
	for (int i = 0; i < N; i++) {
		ans += (i64)a[i] * a[i] % P;
		if (ans >= P)
			ans -= P;
	}
	for (int i = 0; i < N; i++) {
		ans += (i64)a[i] * sum % P;
		if (ans >= P)
			ans -= P;
		sum *= 2;
		if (sum >= P)
			sum -= P;
		sum += a[i];
		if (sum >= P)
			sum -= P;
	}
	std::cout << ans << "\n";
	return 0;
}


#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

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

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi=acos(-1);
typedef double db;
typedef long double ldb;
typedef pair <int,int> pii;
typedef pair <db,db> pdd;
typedef vector<int> vi;
typedef vector<vector<int> > matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,v) memset(x,v,sizeof(x))

#define fi first
#define se second
#define endl "\n"
#define debug(x) (cerr << #x << ": " << x << "\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define pii pair<int,int>

inline int gcd(int a,int b){if(a==0) return b;return gcd(b%a,a);}
inline int power(ll a,ll n,ll m){if(n==0)return 1;ll p=power(a,n/2,m);p=(p%m*p%m)%m;if(n%2) return (p%m*a%m)%m;else return p;}
const double EPS = 1e-9;
const ll MOD =998244353;
const ll hell=1000000007;
const int INF = 1e18;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
    return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1 };

const int N=200005;

void solve()
{

    string s;
    cin>>s;
    int n=s.length();
    int f[10]={0};
    for(int i=0;i<n;i++)
        f[s[i]-'0']++;
    if(n==1)
    {
        if(s[0]=='8')
        {
            cout<<"Yes"<<endl;

        }
        else
            cout<<"No"<<endl;
        return;
    }
    if(n==2)
    {
        int vl=s[0]-'0';
        vl=vl*10+s[1]-'0';
        if(!(vl%8))
        {
            cout<<"Yes"<<endl;
            return;
        }
        vl=s[1]-'0';vl=vl*10+s[0]-'0';
        if(!(vl%8))
        {
            cout<<"Yes"<<endl;
            return;
        }
        cout<<"No"<<endl;
        return;
    }
    for(int i=8;i<1000;i+=8)
    {
        int x=i;vi ve;
        while(x>0)
        {
            ve.pb(x%10);
            x=x/10;
        }
        while(ve.size()<3)
            ve.pb(0);
        int pos=1;vi to;
        for(int j=0;j<ve.size();j++)
        {
            if(f[ve[j]])
            {
                f[ve[j]]--;
                to.pb(ve[j]);
            }
            else
            {pos=0;
                break;
            }
        }
        if(pos)
        {
            cout<<"Yes"<<endl;
            return;
        }
        for(int j:to)
            f[j]++;
    }
    cout<<"No"<<endl;

}

int32_t main()
{

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

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

int main() {
    string s, ans = "No";
    int slen;
    int num[10] = {}, dig[10] = {}, tem;
    bool flg;

    cin >> s;
    slen = s.size();
    for ( int i = 0; i < slen; i++ ) {
        num[s[i] - '0'] ++;
    }

    for ( int i = 0; i < 1000; i+=8 ) {
        tem = i;
        for ( int j = 0; j < min(3, slen); j++ ) {
            dig[tem % 10] ++;
            tem /= 10;
        }

        flg = true;
        for ( int j = 0; j < 10; j++ ) {
            if ( num[j] < dig[j] ) {
                flg = false;
            }
        }

        if ( flg ) {
            ans = "Yes";
        }

        for ( int j = 0; j < 10; j++ ) {
            dig[j] = 0;
        }
    }

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 2e18;
const int MOD = 1e9+7;
const int N = 2e5+1;

int main(){
fastio

int n;
int i;
cin>>n;

map<int,int> cnt;

for(i=0;i<n;i++){
    int x;
    cin>>x;
    for(int j=2;j*j<=x;j++){
        if(x%j)continue;
        cnt[j]++;
        while(x%j==0){
            x/=j;
        }
    }
    if(x>1)
        cnt[x]++;
}

int ans = 0,idx;
for(auto x : cnt){
    if(x.S > ans){
        ans = x.S;
        idx = x.F;
    }
}
cout<<idx;

}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    int ans = 0, rres = 0;
    for(int i = 2; i < 1001; i++){
        int res = 0;
        for(int j = 0; j < N; j++){
            if(A.at(j) % i == 0){
                res++;
            }
        }
        if(rres <= res){
            ans = i;
            rres = res;
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
//const long long inf = 1e15;
//const double Pi = acos(-1.0);
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define rep(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define lc cout<<"\n"
#define print2d(dp,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cerr<<dp[i][j]<<" ";cerr<<"\n";}

ll solve(ll n,ll i){
	if((i-2)<n){
		return (i-1);
	}else{
		return (2*n-i+1);
	}
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
	int tt=1;
	//cin>>tt;
	while(tt--){
		ll n,k;
		cin>>n>>k;
		k=abs(k);
		ll ans =0;
		rep(i,k+2,2*n+1){
			ans += solve(n,i)*solve(n,i-k);
		}
		cout<<ans;
	}
}

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

typedef pair<ll, ll> P;
int INF = 100000000;
ll x[1000], y[1000], p[1000];
int main()
{
  ll n, x = 100000000, k, ans = 0, count = 0, j, i;

  ll t[8][8];
  bool found = false;

  cin >> n >> k;

  rep(i, n)
  {
    rep(j, n)
    {
      cin >> t[i][j];
    }
  }

  vector<ll> p(n);

  rep(i, n)
      p[i] = i+1;

  do
  {
    ll len = t[0][p[0]];

    rep(i, n - 2)
        len += t[p[i]][p[i + 1]];

    len += t[p[n - 2]][0];

    if (len == k)
      ans++;

  } while (next_permutation(p.begin(), p.end()));

  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a= modpow(10, n, k) ;
// 10^nのmod k　を出す
int main() {
  long b,a,n,m,k,r,ans;
  ans=0;
  r=998244353;
  cin>>n>>m>>k;
  for(long i=1; i<k+1; i++){
    if(i==1){
      a=1;
    }
    else{
      a=modpow(i,n,r)-modpow(i-1,n,r);
      if(a<0){
        a=a+r;
      }
    }
    b=modpow(k-i+1,m,r);
    a=a*b;
    a=a%r;
    ans=ans+a;
    ans=ans%r;
  }
  if(m==1 || n==1){
    ans=modpow(k,max(n,m),r);

  }

  cout<<ans;
}

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

//#define TLE

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

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

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

#define M_PI 3.14159265358979323846
#define micro 0.000001

using namespace std;

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

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

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

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

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

	return ret;
}

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

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

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

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


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

map<long long, long long> mp;

ll solve(ll x, ll y){
    if(x == y)return 0;
    if(y < x){
        return abs(x - y);
    }

    if(mp[y] != 0){
        //cout << y << " " << dp[y] << endl;
        return mp[y];
    }

    //cout << y << endl;
    ll z = abs(x - y);
    if(y%2 == 0){
        mp[y] = min(z, solve(x, y/2) + 1);
        return mp[y];
    }
    else{
        mp[y] = min(z, min(solve(x, y-1) + 1, solve(x, y+1) + 1));
        return mp[y];
    }
}

int main(){
    ll x, y;
    cin >> x >> y;

    ll ans = abs(x - y);
    ll s = solve(x, y);
    cout << min(s, ans) << endl;
    return 0;
}

/*
少加头文件，爆零两行泪
DP后效性，爆零两行泪
文件读入错，爆零两行泪
文件名写错，爆零两行泪
调完不编译，爆零两行泪
结论不证明，爆零两行泪
数组没开够，爆零两行泪
ll开 int，爆零两行泪
 dp 没初值，爆零两行泪
深搜没边界，爆零两行泪
广搜忘出队，爆零两行泪
模数没看清，爆零两行泪
越界不特判，爆零两行泪
多测不清空，爆零两行泪
空间胡乱开，爆零两行泪
无向变有向，爆零两行泪
调试忘删除，爆零两行泪
文件不保存，爆零两行泪
题目提交错，爆零两行泪
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
int dp[65][2];
bool fa[65],fb[65];
int geta(int l,int r){
	int re=0;
	FOR(i,l,r){
		re=re*2+fa[i];
	}
	RE re;
}
int getb(int l,int r){
	int re=0;
	FOR(i,l,r){
		re=re*2+fb[i];
	}
	RE re;
}
void solve(int i){
	gmin(dp[i][0],dp[i-1][1]+2);
	gmin(dp[i][0],dp[i-1][0]+fb[i]+1);
	gmin(dp[i][1],dp[i-1][1]-fb[i]+1+1);
	gmin(dp[i][1],dp[i-1][0]+2);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int a,b;
	cin>>a>>b;
	if(a>=b){
		cout<<a-b;RE 0;
	}
	int la,lb;
	la=lb=0;
	V<int> v;
	while(a){
		v.PB(a&1);
		a/=2;
	}
	while(!v.empty()){
		fa[++la]=v.back();
		v.pop_back();
	}
	while(b){
		v.PB(b&1);
		b/=2;
	}
	while(!v.empty()){
		fb[++lb]=v.back();
		v.pop_back();
	}
	dp[la-1][0]=abs(geta(1,la)-getb(1,la-1));
	dp[la-1][1]=abs(geta(1,la)-(getb(1,la-1)));
	dp[la][0]=abs(geta(1,la)-getb(1,la));
	dp[la][1]=abs((getb(1,la)+1)-geta(1,la));
	gmin(dp[la][0],abs(2*geta(1,la)-getb(1,la))+1);
	gmin(dp[la][1],abs((getb(1,la)+1)-2*geta(1,la))+1);
	solve(la);
	dp[la+1][0]=abs(geta(1,la)-getb(1,la+1));
	dp[la+1][1]=abs((getb(1,la+1)+1)-geta(1,la));
	solve(la+1);
	FOR(i,la+2,lb){
		dp[i][0]=1e18;
		dp[i][1]=1e18;
		solve(i);
	}
	cout<<min(dp[lb][0],dp[lb][1]+1);
	RE 0;
}


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

#define rep(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; (i) >= (int)(a); (i)--)
#define all(v) v.begin(), v.end()

typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = vector<V<T>>;

/* 提出時これをコメントアウトする */
#define LOCAL true

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    constexpr char endl = '\n';

    ll n,C;
    cin >> n >> C;
    V<ll> a(n), b(n), c(n);
    rep(i,0,n) cin >> a[i] >> b[i] >> c[i];

    priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> v;
    rep(i,0,n) {
        v.push({a[i], c[i]});
        v.push({b[i]+1, -c[i]});
    }

    ll pre = -1, sum = 0;
    ll cost = 0;
    while (!v.empty()) {
        ll date = v.top().first;
        cost += min(C, sum) * (date - pre);
        pre = date;
        while (!v.empty() && v.top().first == date) {
            sum += v.top().second;
            v.pop();
        }
    }
    cout << cost << endl;
    return 0;
}

#include <bits/stdc++.h>

#define var auto
#define ll long long

using namespace std;

int n;
int k;
int mod;

const int N_MAX = 100;
const int M_MAX = 100;

const int DAT_LEN = 100 * 100 * 100 / 2;
const int TMP_LEN = 7;
int tmp[TMP_LEN][DAT_LEN];

void multiple(int arr[DAT_LEN], int width, int k, int res[DAT_LEN]) {
    for (int i = 0; i < DAT_LEN; i++)
        tmp[0][i] = arr[i];

    for (int i = 1; i < TMP_LEN; i++)
    {
        for (int j = 0; j < DAT_LEN; j++)
            tmp[i][j] = tmp[i - 1][j];
        var shift = (1 << (i - 1)) * width;
        for (int j = shift; j < DAT_LEN; j++) {
            tmp[i][j] += tmp[i - 1][j - shift];
            if (mod < tmp[i][j]) tmp[i][j] -= mod;
        }
    }

    for (size_t i = 0; i < DAT_LEN; i++)
        res[i] = arr[i];

    int offset = width;
    for (int i = 0; i < TMP_LEN; i++)
    {
        if ((k >> i) & 1) {
            for (int j = offset; j < DAT_LEN; j++)
            {
                res[j] += tmp[i][j - offset];
                if (mod < res[j]) res[j] -= mod;
            }
            offset += (1 << i) * width;
        }
    }
}

int hoge[101][DAT_LEN];

int main() {
    cin >> n >> k >> mod;
    hoge[0][0] = 1;
    multiple(hoge[0], 3, k, hoge[1]);
    for (size_t i = 1; i <= n; i++) {
        multiple(hoge[i - 1], i, k, hoge[i]);
    }
    for (int x = 1; x <= n; x++)
    {
        ll res = 0;
        var target1 = hoge[x - 1];
        var target2 = hoge[n - x];
        for (size_t i = 0; i < DAT_LEN; i++)
        {
            res += ((ll)target1[i]) * target2[i];
            res %= mod;
        }
        res *= (k + 1);
        res -= 1;
        res %= mod; //←?
        cout << res << endl;
    }
}


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

int main() {
    int N;
    cin >> N;
    N *= 2;
    vector<pair<int64_t, char>> P(N);
    bool ro = false, go = false, bo = false;
    for (int i = 0; i < N; i++) {
        cin >> P[i].first >> P[i].second;
        if (P[i].second == 'R') ro ^= true;
        if (P[i].second == 'G') go ^= true;
        if (P[i].second == 'B') bo ^= true;
    }
    sort(P.begin(), P.end());

    int64_t R, G, B, RG, RB, GB;
    R = G = B = -1e16;
    RG = RB = GB = 1e16;
    for (int i = 0; i < N; i++) {
        if (P[i].second == 'R') {
            R = P[i].first;
            RG = min(RG, R - G);
            RB = min(RB, R - B);
        } else if (P[i].second == 'G') {
            G = P[i].first;
            RG = min(RG, G - R);
            GB = min(GB, G - B);
        } else if (P[i].second == 'B') {
            B = P[i].first;
            GB = min(GB, B - G);
            RB = min(RB, B - R);
        }
    }
    if (!ro && !go && !bo) cout << 0 << endl;
    else if (ro && go && !bo) cout << min(RG, RB + GB) << endl;
    else if (ro && !go && bo) cout << min(RB, RG + GB) << endl;
    else if (!ro && go && bo) cout << min(GB, RG + RB) << endl;
}

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

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

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

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

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

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

void init(){
}

int find(int a, int b, vi *v){
    int n1 = sz(v[a]), n2 = sz(v[b]);
    int x = 0;
    int mn = inf;
    repu(y,0,n2-1){
        while(x<n1-1 && v[a][x]<v[b][y])
            x++;
        mn = min(mn,abs(v[a][x]-v[b][y]));
        if(x)
            mn = min(mn,abs(v[a][x-1]-v[b][y]));
    }
    return mn;
}

void solve()
{
/*Don't hurry, nothing good comes if you rush*/
    int n;
    cin>>n;
    vi cnt(3,0);
    map<char, int> mm;
    mm['R'] = 0;
    mm['G'] = 1;
    mm['B'] = 2;
    vector<char> c(2*n+1,0);
    vi v[3];
    repu(i,1,2*n){
        int tmp;
        cin>>tmp>>c[i];
        cnt[mm[c[i]]]++;
        v[mm[c[i]]].pb(tmp);
    }
    int odd = 0, f=-1, a=-1, b=-1;
    rep(i,3){
        sort(all(v[i]));
        if(cnt[i]%2){
            if(a==-1)
                a=i;
            else
                b=i;
            odd++;
        }
        else
            f = i;
    }
    if(odd==0){
        cout<<0<<endl;
        return;
    }
    if(cnt[f]==0){
        cout<<find(a,b,v)<<endl;
        return;
    }
    cout<<min(find(a,f,v)+find(b,f,v), find(a,b,v))<<endl;
}

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}

const int N = 5e5+7;
int f[N], pref[N];

int div_(int a, int b) {
	while (a % b) {
		a += 3;
	}
	return a/b;
}

int get(int n, int k) {
    if (pref[n] > pref[k] + pref[n - k]) {
        return 0;
    }
    else {
        return div_(f[n], f[k] * f[n - k] % 3);
    }
}

signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    f[0] = 1;
    for (int i = 1; i < N; ++i) {
    	int t = i;
    	int pw = 0;
    	while (t % 3 == 0) {
    		t /= 3;
    		pw++;
    	}
    	pref[i] = pref[i - 1] + pw;
    	f[i] = f[i - 1] * t % 3;
    }

    string A = "BWR";
    auto num = [&] (char c) {
    	FOR (i, 3) {
    		if (A[i] == c) {
    			return i;
    		}
    	}
    	assert(0);
    };

    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;

    int f = 1;
    if (n % 2 == 0) {
    	f = 2;
    }

    FOR (i, n) {

    	//debug(i);
    	//debug(get(n - 1, i));

    	ans += f * get(n - 1, i) * num(s[i]);
    	ans %= 3;
    }

    //debug(ans);

    cout << A[ans] << endl;

}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;
const ll mod=99824453;
vin dx={-1,0,1,0};
vin dy={0,1,0,-1};

int main(){
    int t;cin>>t;
    vstr ans(t);
    int n;ll a;
    rep(i,t){
        cin>>n;
        if(n%2){
            rep(i,n)cin>>a;
            ans[i]="Second";
            continue;
        }
        set<int> num;
        rep(i,n){
            cin>>a;
            if(num.count(a))num.erase(a);
            else num.insert(a);
        }
        if(num.size())ans[i]="First";
        else ans[i]="Second";
    }
    rep(i,t)cout<<ans[i]<<endl;
}


#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);

	int N;
    vector<string> S;
    vector<vector<int>> dp;

    cin>>N;

    dp.resize(N+1, vector<int>(2));
	S.resize(N);

    dp[0][0] = dp[0][1] = 1;

    for(int i = 0; i < N; i++){
        cin>>S[i];
    }

    for(int i = 1; i <= N; i++){
        if(S[i-1] == "AND") {
            dp[i][0] = dp[i-1][0] * 2 + dp[i-1][1];
            dp[i][1] = dp[i-1][1];
        } else {
            dp[i][0] = dp[i-1][0];
            dp[i][1] = dp[i-1][1] * 2 + dp[i-1][0];

        }
    }

    cout<<dp[N][1]<<endl;

	return 0;
}

#include <iostream>
#include <cmath>
#include <memory.h>

using namespace std;
typedef long long LL;

int main() {
    int n;
    cin >> n;
    string s;
    LL ans = 1, p = 1;
    while (n--) {
        p <<= 1;
        cin >> s;
        if (s == "OR") {
            ans += p;
        }
    }
    cout << ans << endl;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define ll long long
using namespace std;
int	main(){
	double x1,x2,y1,y2;
	cin>>x1>>y1>>x2>>y2;
	double ans=(y1/(y1+y2))*(x2-x1)+x1;
	printf("%.8lf",ans);
}

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

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

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))
#define ALL(v) (v).begin(), (v).end()

#define DEBUG(x) cerr << #x << ": " << (x) << endl
#define DEBUG_VEC(v)                                                           \
  {                                                                            \
    cerr << #v << ": ";                                                        \
    REP(__i, size(v)) cerr << ((v)[__i]) << ", ";                              \
    cerr << endl;                                                              \
  }

int n, m, q;
pair<int, pii> qs[252521];
int a[252521], b[252521];

template <typename T> struct DynBit {
  unordered_map<int, T> dat;
  int N;
  DynBit(int N) : N(N + 2) {}
  void add(int a, T w) {
    a++;
    for (int x = a; x <= N; x += x & -x)
      dat[x] += w;
  }
  T sum(int a) {
    a++;
    T ret = 0;
    for (int x = a; x > 0; x -= x & -x)
      ret += dat[x];
    return ret;
  }
};

int main() {
  scanf("%d%d%d", &n, &m, &q);
  REP(i, q) {
    scanf("%d%d%d", &qs[i].first, &qs[i].second.first, &qs[i].second.second);
  }
  // sum_i sum_j max(a_i, b_j)
  // a_i の寄与
  //   a_i * count_j (a_i > b_j)
  //   元の寄与が sum_j b_j (b_j < a_i)
  // b_j の寄与
  //   b_j * count_i (b_j >= a_i)
  const int mx = 100'000'025;
  DynBit<int> cnt1(mx), cnt2(mx);
  DynBit<ll> sum1(mx), sum2(mx);
  cnt1.add(0, n);
  cnt2.add(0, m);
  ll ans = 0;
  REP(qi, q) {
    int t = qs[qi].first;
    int x, y;
    tie(x, y) = qs[qi].second;
    if (t == 1) {
      // back
      if (a[x] > 0) {
        ll c = cnt2.sum(a[x] - 1);
        ll s = sum2.sum(a[x] - 1);
        ans -= c * a[x] - s;
      }
      cnt1.add(a[x], -1);
      sum1.add(a[x], -a[x]);
      // go
      a[x] = y;
      ll c = cnt2.sum(a[x] - 1);
      ll s = sum2.sum(a[x] - 1);
      ans += c * a[x] - s;
      cnt1.add(a[x], 1);
      sum1.add(a[x], a[x]);
    } else {
      // back
      if (b[x] > 0) {
        ll c = cnt1.sum(b[x]);
        ll s = sum1.sum(b[x]);
        ans -= c * b[x] - s;
      }
      cnt2.add(b[x], -1);
      sum2.add(b[x], -b[x]);
      // go
      b[x] = y;
      ll c = cnt1.sum(b[x]);
      ll s = sum1.sum(b[x]);
      ans += c * b[x] - s;
      cnt2.add(b[x], 1);
      sum2.add(b[x], b[x]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
   ll n ; cin>>n;
   ll x = ((1.08)*(n));
   if(x == 206){
       cout<<"so-so"<<endl;
   }
   else{
        if(x > 206){
       cout<<":("<<endl;
   }
   else{
         cout<<"Yay!"<<endl;
   }
   }

}
int main(){

       solve();

}

#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18+7;

ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ
ll floordiv(ll a, ll b) { return a/b; } // 切り下げ

int show_matrix(vector<vector<int>> &dp) {
  loop(0, dp.size(), i) {
    loop(0, dp[i].size(), j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

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

  ll N;
  cin >> N;
  N *= 1.08;
  if (N < 206) {
    print("Yay!");

  } else if (N == 206) {
    print("so-so");

  } else {
    print(":(");
  }
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define mod 1000000007

void solve(){
    int arr[4];
    for(int i = 0;i<4;i++)cin>>arr[i];
    sort(arr,arr+4);
    if(arr[0] + arr[1] + arr[2] == arr[3]){
        cout<<"Yes";
    }else if(arr[0] + arr[1] == arr[2] + arr[3]){
        cout<<"Yes";
    }else if(arr[0] + arr[3] == arr[1] + arr[2]){
        cout<<"Yes";
    }else{
        cout<<"No";
    }
}

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

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
	ll t,b,c,n,k;
	vector<int> a(4);
	for(int i=0;i<4;i++)
	cin>>a[i];
	int flag=0;
	for(int i=1;i<15;i++){
		int sum1=0,sum2=0;
		for(int j=0;j<4;j++){
			if(i&(1<<j)) sum1+=a[j];
			else sum2+=a[j];
		}
		if(sum1==sum2){
			flag=1;
			break;
		}
	}
	if(flag) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}


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

#define fi first
#define se second
#define rng(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define per(i, b) gnr(i, 0, b)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i, (int)a.size()) cin >> a[i]
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;

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

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int MOD =	1e9 + 7;
const int INF = 1e9 + 9;
const ll LINF = 1e18;
const long double eps = 1e-10;
const char nl = '\n';

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

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

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

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout<<fixed<<setprecision(20);
	int n, k; cin >> n >> k;
	vvi A(n, vi(n));
	rep(i, n) rep(j, n) cin >> A[i][j];

	int lim = k*k/2 + 1;
	int ng = INF;
	int ok = -1;

	auto check = [&](int x) -> bool
	{
		vvi S(n+1, vi(n+1));
		rep(i, n) rep(j, n)
			S[i+1][j+1] = S[i][j+1] + S[i+1][j] - S[i][j] + (A[i][j] >= x);
		int cnt = 0;
		int m = n-k+1;
		rep(i, m) rep(j, m)
			cnt += S[i+k][j+k] - S[i+k][j] - S[i][j+k] + S[i][j] >= lim;
		return cnt == m*m;
	};
	while (ng - ok > 1)
	{
		int mid = (ok + ng)/2;
		if (check(mid)) ok = mid;
		else ng = mid;
	}
	cout << ok << nl;
}

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

int32_t main(){

    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin>>n;
    string s; cin>>s;

    string t;
    while((int)t.size() < n+2) t+="110";

    int ans=0;
    for(int i=0;i<3;i++){
        if(s!=t.substr(i, n)) continue;
        ans+=(int)1e10-(n+i-1)/3;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

#define foreach(i,v) for(auto&& i: v)
#define all(x) (x).begin(), (x).end()

typedef std::vector< std::string > VS;
typedef std::vector<int> VI;
typedef long long ll;
typedef std::pair<int, int> ii;

template <class C> C& mini(C& a, C b) { if (b < a) a = b; return a; }
template <class C> C& maxi(C& a, C b) { if (a < b) a = b; return a; }

template <typename T> void read(std::vector<T>& A) { for (T& x: A) std::cin >> x; }
template <typename T> void readv(std::vector<T>& A) { int N; std::cin >> N; A.resize(N); read(A); }

using namespace std;

#define MOD 1000000007

template <typename T>
void imfix(T& r) {
    if (r >= MOD)
        r -= MOD;
}

template <typename T>
T mfix(T r) {
    return r >= MOD ? r - MOD : r;
}

int main(int argc, const char* argv[]) {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout.setf(ios::fixed);
    VI A;
    readv(A);
    int N = A.size();
    vector<ll> B(N+1), C(N+1);
    B[N] = 0;
    B[N-1] = A.back();
    C[N] = 1;
    C[N-1] = 1;
    for (int i = N-2; i >= 0; i--) {
        B[i] = (A[i] * C[i+1] + B[i+1]) % MOD + ((MOD + A[i] - A[i+1]) * C[i+2] + B[i+2]) % MOD;
        imfix(B[i]);
        imfix(C[i] = C[i+1] + C[i+2]);
    }
    cout << B[0] << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <string> vs;
typedef vector <vector <int>> vvi;
typedef vector <vll> vvll;
typedef map<int, int> mi;
typedef map<string, int> ms;
typedef map<char, int> mc;
typedef map <int, bool> mb;
typedef map<ll, ll> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<string, int> ums;
typedef unordered_map<char, int> umc;
typedef unordered_map <int, bool> umb;
typedef unordered_map<ll, ll> umll;
typedef vector <ld> vld;
typedef vector <bool> vb;
typedef pair <int, int> pii;
typedef pair<ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
#define FOR(i,N) for(ll i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(ll i = a; i <=b;i++)
#define dFOR(i,N) for(ll i = N - 1; i>=0;i--)
#define edFOR(i,b,a) for(ll i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define UNQ(x)  unique(all(x))
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define lb(v, x) lower_bound(all(v) , x)
#define ub(v , x) upper_bound(all(v) , x)
#define pb push_back
#define PI 3.141592653589793
#define FIX cout << fixed << setprecision(15)
#define g(i , a) get<i>(a)
const ll mod = 1e9 + 7;
int CASE = 1;
const int mxn = 1e5 + 1;
const ll INF = 1e18;
bool prime(int n)
{
	if (n <= 1)
		return false;
	if (n == 2 or n == 3)
		return true;
	if (n % 2 == 0 or n % 3 == 0)
		return false;
	for (int i = 5; i * i <= n; i += 6)
		if (n % i == 0 || n % (i + 2) == 0)
			return false;
	return true;
}
ll __gcd(ll a, ll b)
{
	return !b ? a : __gcd(b, a % b);
}
ll a[mxn], dp[mxn][2], way[mxn][2];
void solve()
{
	int n;
	cin >> n;
	eFOR(i, 1, n)
		cin >> a[i];
	dp[1][0] = a[1];
	way[1][0] = 1;
	eFOR(i, 2, n)
	{
		dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] + a[i] * (way[i - 1][0] + way[i - 1][1])) % mod;
		dp[i][1] = (dp[i - 1][0] + mod - a[i] * way[i - 1][0] % mod) % mod;
		way[i][0] = (way[i - 1][0] + way[i - 1][1]) % mod;
		way[i][1] = way[i - 1][0];
	}
	cout << (dp[n][0] + dp[n][1] + mod) % mod;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
	while (T--)
	{
		solve();
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()

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

vv e;

bool check(ll i,ll j){
    ll nl=e[1][i],nr=e[2][i],ml=e[1][j],mr=e[2][j];
    ll n = e[0][i];
    ll m = e[0][j];
    if(nl > ml){
        swap(nl,ml);
        swap(nr,mr);
        swap(n,m);
    }
    else if(nl==ml && (n==3 || n==4) && (m==1 || m==2)){
        swap(nl,ml);
        swap(nr,mr);
        swap(n,m);
    }
    if(nr>ml){
        return true;
    }
    else if(nr==ml){
        if((n==1 || n==3)&&(m==1 || m==2))return true;
        else return false;
    }
    else return false;
}
int main(){
    ll N;
    cin>>N;
    e.resize(3,v(N));

    rep(i,0,N){
        cin>>e[0][i]>>e[1][i]>>e[2][i];
    }
    ll ans = 0;
    rep(i,0,N)rep(j,i+1,N){
        if(check(i,j))ans++;
    }
    cout<<ans;
    return 0;
}

#include <bits/stdc++.h>
#define REP_(i, a_, b_, a, b, ...) \
  for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using i64 = long long;

template<typename T, typename U>
inline bool chmax(T &a, U b) {
  return a < b and ((a = std::move(b)), true);
}
template<typename T, typename U>
inline bool chmin(T &a, U b) {
  return a > b and ((a = std::move(b)), true);
}

template<typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (auto &x : a) is >> x;
  return is;
}
template<typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &a) {
  return os << "(" << a.first << ", " << a.second << ")";
}

void print() { std::cout << "\n"; }
template<class T>
void print(const T &x) {
  std::cout << x << "\n";
}
template<typename Head, typename... Tail>
void print(const Head &head, Tail... tail) {
  std::cout << head << " ";
  print(tail...);
}

void read_from_cin() {}
template<typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define INPUT(type, ...) \
  type __VA_ARGS__;      \
  read_from_cin(__VA_ARGS__);

#ifdef ENABLE_DEBUG
#include "debug_dump.hpp"
#else
#define DUMP(...)
#endif

using namespace std;

auto solve() {
  INPUT(int, n);
  vector<array<i64, 3>> a(n);
  REP(i, n) {
    INPUT(i64, t, l, r);
    l *= 2;
    r *= 2;
    if (t == 1) {
      //
    } else if (t == 2) {
      r -= 1;
    } else if (t == 3) {
      l += 1;
    } else {
      l += 1;
      r -= 1;
    }
    a[i][0] = l;
    a[i][1] = r;
  }
  i64 ans = 0;
  REP(i, n) {
    REP(j, i + 1, n) {
      bool no_over = false;
      if (a[i][1] < a[j][0]) {
        no_over = true;
      } else if (a[j][1] < a[i][0]) {
        no_over = true;
      }
      if (not no_over) {
        ++ans;
      }
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  cout << std::fixed << std::setprecision(15);
  int t = 1;
  REP(test_case, t) {
    auto ans = solve();
    print(ans);
  }
}


#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
	T ret = 1;
	while (n > 0)
	{
		if (n & 1)
		{
			if (p != -1)
				ret = (ret * x) % p;
			else
				ret *= x;
		}
		if (p != -1)
			x = (x * x) % p;
		else
			x *= x;
		n >>= 1;
	}
	return ret;
}

using namespace std;
// using namespace atcoder;

template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

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

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

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

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

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

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

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

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

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

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

using modint = ModInt< mod >;

void solve()
{
	ll h,w,k;
	cin>>h>>w>>k;
	vector<vector<char>>s(h,vector<char>(w,'#'));
	rep(i,k){
		int a,b;
		char c;
		cin>>a>>b>>c;
		a--;b--;
		s[a][b]=c;
	}
	constexpr ll m=998244353;
	vector<vector<ModInt<m>>>dp(h+5,vector<ModInt<m>>(w+5));

	ModInt<m>mul=3;
	mul=mul.pow(h*w-k);
	dp[0][0]=mul;
	rep(i,h){
		rep(j,w){
			if(s[i][j]=='X'){
				dp[i+1][j]+=dp[i][j];
				dp[i][j+1]+=dp[i][j];
			}
			else if(s[i][j]=='R'){
				dp[i][j+1]+=dp[i][j];
			}
			else if(s[i][j]=='D'){
				dp[i+1][j]+=dp[i][j];
			}
			else{

				dp[i+1][j]+=dp[i][j]/3*2;
				dp[i][j+1]+=dp[i][j]/3*2;
			}

		}
	}

	ModInt<m>ans=dp[h-1][w-1];
	cout<<ans;
}

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

#include <bits/stdc++.h>

using namespace std;

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

#define lc u << 1
#define rc u << 1 | 1
#define mid (t[u].l + t[u].r) / 2
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)

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

const int N = 1e6 + 10;
ll t[N];

void modify(int x, int n) {
    while(x <= n) {
        t[x]++;
        x += lowbit(x);
    }
}

ll query(int x) {
    ll ans = 0;
    while(x) {
        ans += t[x];
        x -= lowbit(x);
    }
    return ans;
}

struct Point {
    int x, y;
    friend bool operator < (const Point &a,const Point &b){
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
    friend bool operator == (const Point &a,const Point &b){
        return a.x == b.x && a.y == b.y;
    }
}p[N];

bool vis[N];

void solve() {
    int H, W, M;
    cin >> H >> W >> M;
    int upx = H + 1, upy = W + 1;
    for(int i = 1;i <= M; i++) {
        cin >> p[i].x >> p[i].y;
        if(p[i].x == 1) upy = min(upy, p[i].y);
        if(p[i].y == 1) upx = min(upx, p[i].x);
    }
    for(int i = upx + 1;i <= H; i++) {
        ++M;
        p[M].x = i; p[M].y = 1;
    }
    for(int j = upy + 1;j <= W; j++) {
        ++M;
        p[M].y = j; p[M].x = 1;
    }
    sort(p + 1, p + M + 1);
    M = unique(p + 1, p + M + 1) - (p + 1);

    int h = 1;
    ll ans = 0;
    for(int i = 1;i <= M; i++) {
        // 纵向扫描
        if(vis[p[i].y] == 0) {
            vis[p[i].y] = 1;
            modify(p[i].y, W);
        }

        // 横向扫描
        if(i == M || p[i + 1].x != p[i].x) {
            ans += query(W) - query(p[h].y - 1);
            h = i + 1;
        }
    }
    cout << (1ll * H * W - ans) << endl;
}

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


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <functional>
#include <cctype>
#include <locale>
#include <complex>
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<long long,long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
//s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));
signed main(){
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    ll n,k; cin >> n >> k;
    ll mi=2,ma=2*n;
    chmax(mi,2+k);
    chmin(ma,k+2*n);
    ll ans=0;
    //cout << mi << " " << ma << endl;
    for(ll i=mi;i<=ma;i++){
        ll res=0;
        if(i-n<1) res+=(i-1);
        else res+=(2*n-i+1);

        ll j=i-k;
        if(j-n<1) res*=(j-1);
        else res*=(2*n-j+1);

        ans+=res;
    }
    cout << ans << endl;
}

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

int main() {
    int N, M; cin >> N >> M;
    vector<pair<int, int>> P(M);
    for (auto &[x, y] : P) cin >> x >> y;

    vector<int> X, Y;
    for (auto &[x, y] : P) {
        X.push_back(x);
        Y.push_back(y);
        Y.push_back(y + 1);
        Y.push_back(y - 1);
    }

    sort(ALL(X));
    X.erase(unique(ALL(X)), X.end());

    Y.push_back(N);
    sort(ALL(Y));
    Y.erase(unique(ALL(Y)), Y.end());

    vector<vector<int>> YY(X.size());
    for (auto &[x, y] : P) {
        int idx = lower_bound(ALL(X), x) - X.begin();
        YY[idx].push_back(y);
    }

    vector<int> dp(Y.size());
    dp[lower_bound(ALL(Y), N) - Y.begin()] = 1;

    for (auto &yy : YY) {
        vector<pair<int, int>> nx;
        for (auto &y : yy) {
            int idx = lower_bound(ALL(Y), y) - Y.begin();

            int next = 0;
            if (idx > 0 && dp[idx - 1]) next = 1;
            if (idx + 1 < (int)Y.size() && dp[idx + 1]) next = 1;
            nx.push_back({idx, next});
        }

        for (auto &[idx, next] : nx) dp[idx] = next;
    }
    cout << accumulate(ALL(dp), 0) << endl;
    return 0;
}


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

#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(x) x.begin(),x.end()
typedef long long ll;
ll mod = 1000000007;
ll inf = 1e18;
int main(){

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

    for(int bit=0; bit<(1<<4); bit++){
        int sum1=0;
        int sum2=0;
        rep(j,4){
            if(bit & (1<<j)) sum1+=a[j];
            else sum2+=a[j];
        }
        if(sum1==sum2){
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < n; ++i)
using ll = long long;
using P = pair<int,int>;
int main() {
   int a,b,c,d;cin>>a>>b>>c>>d;
   int sum=a+b+c+d;
   if(sum%2==1){
      cout<<"No"<<endl;
   }
   else{
      int half=sum/2;
      if(half==a||half==b||half==c||half==d)
         cout<<"Yes"<<endl;
      else if(half==a+b||half==a+c||half==a+d)
         cout<<"Yes"<<endl;
      else
         cout<<"No"<<endl;
   }
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using pii = pair<int, int>;

int main() {
  int n, x;
  cin >> n >> x;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    x += s[i] == 'o' ? 1 : -1;
    x = max(x, 0);
  }
  cout << x << endl;
}

#include<bits/stdc++.h>
#define cases long long test;cin>>test;while(test--)
#define f_io ios::sync_with_stdio(false); cin.tie(0)
#define foi(a,n) for(long long i=a;i<n;i++)
#define foj(a,n) for(long long j=a;j<n;j++)
#define fok(a,n) for(long long k=a;k<n;k++)
#define ll long long
#define MOD 1000000007
#define INF 1000000000
using namespace std;

int main()
{
    f_io;
    ll n,x;
    cin >> n >> x;
    string s;
    cin >> s;
    foi(0,n)
    {
        if(s[i]=='o')
            x++;
        else
        {
            if(x)
                x--;
        }
    }
    cout << x;
    return 0;
}


// 問題の URL を書いておく
//

#include <bits/stdc++.h>

using namespace std;

//#define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v) \
do {\
    cout << #v << ": " << v << endl; \
}while(0)

#define PrintVec(v) \
do {\
    for(int __i = 0; __i < v.size(); ++__i) \
    { \
        cout << #v << "[" << __i << "]: " << v[__i] << endl; \
    }\
}while(0)

#define P(fmt, ...) printf(fmt, __VA_ARGS__)
#define L printf("L: %d\n", __LINE__)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#define P(fmt, ...) ((void)0)
#define L ((void)0)

#endif

#define rep(i, n) for(int i = 0; i < (int)(n); ++i)

using ll = long long;

map<int, map<int, int>> cache;
map<int, map<int, bool>> cached;

int test(int N, int y, int x, const map<int, set<int>>& g)
{
    if(cached[y][x]) return cache[y][x];
    cached[y][x] = true;

    int diff[] = {-1, 1};
    rep(i, 2)
    {
        auto ny = y + diff[i];
        if(ny < 0 || ny > 2 * N) continue;
        auto lfound = g.find(ny);

        if(lfound == g.end())
        {
            if(ny == N)
            {
                cache[y][x] = 1;
                return 1;
            }
            continue;
        }
        const auto& l = g.at(ny);
        auto found = upper_bound(l.begin(), l.end(), x - 1);
        if(found == l.begin())
        {
            // 一番上まで行く
            if(ny == N)
            {
                cache[y][x] = 1;
                return 1;
            }
        }
        else
        {
            found--;
            auto nx = *found;
            if(test(N, ny, nx, g) == 1)
            {
                cache[y][x] = 1;
                return 1;
            }
        }
    }

    cache[y][x] = 0;
    return 0;
}

int main(int, const char**)
{
    int N, M;
    cin >> N >> M;

    map<int, set<int>> blacks;
    rep(i, M)
    {
        int X, Y;
        cin >> X >> Y;
        blacks[Y].insert(X);
    }

    P("bsize: %zu\n", blacks.size());

    int ans = 0;
    for(auto itr = blacks.begin(); itr != blacks.end(); ++itr)
    {
L;
        P("y: %d\n", itr->first);
        ans += test(N, itr->first, *(itr->second.rbegin()), blacks);
L;
    }

    // Y == N にポーン置かれてない場合、まっすぐ下に降りるルートを足す
    if(blacks[N].size() == 0) ans++;

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

#include <bits/stdc++.h>

using namespace std;

const int N = 4e5 + 7;
int n, m;

map<int,vector<int> >  plcs;
map<int,vector<int> >  yplcs ;
int xs[N], ys[N];
map<pair<int,int> ,int> gid;

map<int,int> dp ;

set<int> ans ;

void solve(int i)
{
       if(dp[i])return ;
       dp[i] =1 ;
       if(xs[i] == yplcs[ys[i]].back())ans.insert(i) ;

       for(int j = -1 ; j <= 1; j += 2){
              if(ys[i] + j >= 0 && ys[i] + j <= n){
                     int ny = ys[i] + j;
                     auto it = lower_bound(yplcs[ny].begin() , yplcs[ny].end() , xs[i] + 1) ;
                     while(it != yplcs[ny].end()){
                            int nx = *it ;
                            auto it2 = lower_bound(yplcs[ys[i]].begin() , yplcs[ys[i]].end() , xs[i] + 1) ;
                            if(it2!=yplcs[ys[i]].end()){
                                   int m_y = *it2 ;
                                   if(m_y < nx){
                                          break;
                                   }
                            }
                            solve(gid[{nx,ny}]) ;
                            it ++ ;
                     }
              }
       }

}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
       freopen("in.in", "r", stdin);
#endif
       cin >> n >> m;
       plcs[0].push_back(n);
       yplcs[n].push_back(0) ;
       xs[0] = 0 , ys[0] = n;
       n = n * 2 + 1;

       for (int i = 1; i <= m; ++i)
       {
              int x, y;
              cin >> x >> y;
              xs[i] = x, ys[i] = y;
              plcs[x].push_back(y);
              yplcs[y].push_back(x);
              gid[{x,y}] = i ;
       }
       for(auto &u : plcs){
              sort(u.second.begin() , u.second.end()) ;
       }
       for(auto &u : yplcs){
              sort(u.second.begin() , u.second.end()) ;
       }

       solve(0) ;
       cout << ans.size() ;

       return 0;
}


#include <stdio.h>

long long power(long long a, long long b) {
	long long result = 1, i;
	for (i = 0; i < b; i++) {
		result *= a;
	}
	return result;
}

int main() {
	long long i = 1, j = 1, n, turn = 1;;
	scanf("%lld", &n); getchar();
	while (1) {
		if (power(5, i) >= n) break;
		long long x = n - power(5, i);
		turn = 1; j = 1;
		while (1) {
			if (x % 10 == 3 && turn == 1) j = 1;
			else if (x % 10 == 9 && turn == 1) j = 2;
			else if (x % 10 == 7 && turn == 1) j = 3;
			else if (x % 10 == 1 && turn == 1) j = 4;
			else if (x % 10 != 3 && x % 10 != 9 && x % 10 != 7 && x % 10 != 1) break;
			if (power(3, j) > x) break;
			if (power(3, j) == x) {
				printf("%lld %lld\n", j, i);
				return 0;
			}
			j += 4;
			turn = 0;
		}
		i++;
	}
	printf("-1\n");
	return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 20;
const ll mod = 1e9 + 7;
const ll inf = 1e18;

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

	ll n;
	cin >> n;

	for (ll i = 3, x = 1; i <= n; i *= 3, x++)
		for (ll j = 5, y = 1; i + j <= n; j *= 5, y++)
		{
			if (i + j == n){
				cout << x << " " << y;
				return 0;
			}
		}

	cout << -1;
}

/*
1
25
8 2 8 9 2 9 8 10 13 11 1 11 1 6 10 6 1 3 5 3 7 11 10 7 2
*/


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

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;

#define mp make_pair
#define fr first
#define sc second

void solve() {
    int N;
    cin >> N;
    vector<int> A(N), B(N), 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]--;
    }
    for (int i = 0; i < N; i++) {
        if (P[i] != i && A[i] <= B[P[i]]) {
            cout << -1 << '\n';
            return;
        }
    }
    vector<int> pos(N);
    for (int i = 0; i < N; i++) {
        pos[P[i]] = i;
    }
    vector<pii> ord(N);
    for (int i = 0; i < N; i++) {
        ord[i] = mp(A[i], i);
    }
    vector<pii> ans;
    sort(ord.begin(), ord.end());
    for (auto [w, i] : ord) {
        if (P[i] == i) {
            continue;
        }
        int p1 = pos[i], p2 = i;
        ans.emplace_back(p1, p2);
        swap(pos[P[p1]], pos[P[p2]]);
        swap(P[p1], P[p2]);
    }
    cout << ans.size() << '\n';
    for (auto [a, b] : ans) {
        cout << a + 1 << ' ' << b + 1 << '\n';
    }
}

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

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

    return 0;
}

/* 	author : sgupta_2001 	*/

#include<bits/stdc++.h>

using namespace std;

using db = double;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

//containers
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front

//pairs
#define mp make_pair
#define ff first
#define ss second

//loops
#define rep(i, a, b) for(int i = a; i < b; ++i)
#define per(i, a, b) for(int i = b - 1; i >= a; --i)
#define each(x, a) for(auto &x : a)

//popular constants
const int mod = 1e9 + 7; //998244353;
const ll inf = 1e18;
const ld pie = acos((ld) - 1);

//grid problems
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//modulo operations
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

#ifdef SGUPTA_2001
#define deb(x) cerr<< #x <<" "; __print(x); cerr<<'\n';
#else
#define deb(x);
#endif

//debug containers
template<class T> void __print(T x) {cerr<<x;}
template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";}
template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; }
template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";}
template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";}
template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<typename... T> void read(T&... args) { ((cin>>args),...);}
template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';}

const int MAXN = 2002;

vector<int> gr[MAXN];
int t_in[MAXN], t_out[MAXN];
int vis[MAXN];
int timer;

void dfs(int u, int par) {

	t_in[u] = ++timer;

	vis[u] = 1;

	for(auto x : gr[u]) {
		if(x != par && !vis[x]) {
			dfs(x, u);
		}
	}

	t_out[u] = timer;
	return;
}

void solve() {

	int N, M;
	scanf("%d%d", &N, &M);

	rep(i, 0, M) {
		int X, Y;
		scanf("%d%d", &X, &Y);

		X--;Y--;

		gr[X].pb(Y);
	}

	ll ans = 0;

	rep(i, 0, N) {
		dfs(i, -1);

		int p = t_in[i], q = t_out[i];

		rep(j, 0, N) {
			if(p <= t_in[j] && q >= t_out[j]) {
				ans++;
			}
		}

		fill(vis, vis + N + 1, 0);
		fill(t_in, t_in + N + 1, 0);
		fill(t_out, t_out + N + 1, 0);
		timer = 0;
	}

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

	return;
}

int main() {

	#ifdef SGUPTA_2001
		freopen("input.txt", "r", stdin);
		freopen("error.txt", "w", stderr);
	#endif

	auto start = std::chrono::high_resolution_clock::now();

	int t;
	t=1;
	while(t--) solve();

  auto stop = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  // cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< '\n';

	 /*
	 		STUFF TO LOOK:
			1. Check the constraints
			2. Corner cases(n == 0 || n == 1)
			3. Variables in loops
			4. Make sure two ints aren’t multiplied to get a long long
	*/

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n);i++)
#define Graph vector<vector<int>>;
#define iterG(next_v, G, v) for(auto next_v : G[v]
#define ALL(a)  (a).begin(),(a).end()
const int MOD = 1000000007;
const int MAX = 510000;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, x, y;
    cin >> h >> w >> y >> x;
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    int ans = 1;
    int curx, cury;
    curx = x-1;
    cury = y-1;
    // x +
    while(curx + 1 < w and s[cury][curx+1] == '.'){
        curx++;
        ans++;
    }

    curx = x-1;
    cury = y-1;
    // x +
    while(curx - 1 >= 0 and s[cury][curx-1] == '.'){
        curx--;
        ans++;
    }
    curx = x-1;
    cury = y-1;

    while(cury + 1 < h and s[cury+1][curx] == '.'){
        cury++;
        ans++;
    }

    curx = x-1;
    cury = y-1;
    while(cury - 1 >= 0 and s[cury-1][curx] == '.'){
        cury--;
        ans++;
    }
    cout << ans << endl;
    return 0;
}

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

int ri() {
	int n;
	cin >> n;
	return n;
}
int main() {
	int h = ri();
	int w = ri();
	int x = ri() - 1;
	int y = ri() - 1;
	string s[h];
	for (auto &i : s) cin >> i;

	int cnt = -3;
	for (int i = x; i < h && s[i][y] != '#'; i++) cnt++;
	for (int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++;
	for (int j = y; j < w && s[x][j] != '#'; j++) cnt++;
	for (int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++;
	cout << cnt << endl;
}


//Author: stdout
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define debug(x) cerr<<#x<<" = "<<x<<"\n";
#define lowbit(x) ((x)&-(x))
const int inf=0x3f3f3f3f;
const double eps=1e-9;
//const double pi=acos(-1.0);
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
	ull x,y,e=0,a,b;
	cin>>x>>y>>a>>b;
	while(x*(a-1)<b&&x*a<y) x*=a,++e;
	e+=(y-x-1)/b;
	cout<<e<<"\n";
	return 0;
}

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

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

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

const int N=19;

int n,m;
int dp[N][1<<N];

vector<pii> v[N];

int calc(int p,int mask){
	int &ans = dp[p][mask];
	if(ans != -1){
		return ans;
	}
	ans = 0;

	for(auto x:v[p]){
		int y = x.fr, z = x.sc;
		int cnt = mask % (1 << y);
		cnt = ppc(cnt);
		if(cnt > z){
			ans = 0;
			return ans;
		}
	}

	if(p == n){
		return ans = 1;
	}

	rep(i,0,n){
		if(mask >> i & 1) continue;
		ans += calc(p+1,mask|(1<<i));
	}
	return ans;
}

void solve(){
	mem1(dp);
	cin >> n >> m;
	rep(i,0,m){
		int x,y,z;
		cin >> x >> y >> z;

		v[x].pb({y,z});
	}
	cout << calc(0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, n) for(int i = a; i < n; i++)
#define all(A) A.begin(),A.end()
#define an(A, n) A,A+n
#define sz(a) int(a.size())
#define pu push
#define pb push_back
#define pob pop_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define is insert

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<ld> vld;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
typedef set<int> si;

const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};
const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFF = 1LL << 60;
const ld EPS = 1e-9;
const ld OO = 1e15;
const ld PI = acos(-1.0); //M_PI;

const ll N = 1000010;

int main() {

    //TODO:
    ll s;
    ll p;
    cin >> s >> p;
    int flag = 0;
    rep(i, 1, N) {
        if(i * (s - i) == p) {
            flag = 1;
            break;
        }
    }
    if(flag) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

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

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost; // set
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; // multiset
*/

// direction array
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

// Bitmask

/*
bool check(int mask,int pos){return mask&(1<<pos);}
int SET(int mask,int pos){return mask|(1<<pos);}
int FLIP(int mask,int pos){return mask^(1<<pos);}
int reset(int mask,int pos){return mask&~(1 << pos);}
*/

int main()
{
    ll s,p;

    cin>>s>>p;

    for(ll i=1;i*i<=p;i++){
        if(p%i==0 && i+(p/i)==s){
            cout<<"Yes\n";
            return 0;
        }
    }
    cout<<"No\n";
}


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

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int a,b; cin>>a>>b;
    cout<<a/b<<'\n';
    return 0;
}

/*
ID: Trump
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define sll(n) scanf("%lld",&n);
#define sll2(a,b) scanf("%lld",&n);
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c);
#define slb(n) scanf("%Lf",&n);
#define pll(n) printf("%lld",n);
#define pln printf("\n");
#define plb(n) printf("%Lf",n);
#define plb2(n) printf("%.2Lf",n);
#define plb3(n) printf("%.3Lf",n);
#define plb4(n) printf("%.4Lf",n);
#define plb5(n) printf("%.5Lf",n);
#define plb6(n) printf("%.6Lf",n);
using namespace std;
typedef long long ll;
typedef long double lb;
typedef double db;
typedef int it;
typedef bool bl;
//#define MARK
const ll losn=1e6+5;
const ll maxn=1e5+5;
const ll minn=1e3+5;
const ll tiny=1e2+5;
const ll inf=1e9;
const ll binf=1e18;
const ll mod=1e9+7;
const ll lmod=998244353;
const ll hashmod=4698571;
const lb pi=3.1415926;
const ll myn=0;
void gk(){cout<<"---------------"<<endl;}
void ck(){cout<<1<<endl;}
void ck(ll a){cout<<a<<endl;}
void ck(ll a[minn][minn],ll n,ll m){for(ll i=1;i<=n;i++){for(ll j=1;j<=m;j++){cout<<a[i][j]<<" ";}cout<<endl;}}
ll n,m,k,T,u,v,len;

int main(){
//#ifdef MARK
//    freopen("input.in", "r", stdin);
//    freopen("ouput.out", "w", stdout);
//    freopen("ouput.out", "r", stdin);
//    freopen("input.in", "w", stdout);
//#endif
	ll a,b;
	cin>>a>>b;
	ll sums=a+b;
	if(sums>=15 && b>=8){
		cout<<1<<endl;
	}else if(sums>=10 && b>=3){
		cout<<2<<endl;
	}else if(sums>=3){
		cout<<3<<endl;
	}else{
		ck(4);
	}

	return 0;
}


#include <bits/stdc++.h>

int main(){
    char S, T;
    std::cin >> S >> T;
    if(S == 'Y'){
        std::cout << (char)(T + 'A' - 'a') << std::endl;
    }else{
        std::cout << T << std::endl;
    }

    return 0;
}

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = int_fast64_t;
using ld = long double;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pq  = priority_queue<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; }

const ll INF   = (1LL << 30);
const ll INFLL = (1LL << 60);
const ll MAX   = 5000005;
const ll MOD   = 1000000007;

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

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

  if (m % n == 0) cout << "Yes" << endl;
  else cout << "No" << endl;

  return (0);
}


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

using namespace std;
//using namespace atcoder;

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

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

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

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

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

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

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

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

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

// edit

void solve() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N, vector<int>(N));
    REP(i, M) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a][b] = G[b][a] = 1;
    }

    vector<ll> dp(1 << N, INF);
    dp[0] = 1;

    for (int mask = 0; mask < 1 << N; ++mask) {
//        int amas = ~mask & ((1 << N) - 1);
        int amas = 0;
        for (int i = 0; i < N; ++i) {
            if (mask < 1 << i) break;
            if (mask >> i & 1) {
            } else {
                amas |= (1 << i);
            }
        }

        int tmp = amas;
        while (tmp > 0) {
            chmin(dp[tmp | mask], dp[tmp] + dp[mask]);
            tmp = (tmp - 1) & amas;
        }

        for (int i = 0; i < N; ++i) {
            if (mask >> i & 1) continue;

            int nmas = mask | (1ll << i);
            bool ok = true;
            for (int j = 0; j < N; ++j) {
                if (mask >> j & 1) {
                    if (!G[i][j]) {
                        ok = false;
                        break;
                    }
                }
            }
            if (ok) {
                chmin(dp[nmas], dp[mask]);
            } else {
                chmin(dp[nmas], dp[mask] + 1);
            }
        }
    }

    cout << dp.back() << endl;

}

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

    solve();

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
ll N,M;
ll c[324];
ll dp[(1<<18)];
int main(){
cin>>N>>M;
  for(ll i=0;i<M;i++){
  ll a,b;cin>>a>>b;a--;b--;
    if(a<b){c[N*a+b]=1;}else{c[N*b+a]=1;}
  }for(ll i=1;i<(1<<N);i++)dp[i]=N+1;dp[0]=0;
  vector<P>A((1<<N));
  for(ll i=0;i<(1<<N);i++){
  ll sum=0;ll I=i;
    for(ll j=0;j<N;j++){sum+=I%2;I/=2;}
    A[i]=P(sum,i);
  }sort(A.begin(),A.end());vector<ll>B((1<<N));
  for(ll i=0;i<(1<<N);i++)B[i]=A[i].second;
  for(ll bit=0;bit<(1<<N);bit++){
  bool x=true;
    for(ll i=0;i<N-1;i++){
    if(bit & (1<<i)){
    for(ll j=i+1;j<N;j++)
      if(bit & (1<<j)){if(c[N*i+j]==0){x=false;break;}}

    }if(!x)break;
    }if(x)dp[bit]=1;
  }
  for(ll i=0;i<(1<<N);i++){
  ll bit=B[i];
    if(dp[bit]==1)continue;ll Min=N+1;
    for(ll bits=bit; ;bits=((bits-1)&bit)){
      if(dp[(bit^bits)]+dp[bits]<Min)Min=dp[(bit^bits)]+dp[bits];
      if(!bits)break;
    }dp[bit]=Min;
  }cout<<dp[(1<<N)-1]<<endl;

  return 0;
}


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

#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)
#define mem(a,b) memset(a,(b),sizeof(a))
#define for0(a, c) for (int(a) = 0; (a) < (c); (a)++)
#define forc(a, b, c) for (int(a) = (b); (a) <= (c); (a)++)
#define forr(a, b, c) for (int(a) = (b); (a) >= (c); (a)--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(a) int((a).size())

#define disp(a) cout<<#a<<" "<<a<<"\n";
#define show(vec) cout<<#vec<<"\n"; for(auto v : vec) cout<<v<<" "; cout<<"\n";
#define showi(vec) for0(i,sz(vec)) cout<<"i: "<<i<<" "<<#vec<<"["<<i<<"]: "<<vec[i]<<"\n";

#define bs(num, v) binary_search((v).begin(), (v).end(), (num))
#define ub(num, v) upper_bound((v).begin(), (v).end(), (num))
#define lb(num, v) lower_bound((v).begin(), (v).end(), (num))

#define INF 1000000000000000003
#define mod 1e9 + 7

// Common memset settings
//memset(memo, -1, sizeof memo); // initialize DP memoization table with -1
//memset(arr, 0, sizeof arr); // to clear array of integers

// Common remainder formulaes
// (x+y)%m = ((x%m) + (y%m))%m
// (x.y)%m = ((x%m) . (y%m))%m
// (x-y)%m = ((x%m) - (y%m) + m)%m

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

#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair

void solve(){
	ll n; cin>>n;
	double x0,y0; cin>>x0>>y0;
	complex<double> X0(x0,y0);
	double xk,yk; cin>>xk>>yk;
	complex<double> Xk(xk,yk);

	complex<double> Xmid = (X0 + Xk)/2.0;
	double PI = acos(-1);
	double rad = 2*PI/n;
	complex<double> r(cos(rad), sin(rad));
	complex<double> ans = Xmid + (X0-Xmid)*r;
	printf("%.10f %.10f\n", ans.real(), ans.imag());
	return;
}

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

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

	int T = 1;  while(T--)solve();
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

double n, x0, y00, x2, y2;

int main(){
	cin >> n >> x0 >> y00 >> x2 >> y2;
	double zx = (x0+x2)/2, zy = (y00+y2)/2;
	x0 -= zx; y00 -= zy;
	x2 -= zx; y2 -= zy;
	double theta = 180 - ((n-2)*180)/n;
	//theta = 360 - theta;
	double alpha = M_PI*theta/180;
	double x1 = x0*cos(alpha) - y00*sin(alpha) + zx;
	double y1 = y00*cos(alpha) + x0*sin(alpha) + zy;
	cout << fixed << setprecision(10) << x1 << " " << y1 << "\n";
    return 0;
}