#include <bits/stdc++.h>
#define ll long long
#define maxN 100000 + 5
#define IO ios::sync_with_stdio(0);cin.tie(0)
#define endl '\n'
#define x first
#define y second
#define pii pair<int, int>
#define pll pair<ll int, ll int>
#define sz(s) (int)s.size()

using namespace std;

pll finda(pll a) {

	ll int tmp = a.y/2;
	if(a.y % 2) {
		return {-a.x - tmp, -a.x + tmp};
	}
	else{
		return {a.x - tmp, a.x + tmp - 1};
	}
}

signed main()
{
	IO;

	ll int b, c;
	cin >> b >> c;
	if(c == 1 && b != 0) {
		cout << "2" << endl;
		return 0;
	}

	auto [tmpa, tmpb] = finda({b, c});
	auto [tmpc, tmpd] = finda({b, c - 1});
	ll int ans = tmpb - tmpa + 1 + tmpd - tmpc + 1 - max(0ll, min(tmpb,tmpd) - max(tmpa, tmpc) + 1);
	cout << ans << endl;

	return 0;
}

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

using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();

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

template <std::ostream& os = std::cout>
class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
        template <typename T>
        auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
            for (size_t i = 0; i < sz; i++)
                os << vec[i] << (i == sz - 1 ? term : sep);
        }
        template <typename T>
        auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
            for (size_t i = 0; i < h; i++)
                for (size_t j = 0; j < w; j++)
                    os << mat[i][j] << (j == w - 1 ? term : sep);
        }

    private:
        const char *sep, *term;
        void print() const { os << term; }
        void print_rest() const { os << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints<> prints;
Prints<std::cerr> prints_err;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
    ll N, M;
    cin >> N >> M;
    ll x = 1;
    ll c = 10;
    for (; N > 0; N >>= 1) {
        if (N & 1) x = x * c % (M * M);
        c = c * c % (M * M);
    }
    prints()(x / M);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    std::ifstream ifs("./in_out/input.txt");
    std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    std::ofstream ofs("./in_out/output.txt");
    std::cout.rdbuf(ofs.rdbuf());
#endif
    std::cout << std::setprecision(18) << std::fixed;
    solve();
    std::cout << std::flush;
    return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define X first
#define Y second
#define pb push_back
#define sz(s) int64_t(s.size())
#define make_unique(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())

const ll mod = 1e9 + 7;

template<class T>
istream& operator >> (istream& in, vector<T>& v){ for (auto &x : v) { in >> x; } return in; }

template<class T, class U>
istream& operator >> (istream& in, pair<T, U> & v){ in >> v.X >> v.Y;return in; }

template<class T, class U>
ostream& operator << (ostream& out, pair<T, U> & v){ out << v.X << " " << v.Y;return out; }

template<class T, class U>
void chkmax(T &a, U b) {
    a = max(a, (T)b);
    return;
}

template<class T, class U>
void chkmin(T &a, U b) {
    a = min(a, (T)b);
    return;
}

ll ppow (ll x, ll s) {
    if (!s) return 1;
    if (!(s - 1)) return x % mod;
    if (s % 2) return (x * ppow (x, s - 1)) % mod;
    ll b = ppow (x, s / 2);
    return (b * b) % mod;
}

vector<int> zf (string s) {

	int n = sz(s);

	vector<int> z (n);

	for (int i = 1, l = 0, r = 0; i < n; i++) {

		if (i <= r)
			z[i] = min (r - i + 1, z[i - l]);

		while (i + z[i] < n && s[z[i]] == s[i + z[i]])
			z[i]++;

		if (i + z[i] - 1 > r)
			l = i,  r = i + z[i] - 1;

	}

	return z;

}

vector<int> pf(string p) {
    int n = sz(p);
    vector<int> pref_fun(n, 0);

    for (int i = 1;i < n;i++) {
        int j = pref_fun[i - 1];
        while (j > 0 && p[i] != p[j])
            j = pref_fun[j - 1];
        if (p[i] == p[j])
            j++;
        pref_fun[i] = j;
    }
    return pref_fun;
}

vector<vector<int> > mul(vector<vector<int> > &a, vector<vector<int> > &b) {
    int n = sz(a);
    vector<vector<int> > c(n, vector<int> (n, 0));

    for (int i = 0;i < n;i++) {
        for (int j = 0;j < n;j++) {
            for (int k = 0;k < n;k++) {
                ll ga = a[i][k];
                ga *= b[k][j];
                ga %= mod;
                c[i][j] += ga;
                if (c[i][j] >= mod) {
                    c[i][j] -= mod;
                }
            }
        }
    }

    return c;
}

vector<vector<int> > matrix_pow(vector<vector<int> > a, ll y) {
    int n = sz(a);
    vector<vector<int> > res(n, vector<int> (n, 0));
    for (int i = 0;i < n;i++) res[i][i] = 1;
    while (y) {
        if (y%2 == 1) {
            res = mul(res, a);
            y--;
        }else {
            a = mul(a, a);
            y /= 2;
        }
    }
    return res;
}

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

    int n;
    cin >> n;
    map<int,int> cnt;
    while (n--) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    vector<int> go;
    for (auto x : cnt) go.pb(x.X);

    ll ans = 0;
    for (auto i : go) {
        for (auto j : go) {
            ll w = (i - j) * (i - j);
            ans += w * cnt[i] * cnt[j];
        }
    }
    cout << ans / 2;

    return 0;
}

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

const int N = 3e5+10;

int n;
long long a[N],s[N];

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		s[i]=s[i-1]+a[i];
	}

	long long ans = 0;
	for(int i=1;i<=n;i++) ans += (n - 1) * a[i] * a[i];
	for(int i=1;i<=n;i++) ans -= 2 * a[i] * (s[n] - s[i]);
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(n)-1; i>=0; i--)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back
using ll = long long;
using D = double;
using LD = long double;
using P = pair<int, int>;
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; }

template <class Abel> struct BIT {
    vector<Abel> dat[2];
    Abel UNITY_SUM = 0;                     // to be set

    /* [1, n] */
    BIT(int n) { init(n); }
    void init(int n) { for (int iter = 0; iter < 2; ++iter) dat[iter].assign(n + 1, UNITY_SUM); }

    /* a, b are 1-indexed, [a, b) */
    inline void sub_add(int p, int a, Abel x) {
        for (int i = a; i < (int)dat[p].size(); i += i & -i)
            dat[p][i] = dat[p][i] + x;
    }
    inline void add(int a, int b, Abel x) {
        sub_add(0, a, x * -(a - 1)); sub_add(1, a, x); sub_add(0, b, x * (b - 1)); sub_add(1, b, x * (-1));
    }

    /* a is 1-indexed, [a, b) */
    inline Abel sub_sum(int p, int a) {
        Abel res = UNITY_SUM;
        for (int i = a; i > 0; i -= i & -i) res = res + dat[p][i];
        return res;
    }
    inline Abel sum(int a, int b) {
        return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) - sub_sum(1, a - 1) * (a - 1);
    }

    /* debug */
    void print() {
        for (int i = 1; i < (int)dat[0].size(); ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

int main(){
    ll h,w,m; cin >> h >> w >> m;
    vector<P> o(m);
    rep(i,m){
        cin >> o[i].first >> o[i].second;
        o[i].first--; o[i].second--;
    }

    vector<int> index[h+1];
    rep(i,m) index[o[i].first].pb(o[i].second);
    rep(i,h) sort(ALL(index[i]));

    if(index[0].size() > 0){
        int t = index[0][0];
        index[0] = {};
        FOR(i,t,w) index[0].pb(i);
    }

    vector<int> tmp;
    rep(i,m){
        if(o[i].second == 0) tmp.pb(o[i].first);
    }
    if(tmp.size() > 0){
        sort(ALL(tmp));
        int u = tmp[0];
        FOR(i,u,h){
            if(index[i].size() > 0 && index[i][0] == 0) continue;
            index[i].pb(0);
        }
    }

    rep(i,h) index[i].pb(w);
    rep(i,w+1) index[h].pb(i);
    rep(i,h+1) sort(ALL(index[i]));

    /*rep(i,h+1){
        rep(j,(int)index[i].size()){
            cout << index[i][j] << " ";
        }
        cout << endl;
    }*/

    vector<int> index2[w+1];
    rep(i,h+1){
        rep(j,(int)index[i].size()){
            index2[index[i][j]].pb(i);
        }
    }
    rep(i,w+1) sort(ALL(index2[i]));

    BIT<ll> cnt(h+5);
    ll ans = 0;
    rep(i,h) ans += (ll)index[i][0];

    rep(i,index[0][0]){
        ans += cnt.sum(1,(ll)index2[i][0]+1LL);
        for(int j : index2[i]){
            if(cnt.sum(j+1, j+2) == 0) cnt.add(j+1,j+2,1LL);
        }
    }
    cout << ans << endl;
}

//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
//#include<ctime>
//#include<algorithm>//next_permutation
#define rep(i,n) for (ll i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;

const ll MOD = 1000000007;
const ll MAX = 2000001;

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

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

int main(){
  ll n,m; cin >> n >> m;

  vl gold(n);
  rep(i,n) cin >> gold[i];

  vector<set<ll>> e(n);

  rep(i,m){
    ll a,b; cin >> a >> b; --a; --b;
    e[a].insert(b);
  }

  vl dp(n,1e18); //その街に持ってこられる最安の金の価格

  rep(i,n){
    for(auto a : e[i]){

      //cout << i << a << endl;
      dp[a] = min({dp[i],dp[a],gold[i]});
    }
  }

  //cout << "aaaaa" << endl;

  ll ans = -1e18;

  rep(i,n){
    ans = max(ans,gold[i] - dp[i]);
  }

  cout << ans << endl;

}


#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n;
    cin >> n;
    vector<long long> ans;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ans.emplace_back(i);
            if (n != i * i) ans.emplace_back(n / i);
        }
    }
    sort(ans.begin(), ans.end());
    for (const long long &v: ans)
        cout << v << '\n';
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define yn {puts("Yes");}else{puts("No");}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
bool isi(const string&s){for(int l=0,r=sz(s)-1;l<r;++l,--r)if(s[l]!=s[r])return false;return true;}

int main() {
  int a;
  cin>> a;
  cout<<100-(a%100)<<endl;
  return 0;
}


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

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>

const ll mod = 1000000007;
const int INF = 1001001001;

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

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};
istream& operator>>(istream& is, const mint& a){ return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a){ return os << a.x; }

set<char> s;
map<char, int> mp, mp_inv;

int C(char c){
    int num = mp[c];
    int res = 0;
    rep(i, num){
        if(s.find(mp_inv[i]) == s.end())res++;
    }
    return res;
}

