#define _GIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const double EPS = 1e-10;

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

int bfs(int sx, int sy, int gx, int gy, vector<string>& vs) {
  vector<vector<int>> d(vs.size(), vector<int>(vs[0].size(), -1));
  queue<P> que;

  que.push(P(sx, sy));
  d[sy][sx] = 0;

  while (que.size()) {
    P p = que.front(); que.pop();
    if (p.first == gx && p.second == gy) break;

    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < vs[0].size() &&
          0 <= ny && ny < vs.size() &&
          vs[ny][nx] != '#' && d[ny][nx] == -1) {
        d[ny][nx] = d[p.second][p.first] + 1;
        que.push(P(nx, ny));
      }
    }
  }

  return d[gy][gx];
}

int main(void) {
  int H, W;
  cin >> H >> W;
  vector<string> vs(H);
  rep(i, H) cin >> vs[i];

  // 幅優先探索で最短経路を求める
  int shortest = bfs(0, 0, W - 1, H - 1, vs);
  if (shortest == -1) {
    cout << -1 << endl;
    return 0;
  }
  int count = 0;  // 初期状態の白マス全て
  rep(i, H) rep(j, W) if (vs[i][j] == '.') ++count;
  // 全ての白マスから、最短経路を除外する
  count -= shortest + 1;

  cout << count << endl;

  return 0;
}

#include<iostream>
#include<queue>
using namespace std;

int main() {
  int H, W; cin >> H >> W;
  int N = H*W;
  int A[N]; string str;
  int white_num = 0;
  for (int i = 0; i < H; i++) {
    cin >> str;
    for (int j = 0; j < W; j++) {
      white_num += A[i*W+j] = (str[j] == '.');
    }
  }
  queue<pair<int, int>> Q; Q.push(make_pair(0, 1));
  bool visited[N]; for (int i = 0; i < N; i++) visited[i] = false;
  visited[0] = true;
  int score = -1;
  while (!Q.empty()) {
    auto front = Q.front(); Q.pop();
    int u = front.first, d = front.second;
    if (u == H*W-1) { score = white_num-d; break; }
    vector<int> e;
    if (u >= W) e.push_back(u-W);
    if (u % W > 0) e.push_back(u-1);
    if (u % W < W-1) e.push_back(u+1);
    if (u / W < H-1) e.push_back(u+W);
    for (int v : e) {
      if (A[v] && !visited[v]) {
        visited[v] = true;
        Q.push(make_pair(v, d+1));
      }
    }
  }
  cout << score << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N=305;
#define fi first
#define se second
typedef pair<int,int>P;
priority_queue<P>que;
int n,m,b[N],vi[N],cnt[N],a[N][N];
bool check(int mid){
	while(!que.empty())	que.pop();
	for(int i=1;i<=m;i++)	b[i]=0;
	for(int i=1;i<=n;i++)	++b[a[i][cnt[i]=1]];
	for(int i=1;i<=m;i++)	vi[i]=0,que.push(P(b[i],i));
	while(!que.empty()){
		P x=que.top();	que.pop();
		if(vi[x.se])	continue;
		if(x.fi<=mid)	return true;

		vi[x.se]=1;
		for(int i=1;i<=n;i++){
			if(a[i][cnt[i]]!=x.se)	continue;
			while(vi[a[i][cnt[i]]]&&cnt[i]<=m)	++cnt[i];
			if(cnt[i]>m)	return false;
			que.push(P(++b[a[i][cnt[i]]],a[i][cnt[i]]));
		}
	}
	return false;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			scanf("%d",&a[i][j]);
	int l=1,r=n;
	while(l<r){
		int mid=(l+r)>>1;
		if(check(mid))	r=mid;
		else	l=++mid;
	}
	printf("%d\n",l);
	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

void debug_out() { cerr << endl; }

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

#define rep(i,n) for (int i = 0; i < n; ++i)
#define reps(i,s,n) for (int i = s; i < n; ++i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
#define per(i,n) for (int i = n - 1; i >= 0; --i)
#define per1(i,n) for (int i = n; i >= 1; --i)
#define all(c) begin(c),end(c)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline T intceil(T a, T b) { return (a + (b - 1)) / b; }
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef tuple<int,int,int> Tint;
typedef pair<ll,ll> PL;
const long long MOD = 1e9+7;
#define precout() cout << std::fixed << std::setprecision(20);
#define print(a) cout << a << endl;
template<typename T> void printv(vector<T>&v) {
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << " ";
    cout << v.back() << endl;
}
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const int dy[4] = { 0, 1, 0, -1 };
const int dx[4] = { 1, 0, -1, 0 };
// if(nextH >= 0 && nextH < H && nextW >= 0 && nextW < W)
static const long double pi = acos(-1.0);
typedef complex<ld> cd;

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

    int N,M; cin >> N >> M;
    vector<vector<int>> A(N, vector<int>(M));
    rep(i, N) rep(j, M) {
        cin >> A[i][j];
        --A[i][j];
    }

    int ans = 1e9;
    unordered_set<int> ngS;
    while(ngS.size() != M) {
        vector<int> cnt(M, 0);

        rep(i, N) {
            rep(j, M) {
                if(ngS.find(A[i][j]) == ngS.end()) {
                    ++cnt[A[i][j]];
                    break;
                }
            }
        }

        int maxCnt = 0, maxCntSports;
        rep(i, M) {
            if(cnt[i] > maxCnt) {
                maxCntSports = i;
                maxCnt = cnt[i];
            }
        }
        chmin(ans, maxCnt);
        ngS.insert(maxCntSports);
    }

    print(ans);
}


#include<cstdio>

bool include3(int x){
	if(x % 10 == 3){
		return true;
	}else{
		x /= 10;
		if(x){
			return include3(x);
		}
		return false;
	}
}

bool judge(int x){
	if(!(x % 3)){
		return true;
	}else{
		return include3(x);
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		if(judge(i)){
			printf(" %d", i);
		}
	}
	printf("\n");
}

#include <vector>
#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int m = 1e9;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        m = min(__builtin_ctz(a), m);
    }
    cout << m << endl;
}


#include <iostream>
using namespace std;
typedef long long int LLI;
int main()
{
	LLI n, a, b, save;
	cin >> n >> a >> b;

	LLI first, second, ans = 0;
	cin >> first >> second;
	save = ( second - first ) * a;
	if( save > b )
	    ans += b;
	else
	    ans += save;
	first = second;

	for(int i = 2; i < n; i++)
	{
		cin >> second;
	    save = ( second - first ) * a;
		if( save > b )
	    	ans += b;
	 	else
	    	ans += save;
		first = second;
	}
	cout << ans;
}

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define MOD2 998244353
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<=b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
#define T int
#define ordered_set tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
auto clk=clock();

ll mexp(ll a, ll b, ll m){
    ll ans=1;
    a%=m;
    while(b){
        if(b&1) ans=ans*a%m;
        b>>=1;
        a=a*a%m;
    }
    return ans;
}

const int N = 100005;
ll X[N];

int main(){
    IOS
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    ll n, a, b;
    cin >> n >> a >> b;
    FOR(i,1,n){
        cin >> X[i];
    }
    ll ans = 0;
    FOR(i,2,n){
        ans += min((X[i] - X[i-1]) * a, b);
    }
    cout << ans;

    cerr<<endl<<endl<<"Time: "<<fixed<<setprecision(12)<<(long double)(clock()-clk)/CLOCKS_PER_SEC<<" ms"<<endl<<endl;

    return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=405;
int h,w;
string s[M];
int vis[M][M];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int chk(int x,int y)
{
    if(x<0 || x>=h || y<0 || y>=w)return false;
    if(vis[x][y])return false;
    return true;
}
int cw=0,cb=0;
int dfs(int i,int j)
{
    vis[i][j]=1;
    if(s[i][j]=='#')cb++;
    else cw++;
    for(int k=0;k<4;k++)
    {
        int x=dx[k]+i;
        int y=dy[k]+j;
        if(!chk(x,y))continue;
        if(s[i][j]==s[x][y])continue;
        dfs(x,y);
    }
}
 main()

{
    fast
    cin>>h>>w;
    f(i,0,h-1)
    {
        cin>>s[i];
    }
    int ses=0;
    f(i,0,h-1)
    {
        f(j,0,w-1)
        {
            if(vis[i][j])continue;
            else
            {
                cb=cw=0;
                dfs(i,j);
                ses+=(cb*cw);
            }
        }
    }
    cout<<ses<<endl;
    return 0;

}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

ll BIT1[200010];
ll BIT2[200010];

void add1(int A, ll B) {
	while (A > 0) {
		BIT1[A] = min(BIT1[A], B);
		A -= A & -A;
	}
}
ll query1(int A) {
	ll kotae = 1e18;
	while (A <= 200000) {
		kotae = min(kotae, BIT1[A]);
		A += A & -A;
	}
	return kotae;
}
void add2(int A, ll B) {
	while (A <= 200000) {
		BIT2[A] = min(BIT2[A], B);
		A += A & -A;
	}
}
ll query2(int A) {
	ll kotae = 1e18;
	while (A > 0) {
		kotae = min(kotae, BIT2[A]);
		A -= A & -A;
	}
	return kotae;
}

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

	ll N, D;
	cin >> N >> D;
	int A[200001];
	rep1(i, N) cin >> A[i];

	rep1(i, 200000) BIT1[i] = 1e18;
	rep1(i, 200000) BIT2[i] = 1e18;

	rep1(i, N) {
		add1(i, A[i] + i * D);
	}
	rep1(i, N) {
		add2(i, A[i] - i * D);
	}

	ll setuyaku = 0;
	for (int i = 2; i < N; i++) {
		ll kari1 = query1(i + 1) - i * D;
		ll kari2 = query2(i - 1) + i * D;
		if (A[i] > min(kari1, kari2)) {
			setuyaku += A[i] - min(kari1, kari2);
		}
	}

	ll motomoto = 0;
	rep1(i, N - 1) motomoto += A[i];
	rep1(i, N - 1) motomoto += A[i + 1];
	motomoto += (N - 1) * D;

	co(motomoto - setuyaku);

	Would you please return 0;
}


/// IN THE NAME OF GUITAR
#include <bits/stdc++.h>

using namespace std;

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

#define sz(x)                       (ll) x.size()
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define lc                          id << 1
#define rc                          lc | 1

ll Pow(ll a, ll b, ll md, ll ans = 1) {
    for (; b; b >>= 1, a = a * a % md)
        if (b & 1)
            ans = ans * a % md;
    return ans % md;
}

const ll MAXN = 2e3 + 20;
const ll INF  = 1e18;
const ll MOD  = 1e9 + 7;
ll L[MAXN], R[MAXN], A[MAXN][MAXN], ps[MAXN][MAXN], n, m, ans;
stack<ll> st;

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1; i <= n; i++) {
		string s;
		cin >> s;
		for (ll j = 1; j <= m; j++) {
			A[i][j] = (s[j - 1] == '#');
		}
	}
	for (ll i = 1; i < n; i++) {
		for (ll j = 1; j < m; j++) {
			ll cnt = A[i][j] + A[i + 1][j] + A[i][j + 1] + A[i + 1][j + 1];
			if (cnt & 1) ps[i][j] = 0;
			else ps[i][j] = ps[i - 1][j] + 1;
		}
	}
	ans = max(n, m);
	for (ll i = 1; i < n; i++) {
		while (sz(st)) st.pop();
		st.push(0);
		for (ll j = 1; j < m; j++) {
			while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop();
			if (sz(st))
				L[j] = st.top();
			st.push(j);
		}
		while (sz(st)) st.pop();
		st.push(m);
		for (ll j = m - 1; j >= 1; j--) {
			while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop();
			if (sz(st)) R[j] = st.top();
			st.push(j);
		}
		for (ll j = 1; j < m; j++) {
			ans = max(ans, (ps[i][j] + 1) * (R[j] - L[j]));
		}
	}
	printf("%lld\n", ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n)  for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif

