    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef vector<int> vi;
    typedef pair<int,int> pi;
    #define PB push_back
    #define MP make_pair
    #define REP(i,a,b) for(int i = a; i<=b; i++)
void print(stack<char> st){
    while(!st.empty()){
        cout << st.top() << " ";
        st.pop();
    }
}
    int main (){
    	ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int n = 0;
        cin >> n;
        int a[n], b[n];
        int maxa = INT_MIN , minb = INT_MAX;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            maxa = max(maxa, a[i]);
        }
        for(int i = 0; i < n; i++){
            cin >> b[i];
            minb = min(minb, b[i]);
        }
        int ans = minb - maxa + 1;
        if(ans <= 0){
            cout << 0 << "\n";
        }else{
            cout << ans  << "\n";
        }
        return 0;
    }

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define loops(i, s, n) for (ll i = s; i < (ll)n; i++)
#define loop(i, n) loops(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define pub push_back
#define pob pop_back
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> v;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vl;

inline int in_int()
{
  int x;
  cin >> x;
  return x;
}
inline ll in_ll()
{
  ll x;
  cin >> x;
  return x;
}
inline string in_str()
{
  string x;
  cin >> x;
  return x;
}

// for dp
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T>
inline bool chmin(T &a, const T &b)
{
  bool compare = a > b;
  if (a > b)
    a = b;
  return compare;
}
template <typename T>
inline bool chmax(T &a, const T &b)
{
  bool compare = a < b;
  if (a < b)
    a = b;
  return compare;
}
void _INPUT_IMPL_(){};
template <class T, class... Args>
void _INPUT_IMPL_(T &dest, Args &...args)
{
  cin >> dest;
  _INPUT_IMPL_(args...);
}
void _OUTPUT_IMPL_(){};
template <class T, class... Args>
void _OUTPUT_IMPL_(T dest, Args... args)
{
  cout << dest << (sizeof...(Args) ? " " : "");
  _OUTPUT_IMPL_(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  _INPUT_IMPL_(__VA_ARGS__)
#define in_v(type, name, cnt) \
  vector<type> name(cnt);     \
  loop(i, cnt) cin >> name[i];
#define sort_v(v) std::sort(v.begin(), v.end())
#define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行
#define out(...) (_OUTPUT_IMPL_(__VA_ARGS__), std::cout << std::endl)
#define set_fix(x) ((std::cerr << std::fixed << std::setprecision(x)), (std::cout << std::fixed << std::setprecision(x)))

//cout for vector
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v)
{
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

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

//prime numbers
bool IsPrime(ll num)
{
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (ll i = 3; i <= sqrtNum; i += (ll)2)
  {
    if (num % i == 0)
    {
      return false;
    }
  }
  return true;
}
vector<ll> Eratosthenes(ll N)
{
  int arr[N];
  vector<ll> res;
  for (int i = 0; i < N; i++)
  {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++)
  {
    if (arr[i])
    {
      for (int j = 0; i * (j + 2) < N; j++)
      {
        arr[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < N; i++)
  {
    if (arr[i])
    {
      res.push_back(i);
    }
  }
  return res;
}

//digit number
int GetDigit(ll num, ll radix)
{
  unsigned digit = 0;
  while (num != 0)
  {
    num /= radix;
    digit++;
  }
  return digit;
}

int digsum(ll n)
{
  ll res = 0;
  while (n > 0)
  {
    res += n % (ll)10;
    n /= (ll)10;
  }
  return res;
}

int main()
{
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  set<int> a;
  for (int i = 0; i < n; i++)
  {
    int ai;
    cin >> ai;
    a.insert(ai);
  }
  int mn = *a.begin();
  int mx = *a.rbegin();
  dump(mx);
  dump(mn);
  int cnt = 0;
  while (mx != mn)
  {
    a.erase(mx);
    a.insert(mx - mn);
    mn = *a.begin();
    mx = *a.rbegin();
    cnt++;
    // cout << "cnt: " << cnt << " mx: " << mx << " mn: " << mn << endl;
  }

  cout << mx << endl;
  return 0;
}


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

#define  fast              ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define _1a1ee             int t;cin>>t;for(int tt=0; tt<t; ++tt)
#define  MOD               1000000007
#define  pb                push_back
#define  ppb               pop_back
#define  mp                make_pair
#define  ff                first
#define  ss                second
#define  br                cout <<'\n';
#define  sz(x)             ((int)(x).size())
#define  lcm(x,y)          (x*y/__gcd(x,y))
#define  PI                3.141592653589793238462
#define  all(x)            (x).begin(), (x).end()
#define  f(i,k,n)          for(long long int i=k; i<n; ++i)
#define  fr(i,k,n)         for(long long int i=k; i>=n; --i)
#define  ni1(a)            ll a;cin>>a;
#define  ni2(a,b)          ll a,b;cin>>a>>b
#define  ni3(a,b,c)        ll a,b,c;cin>>a>>b>>c
#define  ni4(a,b,c,d)      ll a,b,c,d;cin>>a>>b>>c>>d
#define  ni5(a,b,c,d,e)    ll a,b,c,d,e;cin>>a>>b>>c>>d>>e
#define  ni6(a,b,c,d,e,f)  ll a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f

typedef long long int           ll;
typedef unsigned long long      ull;
typedef long double             lld;
typedef vector<int>             vi;
typedef vector<long long int>   vll;

///////////////////////////////////////////////---#--DEBUG--#---//////////////////////////////////////////////////////////////

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

//////////////////////////////////////////////---#--MAIN-CODE--#---///////////////////////////////////////////////////////////

long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

void solve(){
  ni1(n);
  ll ans = 1.08*n;
  //cout<<ans;
  if(ans < 206)cout<<"Yay!";
  else if(ans == 206)cout<<"so-so";
  else if(ans > 206)cout<<":(";
}

int main()
{

#ifndef ONLINE_JUDGE
  freopen("Error.txt", "w", stderr);
#endif

  fast
  clock_t start,end;
  start = clock();
  {solve();}
  end = clock();
  double time_taken = double(end-start) / double(CLOCKS_PER_SEC);

#ifndef ONLINE_JUDGE
  cerr << "Time: " << (time_taken*1000) << "\n";
#endif

  return 0;
}

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int main(){
  int N; cin >> N;
  N = N * 108 / 100;
  if(N < 206) cout << "Yay!";
  else if(N == 206) cout << "so-so";
  else cout << ":(";
  cout << endl;
  return 0;
}

struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_inst;


#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
void chmin(int& a, int b){ if(a > b){ a = b; } }

int main(){
    int N, M;
    cin >> N >> M;
    vector<int> H(N);
    for(auto& h : H) cin >> h;
    vector<int> W(M);
    for(auto& w : W) cin >> w;
    sort(H.begin(), H.end());
    vector<int> sum1((N + 1) / 2), sum2((N + 1) / 2);
    for(int i = 0; i + 1 < N; i += 2) sum1[i / 2 + 1] = sum1[i / 2] + H[i + 1] - H[i];
    for(int i = N - 2; i > 0; i -= 2) sum2[i / 2] = sum2[i / 2 + 1] + H[i + 1] - H[i];
    int ans = INF;
    for(int w : W){
        int x = lower_bound(H.begin(), H.end(), w) - H.begin();
        if(x & 1) x ^= 1;
        chmin(ans, sum1[x / 2] + sum2[x / 2] + abs(H[x] - w));
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n);
    set<ll> Set;
    ll x,maxv,minv;
    rep(i,n){
        in(x);
        Set.insert(x);
    }
    while(true){
        if(Set.size()==1)break;
        maxv=*Set.rbegin();
        minv=*Set.begin();
        Set.erase(maxv);
        Set.insert(maxv-minv);
    }
    out((ll)*Set.begin());
}


// author : lynmisakura
// TIMESTAMP: 2020-10-17, 20:07:37

#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i = 0;i < n;i++)
#define RNG(i,s,n) for(int i = s;i < n;i++)
#define _RNG(i,s,e) for(int i=s;i>=e;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dup(x,y) (x + y - 1) / (y)
#define all(x) (x).begin(),(x).end()
#define lb(x,key) lower_bound(all(x) , (key))
#define ub(x,key) upper_bound(all(x) , (key))

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

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

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
  cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
  return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
  cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
  return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
  cout << "{" << p.first << "," << p.second << "}" << '\n';
  return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
  for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
  cin >> p.first >> p.second; return in;
}

#define F first
#define S second
/////////////////////////////////////////////////////////////////////////

ll x,y,a,b;
ll ans;
map<ll,ll> M;
int cnt = 0;
void dfs(ll tmp,ll cur = 0){
  //if(cnt++ <= 20) cout << tmp << ' ' << cur << endl;

  if(tmp >= y){
    return;
  }

  chmax(ans , cur + (y - 1 - tmp) / b);

  if((y / tmp) <= x) return;
  if(!M.count(a * tmp) || (M[a * tmp] < cur + 1)){
    M[a * tmp] = cur + 1;
    dfs(a * tmp , cur + 1);
  }

  //if(!M.count(b + tmp) || (M[b + tmp] < cur + 1)){
  //  M[b + tmp] = cur + 1;
  //  dfs(b + tmp , cur + 1);
  //}

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

  cin >> x >> y >> a >> b;
  dfs(x);
  cout << ans << '\n';

  return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define  jx 1000000
char s[100001];
int len,f[26];
int main(){
	cin>>len;
	scanf("%s",s);
	fo(i,0,25)f[i]=jx;
	f[s[0]-'a']=0;
	if(len==1){
		printf("-1");
		return 0;
	}
	fo(i,1,len-2){
		int u=jx;
		fo(j,0,25){
			if(j!=(s[i]-'a'))u=min(u,f[j]+1);
		}
		f[s[i+1]-'a']=min(u,f[s[i+1]-'a']);
	}
	int u=jx;
	fo(j,0,25){
		if(j!=(s[len-1]-'a'))u=min(u,f[j]+1);
	}
	if(u>=jx)printf("-1");
	else printf("%d",u);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = 1e18;

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

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

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

int main() {
    int n, m, k;    cin >> n >> m >> k;
    vector<int> a(n+1, 0);
    rep(i, k) {
        int x;  cin >> x;
        a[x] = 1;
    }
    for (int i = 0; i < n+1; i++) {
        int j = i;
        while (j < n+1 && a[j] == 1)
            j++;
        if (j - i >= m) {
            cout << -1 << endl;
            return 0;
        }
        i = j;
    }
    vector<double> b(n+m+10), c(n+m+10), sb(n+m+10, 0), sc(n+m+10, 0);
    for (int i = n-1; i >= 0; i--) {
        if (a[i]) {
            b[i] = 1;
            c[i] = 0;
        }
        else {
            b[i] = (sb[i+1] - sb[i+m+1]) / m;
            c[i] = (sc[i+1] - sc[i+m+1]) / m + 1;
        }
        sb[i] = b[i] + sb[i+1];
        sc[i] = c[i] + sc[i+1];
    }
    cout << fixed << std::setprecision(15) << c[0] / (1 - b[0]) << endl;
    return 0;
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
const double eps = 1e-10;
const double INF = 1e9;

class Union_Find{
  vector<int> par; vector<int> rank; vector<int> num;
public:
  Union_Find(int n) {
    par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1);
    rep(i,n) par[i] = i;
  }
  int find(int x) {
    if(par[x] == x) return x;
    else  return par[x] = find(par[x]);
  }
  int number(int x) {
    return num[find(x)];
  }
  void unite(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) swap(x, y);
    if (rank[x] == rank[y]) rank[x]++;
    par[y] = x;
    num[x] += num[y];
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

int main(){
  int n;
  cin >> n;
  double x[n], y[n];
  rep(i,n) cin >> x[i] >> y[i];
  double l = 0, r = 100.0;
  while(r - l > eps) {
    double m = (l + r) / 2;
    Union_Find uf(n+2);
    rep(i,n) {
      if(y[i]+m > -m+100.0) uf.unite(n,i);
      if(y[i]-m < m-100.0) uf.unite(n+1,i);
    }
    rep(i,n) rep(j,n) if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) < 4.0*m*m) uf.unite(i,j);
    if(uf.find(n) == uf.find(n+1)) r = m;
    else l = m;
  }
  printf("%.12lf\n",l);
  return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll<<62)-1)
#define cINFINT ((1<<30)-1)

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

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

    ull n = 0;
    string s;
    cin >> n >> s;
    ++n;

    vector<ll> vec( n, 0 );
    REP(i, n) cin >> vec[i];

    ll k = 100000;
    REP(i, n-1) chmin(k, abs(vec[i+1] - vec[i]));

    cout << k << endl;
    REP(kk, k) REP(i, n)
    {
        cout << (vec[i]+kk)/k << ((i==n-1)?"\n":" ");
    }

    // ----------------------------------------------------------------
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, A[N], k = INT_MAX;
int main()
{
    scanf("%d%*s", &n);
    for(int i = 0; i <= n; ++i)
        scanf("%d", &A[i]), k = min(k, i ? abs(A[i] - A[i - 1]) : k);
    printf("%d\n", k);
    for(int i = 1; i <= k; ++i)
        for(int j = 0; j <= n; ++j)
            printf("%d%c", A[j] / k + (A[j] % k >= i), " \n"[j == n]);
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using PLL = pair<pair<long long, long long>, long long>;
using Pxy = pair<long double, long double>;
const int INF = 2001001007;
const int modd = 1000000007;
const long long modl = 1000000007LL;
const long long mod = 998244353LL;
const ll inf = 1e18;
template <typename SA>
void priv(vector<SA> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename SB>
void privv(vector<vector<SB>> &ar){
    rep(i,ar.size()){
        rep(j,ar[i].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[i].size()-1] << endl;
    }
}
template <typename SC>
bool range(SC a, SC b, SC x){return (a <= x && x < b);}
bool rrange(P a, P b, P xy){
    bool s = range(a.first,b.first,xy.first);
    bool t = range(a.second,b.second,xy.second);
    return (s && t);
}
template <typename SD>
void sor(vector<SD> &ar){sort(ar.begin(),ar.end());}
template <typename SE>
void rev(vector<SE> &ar){reverse(ar.begin(),ar.end());}
template <typename SF>
bool chmin(SF &a, const SF &b){if(a>b){a = b; return true;} return false;}
template <typename SG>
bool chmax(SG &a, const SG &b){if(a<b){a = b; return true;} return false;}
template <typename SH>
void eru(vector<SH> &ar){sor(ar);ar.erase(unique(ar.begin(),ar.end()),ar.end());}

void yes(){cout << "Yes" << endl;}
void no (){cout << "No" << endl;}
void yn (bool t){if(t)yes();else no();}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

vector<string> saiki(int n){
    if (n == 1){
        vector<string> ans = {"AB"};
        return ans;
    }
    vector<string> a = saiki(n/2);
    vector<string> ans(2*n-1);
    string c = "";
    rep(i,n) c += 'A';
    rep(i,n) c += 'B';
    ans[0] = c;
    rep(i,n-1){
        string d = a[i];
        d += a[i];
        ans[i+1] = d;
    }
    rep(i,n-1){
        string d = a[i];
        rep(j,n){
            if (d[j] == 'A') d += 'B';
            else d += 'A';
        }
        ans[n+i] = d;
    }
    return ans;
}

int main(){
    int n; cin >> n;
    int m = 1;
    rep(i,n-1) m *= 2;
    auto ans = saiki(m);
    cout << ans.size() << endl;
    rep(i,ans.size()) cout << ans[i] << endl;
}

#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int main(){
    fastIO;
    int n;
    cin >> n;
    vector<vector<int>> sol;
    sol.push_back({0,1});
    for(int i = 2; i <= n; i ++ ){
        vector<vector<int>> nw;
        for(auto x : sol){
            nw.push_back({});
            for(auto r : x){
                nw.back().push_back(r);
            }
            for(auto r : x){
                nw.back().push_back(r);
            }
            nw.push_back({});
            for(auto r : x){
                nw.back().push_back(r);
            }
            for(auto r : x){
                nw.back().push_back((r^1));
            }
        }
        nw.push_back({});
        for(int j = 0 ; j < (1 << i); j ++ ){
            if(j < (1 << (i - 1))){
                nw.back().push_back(0);
            }
            else{
                nw.back().push_back(1);
            }
        }
        sol = nw;
    }
    cout << sol.size() << "\n";
    for(auto p : sol){
        for(auto q : p){
            cout << char(q+'A');
        }
        cout << "\n";
    }
    return 0;
}


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

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){
    int H; cin>>H;
    int W; cin>>W;
    vector<string> S(H); rep(i,H)cin>>S.at(i);

    int ans = 0;

    for(int i=1;i<H;i++){
        for(int j=1; j<W;j++ ){
            if((S.at(i).at(j)=='#'&&S.at(i).at(j-1)=='.')){
                if(!(S.at(i-1).at(j)=='#'&&S.at(i-1).at(j-1)=='.')){
                    ans++;
                }
            }
            if((S.at(i).at(j)=='.'&&S.at(i).at(j-1)=='#')){
                if(!(S.at(i-1).at(j)=='.'&&S.at(i-1).at(j-1)=='#')){
                // if(S.at(i-1).at(j-1)=='#'){
                    ans++;
                }
            }
        }
    }

    for(int j=1; j<W;j++ ){
        for(int i=1;i<H;i++){
            if((S.at(i).at(j)=='#'&&S.at(i-1).at(j)=='.')){
                if(!(S.at(i).at(j-1)=='#'&&S.at(i-1).at(j-1)=='.')){
                    ans++;
                }
            }
            if((S.at(i).at(j)=='.'&&S.at(i-1).at(j)=='#')){
                if(!(S.at(i).at(j-1)=='.'&&S.at(i-1).at(j-1)=='#')){
                    ans++;
                }
            }
        }
    }

    cout<<ans<<endl;

    return 0;
}

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

#define ll long long
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define P pair<ll, ll>

int main(){
    ll K;
    cin >> K;

    string S, T;
    cin >> S >> T;

    vector<ll> card(9);
    vector<ll> a_hand(9), t_hand(9);
    rep(i, 9) card.at(i) = K;
    rep(i, 4){
        card.at(S[i]-'0'-1)--;
        t_hand.at(S[i]-'0'-1)++;
        card.at(T[i]-'0'-1)--;
        a_hand.at(T[i]-'0'-1)++;
    }

    vector<P> conbi;

    for(ll a=1; a<=9; a++){
        if(card.at(a-1) == 0) continue;

        ll a_score = 0;
        card.at(a-1)--;
        a_hand.at(a-1)++;
        rep(i, 9) a_score += (i+1)*pow(10, a_hand.at(i));

        for(ll t=1; t<=9; t++){
            if(card.at(t-1) == 0) continue;

            ll t_score = 0;
            t_hand.at(t-1)++;
            rep(i, 9) t_score += (i+1)*pow(10, t_hand.at(i));

            //cout << "a_score:" << a_score << ", t_score:" << t_score << endl;

            if(a_score < t_score) conbi.push_back(make_pair(a, t));

            t_hand.at(t-1)--;
        }

        a_hand.at(a-1)--;
        card.at(a-1)++;
    }

    double ans = 0;
    for(ll i=0; i<conbi.size(); i++){
        double first_p = (double)card.at(conbi.at(i).first-1)/(9*(double)K-8);
        card.at(conbi.at(i).first-1)--;
        double second_p = (double)card.at(conbi.at(i).second-1)/(9*(double)K-9);
        ans += first_p*second_p;
        card.at(conbi.at(i).first-1)++;
    }

    cout << fixed << setprecision(10) << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<endl
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

vector<string> C(vector<string> s) {
  ll n=s.size();
  rep(i,n)REP(j,i+1,n) swap(s[i][j],s[j][i]);
  return s;
}

ll n,m;
string s[1000];

ll calc(vector<string> t) {
  ll cnt=0;
  rep(k,m) {
    ll f=0;
    rep(l,2) {
      rep(i,n) {
        rep(j,n) {
          if(t[i].substr(0,s[k].size())==s[k]) f=1;
          rotate(t[i].begin(),t[i].begin()+1,t[i].end());
        }
      }
      t=C(t);
    }
    cnt+=f;
  }
  return cnt;
}

vector<string> ans;
ll MM;

void solve(vector<string> t) {
  ll d=calc(t);
  if(MM<d) {
    MM=d;
    ans=t;
  }
  rep(k,5) {
    random_shuffle(all(t));
    rep(i,n) rotate(t[i].begin(),t[i].begin()+rand()%n,t[i].end());
    ll d=calc(t);
    if(MM<d) {
      MM=d;
      ans=t;
    }
  }
}

void Main() {
  srand((unsigned)time(NULL));
  cin >> n >> m;
  rep(i,m) R s[i];
  vector<string> v[15];
  rep(i,m) v[s[i].size()].pb(s[i]);
  rep(e,30) {
    vector<string> t(n);
    rrep(i,15) {
      random_shuffle(all(v[i]));
      rep(j,v[i].size()) {
        ll M=MAX,x=0;
        string rr;
        rep(k,n) {
          rep(l,t[k].size()+1) {
            string r1=t[k].substr(l);
            if(r1.size()<=v[i][j].size()) {
              string r2=v[i][j].substr(0,r1.size());
              if(r1==r2) {
                ll d=t[k].size()+v[i][j].size()-r1.size();
                if(d<=n&&M>d) {
                  M=d;
                  x=k;
                  rr=t[k]+v[i][j].substr(r1.size());
                }
              }
            }
          }
        }
        if(rand()%5==0) M=MAX;
        if(M!=MAX) t[x]=rr;
      }
    }
    rep(i,n) {
      while(t[i].size()<n) {
        //t[i]+='.';
        t[i]+=(char)(rand()%8+'A');
      }
    }
    solve(t);
  }
  rep(i,n) pr(ans[i]);
}

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

vector<int> t;
LL su[220000][2];

int main(){
    int n,m;
    int i,j,k;
    LL a,b,c;
    LL s=1LL<<60;
    cin>>n>>m;
    for(i=0;i<n;i++){
        cin>>a;
        t.push_back(a);
    }
    sort(t.begin(),t.end());
    for(i=0;i<n;i++){
        su[i+1][0]=su[i][0];
        if(i%2)su[i+1][0]+=t[i]-t[i-1];
    }
    for(i=n-1;i>=0;i--){
        su[i][1]=su[i+1][1];
        if(i%2)su[i][1]+=t[i+1]-t[i];
    }
    vector<int> v1;
    for(i=0;i<m;i++){
        cin>>a;
        v1.push_back(a);
    }
    sort(v1.begin(),v1.end());
    for(i=0,j=0;i<m;i++){
        for(;j<n && t[j]<v1[i];j++);
        a=su[j][0]+su[j][1];
        if(j%2)a+=v1[i]-t[j-1];
        else a+=t[j]-v1[i];
        s=min(s,a);
    }
    cout<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
const LL MOD=1e9+7;
LL rec[maxn];

int main()
{
	int len;cin >> len;
	string arr;
	LL ans=1;
	rec[3]=1;rec[4]=2;rec[2]==1;
	for(int i=0;i<4;++i)
	{
		char tem; cin >> tem; arr+=tem;
	}
	if(len<4||arr[0]=='A'&&arr[1]=='A'||arr[1]=='B'&&arr[3]=='B'){cout << 1;return 0;}
	for(int i=5;i<=len;i++)
		rec[i]=(rec[i-1]+rec[i-2])%MOD;
	for(int i=4;i<=len;++i)
		ans=(ans<<1)%MOD;
	if(arr=="BAAB"||arr=="BBBA"||arr=="ABBA"||arr=="BAAA")
	{
		cout <<rec[len]; return 0;
	}
	else
		cout << ans;
	return 0;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll t,i,endi=0,starti=0;
    string s;
    cin>>s;
    string ss;
    for(i=s.size()-1;i>=0;i--)
    {
        ss.push_back(s[i]);
    }
    if(ss==s)cout<<"Yes"<<endl;
    else{
    ss={};
    //if()
    for(i=s.size()-1;i>=0;i--)
    {
        if(s[i]!='0')break;
        else endi++;
    }
    for(i=0;i<s.size();i++)
    {
        if(s[i]!='0')break;
        else starti++;
    }
    string xs;
    if(endi==0)cout<<"No"<<endl;
    else
    {
        if(endi>starti)
        {
            for(i=starti;i!=endi;i++)
            {
              xs.push_back('0');
            }
            xs+=s;
            for(i=xs.size()-1;i>=0;i--)
            {
                ss.push_back(xs[i]);
            }
            if(ss==xs)cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        else cout<<"No"<<endl;

    }

    }
}


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

#pragma GCC optimize("O3")
#ifdef LOCAL
#include "/Users/lbjlc/Desktop/coding/debug_utils.h"
#else
#define print(...) ;
#define printn(...) ;
#define printg(...) ;
#define fprint(...) ;
#define fprintn(...) ;
#endif

#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
// #define mp make_pair
#define fi first
#define se second
#define maxi(x, y) x = max(x, y)
#define mini(x, y) x = min(x, y)
// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }
// #define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
    static uniform_int_distribution<int> dist(0, 1e9 + 6);
    return dist(rng);
}
#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<long long> vll;
#define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val)

template<class T>
void make_unique(T & v) {
    sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() { int x; cin >> x; return x; }
long long getll() { long long x; cin >> x; return x; }
double getd() { double x; cin >> x; return x; }
// pair<int, int> a(geti(), geti()) does not work
// pair<int, int> a({geti(), geti()}) works, since it uses initializer.

const int MAXN = 3e5 + 100;

void solve(int tt) {
    // cout<<"Case #"<<tt<<": ";
}

int main(int argc, char * argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // solve_testcase;

    int n;
    cin>>n;
    string s,t;
    cin>>s;
    cin>>t;

    int cs=0,ct=0;
    for(auto c:s) cs+=(c=='1');
    for(auto c:t) ct+=(c=='1');

    print(cs,ct);

    if(cs<ct||(cs-ct)%2) {
    	cout<<-1<<endl;
    	return 0;
    }

    ll res=0;
    int i=0,j=0;

    while(1) {
	    for(;i<n&&s[i]=='0';i++);
	    for(;j<n&&t[j]=='0';j++);
    	print(i,j);
	    if(i==n&&j==n) break;
		if(i==n) {
			cout<<-1<<endl;
			return 0;
		}

    	if(i>=j) {
    		res+=i-j;
    		i++;j++;
    	}
    	else {
    		int i1=i+1;
		    for(;i1<n&&s[i1]=='0';i1++);
		    if(i1==n) {
		    	cout<<-1<<endl;
		    	return 0;
		    }
		    res+=i1-i;
			i=i1+1;
    	}
    }
    cout<<res<<endl;

    return 0;
}

#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
#include <queue>
#include <string>
#include <map>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <functional>
#include <numeric>
const int MAXN = 500010;
char str_s[MAXN];
char str_t[MAXN];
std::vector<int> vec_index_s;
std::vector<int> vec_index_t;
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int t;
	int n, m, k, x;
	//scanf(" %d", &t);
	//while (t--)
	{
		scanf("%d", &n);
		scanf(" %s", str_s);
		scanf(" %s", str_t);
		int cnt1 = 0;
		int cnt2 = 0;
		for (int i = 0; i < n; i++) {
			if (str_s[i] == '1') {
				cnt1++;
				vec_index_s.push_back(i);
			}
		}
		for (int i = 0; i < n; i++) {
			if (str_t[i] == '1') {
				cnt2++;
				vec_index_t.push_back(i);
			}
		}
		if (cnt2 > cnt1 || (cnt1 - cnt2) % 2) {
			printf("-1\n");
			return 0;
		}
		int index_now_s = 0;
		int index_now_t = 0;
		bool tag = true;
		long long res = 0;
		while (index_now_s < vec_index_s.size()) {
			if (index_now_t < vec_index_t.size() && vec_index_s[index_now_s] < vec_index_t[index_now_t]) {
				index_now_s++;
				if (index_now_s >= vec_index_s.size()) {
					break;
				}
				res += (vec_index_s[index_now_s] - vec_index_s[index_now_s - 1]);
				index_now_s++;
				if (index_now_s >= vec_index_s.size()) {
					break;
				}
			}
			else {
				if (index_now_t < vec_index_t.size()) {
					res += (vec_index_s[index_now_s] - vec_index_t[index_now_t]);
					index_now_s++;
					index_now_t++;
					if (index_now_s >= vec_index_s.size()) {
						break;
					}
				}
				else {
					index_now_s++;
					if (index_now_s >= vec_index_s.size()) {
						tag = false;
						break;
					}
					res += (vec_index_s[index_now_s] - vec_index_s[index_now_s - 1]);
					index_now_s++;
					if (index_now_s >= vec_index_s.size()) {
						break;
					}
				}
			}
		}
		if ((index_now_s >= vec_index_s.size() && index_now_t < vec_index_t.size()) || !tag) {
			printf("-1\n");
			return 0;
		}
		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using P = pair<i64, i64>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
constexpr int INF = 1001001001;

constexpr bool is_multiple = false;

using State = tuple<P, P, P>;

void flip_x(P &pos, i64 p) {
    i64 dx = abs(pos.first - p);

    if (pos.first > p) {
        pos.first -= 2 * dx;
    }
    else {
        pos.first += 2 * dx;
    }
}

void flip_y(P &pos, i64 p) {
    i64 dy = abs(pos.second - p);

    if (pos.second > p) {
        pos.second -= 2 * dy;
    }
    else {
        pos.second += 2 * dy;
    }
}

void print_state(State s) {
    P e0 = get<0>(s);
    P ex = get<1>(s);
    P ey = get<2>(s);

    printf("Origin: %lld %lld\n", e0.first, e0.second);
    printf("Ex: %lld %lld\n", ex.first, ex.second);
    printf("Ey: %lld %lld\n", ey.first, ey.second);
}

void solve() {
    int n;
    cin >> n;
    vector<i64> x(n), y(n);
    rep(i, n) cin >> x[i] >> y[i];
    int m;
    cin >> m;
    // (0, 0), (1, 0), (0, 1)
    vector<State> states(m+1);
    states[0] = {{0, 0}, {1, 0}, {0, 1}};
    rep(i, m) {
        int t, p = -1;
        cin >> t;

        State old = states[i];
        P e0 = get<0>(old);
        P ex = get<1>(old);
        P ey = get<2>(old);

        if (t == 1) {
            e0 = {e0.second, -e0.first};
            ex = {ex.second, -ex.first};
            ey = {ey.second, -ey.first};
        }
        else if (t == 2) {
            e0 = {-e0.second, e0.first};
            ex = {-ex.second, ex.first};
            ey = {-ey.second, ey.first};
        }
        else {
            cin >> p;

            if (t == 3) {
                flip_x(e0, p);
                flip_x(ex, p);
                flip_x(ey, p);
            }
            else {
                flip_y(e0, p);
                flip_y(ex, p);
                flip_y(ey, p);
            }
        }

        states[i+1] = {e0, ex, ey};
        // print_state(states[i+1]);
    }
    int q;
    cin >> q;
    vec a(q), b(q);
    rep(i, q) {
        cin >> a[i] >> b[i];
        b[i]--;

        i64 sx = x[b[i]];
        i64 sy = y[b[i]];
        State state = states[a[i]];
        P e0 = get<0>(state);
        P ex = get<1>(state);
        P ey = get<2>(state);

        i64 gx, gy;

        gx = e0.first + (ex.first - e0.first) * sx + (ey.first - e0.first) * sy;
        gy = e0.second + (ex.second - e0.second) * sx + (ey.second - e0.second) * sy;

        printf("%lld %lld\n", gx, gy);
    }
}

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

#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
const int N = 2e5 + 50;

struct Point{
    int x, y;
}p[N];

int n, m, Q;
int r[N], x[N], y[N], nx[N], ny[N];

signed main(){
    scanf("%lld", &n);
    for(int i = 1; i <= n; ++i)  scanf("%lld%lld", &p[i].x, &p[i].y);
    scanf("%lld", &m);
    for(int i = 1, opt; i <= m; ++i){
        scanf("%lld", &opt);
        r[i] = r[i - 1], x[i] = x[i - 1], y[i] = y[i - 1];
        nx[i] = nx[i - 1], ny[i] = ny[i - 1];
        if(opt == 1){
        	x[i] *= -1;
        	swap(x[i], y[i]);
        	if(nx[i] + ny[i] == 1)  ++r[i];
        	else  --r[i];
		}
        else if(opt == 2){
        	y[i] *= -1;
        	swap(x[i], y[i]);
        	if(nx[i] + ny[i] == 1)  --r[i];
        	else  ++r[i];
		}
        else{
            int p;
            scanf("%lld", &p);
            if(opt == 3)  x[i] = 2 * p - x[i],  nx[i] ^= 1;
            else  y[i] = 2 * p - y[i],  ny[i] ^= 1;
        }
    }
    scanf("%lld", &Q);
    while(Q--){
        int A, B;
        scanf("%lld%lld", &A, &B);
        int dx = p[B].x;
        int dy = p[B].y;
        r[A] = (r[A] % 4 + 4) % 4;
        if(r[A] == 1)  dy *= -1,  swap(dx, dy);
        else if(r[A] == 2)  dx *= -1,  dy *= -1;
        else if(r[A] == 3)  dx *= -1,  swap(dx, dy);
        if(nx[A])  dx *= -1;
        if(ny[A])  dy *= -1;
        printf("%lld %lld\n", dx + x[A], dy + y[A]);
    }
    return 0;
}


#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <utility>
#include <climits>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

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

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

#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl

int main(int argc, char const *argv[])
{
    int x,y,a,b,n,m,k,j;

    cin>>n>>m;

    vi arr(n);
    vi buf(n,0);
    set<int> s;
    for (int i = 0; i <= n; i++)
    {
        s.insert(i);
    }
    int res=n;

    int i=0;
    for (; i < m; i++)
    {
        cin>>a;
        arr[i]=a;

        buf[a]++;
        s.erase(a);
    }
    res=min(res,*s.begin());
    for ( ; i < n; i++)
    {
        cin>>a;
        arr[i]=a;

        s.erase(a);
        buf[a]++;

        buf[arr[i-m]]--;
        if(buf[arr[i-m]]==0){
            s.insert(arr[i-m]);
        }
        res=min(res,*s.begin());
    }
    cout<<res<<endl;
    return 0;
}

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

int main() {
    int n, m;
    cin >> n >> m;
    vector <int> a(n);
    vector <vector <int>> pos(n);

    rep (i, n) {
        cin >> a[i];
        pos[a[i]].push_back(i);
    }

    int ans = 0;
    rep (i, n) {
        if (pos[i].size() == 0) break;
        rep (j, pos[i].size()) {
            int tmp = j == 0 ? pos[i][j] - (-1)
                             : pos[i][j] - pos[i][j - 1];

            if (tmp > m) {
                cout << i << endl;
                return 0;
            }
        }
        if (n - pos[i].back() > m) break;
        ans += 1;
    }
    cout << ans << endl;

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int x1,x2,x3;
    cin>>x1>>x2>>x3;
    if(x1== x2)
    {
        cout<<x3;
    }
    else if(x1==x3)
    {
        cout<<x2;
    }
    else if(x2==x3)
    {
        cout<<x1;
    }
    else
    {
        cout<< 0;
    }

}

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

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

int main()
{
	int a=read(),b=read(),c=read();
	int sum=a+b+c;
	if(a==b||a==c)
	{
		printf("%d\n",sum-2*a);
		return 0;
	}
	if(b==c)
	{
		printf("%d\n",sum-2*b);
		return 0;
	}
	cout<<0<<endl;
	return 0;
}


#include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef unsigned long long ll;
typedef pair<ll,ll> P;
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; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<30;
int dx[]={0,1,0,-1,0,1,-1,1,-1};
int dy[]={0,0,1,0,-1,1,-1,-1,1};

using  E = tuple<int, int, int>;

int main(){
    int h,w;cin >> h >> w;
    vector<vector<int>> a(h, vector<int> (w - 1));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w - 1; j++) {
            cin >> a[i][j];
        }
    }
    vector<vector<int>> b(h - 1, vector<int> (w));
    for (int i = 0; i < h-1; i++) {
        for (int j = 0; j < w; j++) {
            cin >> b[i][j];
        }
    }
    priority_queue<E, vector<E>, greater<E>> pq;
    pq.push({0, 0, 0});
    vector<vector<int>> dp(h + 1, vector<int> (w + 1, INF));
    dp[0][0] = 0;
    while(not pq.empty()){
        auto [c, y, x] = pq.top();pq.pop();
        if(dp[y][x] < c) continue;
        if(x + 1 < w and chmin(dp[y][x + 1], c + a[y][x])){
            pq.push({dp[y][x + 1], y, x + 1});
        }
        if(x - 1 >= 0 and chmin(dp[y][x - 1], c + a[y][x - 1])){
            pq.push({dp[y][x - 1], y, x - 1});
        }
        if(y + 1 < h and chmin(dp[y + 1][x], c + b[y][x])){
            pq.push({dp[y + 1][x], y + 1, x});
        }
        for (int i = 1; i <= y; i++) {
            if(chmin(dp[y - i][x], c + i + 1)){
                pq.push({dp[y - i][x], y - i, x});
            }
        }
    }
    cout << dp[h - 1][w - 1] << endl;
    return 0;
}

// E - 潜入
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
using PR = pair<int,int>;
#define rep(i,n) for(int i=0;i<(int)(n);++i)

int V; // 頂点数
vec<vec<PR>> G; // [from]<to, cost> 隣接リスト
int INF = 1e9;

vi dijkstra(int s){
	vi dist(V, INF);
	dist[s] = 0;
	priority_queue<PR, vec<PR>, greater<PR>> pq;
	pq.push({0, s});
	while(pq.size()){
		auto[from_d, from_v] = pq.top(); pq.pop();
		if(dist[from_v] < from_d) continue;
		for(auto[to_u, to_d]:G[from_v]){
			to_d += dist[from_v];
			if(dist[to_u] <= to_d) continue;
			dist[to_u] = to_d;
			pq.push({to_d, to_u});
		}
	}
	return dist;
}

int main(){
	int R, C; cin>>R>>C;

	auto rc2id = [&](int r, int c){ return r*C + c; };

	V = R*C*2;
	G.resize(V);

	int x, a, b;
	rep(r, R) rep(c, C-1){
		cin>>x;
		a = rc2id(r, c), b = rc2id(r, c+1);
		G[a].emplace_back(b, x), G[b].emplace_back(a, x);
	}

	rep(r, R-1) rep(c, C){
		cin>>x;
		G[rc2id(r, c)].emplace_back(rc2id(r+1, c), x);
	}

	rep(r, R) rep(c, C){
		a = rc2id(r, c), b = rc2id(r, c+R*C);
		G[a].emplace_back(b, 1), G[b].emplace_back(a, 0);
		if(r != R-1) G[rc2id(r+1, c+R*C)].emplace_back(rc2id(r, c+R*C), 1);
	}

	cout<< dijkstra(0)[rc2id(R-1,C-1)] <<endl;
}


#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define bits(x,i) (((x) >> (i)) & 1)
#define mid ((l+r)/2)
#define pr pair<int,int>
using namespace std;
int a[3];
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> a[0] >> a[1] >> a[2];
	sort(a,a+3);
	cout << a[1] + a[2];
}

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

int main() {
	int A, B, C;
	cin >> A >> B >> C;
	cout << A+B+C - min(A,min(B,C)) << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int h,w;
    cin>>h>>w;
    char c;
    int in[h][w];
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>c;
            if(c=='.')in[i][j]=0;
            else in[i][j]=1;
        }
    }
        int ans=0;
        for(int i=1;i<h;i++)
        {
            for(int j=0;j<w-1;j++)
            {
               int sum=in[i-1][j]+in[i-1][j+1]+in[i][j]+in[i][j+1];
               if(sum==1 || sum==3)ans+=1;
            }
        }
    cout<<ans;
    return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rep1(i,n) for(int i = 1; i <= (n); ++i)
// #define MOD 998'244'353
// #define MOD 1'000'000'007
// #define INF 1'000'000'000'000'000'000
using namespace std;
// using namespace atcoder;
typedef long long ll;

int main(){
    int N;
    cin >> N;
    int a[N+1];
    int b[N+1];
    int c[N+1];
    rep1(i, N) cin >> a[i];
    rep1(i, N) cin >> b[i];
    rep1(i, N) cin >> c[i];
    a[0] = 0;
    b[0] = 0;
    c[0] = 0;

    ll ans = 0;

    map<int, ll> mpa;
    map<int, ll> mpbc;

    rep1(i, N){
        mpa[a[i]]++;
    }
    rep1(j, N){
        mpbc[b[c[j]]]++;
    }

    rep1(i, N){
        ans += mpa[i] * mpbc[i];
    }

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/

int main() {
	int n,m;
	std::cin >> n >> m;
	V<lnt> w(n);
	fill(w);
	std::map<lnt,lnt> map;
	lnt minload=1e18;
	rep(i,m) {
		lnt a,b;
		std::cin >> a >> b;
		map[b]=std::max(map[b],a);
		minload=std::min(minload,b);
	}
	rep(i,n) {
		if(w[i]>minload) {
			print(-1);
			return 0;
		}
	}
	V<std::pair<lnt,lnt> > v;
	v.emplace_back(0,0);
	for(auto e:map) v.emplace_back(e);
	{
		lnt max=0;
		int k=v.size();
		rep(i,k) {
			max=std::max(max,v[i].second);
			v[i].second=max;
		}
	}
	V<int> p(n);
	std::iota(all(p),0);
	lnt min=1e18;
	std::sort(all(w));
	do {
		V<V<std::pair<lnt,lnt> > > g(n);
		V<lnt> a(n);
		rep(i,n) a[i]=w[p[i]];
		V<lnt> ac(n+1,0);
		rep(i,n) ac[i+1]=ac[i]+a[i];
		rep(i,n) {
			for(int j=i+1;j<n;j++) {
				lnt x=ac[j+1]-ac[i];
				int l=-1,r=v.size();
				while(r-l>1) {
					int o=(l+r)>>1;
					if(v[o].first<x) l=o;
					else r=o;
				}
				g[i].emplace_back(j,v[l].second);
			}
		}
		auto dfs=[&](auto f, int i) -> lnt {
			lnt max=0;
			for(auto to:g[i]) {
				max=std::max(max,f(f,to.first)+to.second);
			}
			return max;
		};
		lnt x=dfs(dfs,0);
		min=std::min(min,x);
	} while(std::next_permutation(all(p)));
	print(min);
}

#include <bits/stdc++.h>
using namespace std;
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; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }

long long solve() {
    int N, M;
    cin >> N >> M;
    vector<long long> w(N);
    long long maxW = 0;
    for (int i = 0; i < N; ++i) {
        cin >> w[i];
        chmax(maxW, w[i]);
    }
    vector<long long> l(M), v(M);
    for (int j = 0; j < M; ++j) {
        cin >> l[j] >> v[j];
        if (v[j] < maxW) return -1;
    }

    // 前処理
    vector<long long> dist(1<<N, 0);
    for (int bit = 0; bit < (1<<N); ++bit) {
        long long W = 0;
        for (int i = 0; i < N; ++i) {
            if (bit & (1<<i)) W += w[i];
        }
        for (int j = 0; j < M; ++j) {
            if (v[j] < W) {
                chmax(dist[bit], l[j]);
            }
        }
    }

    // DP
    long long res = 1LL<<60;
    vector<int> ids(N);
    iota(ids.begin(), ids.end(), 0);
    do {
        //COUT(ids);
        vector<long long> dp(N, 0);
        for (int i = 1; i < N; ++i) {
            int bit = (1<<ids[i]);
            for (int j = i-1; j >= 0; --j) {
                bit |= (1<<ids[j]);
                chmax(dp[i], dp[j] + dist[bit]);
            }
            //cout << i << ": " << ids[i] << ", " << dp[i] << endl;
        }
        chmin(res, dp[N-1]);
    } while (next_permutation(ids.begin(), ids.end()));
    return res;
}

int main() {
    cout << solve() << endl;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define inrange(a, x, b) (a <= x && x <= b)
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using vc = vector<char>;
using vcvc = vector<vc>;
using pll = pair<ll, ll>;
// using mint = atcoder::static_modint<1'000'000'007>; // atcoder::static_modint<998'244'353>;
template<class T> void print(T x,bool cl=1){cout<<(cl?"\x1b[36m":"")<<x<<(cl?"\x1b[39m":"")<<'\n';}
template<class T> void print1d(T x,ll n=-1,bool cl=true){if(n==-1)n=x.size();rep(i,0,n){cout<<(cl?"\x1b[36m":"")<<x[i]<<(i==n-1?'\n':' ');}cout<<(cl?"\x1b[39m":"");}
template<class T> void print2d(T x,ll r=-1,ll c=-1,bool cl=1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c,cl);}
template<class T, class U> bool isin(T el, U container) { return find(all(container), el) != container.end(); }
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 T> bool even(T n) { return ! (n & 1); }
template<class T> bool odd(T n) { return n & 1; }
template<class T> T rup(T a, T b) { return a / b + !!(a % b); }
template<class Arr, class T, size_t Sz> void myfill(Arr (&a)[Sz], const T &x) { fill((T*)a, (T*)(a + Sz), x); }
template<class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;
ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; }
ll modc(ll a, char op, ll b, ll _mod=mod)
{
        a %= _mod; b %= _mod; ll res = 1;
        switch (op) {
        case '+': res = (a + b) % _mod; break;
        case '-': res = (a - b) % _mod; break;
        case '*': res = a * b % _mod; break;
        case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break;
        case '^': res = intpow(a, b, _mod); break;
        case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break;
        case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break;
        }
        if (res < 0) { res += _mod; } return res;
}

int main()
{
        ll N;
        cin >> N;
        vll A(N);
        rep(i, 0, N) cin >> A[i];

        sort(rall(A));

        vll acc(N + 1);
        rep(i, 0, N) acc[i + 1] = acc[i] + A[i];

        ll ans = 0;
        rep(i, 0, N) {
                ll tmp = acc[N] - acc[i + 1];
                ans += A[i] * (N - 1 - i) - tmp;
        }

        cout << ans << endl;
}


#include<bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2")
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define req(i,a,b) for(int i=(a);i>=(b);--i)
#define rep_(i,a,b) for(int i=(a);i<(b).size();++i)
#define F(a) rep(a,1,n)
#define M(a,b) memset(a,b,sizeof a)
#define DC int T;cin>>T;while(T--)
#define ll long long
#define Z(a,b) sort(a+1,a+b+1)
using namespace std;
const unsigned _mod=998244353;
const unsigned mod=1e9+7;
const ll infi=0x3f3f3f3f3f3f3fll;
const int inf=0x3f3f3f3f;
void rd(auto &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;}
ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;}
ll qpow(ll x,ll y){ll ret=1;while(y){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;}
/*
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
    [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
*/
int h,w,fa[2010],s,t;
char c;
unordered_map<int,bool> mp;
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
    cin>>h>>w;
    rep(i,1,h+w) fa[i]=i;
    merge(1,h+1),merge(1,h+w),merge(h,h+1),merge(h,h+w);
    rep(i,1,h) rep(j,1,w) cin>>c,c=='#'&&(merge(i,h+j),1);
    rep(i,1,h) s+=!mp[find(i)],mp[find(i)]=1;
	mp.clear();
	rep(i,h+1,h+w) t+=!mp[find(i)],mp[find(i)]=1;
	cout<<min(s,t)-1<<'\n';
    return 0;
}

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 1010
#define MOD 1000000007  //998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
	Setup_io() {
		ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
		cout << fixed << setprecision(15);
	}
} setup_io;

char a[N][N];
vector<int>e[2*N];
bool v[2 * N];
bool used[2 * N];
int ansa, ansb;

void dfs(int k) {
	if (used[k])return;
	used[k] = true;
	int sz = e[k].size();
	rep(i, sz) {
		if (!used[e[k][i]])dfs(e[k][i]);
	}
	return;
}

int main() {
	rep(i, N)rep(j, N)a[i][j] = 0;
	rep(i, 2 * N) {
		v[i] = false;
	}
	int n, m;
	cin >> n >> m;
	rep(i, n)cin >> a[i];
	rep2(i, 1, m - 2) {
		if (a[0][i] == '#')v[N + i - 1] = true;
		if (a[n - 1][i] == '#')v[N + i - 1] = true;
	}
	rep2(i, 1, n - 2) {
		if (a[i][0] == '#')v[i - 1] = true;
		if (a[i][m - 1] == '#')v[i - 1] = true;
		rep2(j, 1, m - 2) {
			if (a[i][j] == '#') {
				e[i - 1].pb(N + j - 1);
				e[N + j - 1].pb(i - 1);
			}
		}
	}
	rep(i, n - 2) {
		used[i] = false;
	}
	rep(i, m - 2) {
		used[N + i] = false;
	}
	rep(i, n - 2) {
		if (v[i]) {
			if (!used[i])dfs(i);
		}
	}
	rep(i, m - 2) {
		if (v[N + i]) {
			if (!used[N + i])dfs(N + i);
		}
	}
	ansa = 0;
	rep(i, n - 2) {
		if (!used[i]) {
			dfs(i);
			ansa++;
		}
	}
	rep(i, n - 2) {
		used[i] = false;
	}
	rep(i, m - 2) {
		used[N + i] = false;
	}
	rep(i, n - 2) {
		if (v[i]) {
			if (!used[i])dfs(i);
		}
	}
	rep(i, m - 2) {
		if (v[N + i]) {
			if (!used[N + i])dfs(N + i);
		}
	}
	ansb = 0;
	rep(i, m - 2) {
		if (!used[N + i]) {
			dfs(N + i);
			ansb++;
		}
	}
	n = min(ansa, ansb);
	cout << n << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i=0;i<n;i++)
#define INF 1000000001
#define lINF 1000000000000001
using ll = long long;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
//1e9<int型の最大値<1e10
//1e18<long型の最大値<1e19
//配列直入れ↓
//g++ -std=c++17 -Wshadow -Wall -D_GLIBCXX_DEBUG -o a b.cpp && ./a

int main(){
    int h,w,x,y;
    cin>>h>>w>>x>>y;
    x--;
    y--;
    vector<string> s(h);
    rep(i,h) cin>>s[i];
    int ans=0;
    for(int i=x;i<h;i++){
        if(s[i][y]=='.') ans++;
        else break;
    }
    for(int i=x;i>=0;i--){
        if(s[i][y]=='.') ans++;
        else break;
    }
    for(int j=y;j<w;j++){
        if(s[x][j]=='.') ans++;
        else break;
    }
    for(int j=y;j>=0;j--){
        if(s[x][j]=='.') ans++;
        else break;
    }
    cout<<ans-3<<endl;

}

#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i,n) rep(i,1,n)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

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

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

template<typename T>
struct BIT {
	T* c; int n; // require you to define 0 as the initial value !!
	BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); }
	void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; }
	T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;}
};