int main(){
    string n;
    int k;
    cin >> n >> k;
    int keta = 0;
    for (int i = 0; n[i] != 0; i++) keta++;

    rep(i, 10) {
        mp[(char)('0'+i)] = i;
        mp_inv[i] = '0' + i;
    }
    rep(i, 6) {
        mp[(char)('A'+i)] = i + 10;
        mp_inv[i+10] = 'A' + i;
    }

    vector<vector<vector<mint> > > dp(keta+1, vector<vector<mint> >(k+1, vector<mint>(2)));
    dp[0][0][0] = 1;

    rep(i, keta){
        dp[i+1][0][0] = 0;
        dp[i+1][0][1] = 1;
        rep(j, k){
            if(s.find(n[i]) == s.end()) dp[i+1][j+1][0] = dp[i][j][0];
            else dp[i+1][j+1][0] = dp[i][j+1][0];

            int c = C(n[i]);

            if(j != 0){
                if(i == 0) dp[i+1][j+1][1] = dp[i][j][0] * (c-1) + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j) + dp[i][j+1][1] * (j+1);
                else dp[i+1][j+1][1] = dp[i][j][0] * c + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j) + dp[i][j+1][1] * (j+1);
            }else{
                if(i == 0) dp[i+1][j+1][1] = dp[i][j][0] * (c-1) + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j-1) + dp[i][j+1][1] * (j+1);
                else dp[i+1][j+1][1] = dp[i][j][0] * c + dp[i][j+1][0] * (mp[n[i]]-c) + dp[i][j][1]*(16-j-1) + dp[i][j+1][1] * (j+1);
            }

        }
        s.insert(n[i]);
    }
    // rep(j, k+1){
    //     rep(i, keta+1){
    //         cout << dp[i][j][0] << " ";
    //     }cout << endl;
    // }cout << endl;

    // rep(j, k+1){
    //     rep(i, keta+1){
    //         cout << dp[i][j][1] << " ";
    //     }cout << endl;
    // }
    mint ans;
    ans = dp[keta][k][0] + dp[keta][k][1];
    cout << ans << endl;
}

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

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;
//using namespace atcoder;

template<int mod>
struct Modint{
    int x;
    Modint():x(0){}
    Modint(int64_t y):x((y%mod+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 = (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;
			while(b>0){
				int 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 &p){
			int64_t t;
			is >> t;
			p=Modint<mod>(t);
			return is;
		}
};

using modint = Modint<mod>;

void solve(){
	ll t;
	cin>>t;
	while(t--){
		ll n,a,b;
		cin>>n>>a>>b;
		if(n<a+b){
			cout<<0<<"\n";
			continue;
		}
		auto f=[&](modint x)->modint{
			return x*x;
		};
		modint ans=f(n-a+1)*f(n-b+1);
		modint x=min(a,b)-1;
		ans-=f(x)*f(x+3+2*n-2*(a+b))/4*4;//縦も横も完全にはかぶらない
		modint s=max(a,b);
		ans-=(s-x)*x*(-s+n+1)*(x+3+2*n-2*(a+b))*2;
		ans-=f(s-x)*f(-s+n+1);//縦も横も完全にかぶる
		cout<<ans<<"\n";
	}
}

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


//always use
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long ll;
#define MOD 1000000007
#define HASHMOD 1275341791
//2 147 483 647 (2^31-1) int max
//9 223 372 036 854 775 807 (2^63-1) ll max

//codeforces and USACO
void fast_io(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);}

//USACO
void file_io(string taskname){
  string fin = taskname + ".in";
  string fout = taskname + ".out";
  const char* FIN = fin.c_str();
  const char* FOUT = fout.c_str();
  freopen(FIN, "r", stdin);
  freopen(FOUT, "w", stdout);
  fast_io();}

//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}

//main code
int main(){
  fast_io();
  int n,m;
  cin >> n >> m;
  vector<vector<int>>a(n,vector<int>(m));
  forn(i,n){
    string s;
    cin >> s;
    forn(j,m){
      if (s.at(j)=='.'){
        a[i][j]=1;
      }
    }
  }
  vector<ll>diag(n+m-1,0);
  vector<ll>vert(m,0);
  forn(i,n){
    ll hor=0;
    forn(j,m){
      if (a[i][j]==1){
        ll x=0;
        if (i==0 and j==0){
          x++;
        }
        x=(x+diag[i-j+m-1])%MOD;
        x=(x+hor)%MOD;
        x=(x+vert[j])%MOD;
        if (i==n-1 and j==m-1){
          cout << x <<" ";
        }
        diag[i-j+m-1]=(diag[i-j+m-1]+x)%MOD;
        hor=(x+hor)%MOD;
        vert[j]=(x+vert[j])%MOD;
      }
      else{
        diag[i-j+m-1]=0;
        hor=0;
        vert[j]=0;
      }
    }
  }

}

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

// auto mod int
// depends on Template

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

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

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

char s[2010][2010];
mint tate[2010][2010];
mint yoko[2010][2010];
mint naname[2010][2010];
mint dp[2010][2010];

int main(){
    ll h,w;
    cin >> h >> w;
    rep(i,h)rep(j,w)cin>>s[i+1][j+1];
    dp[1][1]=1;
    tate[1][1]=1;
    yoko[1][1]=1;
    naname[1][1]=1;
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            if(i==1&&j==1)continue;
            if(s[i][j]=='#')continue;
            dp[i][j]+=tate[i-1][j];
            dp[i][j]+=yoko[i][j-1];
            dp[i][j]+=naname[i-1][j-1];
            tate[i][j]=dp[i][j]+tate[i-1][j];
            yoko[i][j]=dp[i][j]+yoko[i][j-1];
            naname[i][j]=dp[i][j]+naname[i-1][j-1];
        }
    }
    cout << dp[h][w]<<endl;
    return 0;
}


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

const int maxn = 200005;
const int maxk = 302;
const long long mod = 998244353;

long long add_mod (long long a, long long b) {
	return (a + b) % mod;
}

long long sub_mod (long long a, long long b) {
	return (a + mod - b) % mod;
}

long long mul_mod (long long a, long long b) {
	return (a * b) % mod;
}

long long pow_mod (long long a, long long b) {
	long long res = 1LL, t = a;
	while (b) {
		if (b & 1)
			res = mul_mod(res, t);
		t = mul_mod(t, t);
		b >>= 1;
	}
	return res;
}

long long div_mod (long long a, long long b) {
	return mul_mod(a, pow_mod(b, mod - 2));
}

long long ncr_mod[maxk][maxk];

void precalc () {
	for (int n = 0; n < maxk; ++n) {
		for (int r = 0; r < maxk; ++r) {
			if (n == 0 || r == 0 || n == r)
				ncr_mod[n][r] = 1LL;
			else
				ncr_mod[n][r] = add_mod(ncr_mod[n - 1][r], ncr_mod[n - 1][r - 1]);
		}
	}
}

long long a[maxn];
long long s[maxk], diff[maxk];

int32_t main () {
	precalc();

	int n, k;
	scanf("%d %d", &n, &k);

	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
		long long cur = 1LL, cur_d = 1LL;
		for (int x = 0; x <= k; ++x) {
			s[x] = add_mod(s[x], cur);
			diff[x] = add_mod(diff[x], cur_d);
			cur = mul_mod(cur, a[i]);
			cur_d = mul_mod(cur_d, mul_mod(2LL, a[i]));
		}
	}

	for (int x = 1; x <= k; ++x) {
		long long ans = 0LL;
		for (int j = 0; j <= x; ++j)
			ans = add_mod(ans, mul_mod(s[x - j], mul_mod(ncr_mod[x][j], s[j])));
		ans = div_mod(sub_mod(ans, diff[x]), 2LL);
		printf("%lld\n", ans);
	}
}

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

#include<bits/stdc++.h>

using namespace std;

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

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
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; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class EPeddler {
public:

    vector<int> graph[200005];

    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n, m; cin >> n >> m;
      vector<ll> a(n);
      rep(i, n) {
        cin >> a[i];
      }
      vector<int> x(m), y(m);
      rep(i, m) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
        graph[x[i]].push_back(y[i]);
      }

      vector<ll> dp(n, LLONG_MIN/2);
      vector<ll> lowest(n, LLONG_MAX/2);

      for(int i=0; i<n; i++) {
        for(auto e: graph[i]) {
          chmin(lowest[e], lowest[i]);
          chmin(lowest[e], a[i]);
        }
      }

      ll ret = LLONG_MIN/2;
      for(int i=0; i<n; i++) {
        chmax(ret, a[i] - lowest[i]);
      }

      cout << ret << endl;
    }
};

signed main() {
  EPeddler solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}


///****In the name of ALLAH, most Gracious, most Compassionate****//

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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

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

#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl

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

const int N=3e5+5;
const int oo=1e9+7;
int a[N];
int32_t main()
{
    FastIO;
    int n,k;
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    priority_queue < pair < int , int > > pq;
    for(int i=0;i<k;i++)
        pq.push({0,i});

    ll ans = 0;
    for(int i=0;i<n and ! pq.empty();i++)
    {
        while(!pq.empty())
        {
            auto x = pq.top();
            pq.pop();

            int now = - x.first ;

//            DBG(now);
//            DBG(a[i]);
//            NL;

            if(a[i] < now)
            {
                pq.push(x);
                break;
            }
            else if(a[i] == now)
            {
                pq.push({ - (now +1) , x.second});
                break;
            }
            else
            {
                ans+=now;
                continue ;
            }
        }
    }
    while(!pq.empty())
    {
        ans += -pq.top().first;
        pq.pop();
    }
    cout<<ans<<"\n";

}

/* B */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int N = 3e5 + 10;
int n, m, a[N], s[N];

ll solve(void)
{
	int num = m;
	ll res = 0;
	for (int k = 1; k <= n; ++k) s[a[k]]++;
	for (int k = 0; k <= n; ++k) {
		if (s[k] >= num) continue;
		res += (ll)(num - s[k]) * k;
		num = s[k];
	}
	return res;
}

signed main(int argc, char** argv)
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
	//	freopen("output.txt", "w", stdout);
	#endif
	while (cin >> n >> m) {
		for (int k = 1; k <= n; ++k) cin >> a[k];
		sort(a + 1, a + n + 1);
		cout << solve() << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0; int f = 0;
    while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); }
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    if (f) x = -x;
}
int n, x, y, f[200], s[200], tp;
int res[200], cnt;
signed main() {
    read (n); f[0] = f[1] = 1;
    for (int i = 2; i <= 87; ++i)
        f[i] = f[i - 1] + f[i - 2];
    int t = n;
    for (int i = 86; i; --i) {
        if (t >= f[i]) {
            ++tp, s[tp] = i, t -= f[i];
        }
    }
    // if (t) puts ("666666");
    // for (int i = 1; i <= tp; ++i) printf ("%lld ", s[i]); puts ("");
    int tag = (s[1] & 1); s[tp + 1] = s[1];
    for (int i = tp; i; --i) s[i] = s[1] - s[i];

    // for (int i = 1; i <= tp; ++i) printf ("%lld ", s[i]); puts ("");
    if (tag) {
        res[++cnt] = 2;
        int now = 1; // 1 x  0 y
        for (int i = 2; i <= tp + 1; ++i) {
            for (int j = s[i - 1] + 1; j <= s[i]; ++j) {
                res[++cnt] = now ? 3 : 4, now ^= 1;
            }
            if (i <= tp) res[++cnt] = now ? 2 : 1;
        }
    } else {
        res[++cnt] = 1;
        int now = 0; // 1 x  0 y
        for (int i = 2; i <= tp + 1; ++i) {
            for (int j = s[i - 1] + 1; j <= s[i]; ++j) {
                res[++cnt] = now ? 3 : 4, now ^= 1;
            }
            if (i <= tp) res[++cnt] = now ? 2 : 1;
        }
    }
    int x = 0, y = 0;
    printf ("%lld\n", cnt);
    for (int i = 1; i <= cnt; ++i) {
        printf ("%lld ", res[i]);
        // if (res[i] == 1) ++x;
        // if (res[i] == 2) ++y;
        // if (res[i] == 3) x = x + y;
        // if (res[i] == 4) y = x + y;
        // printf ("%lld %lld %lld %lld\n", i, res[i], x, y);
    }
    return 0;
}

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