// $ cp-batch FlipAndRectangles | diff FlipAndRectangles.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address FlipAndRectangles.cpp && ./a.out

/*

 11/9/2019

 16:34-16:41 give up

 11/10/2019

 15:40- read editorial

 http://kmjp.hatenablog.jp/entry/2017/08/24/0930
 https://youtu.be/xRYReB8pc2E?t=2803

 */

const int MAX_N=2000+1;
string G[MAX_N];
int H,W;

int cum[MAX_N][MAX_N];
void solve() {
  REP(i,H-1) {
    cum[i][W-1]=1;
    for(int j=W-2; j>=0; --j) {
      int cur=G[i][j]==G[i+1][j],r=G[i][j+1]==G[i+1][j+1];
      if(cur==r) cum[i][j]=cum[i][j+1];
      cum[i][j]+=1;
    }
//    dumpC(cum[i],cum[i]+W);
  }

  int res=max(H,W);
  REP(j,W) {
    stack<II> S; S.emplace(0,0);
    REP(i,H) {
      int a=cum[i][j],b=i;
      while(SZ(S)&&S.top().first>=a) {
        auto p=S.top(); S.pop();
        int x=i-p.second+1,y=p.first;
        SMAX(res,x*y);
        b=p.second;
      }
      S.emplace(a,b);
    }
  }
  cout<<res<<endl;
}

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

  cin>>H>>W;
  REP(i,H) cin>>G[i];
  solve();

  return 0;
}


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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

typedef pair<double, double> pd;
typedef vector<pd> vpd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

template <typename First>
void OUT(First first){
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

template<class t, class u> t chmax(t&a,u b){if(a<b)a=b; return a;};
template<class t, class u> t chmin(t&a,u b){if(a>b)a=b; return a;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  IN(N);
  vpll RL(N);
  REP(i,N){
    IN(RL[i].second, RL[i].first);
  }

  sort(ALL(RL));

  vll Lmax(N, RL[0].second);
  REP(i,N-1){
    Lmax[i+1] = max(Lmax[i], RL[i+1].second);
  }

  /*
  REP(i,N){
    debug_print(RL[i].second, RL[i].first);
  }
  */

  ll ans = max(RL[0].first-Lmax[N-2]+1, (ll)0)+(RL[N-1].first-RL[N-1].second)+1;
  ll Lmax_after = RL[N-1].second;
  for(ll i=N-2; i>0; --i){
    ll Lmax1 = Lmax[i-1];
    ll Rmin1 = RL[0].first;
    ll Lmax2 = RL[i].second;
    ll Rmin2 = RL[i].first;

    ll LR1 = max(Rmin1-Lmax1+1, (ll)0);
    ll LR2 = max(Rmin2-Lmax2+1, (ll)0);
    ll LR1_1 = max(Rmin1-max(Lmax_after, Lmax1)+1, (ll)0);
    ll LR2_1 = max(Rmin2-max(Lmax_after, Lmax2)+1, (ll)0);

    ll ans_tmp = max(LR1+LR2_1, LR1_1+LR2);

    chmax(ans, ans_tmp);
    chmax(Lmax_after, Lmax2);
  }

  OUT(ans);

  return 0;
}

#include <bits/stdc++.h>
#define fi first
#define sc second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;

#define Pr(f,...) fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)

typedef long long ll;

template<typename T>
void read(T &x){
	x=0;
	char c;
	for(c=getchar();!isdigit(c);c=getchar());
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
}

/*
const int N=,P=;

int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
int gcd(int a,int b){ return !b?a:gcd(b,a%b); }
*/

const int N=5e5+50;
int n;
typedef pair<int,int> pii;
pii a[N],c[N];
ll ans;
ll L(int a,int b){ return max(b-a+1,0); }
int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
	//freopen("d.in","r",stdin);
#endif
	scanf("%d",&n);
	rep(i,1,n)scanf("%d%d",&a[i].fi,&a[i].sc);
	sort(a+1,a+n+1,[](pii a,pii b){return a.sc==b.sc?a.fi<b.fi:a.sc<b.sc; });
	sort(a+2,a+n+1,[](pii a,pii b){return a.fi==b.fi?a.sc<b.sc:a.fi<b.fi; });
	c[n]=a[n];
	dec(i,n-1,2){
		c[i].fi=max(a[i].fi,c[i+1].fi);
		c[i].sc=min(a[i].sc,c[i+1].sc);
	}
	//ans=L(c[2].fi,c[2].sc)+L(a[1].fi,a[1].sc);
	ll lmax=0;
	rep(i,2,n){
		ans=max(ans,L(c[i].fi,c[i].sc)+L(max(a[i-1].fi,a[1].fi),a[1].sc));
		//Pr("%d,%d---%d,%d\n",c[i].fi,c[i].sc,a[i-1].fi,a[1].sc);
		cmax(lmax,L(a[i].fi,a[i].sc));
	}
	ans=max(ans,lmax+L(max(a[n].fi,a[1].fi),a[1].sc));
	printf("%lld\n",ans);
	return 0;
}


#include <stdio.h>
#include <math.h>
#define MAXINPUT 100