int n, a[N], vis[N], lst[N], now;
char s[N][N];

void dfs(int x) {
	if(lst[x] == now) return;
	lst[x]=now; vis[x]++;
	FOR(y,n) if(s[x][y] == '1') dfs(y);
}

void solve() {
	qr(n);
	FOR(i,n) scanf("%s",s[i]+1);
	for(now=1;now<=n;now++) dfs(now);
	db ans=0;
	FOR(i,n) ans += 1.0/vis[i];
	printf("%.10lf\n",ans);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define size_t unsigned long long
#define ll long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repr(i, a) for (int i = (int)(a)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using namespace std;
int si()
{
  int x;
  scanf("%d", &x);
  return x;
}
long long sl()
{
  long long x;
  scanf("%lld", &x);
  return x;
}
string ss()
{
  string x;
  cin >> x;
  return x;
}
void pi(int x) { printf("%d ", x); }
void pl(long long x) { printf("%lld ", x); }
void pd(double x) { printf("%.15f ", x); }
void ps(const string &s) { printf("%s ", s.c_str()); }
void br() { putchar('\n'); }

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;
const double PI = 3.14159265358979323846;

struct Node
{
  ll val;
  Node *left;
  Node *right;
};

typedef pair<int, int> P;

const ll N = 20;
const ll M = 400;
int n;
unordered_map<int, bitset<10>> m;

void printResult(const bitset<10> &x, const bitset<10> &y)
{
  cout << "Yes\n"
       << x.count();
  rep(i, x.size())
  {
    if (x.test(i))
      cout << " " << i + 1;
  }
  cout << "\n"
       << y.count();
  rep(i, y.size())
  {
    if (y.test(i))
      cout << " " << i + 1;
  }
  cout << endl;
  return;
}

int main()
{
  cin >> n;
  vector<ll> a(n);
  int count = min(n, 9);
  rep(i, count)
  {
    cin >> a[i];
    a[i] %= 200;
    bitset<10> bs(0);
    bs.set(i);
    if (!m.emplace(a[i], bs).second)
    {
      printResult(m[a[i]], bs);
      return 0;
    }
  }
  for (int i = 0; i < (1 << count); i++)
  {
    bitset<10> bs(i);
    if (bs.count() < 2)
      continue;
    int sum = 0;
    rep(j, count)
    {
      if (bs.test(j))
      {
        sum += a[j];
      }
    }
    sum %= 200;
    if (!m.emplace(sum, bs).second)
    {
      printResult(m[sum], bs);
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>>S(H, vector<char>(W));
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++)cin >> S.at(i).at(j);
  }

  vector<vector<int>>A(H, vector<int>(W));
  A.at(0).at(0)=1;

  vector<vector<int>>tate(H, vector<int>(W));
  vector<vector<int>>yoko(H, vector<int>(W));
  vector<vector<int>>naname(H, vector<int>(W));

  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      if(i>0){
        A.at(i).at(j)+=tate.at(i-1).at(j);
        tate.at(i).at(j)+=tate.at(i-1).at(j);
        A.at(i).at(j)%=1000000007;
      }
      if(j>0){
        A.at(i).at(j)+=yoko.at(i).at(j-1);
        yoko.at(i).at(j)+=yoko.at(i).at(j-1);
        A.at(i).at(j)%=1000000007;
      }
      if(i>0&&j>0){
        A.at(i).at(j)+=naname.at(i-1).at(j-1);
        naname.at(i).at(j)+=naname.at(i-1).at(j-1);
        A.at(i).at(j)%=1000000007;
      }
      tate.at(i).at(j)+=A.at(i).at(j);
      yoko.at(i).at(j)+=A.at(i).at(j);
      naname.at(i).at(j)+=A.at(i).at(j);

      tate.at(i).at(j)%=1000000007;
      yoko.at(i).at(j)%=1000000007;
      naname.at(i).at(j)%=1000000007;

      if(S.at(i).at(j)=='#'){
        A.at(i).at(j)=0;
        tate.at(i).at(j)=0;
        yoko.at(i).at(j)=0;
        naname.at(i).at(j)=0;
      }
    }
  }

  cout << A.at(H-1).at(W-1) << endl;
}


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