template<typename T>
struct SegmentTree{
  using OP = function<T(T,T)>;

  int n, defn;
  vector<T> node;
  T t;
  OP op;

  SegmentTree(int n_, T t, OP op) : t(t),op(op){
    n = 1;
    defn = n_;
    while(n < n_) n<<=1;
    node.assign(2*n,t);
  }

  void init(int n_){
    n = 1;
    defn = n_;
    while(n < n_) n<<=1;
    node.assign(2*n,t);
  }

  void set(int k,T x){
    node.at(k+n) = x;
  }

  void build(){
    for(int k = n - 1; k > 0; k--){
      node.at(k) = op(node.at(2*k),node.at(2*k+1));
    }
  }

  void change(int k, T x){
    k += n;
    node.at(k) = x;
    while(k >>= 1){
      node.at(k) = op(node.at(2*k),node.at(2*k+1));
    }
  }

  void change(int l, int r, T x){
    for(int k = l; k < r; k++) change(k,x);
  }

  template<typename C>
  void update(int k, C c){
    T x = node.at(k+n);
    T x2 = c(x);
    change(k, x2);
  }

  template<typename C>
  void update(int l, int r, C c){
    for(int k = l; k < r; k++) update(k,c);
  }

  T at(int k){
    return node.at(k+n);
  }

  void add(int k, T a){
    T x = at(k)+a;
    change(k,x);
  }

  T query(int a, int b){
    if(a==0 && b==defn) return all_query();
    T L = t, R = t;
    for(a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = op(L, node.at(a++));
      if(b & 1) R = op(node.at(--b), R);
    }
    return op(L, R);
  }

  T all_query() { return node.at(1); }

  template<typename C>
  int max_right(int l, C &check){
    if(l == defn) return defn;
    l += n;
    T tm = t;
    do{
      while(l%2 == 0) l >>= 1;
      if(!check(op(tm, node.at(l)))){
        while(l < n){
          l *= 2;
          if(check(op(tm, node.at(l)))){
            tm = op(tm, node.at(l));
            l++;
          }
        }
        return l - n;
      }
      tm = op(tm, node.at(l));
      l++;
    }while((l&-l) != l);
    return defn;
  }

  template<typename C>
  int max_left(int r, C &check){
    if(r == 0) return 0;
    r += n;
    T tm = t;
    do{
      r--;
      while(r>1 && (r%2)) r >>= 1;
      if(!check(op(node.at(r), tm))){
        while(r < n){
          r = 2*r + 1;
          if(check(op(node.at(r), tm))){
            tm = op(node.at(r), tm);
            r--;
          }
        }
        return r + 1 - n;
      }
      tm = op(node.at(r), tm);
    }while((r&-r) != r);
    return 0;
  }
};