int main(void){
	int h[MAXINPUT],w[MAXINPUT];
	int ansh[MAXINPUT]={},answ[MAXINPUT]={};
	int i=0,j=0;
	int ans=0;
	int num=0;
	int k;
	while(i<MAXINPUT){
		scanf("%d %d",&h[i],&w[i]);
		if(h[i]==0 && w[i]==0)break;
		i++;
		num++;
	}

	for(i=0;i<num;i++,ans=0){
		ans=h[i]*h[i]+w[i]*w[i];
		for(;(ansh[i]==0||answ[i]==0);){
			for(j=1;k==0;j++){
				double temp=ans-(j*j);
				double tempa=0;
				if(temp<=0){
					k=1;
					break;
				}
				temp=sqrt(temp);
				temp=modf(temp,&tempa);
				if(temp==0 && tempa<=150 && j<tempa){
					if(tempa*tempa+j*j==h[i]*h[i]+w[i]*w[i] && j<=h[i]){
						continue;
					}
					ansh[i]=j;
					answ[i]=(int)tempa;
					printf("%d %d\n",ansh[i],answ[i]);
					break;
				}
			}
		ans++;
		k=0;
		}
	}

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

pair<int, int> nxt[256][256];

int main()
{
    int h, w;
    vector<pair<int, pair<int, int> > > v;

    for (int i = 1; i <= 150; i++){
        for (int j = i + 1; j <= 150; j++){
            v.push_back(make_pair(i * i + j * j, make_pair(i, j)));
        }
    }

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

    for (int i = 0; i < v.size() - 1; i++){
        pair<int, int> now = v[i].second, next = v[i + 1].second;
        nxt[now.first][now.second] = next;
    }

    while (scanf("%d %d", &h, &w) && w){
        printf("%d %d\n", nxt[h][w].first, nxt[h][w].second);
    }

    return (0);
}


#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int N = 0, M = 0;
int points[1001];
int scores[1002001];

bool check(int a, int c){
    if(a >= (N + 2) * (N + 1) / 2 - 1){
        return true;
    }
    return (scores[a] <= c && scores[a + 1] > c);
}

int search2(int a, int b, int c){
    int pib = a + b - b / 2;
    if(check(pib, c)){
        return pib;
    }else if(scores[pib] < c){
        return search2(pib, b / 2, c);
    }else{
        return search2(a, b / 2, c);
    }
}

int main(){
    for(;;){
        for(int i = 0; i < 1001; i++){
            points[i] = 0;
        }
        for(int i = 0; i < 1002001; i++){
            scores[i] = 0;
        }
        if(cin >> N){
            if(N == 0){
                break;
            }
            cin >> M;
            for(int i = 1; i <= N; i++){
                cin >> points[i];
            }
            for(int i = 0; i <= N; i++){
                for(int j = i; j <= N; j++){
                    scores[(2 * N - i + 1) * i / 2 + j] = points[i] + points[j];
                }
            }
            int mxsco = 0;
            sort(scores, scores + ((N + 2) * (N + 1)) / 2);
            for(int i = 0; i < (N + 2) * (N + 1) / 2; i++){
                if(scores[i] > M){
                    break;
                }
                int nosco = scores[i] + scores[search2(0, (N + 2) * (N + 1) / 2, M - scores[i])];
                if(nosco >= mxsco &&  nosco <= M){
                    mxsco = nosco;
                }
            }
            cout << mxsco << endl;
        }else{
            break;
        }
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

// 0119 B-0563 18:00 ~ 19:00

int main() {
	int n, m;
	while(cin >> n >> m) {
		if(n == 0 && m == 0) break;

		vector<ll> v(n);
		rep(i, n) cin >> v[i];

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

		vector<ll> p;
		rep(i, n) p.push_back(v[i]);

		rep(i, n) {
			REP(j, i, n) {
				p.push_back(v[i] + v[j]);
			}
		}

		sort(p.begin(), p.end());
		ll ans = 0;
		rep(i, p.size()) {
			int id = upper_bound(p.begin(), p.end(), m - p[i]) - p.begin();
			if(id == 0) {
				if(p[i] <= m) ans = max(ans, p[i]);
			}
			else ans = max(ans, p[i] + p[id-1]);
		}

		cout << ans << endl;
	}

	return 0;
}


#include<stdio.h>
#include<math.h>
int main(){
    double x1, x2;
    double y1, y2;
    double L, l;
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
    L = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
    l = sqrt(L);
    printf("%f", l);
    return 0;
}

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
	double x, y, x1, x2, y1, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	x = fabs(x2 - x1);
	y = fabs(y2 - y1);
	if( x == 0 ) {
		cout << fixed << setprecision(5) << y << "\n";
		return 0;
	} else if( y == 0 ) {
		cout << fixed << setprecision(5) << x << "\n";
		return 0;
	}

	int c = 0;

	double v = x*x + y*y,
	       d = max(x, y),
		   eps = 0.00001;
	do
	{
		d = (d + v / d ) / 2.0L;
		++c;
	}
	while( c < 5 );

	cout << fixed << setprecision(5) << d << "\n";

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#define all(x) (x).begin(),(x).end()
typedef long long ll;

using namespace std;

int main()
{
    int K, T, s, ans;   cin >> K >> T;
    vector<int> v(T);
    s = 0;
    for(int i=0; i<T; i++){
        cin >> v[i];
        s += v[i];
    }
    ans = 0;
    for(int i=0; i<T; i++){
        if(v[i] >= (s+1)/2) ans += v[i] - (s - v[i]) - 1;
    }
    cout << ans;
}

#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 yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#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;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
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> 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; }

int main(){
    INT(k,t);
    vector<int> cake(t);
    in(cake);
    sort(rall(cake));
    int day=k-cake[0];
    int ans;
    if(cake[0]<=day+1)ans=0;
    else ans=cake[0]-day-1;
    out(ans);
}


#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
#define MOD 1000000007
using namespace std;

const long max_comb=300;
vector<ll> fac(max_comb+1); //n! (mod M)
vector<ll> ifac(max_comb+1); //k!^(-1) (mod M)

ll mpow(ll x, ll n){ //x^n(mod M)
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % MOD;
        x = x*x % MOD;
        n = n >> 1;
    }
    return ans;
}

ll minv(ll x){
    return mpow( x, MOD-2 );
}

ll comb(long a, long b){     // C(a,b) = a! * b!^(-1) * (a-b)^(-1)
    if(a == 0 && b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b]* ifac[b] % MOD;
    return tmp * fac[a] % MOD;
}

ll perm(long a, long b){     // P(a,b) = a! * (a-b)!^(-1)
    if(b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = ifac[a-b] % MOD;
    return tmp * fac[a] % MOD;
}

void pre_comb()
{
    fac[0] = 1;
    ifac[0] = 1;
    for(long i = 0; i<max_comb; i++){
        fac[i+1] = fac[i]*(i+1) % MOD; // n!(mod M)
        ifac[i+1] = ifac[i]*minv(i+1) % MOD; // k!^(-1) (mod M)
    }
    return;
}

ll dp[205][100005];

int main(int argc, char* argv[])
{
    long n, x;
    scanf("%ld%ld", &n, &x);

    vector<long> s(n);
    long i,j;
    for(i=0; i<n; i++) {
        scanf("%ld", &s[i]);
    }
    sort(s.rbegin(), s.rend());

    pre_comb();

    dp[0][x]=1;
    for(i=0; i<n; i++) {
        for(j=0; j<=x; j++) {
            if(dp[i][j]) {
                ll tmp = minv(n-i);
                dp[i+1][j] = (dp[i+1][j] + (dp[i][j]* (1+MOD-tmp)))%MOD;
                dp[i+1][j%s[i]] = (dp[i+1][j%s[i]] + dp[i][j]* tmp)%MOD;
            }
        }
    }

    ll ans=0;
    for(i=0; i<s[n-1]; i++) {
        ans = (ans + i*dp[n][i])%MOD;
    }
    ans = (ans * perm(n,n))%MOD;
    printf("%lld\n", ans);

    return 0;
}

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

#define DEBUG 0

#if DEBUG
  #define DLOG(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DLOG(...)
#endif

void debug(auto f) {
#if DEBUG
  f();
#endif
}

ll mPrime = int(1e9) + 7;
ll bN, bX;
vector<ll> bS;
vector<ll> facts;
vector<ll> invFacts;
vector<int> tbl;
vector<int> low;  // low[y] == #{ i | s[i] <= y }

ll mAdd(ll x, ll y) {
  ll z = x + y;
  if (z < mPrime)  return z;
  else             return (z - mPrime);
}

ll mSub(ll x, ll y) {
  ll z = x - y;
  if (z >= 0)  return z;
  else         return (z + mPrime);
}

ll mMul(ll x, ll y) {
  return (x * y) % mPrime;
}

ll eGCD(ll a, ll b, ll& s, ll& t) {
  if (a == 0) {
    s = 0;
    t = 1;
    return b;
  }
  ll u;
  ll g = eGCD(b % a, a, t, u);
  s = u - (b / a) * t;
  return g;
}

ll mInv(ll x) {
  ll s, t;
  eGCD(x, mPrime, s, t);
  return s < 0 ? s + mPrime : s;
}

ll tight(ll);

ll loose(ll y, ll len) {
  ll result = mMul(tight(y), mMul(facts.at(len), invFacts.at(low.at(y))));
  DLOG("loose(%lld, %lld) = %lld\n", y, len, result);
  return result;
}

ll tight(ll y) {
  if (tbl.at(y) >= 0)  return tbl.at(y);
  ll len = low.at(y);
  if (len == 0)  return y;
  ll result = 0;
  for (ll i = 0; i < len; i++) {
    ll z = y % bS.at(i);
    result = mAdd(result, loose(z, len-1));
  }
  tbl.at(y) = result;
  DLOG("tight(%lld) = %lld\n", y, result);
  return result;
}

int main() {
  cin >> bN >> bX;
  bS.resize(bN);
  for (ll i = 0; i < bN; i++) {
    cin >> bS.at(i);
  }
  facts.resize(bN+1);
  invFacts.resize(bN+1);
  facts.at(0) = invFacts.at(0) = 1;
  for (ll i = 1; i <= bN; i++) {
    facts.at(i) = mMul(i, facts.at(i-1));
    invFacts.at(i) = mInv(facts.at(i));
  }
  sort(bS.begin(), bS.end());

  low.resize(bX+1);
  ll idx = 0;
  for (ll i = 0; i < bX+1; i++) {
    if (idx < (ll) bS.size() && bS.at(idx) == i)  idx++;
    low.at(i) = idx;
  }

  tbl.resize(bX+1, -1);
  ll ans = loose(bX, bS.size());
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int NIL = -1;
const int INF = (1<<21);
const long long MOD = 1e9+7;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int N, M; cin >> N >> M;
  int maxL = 0;
  int minR = N;
  for (int i = 0; i < M; ++i) {
    int l ,r; cin >> l >> r;
    maxL = max(maxL, l);
    minR = min(minR, r);
  }
  int ans = minR - maxL + 1;
  if (ans < 0) cout << 0 << endl;
  else cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<int>pf(n+2);
	for(int i=0; i<m; i++){
		int a,b;
		cin>>a>>b;
		pf[a]++;
		pf[b+1]--;
	}
	for(int i=1; i<=n; i++){
		pf[i]+=pf[i-1];
	}
	int cnt=0;
	for(int i=1; i<=n; i++){
		if(pf[i]==m)
		 cnt++;
	}
	cout<<cnt;
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int> >;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define irep(i, n) for(int i = n-1; i >= (int)0; i--)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define rrep(i,m,n) for(int i = m; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define mp make_pair
#define sum accumulate
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
typedef long long ll;
const int INF = 2000000000;
const ll INF64 = 1000000000000000ll;
const ll MOD = 1000000007LL;

int main(){
    string s;
    std::cin >> s;
    std::cout << s.substr(0,s.size()-8) << std::endl;
}

/*#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include <algorithm>
#include<functional>
#include<cstdlib>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

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

int H = 0;
int M = 0;
int S = 0;

void input(void) {
	CI H >> M >> S;

	return;
}

int main(void) {
	int sum = 0;
	int remain = 0;
	int hour = 0;
	int minute = 0;
	int second = 0;

	while (1) {
		input();
		if (H == -1) {
			return 0;
		}
		sum = H * 3600 + M * 60 + S;
		remain = 2 * 3600 - sum;
		second = remain % 60;
		remain -= remain % 60;
		remain /= 60;
		minute = remain % 60;
		remain -= remain % 60;
		remain /= 60;
		hour = remain
	}

	return 0;
}*/
/*#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include <algorithm>
#include<functional>
#include<cstdlib>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

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

int H = 0;
int M = 0;
int S = 0;

void input(void) {
CI H >> M >> S;

return;
}

int main(void) {
int sum = 0;
int remain = 0;
int hour = 0;
int minute = 0;
int second = 0;

while (1) {
input();
if (H == -1) {
return 0;
}
sum = H * 3600 + M * 60 + S;
remain = 2 * 3600 - sum;
second = remain % 60;
remain -= remain % 60;
remain /= 60;
minute = remain % 60;
remain -= remain % 60;
remain /= 60;
hour = remain
}

return 0;
}*/

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include <algorithm>
#include<functional>
#include<cstdlib>
#include<cmath>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

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

const long long int III = 1000010;

string str;
void input(void) {
	CI str;
	return;
}

int main(void) {
	input();
	long long int f_j = 0;
	long long int f_o = 0;
	long long int f_i = 0;

	long long int max_k = 0;for (int i = 0; i < str.size(); i++) {
		if (str[i] == 'J') {
			if (f_o == 0 && f_i == 0) {
				f_j++;
			}
			if (f_o != 0 || f_i != 0) {
				f_j = 1;
				f_o = 0;
				f_i = 0;
			}
		}
		else {
			if (f_j > 0 && f_o != f_j && f_i == 0 && str[i] == 'O') {
				f_o++;
			}
			else {
				if (f_o > 0 && f_i != f_o && str[i] == 'I') {
					f_i++;
					if (f_j >= f_o && f_o == f_i) {
						if (f_o > max_k) {
							max_k = f_o;
						}
						f_j = 0;
						f_o = 0;
						f_i = 0;
					}
				}
				else {

					f_j = 0;
					f_o = 0;
					f_i = 0;

				}
			}
		}
	}

	CO max_k E

	return 0;
}


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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
    #define gc getchar()
    template <typename T>
    inline void read(T& x) {
        x = 0; bool f = 1; char ch;
        for (ch = gc; ch < '0' || '9' < ch; ch = gc) if (ch == '-') f ^= 1;
        for (; '0' <= ch && ch <= '9'; ch = gc) x = (x << 3) + (x << 1) + (ch ^ 48);
        x = f ? x : -x;
    }
    #undef gc
}

const int MAXN = 2e5 + 10;

int n, arr[2][MAXN], cnt[2][MAXN], val;
vector<int> pos;

int main() {
    IO::read(n);
    rep(_, 0, 1) rep(i, 1, n) IO::read(arr[_][i]), cnt[_][arr[_][i]]++;
    reverse(arr[1] + 1, arr[1] + n + 1);
    rep(i, 1, n) if (arr[0][i] == arr[1][i]) val = arr[0][i], pos.push_back(i);
    int l = 1, r = n; bool flg = false;
    for (auto cur_pos : pos) {
        if (flg == false) {
            if (arr[0][l] != val && arr[1][l] != val) {
                swap(arr[1][l], arr[1][cur_pos]);
                ++l;
            } else flg = true;
        }
        if (flg == true) {
            if (arr[0][r] != val && arr[1][r] != val) {
                swap(arr[1][r], arr[1][cur_pos]);
                --r;
            } else return puts("No"), 0;
        }
    }
    puts("Yes");
    rep(i, 1, n) printf("%d ", arr[1][i]);
    return 0;
}

#pragma GCC optimize("Ofast")

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

using namespace std;
using namespace __gnu_pbds;

typedef
tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> orderedSet;

#define pb push_back
#define F first
#define S second
#define all(a) (a).begin(), (a).end()

#define for1(i0, l0, r0) for (int i0 = l0; i0 < r0; ++i0)
#define for2(i0, l0, r0) for (int i0 = l0; i0 <= r0; ++i0)
#define forn(i0, n0) for (int i0 = 0; i0 < n0; ++i0)
#define forn1(i0, n0) for (int i0 = 1; i0 < n0; ++i0)
#define forr(i0, n0) for (int i0 = n0; i0 >= 0; --i0)
#define forr1(i0, r0, l0) for (int i0 = r0; i0 > l0; --i0)
#define forr2(i0, r0, l0) for (int i0 = r0; i0 >= l0; --i0)

#define Sort(a) sort(all(a))
#define Reverse(a) reverse(all(a))
#define relaxMax(a, b) a = max(a, b)
#define relaxMin(a, b) a = min(a, b)

typedef long long ll;
typedef long double ld;
#define ui unsigned int
#define ull unsigned long long

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

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;

typedef vector<vi> vvi;
typedef vector<vd> vvd;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

#define vpss vector<pss>
#define vvs vector<vs>
#define vvpii vector<vpii>
#define vvpll vector<vpll>
#define vpt vector<pt>
#define vvvi vector<vvi>
#define vsi vector<set<int>>
#define ss second
#define ff first

#define printvi(arr) for (int x0 : arr) cout << x0 << ' '; cout << '\n';
#define printvll(arr) for (ll x0 : arr) cout << x0 << ' '; cout << '\n';
#define printpair(pair0) cout << pair0.F << ' ' << pair0.S << '\n';
#define printvp(arr) for (auto pair0 : arr) printpair(pair0);
#define initArray(arr, N0, X0) memset(arr, X0, N0 * sizeof(int))
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void init() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //cout.precision(16);
    //freopen("amusing.in", "r", stdin);
    //freopen("amusing.out", "w", stdout);
}

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

vll fact(maxn);
vll invfact(maxn);

ll powmod(ll a, ll b) {
    if (b == 0) return 1;
    if (b % 2 == 0) {
        ll x = powmod(a, b / 2);
        return (x * x) % mod;
    }
    return (a * powmod(a, b - 1)) % mod;
}

void calcFactorials() {
    fact[0] = 1;
    forn1(i, maxn) fact[i] = (fact[i - 1] * (ll) i) % mod;
    forn(i, maxn) invfact[i] = powmod(fact[i], mod - 2);
}

ll cnk(int n, int k) {
    return ((fact[n] * invfact[k]) % mod) * invfact[n - k] % mod;
}

int h, w;

ll numberOfWays(int x, int y){
    int dx1 = x - 1, dy1 = y - 1;
    int dx2 = h - x - 1, dy2 = w - y;
    return (cnk(dx1 + dy1, dx1) * cnk(dx2 + dy2, dx2)) % mod;
}

void solve() {
    calcFactorials();
    int a, b;
    cin >> h >> w >> a >> b;
    ll ans = 0;
    for2(i, b + 1, w){
        ans += numberOfWays(h - a, i);
        ans %= mod;
    }
    cout << ans << '\n';
}

bool multitest = false;

int main() {
    init();
    int t = 1;
    if (multitest) cin >> t;
    forn(i, t) solve();
}


#include <bits/stdc++.h>

using namespace std;

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

	int n,m;

	cin >> n >> m;
	int l1 = 1;
	int r1 = m+1;
	int l2 = m+2;
	int r2 = 2*m+1;
	for(int i=0;i<m;i++)
	{
		if(l1 < r1)
		{
			cout << l1 << ' ' << r1 << '\n';
			l1++;
			r1--;
		}
		else if(l2 < r2)
		{
			cout << l2 << ' ' << r2 << '\n';
			l2++;
			r2--;
		}
	}
	return 0;
}

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

int main()
{
  int64_t N, M;
  vector<pair<int, int>> ans;
  cin >> N >> M;
  if (N % 2)
  {
    for (int l = 0, r = N - 1; l < r; l++, r--)
    {
      ans.emplace_back(l, r);
    }
  }
  else
  {
    bool flag = true;
    for (int l = 0, r = N - 1; l < r; l++, r--)
    {
      if (flag && r - l <= N / 2)
      {
        r--;
        flag = false;
      }
      ans.emplace_back(l, r);
    }
  }
  for (int i = 0; i < M; i++)
  {
    cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << endl;
  }
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

typedef pair<double,string> pa;
vector<pa> vec;
bool my (pa i,pa j) { return (i.first==j.first?i.second>j.second:i.first<j.first); }
int main(){
    int n;
    while(cin >>  n && n){
        while(n--){
            string na;
            double p,a,b,c,d,e,f,s,m;
            cin>>na>>p>>a>>b>>c>>d>>e>>f>>s>>m;
            vec.push_back(pa((f*s*m-p)/(a+b+c+(d+e)*m),na));
        }
        sort(vec.begin(),vec.end(),my);
        for(long long i=vec.size()-1;i>=0;i--){
            cout<<vec[i].second<<endl;
        }
        cout<<"#"<<endl;
        vec.clear();
    }
}

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

bool is_prime[10001];
int prime[10001];
int main() {
	memset(is_prime, 1, sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;
	int p = 0;
	for (int i = 2; i <= 10000; i++) {
		if (is_prime[i]) {
			prime[p++] = i;
			for (int j = i * 2; j <= 10000; j += i)is_prime[j] = false;
		}
	}
	int n;
	while (scanf("%d", &n), n) {
		int a = upper_bound(prime, prime + p, n) - prime - 1;
		for (int i = a; i > 0; i--) {
			if (prime[i] - prime[i - 1] == 2) {
				printf("%d %d\n", prime[i - 1], prime[i]);
				break;
			}
		}
	}
}


#include<iostream>
#include<vector>
using namespace std;
using ll = long long;

int main(){
    int n;
    cin>>n;
    vector<ll> c(100005);
    for(int i=0;i<n;i++){
        int d;
        cin>>d;
        c[d]++;
        if(i==0&&d!=0){
            cout<<0<<endl;
            return 0;
        }
    }
    ll ans=0;
    if(c[0]==1) ans=1;
    for(int i=1;i<n;i++){
        for(int j=0;j<c[i];j++){
            ans*=c[i-1];
            ans%=998244353;
        }
    }
    cout<<ans<<endl;
}

#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#include <random>
#include <time.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define all(c) begin(c),end(c)
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}

#define INF 1e9+7
#define LLINF 1e18+7
#define N 500000
ll MOD=1e9+7;
vec mem(20,-1);
main(){
    int n=in();
    ll x=in();
    vector<ll> b,l,r,t;
    rep(i,n){
        b.pb(in());
        l.pb(in());
        r.pb(in());
        t.pb(l[i]*b[i]+(r[i])*(x-b[i]));
    }
    vec ind;
    rep(i,n)ind.pb(i);
    sort(all(ind),[&](int y,int z){
        return t[y]>t[z];
    });
    //cout<<t[0]<<" "<<t[1]<<endl;
    ll left=-1,right=n*x;
    while(left<right-1){
        ll mid=(left+right)/2;
        ll mm=mid;
        ll tmp=0;
        rep(i,n){
            tmp-=b[i]*l[i];
        }
        int now=0;
        bool flag=false;
        while(mid>=x){
            tmp+=t[ind[now]];
            if(tmp>=0){
                flag=true;break;
            }
            mid-=x;
            now++;
        }
        if(flag){
            right=mm;continue;
        }
        ll MAX=0;
        ll T=1e11;
        rep2(j,0,now-1){
            int i=ind[j];
            if(mid>=b[i]){
                T=min(T,(x-mid)*r[i]);
            }
            else{
                T=min(T,(x-b[i])*r[i]+(b[i]-mid)*l[i]);
            }
        }
        rep2(j,now,n-1){
            int i=ind[j];
            if(mid>=b[i]){
                MAX=max(MAX,b[i]*l[i]+(mid-b[i])*r[i]);
            }
            else{
                MAX=max(MAX,l[i]*mid);
            }
            MAX=max(MAX,t[i]-T);
        }
        tmp+=MAX;
        if(tmp>=0)right=mm;
        else left=mm;
    }
    cout<<right;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template <class T>
string to_string(T s);
template <class S, class T>
string to_string(pair<S, T> p);
string to_string(string s);
string to_string(const char s[]) { return string(s); }

template <class T>
string to_string(T v) {
  if (v.empty()) return "{}";
  string ret = "{";
  auto itr = v.begin(), next = ++v.begin();
  for (; next != v.end(); ++itr, ++next) ret += to_string(*itr) + ",";
  ret += to_string(*itr) + "}";
  return ret;
}
template <class S, class T>
string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}
string to_string(string s) { return s; }

void debug() { cerr << endl; }
template <class Head, class... Tail>
void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head head, Tail... tail) {
  cout << to_string(head) << " ";
  print(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  string separator = " ";
  template <class T>
  inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus) x = -x;
  }
  inline void read(string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\n' && c != EOF);
  }
  template <class T>
  inline void read(vector<T> &v) {
    for (auto &x : v) read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &... tail) {
    read(head);
    read(tail...);
  }
  template <class T>
  inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0) putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(string x) {
    for (char c : x) putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i) putchar_unlocked(s[i]);
  }
  template <class T>
  inline void write(vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  void set_separator(string s) { separator = s; }
} io;