long long MOD=998244353;

long long MODpow(long long a,int b){
	if(b==0)return 1;
	if(b%2==0){
		long long t=MODpow(a,b/2);
		return (t*t)%MOD;
	}
	return (a*MODpow(a,b-1))%MOD;
}

int main(){
	int N,M,K;
	cin >> N >> M >> K;
	vector<long long> A(K+1,0);
	vector<long long> B(K+1,0);

	for(int i=1;i<=K;i++)A[i]=MODpow(i,N);
	for(int i=1;i<=K;i++)B[i]=MODpow(i,M);

	long long ans=0;
	for(int i=1;i<=K;i++){
		int j=K-i+1;
		if(M==1)j=1;
		long long add=(A[i]-A[i-1]+MOD)*B[j];
		if(N==1)add=(A[i]-A[i-1])*(B[j]-B[j-1]);
		add%=MOD;
		ans+=add;
		ans%=MOD;
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>

#define TRACE(x) cerr << #x << " = " << x << endl

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

#define SZ(x) (int)(x).size()

using namespace std;

typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef vector<int> VI;

template<class Num>
Num mabs(Num A){
  if(A < 0) return -A;
  return A;
}

const int mod = 1e9 + 7;

inline int add(int a, int b){
  int ret = a + b;
  if(ret >= mod) ret -= mod;
  return ret;
}

inline int mult(int a, int b){
  return (int)((ll)a * b % mod);
}

int digni(int x, int y){
  int ret = 1;
  while(y){
    if(y % 2 == 1)
      ret = mult(ret, x);
    x = mult(x, x);
    y /= 2;
  }
  return ret;
}

int inverse(int x){
  return digni(x, mod - 2);
}

inline int divi(int a, int b){
  return mult(a, inverse(b));
}

inline int sub(int a, int b){
  int ret = a - b;
  if(ret < 0) ret += mod;
  return ret;
}

struct Matrix{
  int n, m;
  vector<VI> mat;
  Matrix(){}
  Matrix(int _n, int _m){
    n = _n; m = _m;
    mat.resize(n + 1, VI(m + 1, 0));
  }

  int& operator()(int x, int y){
    return mat[x][y];
  }

  int operator()(int x, int y) const{
    return mat[x][y];
  }

  Matrix operator*(const Matrix &A){
    Matrix ret(n, A.m);
    FOR(i, 1, n + 1){
      FOR(j, 1, A.m + 1){
        FOR(k, 1, m + 1){
          ret(i, j) = add(ret(i, j), mult(mat[i][k], A(k, j)));
        }
      }
    }

    return ret;
  }
};

Matrix matrix_exp(Matrix &A, int y){
  //mora biti kvadratna
  int n = A.n;
  assert(A.n == A.m);
  Matrix ret(n, n);
  FOR(i, 1, n + 1) ret(i, i) = 1;
  while(y){
    if(y % 2 == 1)
      ret = ret * A;
    A = A * A;
    y /= 2;
  }
  return ret;
}

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

  int n, m, k;
  cin >> n >> m >> k;
  vector<int> a(n + 1);
  FOR(i, 1, n + 1){
    cin >> a[i];
  }

  vector<int> E[n + 1];
  REP(i, m){
    int u, v; cin >> u >> v;
    E[u].pb(v);
    E[v].pb(u);
  }

  Matrix poc(1, n);
  Matrix prijelaz(n, n);
  const int pola = divi(1, 2);

  FOR(i, 1, n + 1){
    poc(1, i) = a[i];
    int deg = SZ(E[i]);
    int prob = divi(deg, m);
    prijelaz(i, i) = sub(1, mult(pola, prob));

    for(int nei : E[i]){
      prijelaz(i, nei) = divi(1, mult(2, m));
    }

  }

  prijelaz = matrix_exp(prijelaz, k);
  Matrix sol = poc * prijelaz;

  FOR(i, 1, n + 1){
    cout << sol(1, i) << '\n';
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
#define vi std::vector<ll>
#define si set<ll>
#define pb push_back

vi v[2020];
bool visited[2020];
ll ans;

void dfs(ll s){
    if(visited[s])return;
    visited[s] = true;
    ans++;
    for (auto u: v[s]){
        dfs(u);
    }
    return;
}

ll solve(){
    for (int i = 0; i < 2020; ++i){
        visited[i] = false;
        v[i].clear();
    }

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

    for (int i = 0; i < m; ++i){
        ll a, b;
        cin >> a >> b;
        v[a].pb(b);
    }
    ll totans = 0;

    for (ll i = 1; i <= n; ++i){
        memset(visited, false, 2020);
        ans = 0;
        dfs(i);
        totans += ans;
    }
    cout << totans;
    return 0;
}

int main(){
    fastio;
    ll t= 1;
   // cin >> t;
    for (int i = 0; i < t; ++i){
       // cout << "Case #" << i+1 << ": ";
        solve();
        cout << "\n";
    }
    return 0;
}

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

vector<vector<bool>> ok;

void dfs(map<int, set<int>> &path, int i, int p)
{
    if (ok[p][i])
        return;
    ok[p][i] = true;
    for (auto v : path[i])
        dfs(path, v, p);
}

int main()
{
    int N, M;
    cin >> N >> M;
    map<int, set<int>> path;
    ok.resize(N);
    rep(i, 0, N) ok[i].resize(N);
    rep(i, 0, M)
    {
        int A, B;
        cin >> A >> B;
        path[A - 1].insert(B - 1);
    }
    ll ans = 0;
    rep(i, 0, N)
    {
        dfs(path, i, i);
        ans += count_if(ok[i].begin(), ok[i].end(), [](bool f)
                        { return f; });
    }
    cout << ans << endl;
}


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

#define int long long
#define show(v)                             \
    for (int i = 0; i < (int)v.size(); i++) \
        cout << v[i] << (i == (int)v.size() - 1 ? "\n" : " ");
#define pb push_back
#define INF 1e18 + 5
const int MOD = 1e9 + 7;
const int N = 3e5 + 5;
vector<int> adj[N];
vector<bool> vis(N), take(N);
void test_case()
{
    char a[3];
    cin >> a[0] >> a[1] >> a[2];
    if (a[0] == a[1] && a[1] == a[2])
        cout << "Won";
    else
        cout << "Lost";
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    test_case();
    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
#include <iomanip>
#include <set>
#include <cmath>
#include <bitset>
#include <queue>
#include <climits>
using namespace std;

int main(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int right = c * d - b;

    if (right <=0){
        cout << -1 << endl;
    }
    else{
        float thresh = double(a) / right;
        int ans = ceil(thresh);
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e, p) for (ll i = (ll)s; i < (ll)e; i += p)
#define ALL(vec) vec.begin(), vec.end()

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

    ll l = pow(10, 9), s = -pow(10, 9), a, t, m = 0;
    rep(i, 0, n, 1){
        cin >> a >> t;
        if (t == 1){
            m += a;
            s += a;
            l += a;
        }else if (t == 2){
            if (l < a) l = a;
            s = max(a, s);
        }else{
            if (a < s) s = a;
            l = min(l, a);
        }
    }

    ll q, x;
    cin >> q;
    rep (i, 0, q, 1){
        cin >> x;
        if (x + m <= s) cout << s << endl;
        else if (l <= x + m) cout << l << endl;
        else cout << x + m << endl;
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);

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

	int n,q;
	cin>>n;
	ll l=-1e18,r=1e18,p=0;
	for(int i=0;i<n;i++){
		ll a,t;
		cin>>a>>t;
		if(t==1)l+=a,r+=a,p+=a;
		if(t==2)chmax(r,a),chmax(l,a);
		if(t==3)chmin(l,a),chmin(r,a);
	}
	cin>>q;
	for(int i=0;i<q;i++){
		ll x;
		cin>>x;
		x += p;
		chmax(x,l);
		chmin(x,r);
		cout << x << "\n";
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N; cin >> N;
  vector<array<i64, 5>> A(N);
  REP(0, N, i) {
    REP(0, 5, j) {
      RI(A[i][j]);
    }
  }

  auto check = [&](i64 val) {
    using ull = unsigned long long;
    vector<ull> B(N, 0);
    //cerr << val << endl;
    REP(0, N, i) {
      REP(0, 5, j) {
        if(A[i][j] >= val) {
          B[i] |= 1LL << j;
        }
      }
      //cerr << B[i] << " ";
    }
    //cerr << endl;

    vi FS(32, 0);
    REP(0, N, i) {
      REP(i+1, N, j) {
        //cerr << i << " " << j << endl;
        ull res = (~(B[i] | B[j])) & 31;
        if(FS[res] == 1) {
          //cerr << "found\n";
          return true;
        }
        //cerr << "not found\n";
      }
      REP(0, 32, k) {
        if((ull(k) & B[i]) == ull(k)) {
          //cerr << "add FS " << k << endl;
          FS[k] = 1;
        }
      }
    }
    return false;
  };

  i64 L = 0, H = 1000000000LL+1;
  assert(check(L));
  assert(!check(H));
  while(H - L > 1) {
    i64 M = (H + L) / 2;
    if(check(M)) {
      L = M;
    }
    else {
      H = M;
    }
  }
  WI(L);

  return 0;
}

/*
**    author : Dhiraj Govindvira
**      date : 02 May 2021 ~ Sunday
**      time : 09:20:20 PM
*/

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

#ifdef DHIRAJ
    #include "D:/dhiraj/Programming/debug.h"
#else
    #define dbg(...) 1
    #define cerr if(0) cerr
#endif

using ll = long long int;
#define endl '\n'

template <typename T, typename U>
inline istream & operator >> (istream &in, pair<T, U> &p) {
    in >> p.first >> p.second;
    return in;
}
template <typename T>
inline istream & operator >> (istream &in, vector<T> &v) {
    for(T &x : v) in >> x;
    return in;
}

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

    vector<vector<ll>> a(n, vector<ll>(5));
    for(ll i = 0; i < n; i++) {
        for(ll j = 0; j < 5; j++) {
            cin >> a[i][j];
        }
    }

    auto f = [&](ll m)
    {
        vector<ll> b(32, 0);
        for(ll i = 0; i < n; i++) {
            ll x = 0;
            for(ll j = 0; j < 5; j++) {
                if(a[i][j] >= m) {
                    x |= (1ll << j);
                }
            }
            b[x] = 1;
        }
        for(ll i = 0; i < 32; i++) {
            for(ll j = 0; j < 32; j++) {
                for(ll k = 0; k < 32; k++) {
                    if(b[i] and b[j] and b[k]) {
                        if((i | j | k) == 31) {
                            return 1;
                        }
                    }
                }
            }
        }
        return 0;
    };

    ll l = 1, r = 1e9 + 5;
    while(l <= r)
    {
        ll m = (l + r) / 2;
        if(f(m)) {
            l = m + 1;
        }
        else {
            r = m - 1;
        }
    }
    cout << l - 1 << endl;
}

int main()
{
    #ifdef DHIRAJ
        freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
        freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
        freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
    #endif

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

    ll rep = 3;
    while(rep--) {
        ll tc = 1;
        for(ll i = 1; i <= tc; i++) {
            cerr << "Case #" << i << "\n";
            solve(i);
        }
        if(dbg()) break;
    }
    return 0;
}


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

int main() {

  int a, b, x, y;
  cin >> a >> b >> x >> y;

  int ans = 0;

  if (a == b) {
    ans = x;
  } else if (a < b) {
    if (y < 2 * x) {
      ans = (b - a) * y + x;
    } else {
      ans = (b - a) * 2 * x + x;
    }
  } else {
    if (y < 2 * x) {
      ans = (a - b - 1) * y + x;
    } else {
      ans = (a - b - 1) * 2 * x + x;
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
	int n,a,b,m,jj,jj2;
	long long p,q,ans;

	p=1e15;
	ans=1e15;
	cin >> n >> m ;
	rep(i, n) cin >> h[i];
	rep(i, m) cin >> w[i];
	sort(h, h+n); //h[0:n] を値が小さい順にソート
	sort(w, w+m); //w[0:m] を値が小さい順にソート
	p=0;
	rep(i,n){
		if (i%2>0) p-=h[i];//奇数はマイナス
		else p+=h[i];//偶数はプラス
	}
	q=p;
	jj=0;
	rep(j,m){
		if (w[j]<h[0]) ans=min(ans,p-w[j]);
		else if (w[j]>h[n-1]) ans=min(ans,w[j]-p);
		else {
			rep2(i,jj,n){
				if (w[j]<h[i]) break;
				if (i%2>0) q=q+h[i]+h[i];
				else q=q-h[i]-h[i];
				jj++;
			}
			if (jj%2>0) ans=min(ans,q+w[j]);
			else ans=min(ans,q-w[j]);
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
//#define maxn
using namespace std;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
void rein(char*str)
{
	int n=0;char opt;
	while((opt=getchar())<'a'||opt>'z');
	while(opt>='a'&&opt<='z')str[n++]=opt,opt=getchar();
	str[n]='\0';
}
//int ksm(int x,int y)
//{
//	int r=1;
//	while(y)
//	{
//		if(y&1) r=1ll*r*x%mod;
//		x=1ll*x*x%mod,y>>=1;
//	}
//	return r;
//}
//int a[maxn];
int n;
long long ans;char S[10];
signed main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",S);
		if(S[0]=='O') ans+=(1ll<<i);
	}
	cout<<ans+1;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long n,dp[70][2]={0};
int main()
{
	cin>>n;
	string s[n];
	for(int i=0;i<n;i++)
	  cin>>s[i];
	dp[0][0]=1;
	dp[0][1]=1;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<2;j++)
		{
	    	for(int k=0;k<2;k++)
			{
		  		if(s[i]=="AND")
		     	  dp[i+1][j&k]+=dp[i][j];
		  		else
		     	  dp[i+1][j|k]+=dp[i][j];
	    	}
		}
	}
	cout<<dp[n][1];
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define endl "\n"
using ll=long long;
using ld=long double;
using namespace std;
ll mod= 1e9+7;
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }

ll a[330000];
map<ll,ll>x;
int main(){
  ll n,ans=0;
  cin>>n;
  cout<<ceil(n/100.0);
}

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

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	string s;
	cin>>s;
	int count = 0;
	for (int i = 0; i < s.size(); ++i)
	{
		if (s[i] == 'Z')
		{
			if (s[i+1] == 'O')
			{
				if (s[i+2] == 'N')
				{
					if (s[i+3] == 'e')
					{
						count++;
					}
				}
			}
		}
	}
	cout<<count<<'\n';
	return 0;
}


//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

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

int n;
int arr[505];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);

	int TC;
	cin>>TC;
	while (TC--){
		cin>>n;
		rep(x,1,n+1) cin>>arr[x];

		if (n==2){
			if (arr[1]==2) cout<<1<<endl<<1<<endl;
			else cout<<0<<endl;
			continue;
		}

		vector<int> ans;

		int par=1;
		rep(x,n+1,4){
			int pos;
			rep(y,1,n+1) if (arr[y]==x) pos=y;

			if (pos==x) continue;

			if (pos%2!=par){
				if (par==1){
					if (arr[2]==x)
						ans.pub(3),swap(arr[3],arr[4]);
					else
						ans.pub(1),swap(arr[1],arr[2]);
				}
				else{
					if (arr[3]==x){
						ans.pub(2),swap(arr[2],arr[3]);
						ans.pub(3),swap(arr[3],arr[4]);
						ans.pub(2),swap(arr[2],arr[3]);
					}
					else
						ans.pub(2),swap(arr[2],arr[3]);
				}
				par^=1;
			}

			rep(z,pos,x){
				ans.pub(z),swap(arr[z],arr[z+1]);
				par^=1;
			}
		}

		while (arr[1]!=1 || arr[2]!=2 || arr[3]!=3){
			if (par==1) ans.pub(1),swap(arr[1],arr[2]);
			else ans.pub(2),swap(arr[2],arr[3]);
			par^=1;
		}

		cout<<sz(ans)<<endl;
		for (auto &it:ans) cout<<it<<" "; cout<<endl;
	}
}

#include <bits/stdc++.h>
using namespace std;
#define      mxx                 1e18
#define      mnn                 -1e18
//#define      int                 long long
#define      Y()                 cout<< "YES" <<endl
#define      N()                 cout << "NO"<<endl
#define      endl                "\n"
#define      Ceil(x,y)           ((x+y-1)/y)
#define      sz(s)               (int)s.size()
#define      angle(x)            double(x * acos(-1) / 180.0)
#define      max_3(a,b,c)        max(a, max(b,c))
#define      min_3(a,b,c)        min(a, min(b,c))
#define      gcd(a,b)            __gcd(a,b)
#define      lcm(a,b)           (a*b)/gcd(a,b)
#define      ll                  long long
#define      PI                  acos(-1)
#define      mem(a,v)            memset(a,v,sizeof(a))
#define      SORT(v)             sort(v.begin(),v.end())
#define      REV(v)              reverse(v.begin(),v.end())
#define      B                   begin()
#define      E                   end()
#define      V                   vector
#define      F                   first
#define      S                   second
#define      PSB                 push_back
#define      MP                  make_pair
#define      flash               cout.flush()
#define      InTheNameOfGod      ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
constexpr ll MOD = 998244353;
constexpr ll mod = 1e9  + 7;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
/*-----*/
#define bug1(a)  cerr<<a<<endl;
#define bug2(a,b)  cerr<<a<<" "<<b<<endl;
#define bug3(a,b,c)  cerr<<a<<" "<<b<<" "<<c<<endl;
/*----*/
const ll N=3e6+5;
vector<ll> adj[N];
ll power(ll n,ll p){if(p==0) return 1;if(p==1)return n;if(p%2)return power(n,p-1)*n;else{ll x=power(n,p/2);return x*x;}}
ll modpow(ll a,ll b,ll m){ll ans=1;while(b){if(b&1)ans=(ans*a)%m;b/=2;a=(a*a)%m;}return ans;}
ll nsum(ll num){return (num*(num+1))/2;}
void edge (ll u,ll v) {adj[u].PSB(v) ;adj[v].PSB(u);}
/*------------------START---------------------*/

/*-----*/
void solve(){
   ll n;
   string s;
   cin>>n;
   V<ll> a(n+1),ok(n+1);
   a[0]=ok[0];
   for(ll i=1;i<=n;i++){
    cin>>a[i]; ok[i]=i;
   }
   ll ct=1;
   V<ll> ans;
   for(ll i=1;i<=n*n;i++){
    if(a==ok) break;
    bool ck=false;
      for(ll j=1;j<n;j++){
         if(j%2 == ct%2 && a[j]>a[j+1]){
          ct++; ans.PSB(j); swap(a[j],a[j+1]); ck=true; break;
         }
      }
      if(!ck){
        for(ll j=n-1;j>=0;j--){
           if(j%2 == ct%2){
             ct++;ans.PSB(j); swap(a[j],a[j+1]); break;
           }
        }
      }
   }

   cout<<ans.size()<<endl;
   for(auto it: ans)
    cout<<it<<' ';
   cout<<endl;

   //cout << fixed << setprecision(10);
}
/*-----*/
//signed main(){
int main(){
   InTheNameOfGod

   ll Test=1;
   cin>>Test;
   while(Test--){
     solve();
    }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using pii = pair<int,int>;
const ll mod = 1e9 + 7;
const int maxn = 1e5+5;

void go() {
    ll n;
    cin >> n;
    ll p3 = 1;
    f1(37) {
        p3 *= 3;
        ll p5 = 1;
        fr(j,1,26) {
            //cout << (ll)pow(3,i) + (ll)pow(5,j) << endl;
            p5 *= 5;
            if (p3 + p5 == n) {
                cout << i <<' '<<j<<endl;
                exit(0);
            }
        }
    }
    cout << -1 << '\n';
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        go();
    }

}

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

#define int long long
typedef vector<int> vi;

const int limit = (int)1e18;

void build(vi &v, int f)
{
    int b = f;
    while (b <= limit)
        v.push_back(b), b *= f;
}

int32_t main()
{
    vi a, b;
    build(a, 3);
    build(b, 5);

    int n;
    cin >> n;

    bool flag = false;
    pair<int, int> ans;
    for (int i = 0; i < (int)a.size(); i++)
        for (int j = 0; j < (int)b.size(); j++)
            if (a[i] + b[j] == n)
            {
                flag = true, ans.first = i + 1, ans.second = j + 1;
                break;
            }

    if (flag)
        cout << ans.first << ' ' << ans.second;
    else
        cout << -1;
    return 0;
}


#include<bits/stdc++.h>
#define MAX 10000
typedef long long ll;
using namespace std;
void manak()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
}
int main()
{
    manak();
    int a[2][2];
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<((a[0][0]*a[1][1])-(a[0][1]*a[1][0]));
    return 0;
}

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

int main()
{
  int a,b,c,d;
  cin >> a >> b >> c >> d;
  cout << a*d - b*c << endl;
}


#include <iostream>
#define pb push_back
using namespace std;
long long n,nr,i,a,b,y,xp,c;

int main()
{
    cin>>n>>y>>a>>b;
    xp=0;

        while(a <= (n + b) / n&&n*a<y)
        {
            xp++;
            n*=a;
        }
        /*else
        {
            c=(y-n)/b;
            xp+=c+1;
            if((y-n)%b==0)
                xp--;
            break;
        }*/

    xp+=(y-n-1)/b;
    cout<<xp;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define Rep(i, a, b) for(ll i = a; i <= b; i++)
#define repr(i, a, b) for(ll i = b-1; i >= a; i--)
// #define _GLIBCXX_DEBUG
template <class T> using V = vector<T>;
#define ALL(v) (v).begin(),(v).end()
#define endl '\n'
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int dy2[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx2[] = {1, 1, 0, -1, -1, -1, 0, 1};
// ios::sync_with_stdio(false);
// cin.tie(nullptr);

/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/

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

std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = std::end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = std::end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(std::ios_base::badbit);
    }
  }
  return dest;
}

__int128 parse(string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}

int main() {
    __int128 x, y, a, b;
    string o, p, q, r;
    cin >> o >> p >> q >> r;
    x = parse(o), y = parse(p), a = parse(q), b = parse(r);
    ll cnt = 0;
    while(1){
        if(x*a < x+b){
            __int128 bfx = x;
            x *= a;
            if(bfx != x/a || x >= y){
                cout << cnt << endl;
                return 0;
            }
            cnt++;
        }
        if(x*a >= x+b){
            ll dif = y - x;
            ll add = dif/b;
            if(dif%b == 0) add--;
            cout << cnt+add << endl;
            return 0;
        }
    }

    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll<<62)-1)