int main(){
  int H, W, M; cin >> H >> W >> M;
  vector<int> height(W, H), width(H, W);
  vector<vector<int>> ps(H);

  rep(i, M){
    int x, y; cin >> x >> y; x--; y--;
    chmin(height[y], x); chmin(width[x], y);
    ps[x].emplace_back(y);
  }

  ll ans = 0;

  SegmentTree<int> seg(
    W,
    0,
    [](int a, int b) { return a+b; }
  );

  rep(i, W){
    if(i < width[0]) ans += height[i];
    else seg.set(i, 1);
  }
  seg.build();

  rep(i, height[0]){
    for(auto&&y : ps[i]){
      seg.change(y, 1);
    }
    ans += seg.query(0, width[i]);
  }

  cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

int main() {
    ll N = 0;
    cin >> N;
    set<ll> A;
    ll t;
    ll x = 1000000000000;
    for(ll i = 0; i < N; i++) {
        cin >> t;
        A.insert(t);
        x = min(x, t);
    }
    while(A.size() != 1) {
        set<ll> ans;
        ans.clear();
        for (set<ll>::iterator it = A.begin(); it != A.end(); it++) {
            ans.insert((*it)%x + x);
            //cout << *it << endl;
        }
        ll memo = x;
        for (set<ll>::iterator it = ans.begin(); it != ans.end(); it++) {

            if(*it != memo)x = min(x, *it - memo);
        }
        A.clear();
        A = ans;
        //cout << "##" << endl;
    }
    cout << x << endl;

    return 0;
}

#include<iostream>
#include<bits/stdc++.h>
#include<climits>

#define PI 3.14159265358979323846264338327950L
#define ll long long
#define db double

using namespace std;

const int MOD = 1e9+7;

void go(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout<<fixed;
  cout<<setprecision(20);

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

void solve(){
    int n; cin>>n;
    int arr[n];
    for(int i = 0; i < n; i++) cin>>arr[i];
    sort(arr,arr+n);
    int sol = 0;
    for(int i = 0; i < n; i++){
        sol = __gcd(sol,arr[i]);
    }
    cout<<sol<<endl;
}

int main(){
    go();

    // code here
    int t = 1;
    while(t--) solve();

}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 1e9+7 ;
long double pie = acos(-1) ;
ll INF = 1e18 ;

void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
ll extGCD(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x = 1 ;
        y = 0 ;
        return a ;
    }
    ll d = extGCD(b,a%b,y,x) ;
    y -= a/b*x ;
    return d ;
}
void fix_cout(){cout << fixed << setprecision(20) ;}
template<class T> void chmax(T &a,T b){if(a<b) a = b ;}
template<class T> void chmin(T &a,T b){if(a>b) a = b ;}

string ans = "" ;
int mx = 0 ;
VV<int> a(50,V<int>(50)) ;
VV<int> t(50,V<int>(50)) ;
VV<bool> tt(50,V<bool>(50,true)) ;

chrono::_V2::system_clock::time_point start,now ;

void DFS(int y,int x,int sum,string &cur){
  now = chrono::system_clock::now() ;
  if(chrono::duration_cast<chrono::milliseconds>(now-start).count()>1000) return ;
  sum += a[y][x] ;
  if(mx<sum){
    mx = sum ;
    ans = cur ;
  }
  tt[y][x] = false ;
  if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = false ;
  if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = false ;
  if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = false ;
  if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = false ;
  cur += 'U' ;
  if(y>0&&tt[y-1][x]) DFS(y-1,x,sum,cur) ;
  cur.pop_back() ; cur += 'D' ;
  if(y+1<50&&tt[y+1][x]) DFS(y+1,x,sum,cur) ;
  cur.pop_back() ; cur += 'L' ;
  if(x>0&&tt[y][x-1]) DFS(y,x-1,sum,cur) ;
  cur.pop_back() ; cur += 'R' ;
  if(x+1<50&&tt[y][x+1]) DFS(y,x+1,sum,cur) ;
  cur.pop_back() ;
  tt[y][x] = true ;
  if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = true ;
  if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = true ;
  if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = true ;
  if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = true ;
}
void DFS2(int y,int x,int sum,string &cur){
  now = chrono::system_clock::now() ;
  if(chrono::duration_cast<chrono::milliseconds>(now-start).count()>1970) return ;
  sum += a[y][x] ;
  if(mx<sum){
    mx = sum ;
    ans = cur ;
  }
  tt[y][x] = false ;
  if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = false ;
  if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = false ;
  if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = false ;
  if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = false ;
  cur += 'R' ;
  if(x+1<50&&tt[y][x+1]) DFS2(y,x+1,sum,cur) ;
  cur.pop_back() ; cur += 'L' ;
  if(x>0&&tt[y][x-1]) DFS2(y,x-1,sum,cur) ;
  cur.pop_back() ; cur += 'D' ;
  if(y+1<50&&tt[y+1][x]) DFS2(y+1,x,sum,cur) ;
  cur.pop_back() ; cur += 'U' ;
  if(y>0&&tt[y-1][x]) DFS2(y-1,x,sum,cur) ;
  cur.pop_back() ;
  tt[y][x] = true ;
  if(y>0&&t[y-1][x]==t[y][x]) tt[y-1][x] = true ;
  if(y+1<50&&t[y+1][x]==t[y][x]) tt[y+1][x] = true ;
  if(x>0&&t[y][x-1]==t[y][x]) tt[y][x-1] = true ;
  if(x+1<50&&t[y][x+1]==t[y][x]) tt[y][x+1] = true ;
}

int main(){
  start = chrono::system_clock::now() ;
  int sx,sy ;
  cin >> sy >> sx ;
  for(auto &i:t) for(auto &j:i) cin >> j ;
  for(auto &i:a) for(auto &j:i) cin >> j ;
  int sum = 0 ;
  string cur = "" ;
  DFS(sy,sx,sum,cur) ;
  sum = 0 ;
  cur = "" ;
  for(int i=0;i<50;i++) for(int j=0;j<50;j++) tt[i][j] = true ;
  DFS2(sy,sx,sum,cur) ;
  cout << ans << endl ;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
#define irep(i,n) for(int i = n-1; i >= 0; --i )
using namespace std;
using ll = long long;
using P = pair<int,int>;

const int N=1505;
int v[N][N];
int main() {
  int h,w,n,m;
  cin >> h >> w >> n >> m;
  rep(i,n){
    int a,b;
    cin >> a >> b;
    v[a-1][b-1] = 2;
  }
  rep(i,m){
    int a,b;
    cin >> a >> b;
    v[a-1][b-1] = -1;
  }
  bool b;
  rep(i,h){
    b=false;
    rep(j,w){
      if(v[i][j]==-1) b = false;
      if(v[i][j]==2) b = true;
      if(v[i][j]==0 && b) v[i][j] = 1;
    }
    b=false;
    irep(j,w){
      if(v[i][j]==-1) b = false;
      if(v[i][j]==2) b = true;
      if(v[i][j]==0 && b) v[i][j] = 1;
    }
  }
  rep(j,w){
    b=false;
    rep(i,h){
      if(v[i][j]==-1) b = false;
      if(v[i][j]==2) b = true;
      if(v[i][j]==0 && b) v[i][j] = 1;
    }
    b=false;
    irep(i,h){
      if(v[i][j]==-1) b = false;
      if(v[i][j]==2) b = true;
      if(v[i][j]==0 && b) v[i][j] = 1;
    }
  }

  int ans = 0;
  rep(i,h)rep(j,w) if(v[i][j]>0) ++ans;
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define lop(i,n) for(ll i=0;i<n;i++)
#define lop1(i,n) for(ll i=1;i<=n;i++)
#define lopr(i,n) for(ll i=n-1;i>=0;i--)
#define ll long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define IOS  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define F first
#define S second
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define LCM(a,b) (a*b)/__gcd(a,b)
#define mii map<int,int>
#define mll map<ll,ll>
#define ub upper_bound
#define lb lower_bound
#define sz(x) (ll)x.size()
#define ld long double
#define pcnt(x) __builtin_popcountll(x)
const long long I1=1e9;
const long long I2=1e18;
const int32_t M1=1e9+7;
const int32_t M2=998244353;
template<typename T,typename T1>T maxn(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T minn(T &a,T1 b){if(b<a)a=b;return a;}

void solve()
{
  //code begins from here//
  int n,m;
  cin>>n>>m;
  int a[n][n];
  mem(a,0);
  lop(i,m){
      int x,y;
      cin>>x>>y;
      x--;y--;
      a[x][y]=1;
      a[y][x]=1;
  }
  int N = 1<<n;
  vector <bool> v(N,false);
  for (int i =1 ; i<N; i++){
      bool cc = true;
      lop(k,n){
          for (int j=k+1; j<n; j++){
              if((i&(1<<j)) && (i&(1<<k))){
                  if(!a[j][k]) cc=false;
              }
          }
      }
      v[i]=cc;
  }
  vector <int> dp(N,I1);
  dp[0]=0;
  for (int i=1 ; i<N; i++){
      for (int j=i; j; j=(j-1)&i){
         if(v[j])  minn(dp[i],dp[i^j]+1);
      }
  }
  cout<<dp[N-1]<<endl;
}

signed main()
{
    IOS;
    #ifdef MODULO
        initialize();
    #endif
    #ifdef SIEVE
        sieve();
    #endif
    int testcase=1;
   // cin>>testcase;
    while(testcase--) solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int n;
LL m,f[202][202][202],a[2020];
#define INF (1LL<<60)
int main(){
	scanf("%d%lld",&n,&m);
	for (int i=1; i<=n; i++) scanf("%lld",&a[i]);

	LL ans = m;
	for (int mod=1; mod<=n; mod++){
		memset(f, -1, sizeof(f));
		f[0][0][0] = 0;
		for (int i=0; i<n; i++){
			for (int j=0; j<=i; j++){
				for (int k=0; k<mod; k++){
					f[i+1][j][k] = max(f[i+1][j][k], f[i][j][k]);
					if (f[i][j][k] == -1) continue;
					int p = (k + a[i+1])%mod;
					f[i+1][j+1][p] = max(f[i+1][j+1][p], f[i][j][k] + a[i+1]);
 				}
			}
		}
		LL p = m%mod;
//		printf("%d %d %d %lld\n",n, mod, p, f[n][mod][p]);
		if (f[n][mod][p]!=-1) ans = min(ans, (m - f[n][mod][p])/mod);
	}
	printf("%lld\n",ans);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()

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

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

	unordered_map<int, int> mp;
	rep(i, n)++mp[a[i]];

	long long ans = 0;
	rep(i, n) {
		ans += mp[b[c[i] - 1]];
	}
	cout << ans << endl;

	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

// Mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x;
  mint():x(0){}
  mint(ll x):x((x%mod+mod)%mod){}
  // mint(ll x):x(x){}
  mint& fix() { x = (x%mod+mod)%mod; return *this;}
  mint operator-() const { return mint(0) - *this;}
  mint& operator+=(const mint& a){ if((x+=a.x)>=mod) x-=mod; return *this;}
  mint& operator-=(const mint& a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
  mint& operator*=(const mint& a){ (x*=a.x)%=mod; return *this;}
  mint operator+(const mint& a)const{ return mint(*this) += a;}
  mint operator-(const mint& a)const{ return mint(*this) -= a;}
  mint operator*(const mint& a)const{ return mint(*this) *= a;}
  bool operator<(const mint& a)const{ return x < a.x;}
  bool operator==(const mint& a)const{ return x == a.x;}
};
istream& operator>>(istream&i,mint&a){i>>a.x;return i;}
ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;}
//

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

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  int ans = a[n-1];
  rep(i, n-1) {
    ans = gcd(ans, a[i]);
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// #pragma GCC optimize("O2")
typedef long long int ll;
typedef long double ld;
typedef map <ll,ll> mm;
typedef vector <ll> mv;
typedef pair <ll,ll> mp;
typedef set <ll> ms;
typedef multiset <ll> mms;
typedef queue <ll> mq;
typedef deque <ll> mdq;
typedef stack <ll> mst;
typedef priority_queue <ll> mpq;
typedef priority_queue <ll, vector<ll>, greater<ll> > mmh;
typedef complex<double> cd;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(),v.end()
#define deb(x) cout << #x << " " << x << endl;
#define ff first
#define ss second
#define pb(v) push_back(v)
#define lb(v) lower_bound(v)
#define ub(v) upper_bound(v)
#define cn continue
#define forl(i,n) for(ll i=0;i<n;i++)
#define forlr(i,n) for(ll i=n-1;i>=0;i--)
#define rev(s) reverse(s.begin(),s.end())
void swap(ll &a,ll &b) {
	ll tmp=a;
	a=b;
	b=tmp;
}
ll atoistring(string s) {
	stringstream lol(s);
	ll x;
	lol>>x;
	return x;
}
void pv(mv v) {
	forl(i,v.size()) cout<<v[i]<<" ";
	cout<<"\n";
}
void pa(ll *v, ll size) {
	forl(i,size) cout<<v[i]<<" ";
	cout<<"\n";
}
void removeDups(mv &v) {
	sort(all(v));
	mv::iterator it = unique(v.begin(), v.end());
	v.resize(distance(v.begin(), it));
}
inline ld kthRoot(ld n, ll k) {
	return pow(k, (1.0 / k) * (log(n) / log(k)));
}
ll power(ll x, ll y, ll p) {
	ll res=1;
	x=x%p;
	while(y>0) {
		if(y&1) res = (res*x)%p;
		y=y>>1;
		x=(x*x)%p;
	}
	return res;
}
void checkmod(ll &x, ll mod) {
	if(x>=mod) x%=mod;
}

ll mod = 1e9 + 7;

ll arr[200005], pre[200005], maxi[200005];
void thisIsMain() {
	ll n;
	cin>>n;
	forl(i,n) {
		cin>>arr[i];
		pre[i] = arr[i];
	}
	maxi[0] = arr[0];
	forl(i,n - 1) {
		pre[i + 1] += pre[i];
		maxi[i + 1] = max(maxi[i], pre[i + 1]);
	}
	ll ans = 0;
	ll pos = 0;
	forl(i,n) {
		ans = max(ans, pos + maxi[i]);
		pos += pre[i];
	}
	cout<<ans<<"\n";
}

int main(void)
{
	fastio
	ll t = 1;
	// cin>>t;
	while(t--) thisIsMain();
	return 0;
}

#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<functional>
#include<string>
#include<numeric>
#include<cstring>
#include<random>
#include<array>
#include<fstream>
#include<iomanip>
#include<list>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<queue>

/*
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
*/

using namespace std;

using ll = long long;
using ull = unsigned long long;

#define REP(i,a,b) for(ll i = a; i < b; ++i)
#define PRI(s) std::cout << s << endl
#define PRIF(v, n) printf("%."#n"f\n", (double)v)

template<typename A, typename B>void mins(A& a, const B& b) { a = min(a, (A)b); };
template<typename A, typename B>void maxs(A& a, const B& b) { a = max(a, (A)b); };

vector<vector<bool>> S;
vector<vector<vector<pair<int,int>>>> E;
vector<vector<int>> F;

void dfs(int i, int j, int c) {
	F[i][j] = c;
	for (auto p : E[i][j]) if(F[p.first][p.second]==-1)dfs(p.first, p.second, c);
}

int main() {
	ll H, W; cin >> H >> W;
	S.resize(H, vector<bool>(W));
	E.resize(H, vector<vector<pair<int, int>>>(W));
	F.resize(H, vector<int>(W, -1));

	REP(i, 0, H) REP(j, 0, W) {
		char c; cin >> c;
		S[i][j] = (c == '#');
	}
	S[0][0] = true;
	S[0][W - 1] = true;
	S[H - 1][0] = true;
	S[H - 1][W - 1] = true;

	REP(i, 0, H) {
		int last = -1;
		REP(j, 0, W) {
			if (!S[i][j]) continue;
			if (last == -1) {
				last = j;
				continue;
			}

			E[i][j].push_back({ i,last });
			E[i][last].push_back({ i,j });
			last = j;
		}
	}

	REP(j, 0, W) {
		int last = -1;
		REP(i, 0, H) {
			if (!S[i][j]) continue;
			if (last == -1) {
				last = i;
				continue;
			}

			E[i][j].push_back({ last,j });
			E[last][j].push_back({ i,j });
			last = i;
		}
	}

	int fm = -1;
	REP(i, 0, H) REP(j, 0, W) {
		if (!S[i][j] || F[i][j] != -1)continue;
		dfs(i, j, ++fm);
	}

	int emptycol = 0;
	int emptyrow = 0;
	REP(i, 0, H) {
		bool ok = true;
		REP(j, 0, W) {
			if (S[i][j]) {
				ok = false;
				break;
			}
		}
		if (ok)++emptycol;
	}
	REP(j, 0, W) {
		bool ok = true;
		REP(i, 0, H) {
			if (S[i][j]) {
				ok = false;
				break;
			}
		}
		if (ok)++emptyrow;
	}

	PRI(fm + min(emptycol, emptyrow));

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<map>
#include<bitset>
#include<assert.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<set>
#include<queue>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<chrono>
#include<stack>
#include<deque>
#include<random>
#define long long long
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N=1e6,inf=1e9,mod=1e9+7;

struct abcd
{
  int tr;
  struct treap
  {
    int lt,rt,dt,pri,sz;
    long sm;
  }bst[N+2];
  void cal(int nd)
  {
    int l=bst[nd].lt,r=bst[nd].rt;
    bst[nd].sz=bst[l].sz+bst[r].sz+1;

    bst[nd].sm=bst[l].sm+bst[r].sm+bst[nd].dt;
  }
  //left tree will contain value <= vl.
  pair<int,int>splitbyValue(int nd,int vl)
  {
    if(!nd)return {0,0};pair<int,int>p;
    if(bst[nd].dt<=vl)
    {
      p=splitbyValue(bst[nd].rt,vl);
      bst[nd].rt=p.first;cal(nd);
      return {nd,p.second};
    }
    else
    {
      p=splitbyValue(bst[nd].lt,vl);
      bst[nd].lt=p.second;cal(nd);
      return {p.first,nd};
    }
  }
  pair<int,int>splitbySize(int nd,int ss)
  {
    if(!nd)return {0,0};pair<int,int>p;
    if(bst[bst[nd].lt].sz>=ss)
    {
      p=splitbySize(bst[nd].lt,ss);
      bst[nd].lt=p.second;cal(nd);
      return {p.first,nd};
    }
    else
    {
      p=splitbySize(bst[nd].rt,ss-bst[bst[nd].lt].sz-1);
      bst[nd].rt=p.first;cal(nd);
      return {nd,p.second};
    }
  }
  //lt and rt are bst and max element of lt <= minimum of rt
  int join(int lt,int rt)
  {
    if(!lt)return rt;if(!rt)return lt;
    if(bst[lt].pri>=bst[rt].pri)
    {
      bst[lt].rt=join(bst[lt].rt,rt);
      cal(lt);return lt;
    }
    else
    {
      bst[rt].lt=join(lt,bst[rt].lt);
      cal(rt);return rt;
    }
  }
  int add(int rt,int vl)
  {
    pair<int,int>p=splitbyValue(rt,vl);

    bst[++tr]={0,0,vl,rng()%inf,1,vl};
    rt=join(p.first,tr);
    rt=join(rt,p.second);

    return rt;
  }
  int erase(int rt,int vl)
  {
    pair<int,int>p=splitbyValue(rt,vl-1);
    pair<int,int>p2=splitbySize(p.second,1);

    return join(p.first,p2.second);
  }
}lft,rgt;

int a[N+2],b[N+2];
int main()
{
  ios_base::sync_with_stdio(0);cin.tie(0);

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

  long ans=0;int rt1=0,rt2=0;

  for(int i=1;i<=n;i++)rt1=lft.add(rt1,0);
  for(int i=1;i<=m;i++)rt2=rgt.add(rt2,0);

  while(q--)
  {
    int ty;cin>>ty;

    if(ty==1)
    {
      int x,y;cin>>x>>y;

      pair<int,int>p=rgt.splitbyValue(rt2,a[x]);
      ans-=(1LL*rgt.bst[p.first].sz*a[x]+rgt.bst[p.second].sm);
      rt2=rgt.join(p.first,p.second);

      rt1=lft.erase(rt1,a[x]);

      a[x]=y;
      p=rgt.splitbyValue(rt2,a[x]);
      ans+=(1LL*rgt.bst[p.first].sz*a[x]+rgt.bst[p.second].sm);
      rt2=rgt.join(p.first,p.second);

      rt1=lft.add(rt1,a[x]);

    }
    else
    {
      int x,y;cin>>x>>y;

      pair<int,int>p=lft.splitbyValue(rt1,b[x]);
      ans-=(1LL*lft.bst[p.first].sz*b[x]+lft.bst[p.second].sm);
      rt1=lft.join(p.first,p.second);

      rt2=rgt.erase(rt2,b[x]);

      b[x]=y;
      p=lft.splitbyValue(rt1,b[x]);
      ans+=(1LL*lft.bst[p.first].sz*b[x]+lft.bst[p.second].sm);
      rt1=lft.join(p.first,p.second);

      rt2=rgt.add(rt2,b[x]);

    }
    cout<<ans<<'\n';
  }

  return 0;
}

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

using ll = long long;

#define fi first
#define se second

const int N = 2e5 + 5;
const int INF = 2e9;
const ll LINF = 9e18;

struct query {
  int T, X, Y;
};

int n, m, q;
int a[N], b[N];
ll tree[4][N << 2];

// 0 = Sum_A, 1 = Sum_B, 2 = Freq_A, 3 = Freq_B

void update(int idx, int l, int r, int pos, ll val, int d) {
  if(l > pos || r < pos) return;
  if(l == r) {
    tree[d][idx] += val;
    return;
  }
  int mid = l + r >> 1;
  update(idx << 1, l, mid, pos, val, d);
  update(idx << 1 | 1, mid + 1, r, pos, val, d);
  tree[d][idx] = tree[d][idx << 1] + tree[d][idx << 1 | 1];
}

ll rangeSum(int idx, int l, int r, int from, int to, int d) {
  if(l > to || r < from) return 0;
  if(from <= l && r <= to) return tree[d][idx];
  int mid = l + r >> 1;
  return (
    rangeSum(idx << 1, l, mid, from, to, d) +
    rangeSum(idx << 1 | 1, mid + 1, r, from, to, d)
  );
}

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

  cin >> n >> m >> q;
  vector<query> Q;
  set<int> distinct;
  distinct.insert(0);
  while(q--) {
    int t, x, y; cin >> t >> x >> y;
    Q.push_back({t, x, y});
    distinct.insert(y);
  }
  int cnt = 0;
  map<int, int> idx;
  for(int u : distinct) {
    idx[u] = ++cnt;
  }
  int s = distinct.size();

  update(1, 1, s, idx[0], n, 2);
  update(1, 1, s, idx[0], m, 3);

  ll ans = 0;
  for(query u : Q) {
    if(u.T == 1) {
      int prv = idx[a[u.X]];
      int nxt = idx[u.Y];

      ans -= rangeSum(1, 1, s, 1, prv, 3) * a[u.X];
      ans += rangeSum(1, 1, s, 1, prv, 1);
      ans -= rangeSum(1, 1, s, 1, nxt, 1);
      ans += rangeSum(1, 1, s, 1, nxt, 3) * u.Y;

      update(1, 1, s, prv, -a[u.X], 0);
      update(1, 1, s, nxt, u.Y, 0);
      update(1, 1, s, prv, -1, 2);
      update(1, 1, s, nxt, 1, 2);

      a[u.X] = u.Y;
    } else {
      int prv = idx[b[u.X]];
      int nxt = idx[u.Y];

      ans -= rangeSum(1, 1, s, 1, prv, 2) * b[u.X];
      ans += rangeSum(1, 1, s, 1, prv, 0);
      ans -= rangeSum(1, 1, s, 1, nxt, 0);
      ans += rangeSum(1, 1, s, 1, nxt, 2) * u.Y;

      update(1, 1, s, prv, -b[u.X], 1);
      update(1, 1, s, nxt, u.Y, 1);
      update(1, 1, s, prv, -1, 3);
      update(1, 1, s, nxt, 1, 3);

      b[u.X] = u.Y;
    }
    cout << ans << "\n";
  }
}


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

/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset_find(k) find_by_order(k)
#define oset_order(k) order_of_key(k)*/

using ll = long long;
using db = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

template<typename T> using pqg = priority_queue<T, vector<T>, greater<T> >;

#define FOR(i, a, n) for(int i=(a); i<(n); ++i)
#define ROF(i, a, n) for(int i=(n)-1; i>=(a); --i)

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define ins insert
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define endl '\n'

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

template<typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<typename T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string)s; }
string ts(const string& s) { return '"' + s + '"'; }
string ts(bool b) { return (b ? "true" : "false"); }

string ts(vector<bool> v) {
    string res = "{";
    FOR(i, 0, sz(v))
        res += char('0' + v[i]);
    res += "}";
    return res;
}

template <size_t SZ> string ts(bitset<SZ> b) {
    string res;
    FOR(i, 0, SZ)
        res += char('0' + b[i]);
    return res;
}

template <typename A, typename B> string ts(pair<A, B> p);
template <typename A> string ts(A v) {
    bool first = 1; string res = "{";
    for (const auto& x: v) {
        if (!first) res += ", ";
        first = 0; res += ts(x);
    }
    res += "}"; return res;
}

template <typename A, typename B> string ts(pair<A, B> p) {
    return "(" + ts(p.fi) + ", " + ts(p.se) + ")";
}

void dbgOut() { cerr << "]" << endl; }
template <typename H, typename... T> void dbgOut(H h, T... t) {
    cerr << ts(h); if (sizeof...(t)) cerr << ", ";
    dbgOut(t...);
}

#ifdef LOCAL
    #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbgOut(__VA_ARGS__)
#else
    #define dbg(...) 0
#endif

const int MOD = 1e9+7; // 998244353;
const ll INF = 1e18;
const int MX = 2e5+5;

vi adj[2005];
bool vis[2005];

int dfs(int i) {
    int cnt = 1;

    vis[i] = 1;
    for (auto x : adj[i]) {
        if (!vis[x])
            cnt += dfs(x);
    }
    return cnt;
}

void solve() {
    int n, m; cin >> n >> m;

    while (m--) {
        int x, y; cin >> x >> y;
        --x; --y;
        adj[x].pb(y);
    }

    ll ans = 0;
    FOR(i, 0, n) {
        ans += dfs(i);

        fill_n(vis, 2005, 0);
        dbg(ans);
    }

    cout << ans << endl;
}

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

#ifdef LOCAL
    auto begin = std::chrono::high_resolution_clock::now();
#endif

    solve();

#ifdef LOCAL
    auto end = std::chrono::high_resolution_clock::now();
    cerr << "\nExecution time: " << std::chrono::duration_cast<std::chrono::duration<double>> (end-begin).count() << " seconds\n";
#endif
}

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

#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second

void input(int *arr, int n){
	for(int i=0; i<n; ++i) cin >> arr[i];
}

void input(vector<int>&arr, int n){
	for(int i=0; i<n; ++i) cin >> arr[i];
}

void print(int *arr, int n){
	for(int i=0; i<n; ++i) cout << arr[i] << " ";
	cout << "\n";
}

void print(vector<int>&arr, int n){
	for(int i=0; i<n; ++i) cout << arr[i] << " ";
	cout << "\n";
}

vector<vector<int> >vec;
vector<bool>visited;
ll ans = 0;

void dfs(int node){
	visited[node] = true;
	for(int to : vec[node]){
		if(visited[to])
			continue;
		else{
			ans += 1;
			//cout << ans << " " << node << " " << to << "\n";
			dfs(to);
		}
	}
}

int main(){
	FAST;
	int test = 1;
	//cin >> test;
	for(int tt=1; tt<=test; ++tt){
		int n, m;
		cin >> n >> m;
		vec = vector<vector<int> >(n+1, vector<int>());
		for(int i=1; i<=m; ++i){
			int u, v;
			cin >> u >> v;
			vec[u].push_back(v);
		}
		for(int i=1; i<=n; ++i){
			visited = vector<bool>(n+1, false);
			//cout << i << "\n";
			dfs(i);
		}
		cout << ans+n << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
#define all(s) (s).begin(),(s).end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define se second
#define P pair<ll,ll>
const ll mod = 998244353;
//const ll mod = 1000000007;
const ll inf = 2000000000000000000ll;
static const long double pi = 3.141592653589793;
void vcin(vector<ll> &n){for(int i=0;i<int(n.size());i++) cin>>n[i];}
void vcout(vector<ll> &n){for(int i=0;i<int(n.size());i++){cout<<n[i]<<" ";}cout<<endl;}
void YesNo(bool a){if(a){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}
void YESNO(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
template<class T,class U> void chmax(T& t,const U& u){if(t<u) t=u;}
template<class T,class U> void chmin(T& t,const U& u){if(t>u) t=u;}
ll modPow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
ll binary_gcd(ll u, ll v){
    if(min(u, v) == 0) return u|v;
    int shift = __builtin_ctzll(u|v);
    u >>= __builtin_ctzll(u);
    while(v){
        v >>= __builtin_ctzll(v);
        if(u > v) swap(u, v);
        v -= u;
    }
    return u << shift;
}

ll pov(ll base, ll exp, ll mod){
	ll ret = 1;
	while(exp){
		if(exp&1) ret = (__int128)ret * base % mod;
		base = (__int128)base * base % mod;
		exp >>= 1;
	}
	return ret;
}

bool check_composite(ll n, int a, ll d, int s){
	ll x = pov(a, d, n);
	if(x == 1 || x == n-1) return 0;
	for(int r=0;r<s;r++){
		x = (__int128)x * x % n;
		if(x == n-1) return 0;
	}
	return 1;
}

bool MillerRabin(ll n, int iter = 5){
	if(n < 4) return  n == 2 || n == 3;
	int s = __builtin_ctzll(n-1);
	ll d = (n-1) >> s;
	for(int i=0;i<=iter;i++){
		int a = rand() % (n-3) + 2;
		if(check_composite(n, a, d, s)) return 0;
	}
    return 1;
}

ll rho_pollard(ll n){
	if((n&1) == 0) return 2;
	ll x, y, c, g = 1;
	x = y = rand() % n + 1;
	c = rand() % n + 1;
	while(g == 1){
		x = ((__int128)x*x+c) % n;
		y = ((__int128)y*y+c) % n;
		y = ((__int128)y*y+c) % n;
		g = binary_gcd(abs(x-y), n);
	}
	return g;
}

void factorize(ll num, vector<ll> &facts){
	if(num == 1) return;
	if(MillerRabin(num)){
		facts.push_back(num);
		return;
	}
	ll divis = rho_pollard(num);
	factorize(divis, facts);
	factorize(num/divis, facts);
}
vector<pair<ll,ll>> ml(vector<ll> fact){
  vector<pair<ll,ll>> f;
  if(int(fact.size())==0){
    return f;
  }
  sor(fact);
  ll x=fact[0];
  ll y=1;
  for(int i=1;i<int(fact.size());i++){
    if(x==fact[i]){
      y++;
    }
    else{
      pair<ll,ll> z;
      z.fi=x;
      z.se=y;
      f.push_back(z);
      x=fact[i];
      y=1;
    }
  }
  pair<ll,ll> z;
  z.fi=x;
  z.se=y;
  f.push_back(z);
  return f;
}
bool isp(ll a){
  bool res=true;
  if(a==1){
    return false;
  }
  for(int i=2;i*i<=a;i++){
    if(a%i==0){
      return false;
    }
  }
  return true;
}
int main() {
  /* mod は 1e9+7 */
  ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  cout<< fixed << setprecision(20);
  ll a,b;
  cin>>a>>b;
  vector<ll> m(1050000);
  m[0]=1;
  vector<ll> prime;
  map<ll,ll> ip;
  vector<ll> check;
  for(int i=2;i<=72;i++){
    if(isp(i)){
      prime.push_back(i);
    }
  }
  check.push_back(0);
  for(ll i=a;i<=b;i++){
    ll f=0;
    for(int j=0;j<20;j++){
      if(i%prime[j]==0){
        f+=(1<<j);
      }
    }
    for(int j=0;j<int(check.size());j++){
      bool t=true;
      if((check[j]&f)==0){
        if(m[check[j]+f]==0){
          check.push_back(check[j]+f);
        }
        m[check[j]+f]+=m[check[j]];
      }
    }
  }
  ll ans=0;
  for(int i=0;i<1050000;i++){
    ans+=m[i];
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=(1<<20);
bool vis[80];
int prime[25],cnt;
ll dp[N];
void init(){
    int maxn=72;
    cnt=0;
    for(int i=2;i<=maxn;i++){
        if(!vis[i])
            prime[++cnt]=i;
        for(int j=1;j<=cnt&&prime[j]*i<=maxn;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
int main(){
    init();
    ll a,b;
    scanf("%lld%lld",&a,&b);
    dp[0]=1;//空集
    for(ll i=a;i<=b;i++){
        int msk=0;
        for(int j=1;j<=cnt;j++){//得到i含有的质因子
            if(i%prime[j]==0)
                msk|=(1<<(j-1));
        }
        for(int j=0;j<(1<<cnt);j++){
            if(!(msk&j))//表示两个状态互质
                dp[msk|j]+=dp[j];
        }
    }
    ll ans=0;
    for(int i=0;i<(1<<cnt);i++)
        ans+=dp[i];
    printf("%lld\n",ans);
    return 0;
}


#include <stdio.h>
#include<algorithm>
#include <math.h>
#include<iostream>
#include <stack>
#include<string.h>
#include<vector>
using namespace std;
int main()
{
	long long int n,x;
	string a;
	cin>>n>>x;
	cin>>a;
//	scanf("%s",a);
	for( int i=0;i<n;i++){
		if(a[i]=='o'){
			x++;
		}
		else {
			if(x>0){
				x--;
			}
		}
	}
	cout<<x;

}

#include<bits/stdc++.h>
/*#include<boost/multiprecision/cpp_int.hpp>
using boost :: multiprecision::cpp_int;*/
#define ll long long int
#define mod 1000007
using namespace std;
/*int gcd(int a, int b)
{
  if (a == 0)
    return b;
  return gcd(b % a, a);
}*/

void solve()
{
  char s[3];
  char k;
  cin >> s;
  k = s[0];
  for (int i = 0; i < 2; i++)
  {
    s[i] = s[i + 1];
  }
  s[2] = k;
  for (int i = 0; i < 3; i++)
  {
    cout << s[i];
  }
}
int main()
{

  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();

  return 0;

}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-5;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//

int main() {
    ll N;
    cin >> N;
    vc<complex<ld>> S(N), T(N);
    rep(i, N) {
        ld a, b;
        cin >> a >> b;
        S[i] = {a, b};
    }
    rep(i, N) {
        ld a, b;
        cin >> a >> b;
        T[i] = {a, b};
    }

    complex<ld> sc(0, 0), tc(0, 0);
    rep(i, N) sc += S[i], tc += T[i];
    sc /= N, tc /= N;

    rep(i, N){
        S[i] -= sc;
        T[i] -= tc;
    }
    rep(i, N) rep(j, N){
        vc<complex<ld>> ss(N), tt(N);
        rep(si, N) ss[si] = S[si] * polar(1.0L, -arg(S[i]));
        rep(ti, N) tt[ti] = T[ti] * polar(1.0L, -arg(T[j]));

        bool ok = true;
        rep(si, N) {
            bool isok = false;
            rep(ti, N){
                if (abs(ss[si] - tt[ti]) < EPS) isok = true;
            }
            if(!isok){
                ok = false;
                break;
            }
        }
        if(ok){
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
}

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

#define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i)
#define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i)
mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<typename T>void chkmax(T&x,T y){if(x<y)x=y;}
template<typename T>void chkmin(T&x,T y){if(x>y)x=y;}
inline int read() {
  #define nc getchar()
  int x=0;char c=nc;
  while(c<48)c=nc;
  while(c>47)x=x*10+(c^48),c=nc;
  return x;
  #undef nc
}

typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int maxn=5e5+10;
int n;
const string A="atcoder";
string str;

void solve() {
//  scanf("%s",str+1);
//  n=strlen(str+1);
  cin>>str;
  n=str.size();
  if(A<str)return puts("0"),void();
  int pos=-1,cur=0;
  for(char c:str){
    if(c!='a'){
      pos=cur;break;
    }
    cur++;
  }
  if(pos==-1)return puts("-1"),void();
  int cnt=0;
  while(pos){
    swap(str[pos-1],str[pos]),pos--,cnt++;
    if(A<str)break;
  }
  printf("%d\n",cnt);
}

signed main() {
  int T=read();
  while(T--)solve();
//  solve();
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define black_tree tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define pair_black_tree tree<pair<ll,ll>,null_type,less_equal<pair<ll,ll>>,rb_tree_tag,tree_order_statistics_node_update>
#define gp __gnu_pbds
#define INF 1000000000
#define MOD 1000000007
#define MAX 1000001
#define endl '\n'
#define ll long long
#define ld long double
#define lli long long int
#define ull unsigned long long
#define ulli unsigned long long int
#define pb push_back
#define pf push_front
#define ook order_of_key
#define fbo find_by_order
#define ffo find_first_of
#define np next_permutation
#define mp make_pair
#define eb emplace_back
#define mae max_element
#define mie min_element
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
using namespace std;
using namespace gp;
vector<ll>v;
ll n,k,ans,cnt,g[9][9];
string add(string a,string b){
	if(a.size()>b.size()) swap(a,b);
	string s="";
	ll l1=a.size(),l2=b.size(),diff=l2-l1,carry=0;
	for(int i=l1-1;i>=0;i--){
		ll num=(a[i]-'0'+b[i+diff]-'0')+carry;
		s.pb(num%10+'0');
		carry=num/10;
	}
	for(int i=l2-l1-1;i>=0;i--){
		ll num=b[i]-'0'+carry;
		s.pb(num%10+'0');
		carry=num/10;
	}
	if(carry) s.pb(carry+'0');
	reverse(s.begin(),s.end());
	return s;
}
string multiply(string a,string b){
	if(a.empty() || b.empty()) return "0";
	ll l1=a.size(),l2=b.size(),fi=0,fj=0,carry,num1,num2;
	vector<ll>result(l1+l2,0LL);
	for(int i=l1-1;i>=0;i--){
		carry=0,num1=a[i]-'0',fj=0;
		for(int j=l2-1;j>=0;j--){
			num2=b[j]-'0';
			ll sum=num1*num2+result[fi+fj]+carry;
			carry=sum/10;
			result[fi+fj]=sum%10;
			fj++;
		}
		if(carry) result[fi+fj]+=carry;
		fi++;
	}
	ll i=result.size()-1;
	while(i>=0 && !result[i]) i--;
	if(i==-1) return "0";
	string s="";
	while(i>=0) s+=to_string(result[i--]);
	return s;
}
int main(){
	//freopen("file.in","r",stdin);
	//freopen("file.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>g[i][j];
		}
	}
	for(int i=2;i<=n;i++) v.pb(i);
	do{
		ans=0;
		ans+=g[1][v[0]];
		for(int i=1;i<v.size();i++) ans+=g[v[i-1]][v[i]];
		ans+=g[v[v.size()-1]][1];
		if(ans==k) cnt++;
	}
	while(np(v.begin(),v.end()));
	cout<<cnt;
}

#include<bits/stdc++.h>
using namespace std;
#define speed ios_base::sync_with_stdio;cin.tie(nullptr);cin.tie(nullptr);
typedef long long int ll;
#define t int t;cin>>t;while(t--)
#define For(n) for(int i=0;i<n;i++)
#define ne(a) int a;cin>>a;
bool great(pair<int,int>a,pair<int,int>b)
{
	return (a.first<b.first);
}
template<class c>
c gcd (c a,c b)
{
	if(b==0)
		return a;
	gcd(b,a%b);
}
int main()
{
	speed
	ne(n) ne(k)
	int arr[n][n];
	for(int i=0;i<n;i++)
	{
		for(int j =0;j<n;j++)
		{
			cin>>arr[i][j];
		}
	}
	int count =0;int result = 0;
	int tarr[n];
	for(int i =0;i<n;i++)
	{
		tarr[i] = i;
	}

	do {
		int count =0;
       	for(int i=0;i<n;i++)
       	{
       		count+=arr[tarr[i]][tarr[(i+1)%n]];
       	}
       	if(count==k)
       		result++;
  	} while (next_permutation(tarr+1, tarr + n));
	cout<<result<<endl;
}


#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
#include<tuple>
#include<time.h>
#include<list>

#define DIV 1000000007
using namespace std;
using ll = long long;
using ldb = long double;

vector<int> par;
int root(int a) {
	if (par[a] == a)return a;
	else return par[a] = root(par[a]);
}

void unite(int a, int b) {
	int art = root(a), brt = root(b);
	if (art == brt) return;
	par[brt] = art;
}

int main() {
	int N, M; cin >> N >> M;
	par = vector<int>(N + 1);
	vector<ll> p(N + 1), q(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> p[i];
		par[i] = i;
	}

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

	for (int i = 0; i < M; i++) {
		int c, d; cin >> c >> d;
		unite(c, d);
	}

	vector<ll> rtp(N + 1), rtq(N + 1);
	for (int i = 1; i <= N; i++) {
		int rt = root(i);
		//cout << "i=" << i << " rt=" << rt << endl;
		rtp[rt] += p[i], rtq[rt] += q[i];
	}
	bool fl = true;

	for (int i = 1; i <= N; i++) {
		//cout << rtp[i] << " " << rtq[i] << endl;
		if (rtp[i] != rtq[i]) {
			fl = false;
			break;
		}
	}

	if (fl) cout << "Yes" << endl;
	else cout << "No" << endl;
}

#include <bits/stdc++.h>

#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define FILL0(x) memset(x,0,sizeof(x))
#define FILL1(x) memset(x,-1,sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

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

vector<ll> G[200000];

bool visited[200000];

vector<vector<ll>> group;

void dfs(int c, ll *ab){

    visited[c] = true;
    group[group.size()-1].push_back(c);

    rep(i, G[c].size()){
        if(!visited[G[c][i]])    dfs(G[c][i], ab);
    }
    return;
}

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

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

    ll a[n], b[n];
    rep(i, n){
        cin >> a[i];
    }
    rep(i, n){
        cin >> b[i];
    }
    rep(i, m){
        int c, d;
        cin >> c >> d;
        c--; d--;

        G[c].push_back(d);
        G[d].push_back(c);

    }

    ll ab[n];
    rep(i, n){
        ab[i] = a[i]-b[i];
    }

    rep(i, n){
        if(!visited[i]){
            vector<ll> gg;
            group.push_back(gg);
            dfs(i, ab);
        }
    }

    rep(i, group.size()){
        ll tmp = 0;
        rep(j, group[i].size()){
            tmp += ab[group[i][j]];
        }
        if(tmp != 0){
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(v) (v).size()
#define all(v) (v).begin(),(v).end()
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Time (double)clock()/CLOCKS_PER_SEC
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pi;
template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}
template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}
template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}
const int inf=1000000000,mod=1000000007;
const ll infll=1000000000000000000ll,modll=ll(mod);
const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);
//#define mydef
//#define LOCAL
#ifdef mydef
#define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#endif
#define debug
#define putnum(x) cout<<#x<<": "<<x<<endl
#define putvec(v) cout<<#v<<": ";REP(i,0,sz(v)) cout<<v[i]<<" ";cout<<endl
template <class T> inline void read(T &x){
	T w=1;x=0;char c=getchar();
	while(c<'0'||c>'9') {if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
	x*=w;
}
template <class T> inline void write(T x) {
	if(x<0) putchar('-'),x=~x+1;
	if(x) write(x/10),putchar(x%10+'0');
}

const int maxn=1005;
const int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
int h,w,ans1,ans2;
int cnt1[maxn],cnt2[maxn];
int a[maxn][maxn],vis[maxn][maxn][4],hav[maxn][maxn];
string str;

struct point {
	int x,y;
	int dir;
};
queue<point> q;

bool cango(point pos) {
	if(pos.x<1||pos.y<1||pos.x>h||pos.y>w) return false;
	return true;
}

vector<int> v1[maxn],v2[maxn];

void dfs1(int pos) {
	cnt1[pos]=0;
	for(int i=0;i<sz(v1[pos]);i++) {
		int to=v1[pos][i];
		if(!cnt1[to]) continue;
		dfs1(to);
	}
	return;
}

void dfs2(int pos) {
	cnt2[pos]=0;
	for(int i=0;i<sz(v2[pos]);i++) {
		int to=v2[pos][i];
		if(!cnt2[to]) continue;
		dfs2(to);
	}
	return;
}

int main() {
	#ifdef LOCAL
		freopen("input.in","r",stdin);
		freopen("output.out","w",stdout);
	#endif
	scanf("%d%d",&h,&w);
	for(int i=1;i<=h;i++) {
		cin>>str;
		for(int j=1;j<=w;j++) {
			if(str[j-1]=='#')
				a[i][j]=1;
		}
	}
	for(int i=0;i<4;i++) {
		q.push((point){1,1,i});
		vis[1][1][i]=1;
	}
	while(!q.empty()) {
		point pos=q.front();
		hav[pos.x][pos.y]=1;
		q.pop();
		point to=pos;
		to.x+=dx[to.dir];
		to.y+=dy[to.dir];
		if(!cango(to)) continue;
		point ag=to;
		ag.x+=dx[to.dir];
		ag.y+=dy[to.dir];
		if(!cango(ag)||a[to.x][to.y]==1) {
			for(int i=0;i<4;i++) {
				if(!vis[to.x][to.y][i]) {
					q.push((point){to.x,to.y,i});
					vis[to.x][to.y][i]=1;
				}
			}
		} else {
			if(!vis[to.x][to.y][to.dir]) {
				q.push(to);
				vis[to.x][to.y][to.dir]=1;
			}
		}
	}
/*	for(int i=1;i<=h;i++) {
		for(int j=1;j<=w;j++) {
			cout<<hav[i][j]<<" ";
		}
		cout<<endl;
	}*/

	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(!hav[i][j])
				cnt1[i]=1,cnt2[j]=1;
	for(int i=1;i<=h;i++) {
		int is=0;
		for(int j=1;j<=w;j++) {
			if(hav[i][j]) continue;
			if(is&&a[i][j]) {
				v2[j].pb(is);
				v2[is].pb(j);
			}
			if(a[i][j]) is=j;
		}
	}
	for(int i=1;i<=w;i++) {
		int is=0;
		for(int j=1;j<=h;j++) {
			if(hav[j][i]) continue;
			if(is&&a[j][i]) {
				v1[j].pb(is);
				v1[is].pb(j);
			}
			if(a[j][i]) is=j;
		}
	}
	for(int i=1;i<=h;i++) {
		if(cnt1[i]) {
			ans1++;
			dfs1(i);
		}
	}
	for(int i=1;i<=w;i++) {
		if(cnt2[i]) {
			ans2++;
			dfs2(i);
		}
	}
	cout<<min(ans1,ans2)<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;

const ll N=1010;

char c[1010][1010];
char d[1010][1010];

/*---------------- Union Find (DSU) ------------------*/
int pa[N],sz[N];
ll find_pa(ll pos){
	if(pa[pos]!=pos)pa[pos]=find_pa(pa[pos]);
	return pa[pos];
}
void join(int a, int b) {
	int x=find_pa(a),y=find_pa(b);
	if (x!=y) {
		if(sz[x]>=sz[y]) sz[x]+=sz[y],pa[y]=x;
		else sz[y]+=sz[x],pa[x]=y;
	}
}
void dsu_init(ll n){fore(i,0,n)pa[i]=i,sz[i]=1;}

int main(){FIN;
	ll n,m; cin>>n>>m;
	fore(i,0,n){
		fore(j,0,m){
			cin>>c[i][j];
		}
	}
	c[0][0]='#';
	c[0][m-1]='#';
	c[n-1][0]='#';
	c[n-1][m-1]='#';
	dsu_init(n);
	fore(j,0,m){
		vector<ll> v;
		fore(i,0,n){
			if(c[i][j]=='#')v.pb(i);
		}
		fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
	}
	ll res0=0;
	fore(i,1,n){
		if(find_pa(0)!=find_pa(i)){
			res0++;
			join(0,i);
		}
	}
	fore(i,0,n){
		fore(j,0,m){
			d[j][i]=c[i][j];
		}
	}
	swap(n,m);
	fore(i,0,n){
		fore(j,0,m){
			c[i][j]=d[i][j];
		}
	}
	dsu_init(n);
	fore(j,0,m){
		vector<ll> v;
		fore(i,0,n){
			if(c[i][j]=='#')v.pb(i);
		}
		fore(i,0,SZ(v)-1)join(v[i],v[i+1]);
	}
	ll res1=0;
	fore(i,1,n){
		if(find_pa(0)!=find_pa(i)){
			res1++;
			join(0,i);
		}
	}
	cout<<min(res0,res1)<<"\n";

	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll l,dp[N][14];

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>l;
	dp[0][0]=1;
	REP1(i,l){
		REP1(k,12){
			REP(j,i){
				dp[i][k]+=dp[j][k-1];
			}
		}
	}
	cout<<dp[l][12]<<"\n";
	return 0;
}

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

template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int L;
  cin >> L;

  vector<vector<long>> ncr;
  auto pas = [&](auto n) {
    if (n < (int)ncr.size()) return;
    ncr.resize(n + 1, vector<long>(n + 1));
    ncr.at(0).at(0) = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= i; j++) {
        ncr.at(i).at(j) += ncr.at(i - 1).at(j);
        if (j) ncr.at(i).at(j) += ncr.at(i - 1).at(j - 1);
      }
    }
  };
  pas(1000);

  cout << ncr.at(L - 12 + 11).at(11) << '\n';
}


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

const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int INF = 2e9;
const long long inf = 1LL<<62;

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<998244353> mint;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  mint ans = 0;
  for (int i=0; i<n; i++) {
    cin >> a[i];
    ans += mint(a[i])*mint(a[i]);
  }
  sort(a.begin(), a.end());
  vector<mint> acc(n+1, 0);
  for (int i=0; i<n; i++) {
    acc[i+1] = mint(2)*acc[i] + mint(a[i]);
  }
  for (int i=1; i<n; i++) {
    ans += mint(a[i])*acc[i];
  }
  cout << ans << endl;
}

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

#define int long long
typedef long long ll ;
typedef pair <int, int >  pii ;
typedef vector <int> vi ;
#define umap unordered_map
// #define endl '\n'

#define vec vector
#define all(v) (v).begin() , (v).end()
#define sumof(v) accumulate(all(v), 0LL, plus<ll>())
#define maxof(v) max_element(all(v))
#define minof(v) min_element(all(v))
#define rev(v) reverse(all(v))
#define sz(v)  (long long)(v).size()

#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define PI acos(-1)

#define tc(t) ll testcases ; cin >> testcases ; for(int testcase = 1 ; testcase <= testcases ; testcase++)
#define printstl(c) for (auto it = (c).begin(); it != --((c).end()); it++) cout << *it << " "; cout << *(c.rbegin()) << "\n" << flush
#define printv(v, s, e) for(int i=(s); i<(e); i++) cout<<(v)[i]<<" "; cout<<endl
#define printnl(v) cout << v << "\n" << flush
#define invec(vec_name, vec_size) vec<ll> vec_name(vec_size); for(int i=0; i<vec_size; i++) cin >> vec_name[i];
#define fastio ios::sync_with_stdio(false); cin.tie(0);

#ifndef ONLINE_JUDGE
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__);
#else
#define pr(...) {} ;
#endif
#define Printer os << "[ "; for (auto &it : p) os << it << " "; return os << "]";
#define PairPrint return os << "(" << p.first << ", " << p.second << ")";
#define printCase(n) cout<<"Case #"<<(n)<<":"<<" "
template <class T> ostream &operator <<(ostream &os, const vector<T> &p) {Printer}
template <class T> ostream &operator <<(ostream &os, const unordered_set<T> &p) {Printer}
template <class T> ostream &operator <<(ostream &os, const set<T> &p){Printer}
template <class T> ostream &operator <<(ostream &os, const multiset<T> &p){Printer}
template <class S, class T> ostream &operator <<(ostream &os, const pair<S, T> &p) {PairPrint}
template <class S, class T> ostream &operator <<(ostream &os, const unordered_map<S, T> &p){Printer}
template <class S, class T> ostream &operator <<(ostream &os, const map<S, T> &p){Printer}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n"; }
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ", "; dbs(str.substr(idx + 1), s...); }
ll modExp(ll x, ll y, ll mod) {x %= mod, y %= (mod - 1) ; ll res = 1; while (y) {if (y & 1) res = (res * x) % mod; y /= 2, x = (x * x) % mod; } return res % mod; }
int cel(int num, int den) {if(num % den == 0) return num / den; else return 1 + num / den; }
// vi r = {0,1,1,0};
// vi c = {0,1,0,1};
// vector<int> getRand(int x, int val)
// {
//     srand(time(NULL));
//     vi random;
//     for(int i=0; i<x; i++) random.pb(rand()%val);
//     return random;
// }
vector<int> constructPrefix(vector<int> v, int mod)
{
    int n = v.size();
    vector<int> ans;
    int sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum = (sum%mod + v[i]%mod)%mod;
        ans.push_back(sum);
    }
    return ans;
}
// void seive()
// {
//     for(int i = 2; i <= LIMIT; i++)
//     {
//         if(not done[i])
//         {
//             primes.pb(i);
//             for(int j = i; j <= LIMIT; j += i)
//             {
//                 done[j] = 1;
//             }
//         }
//     }
// }
// void getFreq(vi &v, map<int, int> &m)
// {
//     for(int i = 0; i < v.size(); i++)
//     {
//         m[v[i]]++;
//     }
// }

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
        freopen("./input.txt", "r", stdin);
    #endif

        int n;
        cin>>n;
        invec(v,n);
        sort(all(v));
        vi mn(n,0), dp(n,0);
        mn[0]= v[0];
        int mod = 998244353;
        dp[0]= (v[0]*v[0])%mod;
        for(int i=1; i<n; i++)
        {
            v[i] = v[i]%mod;
            mn[i] = (mn[i-1]*2 +v[i])%mod;
            dp[i]= (dp[i-1]%mod + (((mn[i-1] + v[i])%mod)*v[i])%mod)%mod;
        }
        // pr(dp)
        cout<<dp[n-1]<<endl;

}


#include<iostream>
#include<cstring>
#define debug(a) cout<<#a<<" = "<<a<<endl
using namespace std;

typedef long long ll;
using PII = pair<int,int>;
const int N = 2e5 + 10 , inf = 1e9;
int he[N << 1] ,nex[N << 1] , v[N << 1] , idex;

void add(int a,int b){
    nex[idex] = he[a];
    v[idex] = b;
    he[a] = idex ++;
}

int n , k;
int f[N] , g[N];
int mid;
int cnt;

void dfs(int fa,int node){
    f[node] = inf , g[node] = 0;
    for(int i = he[node] ; ~i; i = nex[i]){
        if(v[i] == fa) continue;
        dfs(node , v[i]);
        f[node] = min(f[node] , f[v[i]] + 1);
        g[node] = max(g[node] , g[v[i]] + 1);
    }
    if(f[node] + g[node] <= mid) g[node] = -inf;
    else if(g[node] == mid) g[node] = -inf, f[node] = 0 , ++cnt;
}

int main(){
    memset(he,0xff,sizeof he);
    cin >> n >> k;
    for(int i = 1 , a , b ; i < n ; i ++){
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    int l = 0 , r = n ;
    while(l < r){
        mid = (l + r) >> 1;
        cnt = 0;
        dfs( -1 ,1);
        if(g[1] >= 0) cnt ++ ;
        if(cnt > k) l = mid + 1;
        else r = mid;
    }
    cout << l << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ALL(v) v.begin(), v.end()
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define dbg(x) cout << (#x) << " is " << (x) << endl;
#define dbg2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << y << endl;
#define dbgarr(x, sz)                                             \
  for (int asdf = 0; asdf < (sz); asdf++) cout << x[asdf] << ' '; \
  cout << endl;
#define dbgarr2(x, rose, colin)                \
  for (int asdf2 = 0; asdf2 < rose; asdf2++) { \
    dbgarr(x[asdf2], colin);                   \
  }
#define dbgitem(x)                                                           \
  for (auto asdf = x.begin(); asdf != x.end(); asdf++) cout << *asdf << ' '; \
  cout << endl;

const int inf = -1e8, MAXN = 2e5;

int n, K, f[MAXN], g[MAXN];
vi adj[MAXN];
struct Solution {
  int t;
  int solve() {
    int low = 0, high = n;  // time needed to spread info
    while (low <= high) {
      t = (low + high) / 2;
      if (able())
        high = t - 1;
      else
        low = t + 1;
    }
    return low;
  }
  bool able() {
    int res = 0;
    dfs(0, -1);
    REP(i, n) {
      res += f[i] == t;
    }
    return res + (g[0] >= 0) <= K;
  }
  void dfs(int i, int p) {
    f[i] = -1, g[i] = 0;
    for (int j : adj[i]) {
      if (j == p) continue;
      dfs(j, i), f[i] = max(f[i], f[j] - 1), g[i] = max(g[i], g[j] + 1);
    }
    if (f[i] >= g[i])
      g[i] = -1;  // every node is covered
    else if (g[i] == t) // make this a source
      g[i] = -1, f[i] = t;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> K;
  int a, b;
  REP(i, n - 1) {
    cin >> a >> b, adj[--a].push_back(--b), adj[b].push_back(a);
  }
  Solution test;
  cout << test.solve() << endl;
}


// ＵＴＦ−８

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<ll>;

/* #include<atcoder/all> */
/* using namespace atcoder; */

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

int main(void) {
    constexpr ll MOD = 1 ? 1e9+7 : 998244353;
    const double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    auto f = [&](char x) {
        if('0' <= x && x <= '9')
            return x - '0';
        return x - 'A' + 10;
    };

    if(1) {
        string n;
        ll k;
        cin >> n >> k;
        ll s = n.size();

        vector<vector<ll>> dq(s, vector<ll>(16+1)); // dp ぴったし, dq 未満
        dq[0][0] = 1;
        dq[0][1] = n[0] == '1' ? 0 : (f(n[0]) - 1);

        set<ll> v;
        v.insert(f(n[0]));
        for(ll i=1; i<s; i++) {
                (dq[i][0] += (dq[i-1][0])) %= MOD;
            for(ll j=0; j<=16; j++) {
                (dq[i][j] += (j * dq[i-1][j])) %= MOD;
                if(j == 0)
                    (dq[i][j+1] += ((15-j) * dq[i-1][j])) %= MOD;
                else if(j < 16)
                    (dq[i][j+1] += ((16-j) * dq[i-1][j])) %= MOD;
            }
            for(ll j=0; j<f(n[i]); j++) {
                (dq[i][v.size() + !v.count(j)] += 1) %= MOD;
            }
            v.insert(f(n[i]));
        }
        ll r = dq[s-1][k] + (v.size() == k);
        cout << (r % MOD) << endl;
    }

    return 0;
}

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

int par[MAX];
int depth[MAX];
int siz[MAX];
//初期化
void init(){
    for(int i=0;i<MAX;i++){
        par[i]=i;
        siz[i]=1;
    }
}
//根を探索
int root(int x){
    if(par[x]==x){
        return x;
    }else{
        return par[x]=root(par[x]);
    }
}

//統合
void unit(int x,int y){
    x=root(x);
    y=root(y);
    if(x==y)return;
    if(depth[x]>depth[y])swap(x,y);
    par[x]=y;
    siz[y]+=siz[x];
    if(depth[x]==depth[y])depth[x]++;
}
// 同じ集合か判定
bool issame(int x,int y){
    return root(x)==root(y);
}

//集合の大きさ
int size(int x){
    return siz[root(x)];
}
bool ext[MAX];
int modPow(long long a, long long n) {
    if(n==0)return 1;
  if (n == 1) return a % MOD;
  if (n % 2 == 1) return (a * modPow(a, n - 1)) % MOD;
  long long t = modPow(a, n / 2);
  return (t * t) % MOD;
}
void solve(){
    int n;
    cin>>n;
    init();
    REP(i,n){
        int f;
        cin>>f;
        f--;
        unit(i,f);
        ext[f]=1;
    }
    map<int,int>mp;
    REP(i,n){
        if(!ext[i])continue;
        mp[root(i)]++;
    }
    int ans=modPow(2,SZ(mp))-1;
    if(ans<0)ans+=MOD;
    OUT(ans);

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


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

set<string> st;

void solve1()
{
	vector<string> print;
	string s;
	int cnt = 0;
	for (auto iter = st.begin(); iter != st.end(); iter++) {
		s += *iter;
		if (cnt == 20) break;
		if (s.length() > 18) {
			while (s.length() > 20) {
				s.pop_back();
			}
			print.push_back(s);
			cnt++;
			s = "";
		}
	}

	for (auto iter = print.begin(); iter != print.end(); iter++) {
		cout << *iter;
		for (int i = 0; i < 20 - (*iter).length(); i++) {
			cout << ".";
		}
		cout << endl;
	}
}

void solve2()
{
	string print[20];
	int h = 0;
	for (auto iter = st.begin(); iter != st.end(); iter++) {
		print[h % 20] += *iter;
		h++;
	}
	for (int h = 0; h < 20; h++) {
		for (int w = 0; w < 20; w++) {
			cout << print[h][w];
		}
		cout << endl;
	}
}

bool seen[20][20];
string M[20];

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

bool comp(const std::string& lh, const std::string& rh) {

    return lh.length() < rh.length();
}

class Comp {
public:
    bool operator() (const std::string& lh, const std::string& rh) {

        return lh.length() < rh.length();
    }
};

int main()
{
	int n,m;
	cin >> n >> m;
	vector<string> s;
	for (int i = 0; i < m; i++) {
		string t;
		cin >> t;
		//s.push_back(t);
		st.emplace(t);
	}

	solve1();
	//solve2();
}

#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) ((x)&(-x))
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u],v=sq[i].to;i;i=sq[i].nxt,v=sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

inline ll readll()
{
    ll x=0;int f=1;char ch=getchar();
    while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

ll f[110],n;
int m;
vi ans,a;

int main()
{
    f[0]=1;f[1]=1;m=88;
    rep(i,2,m) f[i]=f[i-2]+f[i-1];
    //rep(i,1,10) cout << f[i] << " ";cout << endl;
    n=readll();
    per(i,m,1)
    {
        if (n>=f[i]) {n-=f[i];a.pb(i);}
    }
    int siz=a.size();
    int len=(a[0]+1)/2,pos=siz-1;
    //rep(i,0,siz-1) printf("%d ",a[i]);puts("");
    rep(i,1,len)
    {
        ans.pb(3);ans.pb(4);
        while ((pos>=0) && ((a[pos]+1)/2==i))
        {
            if (a[pos]&1) ans.pb(2);else ans.pb(1);pos--;
        }
    }
    len=ans.size();
    printf("%d\n",len);
    per(i,len-1,0) printf("%d\n",ans[i]);
    return 0;
}