int main() {
  int N;
  io.read(N);
  vector<int> A(N);
  io.read(A);
  sort(ALL(A), greater<int>());
  map<int, int> mp;
  for (auto x : A) mp[x]++;
  int ans = 0;
  REP(i, N) {
    if (mp[A[i]] < 1) continue;
    REP(t, 31) {
      int x = 1 << t;
      int pad = x - A[i];
      if (mp.count(pad) && (mp[pad] > (A[i] == pad))) {
        ans++;
        mp[pad]--;
        mp[A[i]]--;
        break;
      }
    }
  }
  io.write(ans);
  io.write("\n");
  return 0;
}

#include <bits/stdc++.h>

using namespace std;
inline void prep ()
{
  cin.tie (0);
  cin.sync_with_stdio (0);
};
const double pi = acos(-1.);
//printf(%.12lf\n,mid);
// __builtin_popcount(int) count 1's in binary
// get decimal part of number
/*
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
// round = (int)(num+.5)
long long mod = 1000000007;

long long mymod(long long num){
  if (num< 0 && !(num%mod == 0)){
    if (mod == 1){
      return 0;
    }
    return mod-abs(num)%mod;
  }else{
    return num%mod;
  }
}

/*string sentence = And I feel fine...;
    istringstream iss(sentence);
*/

double eps = .000000001;

int main(){
  prep();
  int n, m;
  cin >> n >> m;
  int arr[n][m];
  for (int i=0; i<n; i++){
      for (int j=0; j<m; j++){
          cin >> arr[i][j];
          arr[i][j]--;
      }
  }
  int pointers[n] = {0};
  int cnts[m] = {0};
  int res = 0;

  for (int i=0; i<n; i++){
      cnts[arr[i][0]]++;
  }
  for (int i=0; i<m; i++){
      res = max(res, cnts[i]);
  }
  bool erased[m] = {false};
  for (int x=0; x<m-1; x++){
      int maxx = 0;
      int maxind = 0;
      for (int i=0; i<m; i++){
          if (cnts[i] > maxx){
              maxx = cnts[i];
              maxind = i;
          }
      }
      erased[maxind] = true;
      for (int i=0; i<n; i++){
          cnts[arr[i][pointers[i]]]--;
          while (erased[arr[i][pointers[i]]]){
              pointers[i]++;
          }
          cnts[arr[i][pointers[i]]]++;

      }
      maxx = 0;
      for (int i=0; i<m; i++){
          maxx = max(cnts[i], maxx);
      }
      res = min(res, maxx);
  }
  cout << res;
  return 0;
}


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<ll,ll>
#define                           node(a,b,c)                 mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=100005;
vector<int>adj[M];
int n,m;
int in[M];
int par[M];
void dfs(int u)
{
    for(auto v:adj[u])
    {
         if(in[v]==1)
         {
             par[v]=u;
         }
         else
         {
             in[v]--;
         }
    }
    for(auto v:adj[u])
    {
        if(par[v]==u)
        {
            dfs(v);
        }
    }
}
 main()