#define cINFINT ((1<<30)-1)

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

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

    ull n = 0;
    cin >> n;
    vector<ll> vec( 2*n, 0 );
    ll sum = 0;
    REP(i, 2*n) {cin >> vec[i]; sum+=vec[i];}

    ll aosum = 0;
    priority_queue<ll, vector<ll>, greater<ll>> q;
    REP(i, n)
    {
        ll a = n - 1 - i;
        ll b = n + 0 + i;
        q.push(vec[a]); q.push(vec[b]);
        aosum += q.top();
        q.pop();
    }

    cout << sum - aosum << endl;

    // ----------------------------------------------------------------
    return 0;
}

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> V(2*N);
  int i;
  for(i=0;i<2*N;i++)cin >> V[i];
  priority_queue<int,vector<int>,greater<int>> q;
  for(i=0;i<N;i++) {
    q.push(V[N-i-1]);
    q.push(V[N+i]);
    q.pop();
  }
  long long ans = 0;
  while(!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

#define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second

using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;

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

template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

template <typename T>
struct coord_comp {
    vector<T> v;
    bool sorted = false;

    coord_comp() {}

    int size() { return v.size(); }

    void add(T x) { v.push_back(x); }

    void build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        sorted = true;
    }

    int get_idx(T x) {
        assert(sorted);
        return lower_bound(v.begin(), v.end(), x) - v.begin();
    }

    T &operator[](int i) { return v[i]; }
};

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main() {
    int n;
    scanf("%d", &n);
    int a[n];
    rep(i, n) scanf("%d", &a[i]), a[i] %= 200;

    bool dp[n + 1][200][2][2];
    rep(i, n + 1) rep(j, 200) rep(k, 2) rep(l, 2) dp[i][j][k][l] = false;
    dp[0][0][0][0] = true;
    tuple<int, int, int> prv[n + 1][200][2][2];
    rep(i, n) rep(j, 200) rep(k, 2) rep(l, 2) if (dp[i][j][k][l]) {
        dp[i + 1][(j + a[i]) % 200][1][l] = true;
        prv[i + 1][(j + a[i]) % 200][1][l] = {0, k, l};

        dp[i + 1][(j - a[i] + 200) % 200][k][1] = true;
        prv[i + 1][(j - a[i] + 200) % 200][k][1] = {1, k, l};

        dp[i + 1][j][k][l] = true;
        prv[i + 1][j][k][l] = {2, k, l};
    }

    if (dp[n][0][1][1]) {
        puts("Yes");
        int j = 0, k = 1, l = 1;
        vector<int> b, c;
        rFor(i, n + 1, 1) {
            auto [t, nk, nl] = prv[i][j][k][l];
            if (t == 0) {
                b.push_back(i - 1);
                j = (j + 200 - a[i - 1]) % 200;
            } else if (t == 1) {
                c.push_back(i - 1);
                j = (j + a[i - 1]) % 200;
            }
            k = nk;
            l = nl;
        }

        reverse(b.begin(), b.end());
        reverse(c.begin(), c.end());
        printf("%d ", b.size());
        for (auto x : b) printf("%d ", x + 1);
        printf("\n");
        printf("%d ", c.size());
        for (auto x : c) printf("%d ", x + 1);
        printf("\n");
        return 0;
    }
    puts("No");
}

#include <bits/stdc++.h>

using namespace std;

int main() {
    int N;
    cin >> N;
    N = min(8, N);
    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
        A[i] %= 200;
    }

    vector<vector<int>> rs(200);
    for (int i = 1; i < (1 << N); ++i) {
        int acc = 0;
        for (int j = 0; j < N; ++j) {
            if ((i >> j) & 1) {
                acc += A[j];
                acc %= 200;
            }
        }
        rs[acc].push_back(i);
    }

    int m = -1;
    for (int i = 0; i < 200; ++i) {
        if (rs[i].size() > 1) {
            m = i;
            break;
        }
    }

    if (m < 0) {
        cout << "No" << endl;
        return 0;
    }

    int b = rs[m][0];
    vector<int> B;
    for (int i = 0; i < N; ++i) {
        if ((b >> i) & 1) {
            B.push_back(i);
        }
    }

    int c = rs[m][1];
    vector<int> C;
    for (int i = 0; i < N; ++i) {
        if ((c >> i) & 1) {
            C.push_back(i);
        }
    }

    cout << "Yes" << endl;
    cout << B.size() << " ";
    for (int e : B) {
        cout << e + 1<< " ";
    }
    cout << endl;
    cout << C.size() << " ";
    for (int e : C) {
        cout << e + 1 << " ";
    }
    cout << endl;
}