{
    fast
    cin>>n>>m;
    f(i,1,n-1+m)
    {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        in[v]++;
    }
    int root=0;
    f(i,1,n)
    {
        if(in[i]==0)root=i;
    }
    dfs(root);
    f(i,1,n)
    {
        cout<<par[i]<<"\n";
    }
    return 0;

}

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define INF bit(60)
#define pb push_back
#define mod 1000000007

using namespace std;
using uif = uint_fast64_t;
using ll = long long int;
using tTree = __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

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

#define FACSIZE 200002

ll invfac[FACSIZE];
ll fac[FACSIZE];

template <class T>
bool chmax(T &, const T &);
template <class T>
bool chmin(T &, const T &);
ll gcd(ll, ll);
ll powLL(ll x, ll y);

ll mod_pow(ll, ll);
ll mod_add(ll, ll);
ll mod_mul(ll, ll);
ll mod_div(ll, ll);
ll comb(ll, ll);
void make_fact(ll);
void make_invfact(ll);
void fact_init();

int main(void)
{
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n + m - 1);
  vector<ll> b(n + m - 1);
  rep(i, n + m - 1) cin >> a[i] >> b[i];

  vector<ll> dis(n + 1, 0);

  vector<vector<ll>> par(n + 1);
  vector<vector<ll>> child(n + 1);
  vector<ll> cnt(n + 1, 0);

  vector<ll> ans(n + 1, -1);

  rep(i, n + m - 1)
  {

    auto p = a[i];
    auto c = b[i];
    par[c].push_back(p);
    child[p].push_back(c);
  }

  ll root;
  rep1(i, n)
  {
    if (par[i].empty())
      root = i;
    cnt[i] = par[i].size();
  }

  //cout << root << endl;
  queue<pair<ll, ll>> que;
  que.push(make_pair(root, 0));
  dis[root] = 0;
  ans[root] = 0;

  while (!que.empty())
  {

    auto q = que.front();
    auto p = q.first;
    auto d = q.second;
    que.pop();
    for (auto c : child[p])
    {
      if (dis[c] < d + 1)
      {
        if (cnt[c] > 1)
        {
          cnt[c]--;
          continue;
        }
        dis[c] = dis[p] + 1;
        ans[c] = p;
        que.push(make_pair(c, dis[c]));
      }
    }
  }
  /*
  for (auto x : ans)
    cout << x << " ";
  cout << endl;
  //*/
  rep1(i, n) cout << ans[i] << endl;

  return 0;
}

ll mod_pow(ll x, ll r)
{
  if (r == 0)
    return 1;
  else if (r == 1)
    return x % mod;
  else if (r % 2 == 0)
  {
    ll t = mod_pow(x, r / 2) % mod;
    return mod_mul(t, t);
  }
  else
  {
    ll t = mod_pow(x, r / 2) % mod;
    ll k = mod_mul(t, t);
    return (k % mod) * (x % mod);
  }
}
ll mod_add(ll a, ll b)
{
  return ((a % mod) + (b % mod)) % mod;
}

ll mod_mul(ll a, ll b)
{
  return ((a % mod) * (b % mod)) % mod;
}

ll mod_div(ll a, ll b)
{
  return mod_mul(a, mod_pow(b, mod - 2));
}

void fact_init()
{
  make_fact(FACSIZE - 1);
  make_invfact(FACSIZE);
}

void make_fact(ll n)
{
  fac[0] = 1;
  rep(i, n)
  {
    fac[i + 1] = mod_mul(fac[i], i + 1);
  }
}

void make_invfact(ll n)
{
  invfac[n] = mod_pow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--)
  {
    invfac[i] = mod_mul(invfac[i + 1], i + 1);
  }
}

ll comb(ll n, ll r)
{

  return mod_mul(mod_mul(fac[n], invfac[r]), invfac[n - r]);
}
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;
}

ll qp(ll a, ll b)
{
  ll ans = 1LL;
  do
  {
    if (b & 1LL)
      ans = 1LL * mod_mul(ans, a) % mod;
    a = 1LL * mod_mul(a, a) % mod;
  } while (b >>= 1LL);
  return ans;
}
ll qp(ll a, ll b, ll mo)
{
  ll ans = 1LL;
  do
  {
    if (b & 1LL)
      ans = 1LL * (ans % mo) * (a % mo);
    a = 1LL * (a % mo) * (a % mo);
  } while (b >>= 1LL);
  return ans;
}
ll gcd(ll a, ll b)
{
  return b ? gcd(b, a % b) : a;
}
ll powLL(ll x, ll y)
{
  ll ans = 1LL;
  for (ll i = 0LL; i < y; i++)
    ans *= x;
  return ans;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
const ll mod = 1e9+7;
const ll INF = 1e18;
#define All(a) (a).begin(),(a).end()
#define Pi acos(-1)

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  chrono::system_clock::time_point start,end;
  start = chrono::system_clock::now();

  ll n;
  cin >> n;
  vector<ll> a;
  ll cnt = 0;
  if (n == 0) {
    cout << 0 << '\n';
    return 0;
  }
  for (ll i = 0; n != 0; i++) {
    a.push_back(n % -2);
    if (a[i] == -1) {
      a[i] = 1;
      n = (n-1)/-2;
    }
    else n = n / -2;
    ++cnt;
  }
  rep(i,cnt) cout << a[cnt-i-1];
  end = chrono::system_clock::now();
  auto elapsed = chrono::duration_cast< chrono::milliseconds >(end - start).count();
  cerr << elapsed << "ms" << '\n';
}

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

int n, base(-2);
string s;

int main(){
  cin >> n;
  if (n == 0) cout << "0";
  while (n != 0){
    int r = n % base;
    r += (r < 0? 2 : 0);
    s += (n % base == 0 ? "0" : "1");
    n = (n - r) / base;
  }
  reverse(s.begin(), s.end());
  cout << s << endl;
}


#include<bits/stdc++.h>
#define pir pair<int,int>
#define qmin(x,y) (x=min(x,y))
#define qmax(x,y) (x=max(x,y))
#define rsort(x,y) sort(x,y),reverse(x,y)
#define fr first
#define se second
using namespace std;
typedef long long ll;
const int Maxn=4100000;
const int inf=0x3f3f3f3f;

int n,x[Maxn],y[Maxn],dir[Maxn],bj[Maxn];
vector<pir> v[Maxn];

int getadir() {
    char ch=getchar();
    while(!isalpha(ch)) ch=getchar();
    switch(ch) {
        case 'U' : return 0;
        case 'R' : return 1;
        case 'D' : return 2;
        case 'L' : return 3;
    }
}

signed main() {
    // freopen("test.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d%d",&x[i],&y[i]);
        dir[i]=getadir();
    }
    int ans=inf;
    for(int i=1;i<=n;i++) if((dir[i]&1)==0) v[x[i]].push_back(make_pair(y[i],i)),bj[x[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int temp=0;
        for(pir j:v[i]) {
            if(dir[j.se]==2) {
                if(temp) qmin(ans,j.fr-y[temp]);
            }
            else temp=j.se;
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) if(dir[i]&1) v[y[i]].push_back(make_pair(x[i],i)),bj[y[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int temp=0;
        for(pir j:v[i]) {
            if(dir[j.se]==3) {
                if(temp) qmin(ans,j.fr-x[temp]);
            }
            else temp=j.se;
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) v[x[i]+y[i]].push_back(make_pair(x[i],i)),bj[x[i]+y[i]]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int tempx=0,tempy=0;
        for(pir j:v[i]) {
            if(dir[j.se]==0) {
                if(tempy) qmin(ans,(j.fr-x[tempy])*2);
            }
            else if(dir[j.se]==1) tempy=j.se;
            else if(dir[j.se]==2) tempx=j.se;
            else if(tempx) qmin(ans,(j.fr-x[tempx])*2);
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    for(int i=1;i<=n;i++) v[x[i]-y[i]+2000000].push_back(make_pair(x[i],i)),bj[x[i]-y[i]+2000000]=1;
    for(int i=1;i<Maxn;i++) if(bj[i]) {
        sort(v[i].begin(),v[i].end());
        int tempx=0,tempy=0;
        for(pir j:v[i]) {
            if(dir[j.se]==3) {
                if(tempy) qmin(ans,(j.fr-x[tempy])*2);
            }
            else if(dir[j.se]==0) tempy=j.se;
            else if(dir[j.se]==1) tempx=j.se;
            else if(tempx) qmin(ans,(j.fr-x[tempx])*2);
        }
    }
    for(int i=1;i<Maxn;i++) if(bj[i]) v[i].clear(),bj[i]=0;
    if(ans==inf) return 0*puts("SAFE");
    ans*=5;
    printf("%d\n",ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e16;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

const ll N = 2e5 + 10;
ll n, x[N], y[N];
char c[N];
map<ll, set<ll>> l, r, u, d;

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

    cin >> n;
    zep(i, 0, n)cin >> x[i] >> y[i] >> c[i];

    ll ans1 = INF;

    zep(i, 0, n){
        if(c[i] == 'L')l[y[i]].insert(x[i]);
        if(c[i] == 'D')u[x[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'R' && l.find(y[i]) != l.end()){
            if(l[y[i]].lower_bound(x[i]) != l[y[i]].end()){
                ans1 = min(ans1, (*l[y[i]].lower_bound(x[i])) - x[i]);
            }
        }
        if(c[i] == 'U' && u.find(x[i]) != u.end()){
            if(u[x[i]].lower_bound(y[i]) != u[x[i]].end()){
                ans1 = min(ans1, (*u[x[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    ans1 *= 5;

    while(!l.empty())l.erase(l.begin());
    while(!u.empty())u.erase(u.begin());

    ll ans2 = INF;

    zep(i, 0, n){
        if(c[i] == 'L')l[x[i] - y[i]].insert(y[i]);
        if(c[i] == 'D')d[x[i] - y[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'U' && l.find(x[i] - y[i]) != l.end()){
            if(l[x[i] - y[i]].lower_bound(y[i]) != l[x[i] - y[i]].end()){
                ans2 = min(ans2, (*l[x[i] - y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    zep(i, 0, n){
        if(c[i] == 'R' && d.find(x[i] - y[i]) != d.end()){
            if(d[x[i] - y[i]].lower_bound(y[i]) != d[x[i] - y[i]].end()){
                ans2 = min(ans2, (*d[x[i] - y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    while(!l.empty())l.erase(l.begin());
    while(!d.empty())d.erase(d.begin());

    zep(i, 0, n){
        if(c[i] == 'R')r[x[i] + y[i]].insert(y[i]);
        if(c[i] == 'D')d[x[i] + y[i]].insert(y[i]);
    }

    zep(i, 0, n){
        if(c[i] == 'U' && r.find(x[i] + y[i]) != r.end()){
            if(r[x[i] + y[i]].lower_bound(y[i]) != r[x[i] + y[i]].end()){
                ans2 = min(ans2, (*r[x[i] + y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    zep(i, 0, n){
        if(c[i] == 'L' && d.find(x[i] + y[i]) != d.end()){
            if(d[x[i] + y[i]].lower_bound(y[i]) != d[x[i] + y[i]].end()){
                ans2 = min(ans2, (*d[x[i] + y[i]].lower_bound(y[i])) - y[i]);
            }
        }
    }

    ans2 *= 10;

    ll ans = min(min(ans1, ans2), INF);
    if(ans == INF){
        print("SAFE")
    }else{
        print(ans)
    }

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int Partition(int a[],int p,int r) {
	int x = a[r];
	int i = p - 1;
	for (int j = p; j < r; j++) {
		if (a[j] <= x) {
			i++;
			int temp = a[j];
			a[j] = a[i];
			a[i] = temp;
		}
	}
	a[r] = a[i + 1];
	a[i + 1] = x;
	return i + 1;
}
int main() {
	int n = 0;
	int A[100005];
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &A[i]);
	}
	int q = Partition(A, 0, n - 1);
	for (int i = 0; i < n; i++) {
		if (i)
			printf(" ");
		if (i == q) {
			printf("[%d]", A[i]);
		}
		else {
			printf("%d", A[i]);
		}
	}
	printf("\n");
	return 0;
}

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

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

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

void add(vector<ll>& vs, ll a){
  for(ll v : vs){
    minch(a,(a^v));
  }
  if(a!=0){
    vs.push_back(a);
    sort(all(vs)); reverse(all(vs));
  }
}

bool is_contain(const vector<ll>& vs, ll a){
  for(ll v : vs){
    minch(a,(a^v));
  }
  return (a==0);
}

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

  int T;
  cin>>T;
  while(T--){
    int N;
    cin>>N;
    vector<ll> A(N);
    rep(i,N)cin>>A[i];
    string S;
    cin>>S;

    vector<ll> dp;
    dp.push_back(0);

    bool ok=true;
    for(ll i=N-1;i>=0;i--){
      if(S[i]=='0'){
        add(dp,A[i]);
      }else{
        if(!is_contain(dp,A[i]))ok=false;
      }
    }
    if(ok)cout<<"0\n";
    else cout<<"1\n";
  }

  return 0;
}


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

int numOf1s(ll bit){
    int cnt = 0;
    while(bit){
        if(bit & 1 == 1) ++cnt;
        bit = bit >> 1;
    }
    return cnt;
}

int main(){
    int N; cin >> N;
    vector<vector<P>> vec(N);
    rep(i, N){
        int a; cin >> a;
        rep(j, a){
            int x,y; cin >> x >> y;
            --x;
            vec[i].push_back(P(x,y));
        }
    }

    int result = 0;
    for(ll bit=0; bit < (1<<N); ++bit){
        bool valid = true;
        for(int i=0; i<N; ++i){
            if((bit >> i & 1) != 1) continue;
            for(auto p : vec[i]){
                if(p.second != (bit >> p.first & 1)){
                    valid = false;
                }
            }
        }
        if(valid) result = max(result, numOf1s(bit));
    }
    cout << result << endl;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cstdio>

#define REP(n) for(int i = 0;i < n;i++)
#define LLREP(n) for(long long int  i = 0;i < n;i++)
typedef long long int ll;
using namespace std;
#define MAX_A 1000000007

int main(){

    string s;
    int tmp = 0;
    while(getline(cin,s)){
        for(int i = 0;i < 26;i++){
            string tmp(s);
            for(int j = 0;j < s.length();j++){

                if(isalpha(s[j])){
                    tmp[j] = (s[j]-'a'+i)%26 + 'a';
                }
            }

            if(tmp.find("the")!=-1||tmp.find("this")!=-1||tmp.find("that")!=-1) {
                cout << tmp << endl;
                break;
            }
        }

    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
long long INF = (1LL<<62);

string X;
int main(int argc, char* argv[]){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> X;
	string s;

	for(int i=0; i<X.size(); i++){
		if(i == 0){
			s.push_back(X[i]);
		}
		else{
			if(s[s.size()-1] == 'S' && X[i] == 'T'){
				s.pop_back();
			}
			else{
				s.push_back(X[i]);
			}
		}
	}
	printf("%d\n", (int)s.size());
	return 0;
}

#include<iostream>
#include<string>
#include<numeric>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define minimize(a,b) (a>(b)?a=(b),1:0)
#define maximize(a,b) (a<(b)?a=(b),1:0)
#define ceil(a,b) (((a)+(b)-1)/(b))
#define yn(i) ((i)?"Yes":"No")
#define chmin(a,b) (a>(b)?a=(b),1:0)
#define abs(n) max((n),-(n))
#define dist(a,b) max((a)-(b),(b)-(a))
#define chmax(a,b) (a<(b)?a=(b),1:0)
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define MOD 1000000000
#define rep(i,n) for(int i=0;i<(n);i++)

#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
  auto itr=v.begin();
  while(itr!=v.end()){
    if(itr!=v.begin())cout << " ";
    cout << *(itr++);
  }
  return os;
}

string key="keyence";

void add(char m,string s,set<string> st){
  int n=s.size();
  rep(i,n){
    if(s[i]!=m) continue;
    string t;
    repa(j,i,n){
      t.push_back(s[j]);
      st.insert(t);
    }
  }
}

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

int main(){
  string s; cin >>s;
  int d=0,ans=0,n=s.size();
  rep(i,n){
    if(s[i]=='S'){
      d+=1; continue;
    }
    if(d>0){
      d-=1; continue;
    }
    ans++;
  }
  print(ans+d);
}


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

string hands( const vector<int>& c ) {
	int n = (int)c.size();

	int p1 = 0;
	int c3 = 0;

	for( int i = 0; i < n; ++i ) {
		if( c[i] == 4 ) {
			return "four card";
		}
		else if( c[i] == 3 ) { c3 += 1; }
		else if( c[i] == 2 ) { p1 += 1; }
	}

	if( c3 > 0 && p1 > 0 ) {
		return "full house";
	}
	else if( c3 > 0 ) {
		return "three card";
	}
	else if( p1 == 2 ) {
		return "two pair";
	}
	else if( p1 == 1 ) {
		return "one pair";
	}

	int x = 0;
	for( int i = 0; i <= n; ++i ) {
		x = (c[i%n] == 1) ? (x+1) : 0;

		if( x == 5 ) { break; }
	}

	return x == 5 ? "straight" : "null";
}

int main() {
	ios_base::sync_with_stdio( false );

	string line;
	while( getline( cin, line ) ) {
		replace( line.begin(), line.end(), ',', ' ' );

		istringstream iss( line );
		vector<int> cnt( 13 );
		int cd;
		while( iss >> cd ) { cnt[cd-1] += 1; }

		cout << hands( cnt ) << '\n';
	}

	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;

//定数
//円周率
const double pi = 3.141592653589793238462643383279;
//天井
const int INF = 1000000000; // = 10^9
const ll LINF = 100000000000000000; // = 10^17
//ABC文字列
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZABC";
const string abc = "abcdefghijklmnopqrstuvwxyzabc";
//よくあるmodくん
const ll MOD = 1000000007; // = 10^9 + 7
//前処理テーブルの大きさ指定
const int MAX = 1200000; // = 10^6 + 2*(10^5)
//ちなみに、1024MBで持てる最大長の配列は10^8程度

//データ構造
//隣接リスト用構造体（有向グラフ向け）
struct edge {
  ll to; // E.toでその辺の終点へアクセスできる。
  ll cost; // e.costでその辺の重みにアクセスできる。
};
//Union_Find木
struct UnionFind {
  vector<int> UF; // UF.at(i) : iの親の番号
  vector<int> SIZE; // SIZE.at(root(i)) : iと連結されてる要素の数
  UnionFind(int N) : UF(N), SIZE(N, 1) { // 最初は全てが根であるとして初期化
    for(int i = 0; i < N; i++) {
      UF.at(i) = i;
    }
  }
  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (UF.at(x) == x) {
      return x;
    }
    return UF.at(x) = root(UF.at(x));
  }
  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x); // xの根をrx
    int ry = root(y); // yの根をry
    if (rx == ry) {
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    }
    // xとyの根が同じでない(=同じ木にない)時：小さい方の根を大きい方の根につける。
    if (SIZE.at(rx) < SIZE.at(ry)) {
      UF.at(rx) = ry;
      SIZE.at(ry) += SIZE.at(rx);
      SIZE.at(rx) = 0;
    }
    else {
      UF.at(ry) = rx;
      SIZE.at(rx) += SIZE.at(ry);
      SIZE.at(ry) = 0;
    }
  }
  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す。
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
  int size(int x) { // xと連結されてる要素の数を返す。
    return SIZE.at(root(x));
  }
};

//関数 (計算量)
//ctoi (O(1))
int ctoi(char c){
  if (c == '0') return 0;
  if (c == '1') return 1;
  if (c == '2') return 2;
  if (c == '3') return 3;
  if (c == '4') return 4;
  if (c == '5') return 5;
  if (c == '6') return 6;
  if (c == '7') return 7;
  if (c == '8') return 8;
  if (c == '9') return 9;
  return -1;
}
//to_char (O(1))
char to_char(int i){
  if (i == 0) return '0';
  if (i == 1) return '1';
  if (i == 2) return '2';
  if (i == 3) return '3';
  if (i == 4) return '4';
  if (i == 5) return '5';
  if (i == 6) return '6';
  if (i == 7) return '7';
  if (i == 8) return '8';
  if (i == 9) return '9';
  return ' ';
}

// A^N mod M を計算する。modしたくなかったらM = LINFとかにすればよい。(O(√N))
ll modpow(ll A, ll N, ll M) {
  ll ans = 1;
  while (N > 0) {
    if (N & 1) ans = ans * A % M;
      A = A * A % M;
      N >>= 1;
   }
  return ans;
}

//メイン処理
int main() {
  cout << fixed << setprecision(16); //精度向上
  //ここまでテンプレ
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<vector<ll>> DP(N, vector<ll>(2, -LINF));
  DP.at(0).at(0) = A.at(0);
  for (int i = 1; i < N; i++) {
    DP.at(i).at(0) = max(DP.at(i - 1).at(0), DP.at(i - 1).at(1)) + A.at(i);
    DP.at(i).at(1) = max(DP.at(i - 1).at(0) - 2 * A.at(i - 1) - A.at(i),
                         DP.at(i - 1).at(1) + 2 * A.at(i - 1) - A.at(i));
  }
  cout << max(DP.at(N - 1).at(0), DP.at(N - 1).at(1)) << endl;
}

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

using namespace std;

int main(void){
  int N;
  cin >> N;
  int A[N];
  rep(i, N)
    cin >> A[i];
  // 入力終了

  // A[]中の負の数の数
  int negative_num = 0;
  rep(i, N)
    if(A[i] < 0)
      negative_num++;

  long long ans = 0;

  // 負の数が偶数個なら，操作の繰り返しで全て正にできる
  if(negative_num % 2 == 0){
    rep(i, N)
      ans += abs(A[i]);
  }
  // 奇数個なら，1つを残して全て正にできる
  // 絶対値が一番小さいものを負にしておけばOK
  else{
    // 絶対値が一番小さいものを探す
    int abs_min = 1000000000;
    rep(i, N)
      if(abs(A[i]) < abs_min)
	abs_min = abs(A[i] );

    // 絶対値が最小のもの以外は正で足す
    // 絶対値が最小のものは負で足す
    rep(i, N)
      if(abs(A[i]) != abs_min)
	ans += abs(A[i] );
      else
	ans -= abs(A[i] );
  }

  // 解答
  cout << ans << endl;

  return 0;
}


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

struct Edge {
	int src, dst;
	Edge() : src(0), dst(0) {}
	Edge(int s, int d) : src(s), dst(d) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;

vector<int> kosaraju(const Graph &g) {
	int n = g.size(), sz = 0; Graph rg(n);
	vector<int> stk, cmp(n, -1), added(n), visited(n), ord(n);
	for(auto &es : g) {
		sz += es.size();
		for(const Edge &e : es) rg[e.dst].emplace_back(e.dst, e.src);
	}
	stk.resize(n + sz); sz = 0;
	for(int i = 0; i < n; i++) {
		if(visited[i]) continue;
		int s = 0; stk[s++] = i;
		while(s != 0) {
			int v = stk[s - 1]; visited[v] = true;
			bool pushed = false;
			for(auto &e : g[v]) {
				int dst = e.dst;
				if(!visited[dst]) { stk[s++] = dst; pushed = true; }
			}
			if(pushed) continue;
			int t = stk[s - 1];
			if(!added[t]) { added[t] = true; ord[n - ++sz] = t; }
			--s;
		}
	}
	int k = 0;
	for(int &u : ord) {
		if(cmp[u] != -1) continue;
		int s = 0; stk[s++] = u;
		while(s != 0) {
			int v = stk[--s]; cmp[v] = k;
			for(auto &e : rg[v]) {
				int d = e.dst;
				if(cmp[d] == -1) stk[s++] = d;
			}
		}
		++k;
	}
	return cmp;
}

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

	int V, E, Q;
	cin >> V >> E;

	Graph G(V);
	for(int i = 0; i < E; i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back({ u, v });
	}

	auto res = kosaraju(G);
	cin >> Q;
	while(Q--) {
		int u, v;
		cin >> u >> v;
		cout << (res[u] == res[v]) << endl;
	}
}

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> G[1000000];
vector<pair<int, int> > bridge;
vector<int> articulation;
int ord[1000000], low[1000000];
bool vis[1000000];

void dfs(int v, int p, int &k)
{
	vis[v] = true;

	ord[v] = k++;
	low[v] = ord[v];

	bool isArticulation = false;
	int ct = 0;

	for (int i = 0; i < G[v].size(); i++){
		if (!vis[G[v][i]]){
			ct++;
			dfs(G[v][i], v, k);
			low[v] = min(low[v], low[G[v][i]]);
			if (~p && ord[v] <= low[G[v][i]]) isArticulation = true;
			if (ord[v] < low[G[v][i]]) bridge.push_back(make_pair(min(v, G[v][i]), max(v, G[v][i])));
		}
		else if (G[v][i] != p){
			low[v] = min(low[v], ord[G[v][i]]);
		}
	}

	if (p == -1 && ct > 1) isArticulation = true;
	if (isArticulation) articulation.push_back(v);
}

int main()
{
	int n, m;

	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++){
		int x, y;
		scanf("%d %d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}

	int k = 0;
	for (int i = 0; i < n; i++){
		if (!vis[i]) dfs(i, -1, k);
	}

	sort(bridge.begin(), bridge.end());
	sort(articulation.begin(), articulation.end());

	//printf("%d\n", articulation.size());

	for (int i = 0; i < articulation.size(); i++)
		printf("%d\n", articulation[i]);

	//printf("%d\n", bridge.size());

	/*for (int i = 0; i < bridge.size(); i++)
		printf("%d %d\n", bridge[i].first + 1, bridge[i].second + 1);
	*/

	return (0);
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
const int N = 4e6 + 10, M = 2e3 + 10, MOD = 1e9 + 7;
int fac[N], inv[N], dp[M][M], ps[M][M];

int sum(int a, int b) {return (a + b) % MOD;}
int mul(int a, int b) {return (a * b) % MOD;}
int Pow(int a, int b)
{
    int res = 1;
    for (; b; b>>=1, a = mul(a, a)) if (b & 1) res = mul(res, a);
    return res;
}

int C(int n, int k)
{
    if (k < 0 || n < 0 || k > n) return 0;
    return mul(fac[n], mul(inv[k], inv[n - k]));
}

int32_t main()
{
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = mul(fac[i - 1], i);
    inv[N - 1] = Pow(fac[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--) inv[i] = mul(inv[i + 1], i + 1);
    int n, k; cin >> n >> k;
    if (k == 1) return cout << "1\n", 0;
    ps[1][1] = dp[1][1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i][1] = mul(ps[i - 1][1], C(i * k - 2, k - 2));
        for (int j = 2; j <= i; j++) {
            dp[i][j] = mul(ps[i - 1][j - 1], C(i * k - j - 1, k - 2));
        }
        for (int j = i; j >= 1; j--) ps[i][j] = sum(ps[i][j + 1], dp[i][j]);
    }
    cout << mul(ps[n][1], fac[n]) << "\n";
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<typename T>
void read(T &x)
{
    x=0;char c=getchar();bool p=0;
    for(;c<'0'||c>'9';c=getchar())if(c=='-')p=1;
    for(;c>='0'&&c<='9';c=getchar())x=(x<<3)+(x<<1)+(c^48);
    if(p)x=-x;
}
const ll MOD=1000000000+7;
ll f[2005][2005];
ll fac[4000005],inv[4000005];
int n,k;
ll ksm(ll basic,ll mi)
{
    ll tmp=1;
    while(mi)
    {
        if(mi&1)tmp=tmp*basic%MOD;
        mi>>=1;
        basic=basic*basic%MOD;
    }
    return tmp;
}
ll C(ll x,ll y)
{
    return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int main()
{
    read(n),read(k);
    fac[0]=inv[0]=1;
    for(ll i=1;i<=n*k;++i)fac[i]=fac[i-1]*i%MOD;
    inv[n*k]=ksm(fac[n*k],MOD-2);
    for(ll i=n*k-1;i;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    if(k==1)
    {
        printf("1\n");
        return 0;
    }
    f[0][0]=1;
    for(int i=1;i<=n;++i)
    {
        f[i][0]=1;
        for(int j=1;j<=i;++j)
        {
            f[i][j]=(f[i][j]+f[i-1][j]+1ll*f[i][j-1]*(n-j+1)%MOD*C(n*k-i-(j-1)*(k-1)-1,k-2)%MOD)%MOD;
        }
    }
    printf("%lld\n",f[n][n]);
    return 0;
}


#include <iostream>
#include <iomanip>
#include <string>

typedef long long ll;
using namespace std;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define repi(i, n, init) for(ll i=init;i<(n);i++)

int main() {
    string s;
    cin >> s;

    if (s == "zyxwvutsrqponmlkjihgfedcba") {
        cout << -1 << endl;
        return 0;
    }

    bool c[26] = {false};
    int n = s.size();
    repi(i, n, 0) {
        c[s[i] - 'a'] = true;
    }
    if (n == 26) {
        for (int i = n - 1; i >= 0; i--) {
            bool flag = false;
            repi(j, 26, s[i] - 'a') {
                if (!c[j]) {
                    if (i + 1 != n) {
                        s.erase(i);
                    }
                    s.push_back('a' + j);
                    flag = true;
                    break;
                }
            }
            if (flag)break;
            c[s[i] - 'a'] = false;
            s.erase(i);
        }
    } else {
        for (int i = n - 1; i >= 0; i--) {
            bool flag = false;
            repi(j, 26, 0) {
                if (!c[j]) {
                    s.push_back('a' + j);
                    flag = true;
                    break;
                }
            }
            if (flag)break;
        }
    }

    cout << s << endl;
    return 0;
}

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

int main() {
  string s;
  int alp[27];
  for (int i = 0; i < 26; i++) {
    alp[i] = 0;
  }
  alp[26] = 1;

  cin >> s;
  if (s == "zyxwvutsrqponmlkjihgfedcba") {
    puts("-1");
  } else {
    string ans = "";
    for (int i = 0; i < (int)s.size(); i++) {
      alp[s[i] - 'a']++;
    }

    if (s.size() < 26) {
      for (int i = 0; i < 26; i++) {
        if (alp[i] == 0) {
          ans = s + (char)('a' + i);
          break;
        }
      }
    } else {
      string s_next = s;
      next_permutation(s_next.begin(), s_next.end());
      for (int i = 0; i < (int)s.size(); i++) {
        ans += s_next[i];
        if (s[i] != s_next[i]) break;
      }
    }
    cout << ans << endl;
  }

  return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
#define int long long
using namespace std;
const int N=1e5+100;
int n,m,k,a[N],b[N],f[N],c[N];
int fa[N][64],sum[N];
signed main()
{
	scanf("%lld",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&f[i]);
	scanf("%lld%lld",&m,&k);
	for (int i=1;i<=m;i++) scanf("%lld",&a[i]);
	for (int i=1;i<=n;i++) b[i]=i;
	for (int i=1;i<=m;i++) swap(b[a[i]],b[a[i]+1]);
	for (int i=1;i<=n;i++) fa[i][0]=b[i];
	for (int j=1;j<=62;j++)
	{
		for (int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];
	}
	for (int i=1;i<=n;i++)
	{
		int cnt=k,x=i;
		for (int j=62;j>=0;j--)
		{
			if (cnt>=(1ll<<j))
			{
				cnt-=(1ll<<j);
				x=fa[x][j];
			}
		}
		if (x==1) c[i]=f[x];
		else c[i]=f[x]-f[x-1];
	}
	for (int i=1;i<=n;i++) sum[i]=sum[i-1]+c[i];
	for (int i=1;i<=n;i++)
	{
		double ans=sum[i];
		printf("%.12lf\n",ans);
	}
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair<int, int>
#define int long long
using namespace std;

const int INF = 1e9;
int n, m;
int ans = INF;
int a[85][85], b[85][85];
bool f[85][85][52000];

void attempt(int i, int j, int diff)
{

    if (i == n && j == m)
    {
        ans = min(ans, abs(diff + a[i][j] - b[i][j]));
        ans = min(ans, abs(diff - a[i][j] + b[i][j]));
        return;
    }
    if (f[i][j][diff])
        return;
    f[i][j][diff] = 1;

    if (i < n){
        attempt(i+1, j, abs(diff + a[i][j] - b[i][j]));
        attempt(i+1, j, abs(diff - a[i][j] + b[i][j]));
    }
    if (j < m){
        attempt(i, j+1, abs(diff + a[i][j] - b[i][j]));
        attempt(i, j+1, abs(diff - a[i][j] + b[i][j]));
    }

}

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

//    freopen("in.inp", "r", stdin);
//    freopen("out.out", "w", stdout);

    cin >> n >> m;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> b[i][j];

    attempt(1, 1, 0);
    cout << ans;
}


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

int main() {
    int a, b;
    cin >> a >> b;
    int cnt = 0;

    for (int i = a; i <= b; i++) {
        if (i%10==i/10000 && i/10%10==i/1000%10) {
            cnt += 1;
        }
    }
    cout << cnt << endl;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

namespace IO {
    template <typename _T>
    inline bool read(_T& x) {
        x = 0;
        _T y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-')
                y = -1;
            c = getchar();
        }
        if (c == EOF)
            return false;
        while ('0' <= c && c <= '9')
            x = x * 10 + c - '0', c = getchar();
        x *= y;
        return true;
    }

    template <typename _T>
    inline _T input() {
        _T x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-')
                y = -1;
            c = getchar();
        }
        if (c == EOF)
            return 0;
        while ('0' <= c && c <= '9')
            x = x * 10 + c - '0', c = getchar();
        x *= y;
        return x;
    }
};  // namespace IO
using namespace IO;

namespace modular {
    const int MOD = 1000000007;

    inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }

    inline void inc(int& x, int y) { (x += y) >= MOD ? x -= MOD : 0; }

    inline int mul(int x, int y) { return 1LL * x * y % MOD; }

    inline int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x))
            if (y & 1)
                ans = mul(ans, x);
        return ans;
    }
};  // namespace modular

#define MAX_N 2000007
#define reg register
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
typedef long long ll;

int N;
char s[MAX_N], t[MAX_N];
std::queue<int> q;

inline void solve() {
    int j = N, tag = 0, ans = 0, las = N + 1;
    irep(i, N, 1) {
        while (j && (s[j] != t[i] || j > i))
            j--;
        if (!j)
            return puts("-1"), void();
        if (las == j) {
            while (!q.empty() && q.front() - tag >= i)
                q.pop();
        } else {
            tag++;
            if (i != j) {
                while (!q.empty() && q.front() - tag > i)
                    q.pop();
                q.push(j + tag);
                ans = std::max(ans, (int)q.size());
            }
        }
        las = j;
    }
    printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
    FIN("in");
#endif
    read(N);
    scanf("%s%s", s + 1, t + 1);
    rep(i, 1, N) if (s[i] != t[i]) {
        solve();
        return 0;
    }
    puts("0");
    return 0;
}

#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
void read(int&a) {
	a = 0; char k = getchar(); int f = 1;
	for(;!isdigit(k); k = getchar()) if (k == '-') f = -1;
	for(; isdigit(k); k = getchar()) a = a * 10 + k - '0';
	a *= f;
}
const int N = 1e6 + 5;
int n, a[N], b[N], flag = true;
char s[N];
queue<int> Q;
int main()
{
	scanf("%d", &n);
	scanf("%s", s); for(int i = 0; i < n; ++i) a[i] = s[i] - 'a' + 1;
	scanf("%s", s); for(int i = 0; i < n; ++i) b[i] = s[i] - 'a' + 1, flag &= b[i] == a[i];
	int s = n - 1, t = n - 1;
	if(flag) {
		printf("0\n");
		return 0;
	}
	int ans = 1;
	while(t >= 0) {
		while(t > 0 && b[t] == b[t - 1]) t -= 1;
		while(s >= 0 && (s > t || a[s] != b[t])) s -= 1;
		if(s < 0) {
			printf("-1\n");
			return 0;
		}
		while(Q.size() && Q.front() - Q.size() >= t) Q.pop();
		if(s != t) Q.push(s);
		ans = max(ans, (signed)Q.size() + 1);
		--t;
	}
	printf("%d\n", ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;

int h,w,n;

template<typename int_t>
void readx(int_t &x)
{
	x=0; int_t k=1; char ch=0;
	while (ch<'0' || ch>'9') { ch=getchar(); if (ch=='-') k=-1; }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	x*=k;
}

int main()
{
	readx(h); readx(w); readx(n);
	if (h<w) swap(h,w);
	printf("%d\n", (n/h) + (bool)(n%h) );
	return 0;
}

#include<stdio.h>

int main(){

	int h, w, n, ans;

	scanf("%d", &h);
	scanf("%d", &w);
	scanf("%d", &n);

	if(w>=h){
		if(n%w == 0){
			ans = n/w;
		}else if(n%w != 0){
			ans = n/w+1;
		}

	}else if(w<=h){
		if(n%h == 0){
			ans = n/h;
		}else if(n%h != 0){
			ans = n/h+1;
		}
	}
	printf("%d", ans);
}


#include <bits/stdc++.h>
using namespace std;
int getDivCount(int);

int main()
{
    int N, A, tmp = 0;
    int res = 1;
    cin >> N;
    for (int i = 0; i < N; ++i)
    {
        cin >> A;
        tmp = getDivCount(A);
        if (tmp == 0)
        {
            cout << tmp << endl;
            return 0;
        }
        res = i == 0 ? tmp : res > tmp ? tmp : res;
    }
    cout << res << endl;
}
int getDivCount(int num)
{
    int count = 0;
    while (num % 2 == 0)
    {
        num /= 2;
        ++count;
    }
    return count;
}

#include <iostream>
#include <cstdio>

using namespace std;

int main(void){
  int copy,n;
  cin >>n;
  for(int i=1;i<=n;i++){
	  if(i%3==0){
		cout << " " << i;
		continue;
	  }
	  copy=i;
	  while(1){
		  if (copy%10==3){
			  cout << " " << i;
			  break;
		  }
		  copy/=10;
		  if(copy!=0)
			  continue;
		  else
			  break;
	  }
  }
  cout << endl;
  return 0;
}


#include<bits/stdc++.h>
#define mo 1000000007
#define ny 499122177
#define maxn 1000000000000000000LL
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read(){
    long long xx=0,flagg=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')
        ch=getchar();
    if(ch=='-'){
        flagg=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        xx=xx*10+ch-'0';
        ch=getchar();
    }
    return xx*flagg;
}
void pus(long long xx,long long flagg){
    if(xx<0){
        putchar('-');
        xx=-xx;
    }
    if(xx>=10)
        pus(xx/10,0);
    putchar(xx%10+'0');
    if(flagg==1)
        putchar(' ');
    if(flagg==2)
        putchar('\n');
    return;
}
int n,m,d1,d2,i,j,flag,cnt,book[605][605];
void ss(int x){
	flag=0;
	while((x&3)==0){
		x>>=2;
		flag++;
	}
	if(x&1){
		for(i=0;i<m;i++)
			for(j=0;j<m;j++)
				if(((i>>flag)+(j>>flag))&1)
					book[i][j]=1;
	}
	else{
		for(i=0;i<m;i++)
			for(j=0;j<m;j++)
				if((i>>flag)&1)
					book[i][j]=1;
	}
}
int main(){
//	freopen("number.in","r",stdin);
//	freopen("number.out","w",stdout);
	n=read();d1=read();d2=read();
	m=n*2;
	ss(d1);
	ss(d2);
	for(i=0;i<m;i++)
		for(j=0;j<m;j++){
			if(book[i][j]==0){
				pus(i,1);
				pus(j,2);
				cnt++;
			}
			if(cnt==n*n)
				return 0;
		}
  	return 0;
}

#include <iostream>
#include <fstream>
using namespace std;
const int nmax=100005;
int p[nmax],viz[nmax],cyc[nmax],newp[nmax];
long long x[nmax],dif[nmax];
long long k,nr;
int n,i,j,xx,m;
int main()
{
    //freopen("data.in","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>x[i];
    }
    for(i=1;i<n;i++)
        dif[i]=(x[i+1]-x[i]),p[i]=i;
    cin>>m>>k;
    for(i=1;i<=m;i++)
    {
        cin>>xx;
        swap(p[xx-1],p[xx]);
    }
    for(i=1;i<n;i++)
        if(!viz[i])
    {
        nr=0;xx=i;
        while(!viz[xx])
        {
            viz[xx]=1;
            cyc[nr++]=xx;
            xx=p[xx];
        }
        long long md=(k)%nr;
        for(j=0;j<nr;j++)
            newp[cyc[j]]=(cyc[(j+md)%nr]);
    }
    for(i=1;i<=n;i++)
    {
        if(i>1)x[i]=x[i-1]+dif[newp[i-1]];
        cout<<x[i]<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct edge{
	int y,nxt,v;
}e[2000010];
set<int>c[2000010];
int n,m,head[2000010],dis[2000010],vis[2000010],tot;
void add(int x,int y,int v){
	e[++tot]=edge{y,head[x],v};
	head[x]=tot;
}
void dijkstra(){
	priority_queue<pair<int,int> >q;
	memset(dis,0x3f3f3f3f,sizeof(dis));
	dis[1]=0; q.push(make_pair(0,1));
	while(q.size()){
		int x=q.top().second; q.pop();
		if(vis[x]) continue; vis[x]=1;
//		cout<<x<<':'<<dis[x]<<endl;
		for(int i=head[x];i;i=e[i].nxt){
			int y=e[i].y,v=c[x].count(e[i].v)?0:1;
//			cout<<"y = "<<y<<endl;
			if(dis[x]+v<dis[y]){
				dis[y]=dis[x]+v;
				q.push(make_pair(-dis[y],y));
				c[y].clear(); c[y].insert(e[i].v);
			}
			else if(dis[x]+v==dis[y]) c[y].insert(e[i].v);
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int x,y,v;
		scanf("%d%d%d",&x,&y,&v);
		add(x,y,v); add(y,x,v);
	}
	dijkstra();
//	cout<<0x3f3f3f3f3f<<endl;
	if(dis[n]==0x3f3f3f3f) printf("-1");
	else printf("%d",dis[n]);
	return 0;
}

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

int main() {
    // 入力
    int N, Q;
    cin >> N >> Q;
    vector<int> S(N), T(N), X(N);
    for (int i = 0; i < N; i++) {
        cin >> S.at(i) >> T.at(i) >> X.at(i);
    }
    vector<int> D(Q);
    for (int i = 0; i < Q; i++) {
        cin >> D.at(i);
        D[i] = D[i];
    }

    // イベント
    vector<tuple<int, int, int>> event;
    for (int i = 0; i < N; i++) {
        event.push_back(make_tuple(S[i] - X[i], 1, X[i]));   // 通行止め開始イベント
        event.push_back(make_tuple(T[i] - X[i], -1, X[i]));  // 通行止め終了イベント
    }
    sort(event.begin(), event.end());  // 時系列順に並べる

    set<int> se;  // 現在出発した人が当たる可能性のある通行止めの情報を保存する set

    vector<int> ans(Q);
    int t = 0;
    int id = 0;
    for (int i = 0; i < Q; i++) {
        t = D[i];
        for (; id < (int)event.size() && get<0>(event[id]) <= t; id++) {
            if (get<1>(event[id]) == 1) {
                se.insert(get<2>(event[id]));
            } else {
                se.erase(get<2>(event[id]));
            }
        }
        if (se.empty()) {
            ans[i] = -1;
        } else {
            ans[i] = *se.begin();
        }
    }

    for (int i = 0; i < Q; i++) {
        cout << ans[i] << endl;
    }

    return 0;
}


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

int main(){
	int d=0;
	while(cin>>d){
		int sum=0;

		for (int i = 0; i < 600; i=i+d) {
		sum+=i*i*d;
	}
	cout<<sum<<endl;
	}
}

#include <bits/stdc++.h>

//const static double	de_PI	= 3.14159265358979323846;
//const static int	de_MOD	= 1000000007;
//const static int	de_MAX	= 999999999;
//const static int	de_MIN = -999999999;

int main(void) {

	//std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());

	double n = 0;
	std::vector<double> ans;

	while (std::cin >> n) {
		double sum = 0;
		double div = 600 / n;
		for (int i = 1; i < div; i++) {
			sum += n*(n*i)*(n*i);
		}
		ans.push_back(sum);
	}

	std::for_each(ans.begin(), ans.end(), [](int i) {std::cout << i << std::endl; });
}