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

int main() {
    double n, m;
    cin >> n >> m;
    vector<int> L(m), R(m);
    rep(i, m) cin >> L[i] >> R[i];
    int maxi = *max_element(L.begin(), L.end());
    int mini = *min_element(R.begin(), R.end());
    if(mini < maxi) {
        cout << 0 << endl;
    } else {
        cout << mini - maxi + 1 << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N=200000+10;
const int INF=10000000;
int p1[N],p2[N];
map<pair<int,int>,int>m;
int find(int x,int *p)
{
    return p[x]==x?p[x]:p[x]=find(p[x],p);
}
void cha(int x,int y,int *p)
{
    int px=find(x,p);
    int py=find(y,p);
    if(px!=py)
    {
        p[px]=py;
    }
}
int main()
{
    int n,k,l;
    cin>>n>>k>>l;
    for(int i=1;i<=n;i++)
        p1[i]=i,p2[i]=i;
    for(int i=1;i<=k;i++)
    {
        int x,y;
        scanf("%d%d,",&x,&y);
        cha(x,y,p1);
    }
    for(int i=1;i<=l;i++)
    {
        int x,y;
        scanf("%d%d,",&x,&y);
        cha(x,y,p2);
    }
    for(int i=1;i<=n;i++)
    {
        find(i,p1);
        find(i,p2);
    }
    for(int i=1;i<=n;i++)
        m[make_pair(p1[i],p2[i])]++;
    for(int i=1;i<=n;i++)
        printf("%d ",m[make_pair(p1[i],p2[i])]);
    return 0;
}


#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

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

    int r;
    cin >> r;
    if (r < 1200) {
        cout << "ABC\n";
    } else if (r < 2800) {
        cout << "ARC\n";
    } else {
        cout << "AGC\n";
    }

    return 0;
}

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

int main() {
    string s; cin >> s;
    if (s == "SSS") {
        cout << 0 << endl;
        return 0;
    }
    int ans = 1;
    if (s[0] == 'R' && s[1] == 'R') ans++;
    if (s[1] == 'R' && s[2] == 'R') ans++;
    cout << ans << endl;
}


#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

#include <set>
#include <list>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>

#include <algorithm>
#include <utility>

using namespace std;
namespace patch
{
    template <typename T>
    string to_string(const T& n)
    {
        ostringstream stm;
        stm << n;
        return stm.str();
    }
    int stoi(string str)
    {
        int ret;
        istringstream stm(str);
        stm >> ret;
        if (stm.fail())
        {
            throw -1;
        }
        return ret;
    }
    vector<string> split(const string &str)
    {
        istringstream stm(str);
        vector<string> ret;
        copy(istream_iterator<string>(stm),
            istream_iterator<string>(),
            back_inserter(ret));
        return ret;
    }
    vector<string> split(const string &str, char delim)
    {
        istringstream stm(str);
        vector<string> ret;
        string buf;
        while (getline(stm, buf, delim))
        {
            ret.push_back(buf);
        }
        return ret;
    }
}

int bubbleSort(vector<int>& vs)
{
    int end = vs.size();
    if (end == 1)
    {
        return 0;
    }
    int cnt = 0;
    for (int j = 0; j < end - 1; ++j)
    {
        for (int i = 0; i < end - j - 1; ++i)
        {
            if (vs[i] > vs[i+1])
            {
                swap(vs[i], vs[i+1]);
                ++cnt;
            }
        }
    }
    return cnt;
}

// bubble sort
int main()
{
    ios::sync_with_stdio(false);

    // line 1
    string s1;
    getline(cin, s1);
    int n = patch::stoi(s1);

    // line 2
    vector<int> vs(n);
    vector<string> ss;
    string s2;
    getline(cin, s2);
    ss = patch::split(s2);
    for (int n = 0, sz = ss.size(); n < sz; ++n)
    {
        vs[n] = patch::stoi(ss[n]);
    }

    int cnt = bubbleSort(vs);

    bool isFirst = true;
    for (int n : vs)
    {
        //cout << n << " ";
        if (isFirst)
        {
            cout << n;
            isFirst = false;
        }
        else
        {
            cout << " " << n;
        }
    }
    cout << "\n";
    cout << cnt << "\n";

    return 0;
}

#include <iostream>

using namespace std;

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

int bubblesort(int *p, int n)
{
    int i, v;
    bool flag = true;
    int count = 0;
    while (flag)
    {
        flag = false;
        for (i = n - 1; i >= 1; i--)
        {
            if (p[i] < p[i - 1])
            {
                v = p[i];
                p[i] = p[i - 1];
                p[i - 1] = v;
                count++;
                flag = true;
            }
        }
    }
    return count;
}

int main(void)
{
    int A[100];
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) cin >> A[i];
    int count = bubblesort(A, N);
    print(A, N);
    cout << count << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <cctype>
#include <map>
#include <limits>
#include <iomanip>

using namespace std;

const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define ll long long

int main()
{
	double r;
	cin >> r;

	double result = r * 2 * M_PI;

	cout << result << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
       string s;
       cin>>s;
       reverse  (s.begin(),s.end());
       cout<<s<<endl;

    return 0;
}


#include <stdio.h>

int main() {
    int i, k, v,an1,an2,suu;
    int MAX_V = 10000;
    int prime[MAX_V+1];
    for(i = 0; i <= MAX_V; i++) {
        prime[i] = 1;
    }
    for(i = 2; i*i <= MAX_V; i++) {
        if(prime[i]) {
            for(k = 2 * i; k <= MAX_V; k += i) {
                prime[k] = 0;
            }
        }
    }
   while(1){
        scanf("%d",&suu);
        if(suu != 0){
            for(k=2;k<=suu;k++){
            	if(prime[k-2]==1 && prime[k]==1){
        	    	an1=k-2;
        	    	an2=k;
            	}
            }
            printf("%d %d\n",an1,an2);
        }
        else{
    	    break;
        }
    }
    return 0;
}

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

vector<vector<string>>boards={
{},{},{},
{
"aab",
"c.b",
"cdd"
},
{
"abcc",
"abdd",
"eefg",
"hhfg",
},
{
"abc..",
"abc..",
"deeff",
"d.ghh",
"iigjj"
},
{
"abc...",
"abc...",
"d.eeff",
"dghh..",
".g.ijj",
"kk.ill",
},
{
"abc....",
"abc....",
"def....",
"def....",
"g..hhii",
"g..jjkk",
".llmmnn",
}
};

int n;
char ans[2000][2000];

void print(int x,int y,int d){
	rep(i,d)rep(j,d){
		ans[x+i][y+j]=boards[d][i][j];
	}
}
void answer(){
	rep(i,n)printf("%s\n",ans[i]);
	exit(0);
}

int main(){
	cin>>n;
	if(n==2){puts("-1");return 0;}
	rep(i,n)rep(j,n)ans[i][j]='.';
	if(n==3){
		print(0,0,3);
		answer();
	}
	int x=0;
	while(1){
		if(n-x>=8){
			print(x,x,4);
			x+=4;
		}
		else{
			print(x,x,n-x);
			answer();
		}
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
typedef long long ll;
const int INF = 1e9+1;
const int MOD = 2e9 + 9;
const ll LINF = 1e18;
using namespace std;

int main(){
	string s, t;
	cin >> s >> t;
	cout << t + s << endl;
	return 0;
}

#include <stdio.h>

int main() {
    char aa[105], bb[105];
    scanf("%s %s", aa, bb);
    printf("%s%s", bb, aa);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int A, B, C;
    cin >> A >> B >> C;

    if ((A - C) * (B - C) < 0) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}

#include <iostream>
#include <iomanip>
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()
{

    int a, b, c;
    cin >> a >> b >> c;
    cout << (a <= c && c <= b || b <= c && c <= a ? "Yes" : "No") << endl;
    return 0;
}


/*
g++ --std=c++17 A.cpp
*/

#include <iostream>
#include <iomanip> //! for setprecision(20)
#include <math.h>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <limits.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

typedef long long LL;

#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; }
#define dumpMap(m) { for (auto it: m) cout << it.first << "=>" << it.second << ' '; }

const int MOD = 1000000007;
using mint = static_modint<MOD>;

/*
  == AC Library Cheat sheet
  documentation: file:///Users/nobu/Downloads/ac-library/document_ja/index.html

  mint
    mint m.pow(int p)  //! return m^p
    mint m.inv() //! returns i that gives (m * i).val() == 1
    int m.val()

  fenwick_tree (BIT)
    fenwick_tree<T> fw(int n) //! init a[0] .. a[n-1] with all 0
    void fw.add(int idx, T x); //! a[idx] += x
    T fw.sum(int l, int r); //! return a[l] + .. + a[r-1]

  dsu (UnionFind)
    dsu d(int n)  //! prepare dsu with n nodes
    void d.merge(int x, int y)  //! connect node x and y
    bool d.same(int x, int y)  //! return true if node x and y are connected
    int d.leader(int x)  //! return the leader node of the connected group
    int d.size(int x) //! return the size of the group that node x belongs to
    vector<vector<int>> d.groups() //! return a vector of vectors that contain the nodes in each group

*/

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

    int n, q;
    cin >> n >> q;
    fenwick_tree<LL> fw(n);
    rep(i, n) {
        int a;
        cin >> a;
        fw.add(i, a);
    }

    rep(i, q) {
        int t;
        cin >> t;
        if (t == 0) {
            int p, x;
            cin >> p >> x;
            fw.add(p, x);
        } else if (t == 1) {
            int l, r;
            cin >> l >> r;
            cout << fw.sum(l, r) << endl;
        }
    }

    return 0;
}

#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <stack>
#include <string>
#include <vector>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;

int main()
{
    int n,m;
    cin >> n >> m;
    int max = 0,min = 1e9;
    vector<vector<int>> G(m,vector<int>(2));
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cin >> G[i][j];
        }
        if (max < G[i][0])
        {
            max = G[i][0];
        }
        if (min > G[i][1])
        {
            min = G[i][1];
        }
    }
    if (max > min)
    {
        cout << "0" << endl;
    }else{
        cout << min-max+1 << endl;
    }
}


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

int main(void){
	int a,b,c;

	while(cin>>a>>b>>c && (a||b||c)){
		int i,n;
		cin>>n;

		int res[a+b+c+1];
		int tmp[n][3];
		fill(res,res+a+b+c+1,2);

		for(i=0;i<n;i++){
			int I,J,K,R;
			cin>>I>>J>>K>>R;
			if(R == 1){
				res[I] = res[J] = res[K] = 1;
				i--;
				n--;
			}
			else{
				tmp[i][0] = I;
				tmp[i][1] = J;
				tmp[i][2] = K;
			}
		}
		int size = i;
		for(i=0;i<size;i++){
			if(res[tmp[i][0]] == 1 && res[tmp[i][1]] == 1) res[tmp[i][2]] = 0;
			else if(res[tmp[i][0]] == 1 && res[tmp[i][2]] == 1) res[tmp[i][1]] = 0;
			else if(res[tmp[i][1]] == 1 && res[tmp[i][2]] == 1) res[tmp[i][0]] = 0;
		}
		for(i=1;i<=a+b+c;i++){
			cout<<res[i]<<endl;
		}
	}

	return 0;
}

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

using namespace std;

struct Data
{
	int p[3], r;
};

int main()
{
	int a, b, c;
	while(scanf("%d%d%d", &a, &b, &c), a){
		int n;
		scanf("%d", &n);
		vector<int> state(a+b+c, 2);
		vector<Data> d(n);

		for(int i=0; i<n; ++i){
			for(int j=0; j<3; ++j){
				scanf("%d", &d[i].p[j]);
				d[i].p[j]--;
			}
			scanf("%d", &d[i].r);

			if(d[i].r == 1){
				for(int j=0; j<3; ++j)
					state[d[i].p[j]] = 1;
			}
		}

		for(int i=0; i<n; ++i){
			if(d[i].r == 0){
				for(int j=0; j<3; ++j){
					if(state[d[i].p[j]]==1 && state[d[i].p[(j+1)%3]]==1 && state[d[i].p[(j+2)%3]]==2){
						state[d[i].p[(j+2)%3]] = 0;
						break;
					}
				}
			}
		}

		for(int i=0; i<a+b+c; ++i)
			printf("%d\n", state[i]);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 300005;
int E[N];

int main() {
    int n;
    string s;
    cin >> n >> s;
    for (int i = n - 1; i >= 0; i--) {
		E[i] = E[i+1] + (s[i] == 'E');
	}
	int W = 0, res = n;
	for (int i = 0; i < n; i++) {
		res = min(res, W + E[i+1]);
		W += s[i] == 'W';
	}
	cout << res;
    return 0;
}

#include <iostream>
using namespace std;

int main() {
  int n, q, t, cnt = 0;
  cin >> n;
  int s[n];

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> t;
    for (int j = 0; j < n; j++) {
      if (t == s[j]) {
        cnt += 1;
        break;
      }
    }
  }

  cout << cnt << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n, m;
	cin >> n >> m;

	int a[n + 2];
	memset(a, 0, sizeof a);

	for (int i = 0; i < m ; i++)
	{
		int x, y;
		cin >> x >> y;

		a[x]++;
		a[y + 1] --;

	}

	for(int i = 1; i <= n; ++i){
		  a[i] += a[i-1];
	  }

	 int count = 0;
	for(int i = 1; i <= n; ++i){
		if(a[i] == m){
			count++;
		}
	}

	cout << count << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define flash ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define endl "\n"
#define int long long int
#define ll long long
#define ld long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
const int mod = 1000000007;
const int N=200005;
const int inf=1e18;
vector<vector<int>>adj(N);

bool isPrime(ll n){if(n<2)return false;for(ll i=2;i*i<=n;++i){if(n%i==0){return false;}}return true;}
ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res%mod;}
ll gcd(ll a,ll b){if (a==0)return b;return gcd(b%a,a);}

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

signed main(){
    flash;
    fun();
    int t=1;
    //cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        int l,r;
        int ans1=-inf,ans2=inf;
        while(m--){
            cin>>l>>r;
            ans1=max(l,ans1);
            ans2=min(ans2,r);
        }
        int ans=ans2-ans1+1;
        if(ans>0)
        cout<<ans<<endl;
        else
        {
            cout<<0;
        }

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=42, Max=(1<<17)+10, MOD=1e9+7;
ll n, x, y, z, f[N][Max], sum=1, ans;
inline ll M(ll a){return a%MOD;}

void work(){
	for(int i = 0; i < n; i++) sum = M(sum*10);
	ll cnt = (1<<x+y+z) -1;
	ll tot = (1<<x-1) | (1<<x+y-1) | (1<<x+y+z-1);
	f[0][0] = 1;
	for(int i = 1; i <= n; i++)
		for(int j = 0; j <= cnt; j++)
			for(int h = 1;h <= 10; h++){
				ans = (j<<h)|(1<<h-1);
				ans &= cnt;
				if((ans&tot)!=tot) f[i][ans] = M(f[i][ans]+f[i-1][j]);
			}
	for(int i = 0; i <= cnt; i++)
		sum = M(sum-f[n][i]+MOD);
}

int main(){
	scanf("%lld%lld%lld%lld",&n,&x,&y,&z);
	work();
	printf("%lld",sum);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 998244353;
vector<ll> fac(200005);
vector<ll> ifac(200005);
ll modpow(ll x, ll n){
    ll ans = 1;
    while(n != 0){
        if(n & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        n = n >> 1;
    }
    return ans;
}
ll comb(ll a, ll b){
    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;
}
int main(){
    int n, m, k;
    cin >> n >> m >> k;
    fac[0] = 1;
    ifac[0] = 1;
    for(ll i = 0; i < n; i++){
        fac[i + 1] = fac[i] * (i + 1) % mod;
        ifac[i + 1] = ifac[i] * modpow(i + 1, mod - 2) % mod;
    }
    ll ans = 0;
    n--;
    for(int i = 0; i <= k; i++){
        ll p = modpow(m - 1, n - i) * comb(n, i) % mod;
        ans = (ans + p) % mod;
    }
    ans = ans * m % mod;
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e10;
const ll MOD = 100000000;
const ll MOD2 = 998244353;
typedef pair<ll,ll> P;
const ll MAX = 100;
constexpr ll nx[8] = {1,0,-1,0,-1,-1,1,1};
constexpr ll ny[8] = {0,1,0,-1,-1,1,-1,1};
typedef pair<ll,ll> P;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m,v,p;
    cin >> n >> m >> v >> p;
    vector<ll> a(n);
    cins(all(a));
    SORT(a);
    v--;
    ll l = -1,r = n-1;
    while(r-l > 1){
        vector<ll> b = a;
        ll mid = (l+r)/2;
        b[mid] += m;
        ll cnt = 0,same = 0,big = 0;
        vector<ll> mod;
        rep(i,n){
            if (i == mid) continue;
            if (b[mid] > b[i]){
                if (b[mid]-b[i] >= m) cnt += m;
                else{
                    cnt += b[mid]-b[i];
                    mod.push_back(m-(b[mid]-b[i]));
                }
            }
            else if (b[mid] == b[i]){
                same++;
            }
            else{
                big++;
                cnt += m;
            }
        }
        RSORT(mod);
        if (cnt >= v*m){
            //NONE
        }
        else if (cnt+same*m >= v*m){
            ll s = v*m-cnt;
            big += (s%m ? s/m+1 : s/m);
        }
        else{
            ll s = v*m-(cnt+same*m);
            big += same;
            ll i = 0;
            while(s > 0){
                s -= mod[i];
                big++;
                i++;
            }
        }
        if (big < p) r = mid;
        else l = mid;
        //PRINT(a[mid] << " " << cnt << " " << same << " " << big);
    }
    //PRINT(a[r]);
    PRINT(a.end()-lower_bound(all(a),a[r]));
}


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

int main(void){
  int n,t;
  cin>>n>>t;

  vector<int>time(n);

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

  int dif;
  long long sum=t;
  for(int i=1;i<n;i++){
    dif=time[i]-time[i-1];

    if(dif<=t)sum+=dif;
    else sum+=t;
  }

  cout<<sum<<endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k; k < (int)n ? i < (int)n : i > (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define div2(a) (!(a & 1))
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
template <class T>
T mod(T a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
template <class T>
T addmod(T a, T b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
template <class T>
T prodmod(T a, T b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
template <class T>
T bin_exp(T a, T b)
{
    T res = 1;
    T temp = 1;
    if (b <= 1)
        return a;
    if (b % 2 == 0)
        res = bin_exp(a * a, b / 2);
    else
    {
        res *= a;
        temp = bin_exp(a, --b);
    }
    return res * temp;
}
template <class T>
T bin_expmod(T a, T b)
{
    T res = 1;
    T temp = 1;
    if (b <= 1)
        return a;
    if (b % 2 == 0)
        res = bin_expmod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = bin_exp(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
int digit(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumAllInts(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
void print(vi &vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << " ";
    }
}

void solve()
{
    int n,t;
    cin>>n>>t;
    int temp;cin>>temp;
    ll ans = t;
    fo(i,n-1){
        int te; cin>>te;
        int diff = te - temp;
        ans+=min(diff,t);
        temp = te;
    }
    cout<<ans;
}
int main()
{
    quickie;
    //txt;
    int t = 1;
    while (t--)
    {
        solve();
        newl;
    }
    return 0;
}


#include <iostream>
#include <vector>
int main() {
    int n;
    std::vector<int> v;
    std::cin >> n; //skip
    while (std::cin >> n) {
        v.push_back(n);
    }
    for (auto&& e = v.crbegin(); e != v.crend(); ++e) {
        std::cout << ((e == v.crbegin()) ? "" : " ") << *e;
    }
    std::cout << std::endl;
}

#include<iostream>
using namespace std;

int main(){
  int array[110]={};
  int n;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>array[i];
  }
  for(int i=n-1;i>=1;i--){
    cout<<array[i]<<" ";
  }
  if(n != 0){
    cout<<array[0]<<endl;
  }
  return 0;
}


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

int main(){
    int n;
    cin >>n;
    vector<int> v(n);
    for( auto &i : v ) cin >> i;
    int mx = 0;
    Long ans = 0LL;
    for( int i = 0 ; i < n ; ++i ){
        mx = max( mx , v[i]);
        ans += 1LL * ( mx - v[i] );
    }
    cout << ans << endl;
    return 0;
}

#include<iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include<map>
#include<cstring>
#include<iomanip>
#include <string>
#include <vector>
#include<unordered_map>
#include<unordered_set>
using namespace std;

typedef long long unsigned llu;
typedef long double ld;
#define ll long long
#define PI 3.14159265358979323846
#define For(i, a, b) for (int i = a, sz = (int)b; i < sz; i++)
#define rFor(i, a, b, c) for (int i = (a); i >= (b); i -= (c))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define fill(a, b) memset(a, b, sizeof(a))
#define IO ios_base::sync_with_stdio(false), cin.tie(0),cout.tie(0)
const int mod = 1000000007;
const ll inf = 1000000000009;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) { return a*b/gcd(a,b);}

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdoutn

int main()
{
	IO;
	int n{};
	cin>>n;
	vector<ll>v(n);
	For(i,0,n)cin>>v[i];
	ll ans{};
	For(i,1,n)
	{
		if(v[i]<v[i-1])ans+=v[i-1]-v[i],v[i]=v[i-1];
	}
	cout<<ans<<'\n';
//	system("pause");
	return 0;
}


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

typedef long long ll;
int D,l,cnt[100];
ll pw[100],v[100];

int getlen(ll x){
	int len=0;
	while(x)++len,x/=10;
	return len;
}

ll dfs(int i,ll now){
	if(i==l/2)return now==D;
	ll ret=0;
	for(int d=-9;d<=9;++d){
		ll nxt=now+v[i]*d;
		if(abs(nxt-D)>v[i])continue;
		ret+=(cnt[d+9]-(i==0&&d>=0))*dfs(i+1,nxt);
	}
	return ret;
}

int main(){
	scanf("%d",&D);
	for(int i=pw[0]=1;i<19;++i)pw[i]=10*pw[i-1];
	for(int i=0;i<=9;++i)for(int j=0;j<=9;++j)++cnt[i-j+9];
	int ld=getlen(D);
	ll ans=0;
	for(l=ld;l<=2*ld;++l){
		for(int i=0;i<=(l-1)/2;++i)v[i]=pw[l-i-1]-pw[i];
		ll tmp=dfs(0,0);
		if(l%2)tmp*=10;
		ans+=tmp;
	}
	printf("%lld\n",ans);
	return 0;
}

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
ll Power[20];
ll dfs(ll l , ll r , ll d) {
    if(l >= r) {
        if(d == 0) return l == r ? 10 : 1;
        return 0;
    }
    if(d % 10 == 0) {
        if(l != 1) return 10 * dfs(l + 1 , r - 1 , d / 10);
        return 9 * dfs(l + 1 , r - 1 , d / 10);
    }
    ll differ = d % 10 - 10;//选取的数一定要使l位进1否则取反之后的必定小与改数，那么显然不可能。所以differ是d % 10 - 10
    ll next = d - differ + differ * Power[r - l];//这里先让d进一位，然后由于对称性，r位+differ=l位+differ所以选择+ differ * Power[r - l]，这样才能满足对称条件
    if(l == 1) return (9 + differ) * dfs(l + 1 , r - 1 , abs(next) / 10);
    return (10 + differ) * dfs(l + 1 , r - 1 , abs(next) / 10);
}
int main() {
    ll d;
    cin >> d;
    Power[0] = 1;
    for(int i = 1 ; i <= 18 ; i++) {
        Power[i] = Power[i - 1] * 10;
    }
    ll ans = 0;
    for(int i = 2 ; i <= 18 ; i++) {
        ans += dfs(1 , i , d);
    }//这里选择18位由于原来的数也就最多9位，如果取得数位数是两倍的话显然是不可能的。
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n , dp[5005][5005];
pair<ll,ll> a[5005];
bool cmp(pair<ll,ll> x,pair<ll,ll> y){
    return x.first+x.second < y.first+y.second;
}
int main(){
    ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n;
    for(ll i=0;i<n;i++)cin>>a[i].first>>a[i].second;
    sort(a,a+n,cmp);
    memset(dp,0x3f,sizeof(dp));
    dp[0][0]=0;
    for(ll i=0;i<n;i++){
        for(ll j=0;j<=i;j++){
            if(dp[i][j]<=a[i].first)
                dp[i+1][j+1]=min(dp[i+1][j+1] , dp[i][j] + a[i].second);
            dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
        }
    }
    for(ll i=n;i>=0;i--)if(dp[n][i]<(1e16))return cout<<i , 0;
}

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define int long long int
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
#define capi(x) int x;cin>>x
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using vi = vector<int>;
using pi = pair<int, int>;
using vp = vector<pi>;

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

#define P (1'000'000'007)

signed main() {
    int d, g;
    cin >> d >> g;

    vi vs;
    vp vc;

    rep(i, d) {
        int p, c;
        cin >> p >> c;

        vs.push_back(p - 1);
        vc.push_back(make_pair(p, (i + 1) * 100 * p + c));
    }

    int ans = INT_MAX;
    rep(i, 1 << d) {
        int cost = g;
        int res = 0;
        rep(j, d) {
            if (((1 << j) & i) > 0) {
                cost -= vc[j].second;
                res += vc[j].first;
            }
        }

        for (int j = d - 1; j >= 0; --j) {
            if (cost <= 0) break;
            if(((1 << j) & i) > 0) continue;
            int x = min(vs[j], 1 + (cost - 1) / ((j + 1) * 100));
            res += x;
            cost -= x * (j + 1) * 100;
        }

        if (cost <= 0) {
            chmin(ans, res);
        }
    }

    cout << ans;

    return 0;
}


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

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()

void boost(){
  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int N=5e5+5;
ll tree[N];

ll read(int idx) {
  ll sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

void update(int idx, ll val) {
  while (idx <N) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}

void solve(){
  int n,q;
  cin>>n>>q;
  int x;
  fr(i,1,n+1){
    cin>>x;
    update(i,x);
  }
  int t,l,r;
  while(q--){
    cin>>t>>l>>r;
    if(t==0){
      update(l+1,r);
    }
    else{
      cout<<read(r)-read(l)<<endl;
    }
  }
  return;
}

int main()
{
  boost();

  int tc=1;
  //cin>>tc;
  while(tc--)
    solve();
  return 0;
}

#include<bits/stdc++.h>
#include<atcoder/fenwicktree>
using ll = int_fast64_t;
#define REP(i,b,e) for(ll i=b; i<e; i++)

int main(){
	int n, q;
	scanf("%d %d", &n, &q);
	atcoder::fenwick_tree<ll> bit(n);
	REP(i, 0, n){
		ll a;
		scanf("%ld", &a);
		bit.add(i, a);
	}

	REP(i, 0, q){
		ll t, x, y;
		scanf("%ld %ld %ld", &t, &x, &y);
		if(t==0) bit.add(x, y);
		else printf("%ld\n", bit.sum(x, y));
	}
	return 0;
}


#include <cmath>
#include <cstring>
#include <iostream>
#include <ostream>
#include <vector>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

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

// n 以下の素数のリストを返す O(nlog(log(n)))
std::vector<int> eratosthenes(int n) {
  std::vector<int> is_prime(n + 1, 1);
  std::vector<int> primes;

  is_prime[0] = is_prime[1] = 0;
  primes.push_back(2);
  for(int i = 3; i <= n; i += 2) {
    if(is_prime[i]) {
      primes.push_back(i);
      for(int j = 3; i * j <= n; j += 2) {
        is_prime[i * j] = 0;
      }
    }
  }

  return primes;
}

int main() {
  int n;
  std::vector<int> primes = eratosthenes(10000);

  while(cin >> n, n) {
    int a = 2, b = 3;
    for(int i = 1; i < primes.size(); i++) {
      if (primes[i] > n) break;

      if(primes[i] - primes[i - 1] == 2) {
        a = primes[i - 1];
        b = primes[i];
      }
    }
    cout << a << " " << b << endl;
  }

  return 0;
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <string.h>
#include <cmath>
#include <queue>
#include <string>
#include <stack>
#include <utility>
#include <queue>
#include <cmath>
#define mp make_pair
#define pii pair<int,int>
#define ff first
#define ss second
#define ll long long
#define vi vector<int>

using namespace std;

ll _sieve_size;
bitset<10000010> bs;
vi primes;
void sieve(ll upperbound) {
_sieve_size = upperbound + 1;
bs.set();
bs[0] = bs[1] = 0;
for (ll i = 2; i <= _sieve_size; i++) if (bs[i]) {
for (ll j = i * i; j <= _sieve_size; j += i) bs[j] = 0;
primes.push_back((int)i);
} }
bool isPrime(ll N) {
if (N <= _sieve_size) return bs[N];
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes[i] == 0) return false;
return true;
}

int a;

int main()
{
	sieve(11000);
	while(1)
	{
		scanf("%d",&a);
		if(a==0)break;
		if(a%2==0)
		a-=3;
		else
		a-=2;
		//printf("%d",b);
		while(1)
		{
			if(isPrime(a)&&isPrime(a+2))
			{
				printf("%d %d\n",a,a+2);
				break;
			}
			a-=2;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 * 2;

int main() {
	int a[3];
	rep(i, 3) cin >> a[i];
	int k;
	cin >> k;
	sort(a, a + 3, greater<int>());
	ll out = a[0] * pow(2, k) + a[1] + a[2];
	cout << out << endl;
}

#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

int main()
{
        vll nums(3);
        cin >> nums[0] >> nums[1] >> nums[2];
        ll K;
        cin >> K;
        rep(i, 0, K) {
                *max_element(all(nums)) *= 2;
        }
        cout << accumulate(all(nums), 0LL) << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int simulate(vector<long long int> A){
  int N = A.size(), ret = 0;
  while(1){
    int t = 0;
    for(int i = 0; i < N; ++i){
      if(A[i] > A[t]) t = i;
    }
    if(A[t] < N) break;
    for(int i = 0; i < N; ++i){
      if(i != t) ++A[i];
      else A[i] -= N;
    }
    ++ret;
  }
  return ret;
}

int main(){
  long long int K, M = 1e16 + 1e3 + 1;
  cin >> K;
  vector<long long int> A(50);
  for(long long int i = 0; i < 50; ++i) A[i] = i + K/50;
  K %= 50;
  for(int i = 0; i < K; ++i) ++A[49-i];
  //cout << simulate(A) << endl;
  cout << 50 << endl;
  cout << A[0];
  for(int i = 1; i < 50; ++i) cout << " " << A[i];
  cout << endl;
  return 0;
}

#include <iostream>
#include <vector>

using namespace std;

vector<int> permMul(const vector<int>& a, const vector<int>& b){
    vector<int> res(a.size());
    for(int i=0;i<a.size();i++) res[i] = a[b[i]];
    return res;
}

vector<int> permPow(const vector<int>& p, long long K){
    if(K == 0){
        vector<int> res = p;
        for(int i=0;i<res.size();i++) res[i] = i;
        return res;
    }
    vector<int> half = permPow(p, K/2);
    vector<int> res = permMul(half, half);
    if(K%2 == 1) res = permMul(res, p);
    return res;
}

int main(){
    int N;
    while(cin >> N){
        vector<long long> x(N);
        for(auto& t : x) cin >> t;
        vector<long long> d(N-1);
        for(int i=0;i<N-1;i++) d[i] = x[i+1] - x[i];
        int M; long long K;
        cin >> M >> K;
        vector<int> s(N-1);
        for(int i=0;i<N-1;i++) s[i] = i;
        for(int i=0;i<M;i++){
            int a; cin >> a;
            swap(s[a-2], s[a-1]);
        }
        cout << x[0] << endl;
        s = permPow(s, K);
        for(int i=1;i<N;i++){
            x[i] = x[i-1] + d[s[i-1]];
            cout << x[i] << endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define REP(i,s,t) for( int i = s; i < t; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    int x[N], y[N];
    rep(i, N) cin >> x[i] >> y[i];
    int cnt[N] = {};
    rep(i, 250000) {
        double maxd = -INF;
        int index = 0;
        double theta = (i/125000.0) * M_PI;
        rep(j, N) {
            if (maxd < x[j] * cos(theta) + y[j] * sin(theta)){
                index = j;
                maxd = x[j] * cos(theta) + y[j] * sin(theta);
            }
        }
        cnt[index]++;
    }
    cout << fixed;
    rep(i, N) cout << cnt[i] / 250000.0 << endl;

    return 0;
}

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P   = pair<ll, ll>;
using mat = vector<vector<ll>>;

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-10;

template <typename T> void dump(T x) { cout << x << endl; }
void dumpf(ld x, int t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}

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

// ll dx[4] = {1, 0, -1, 0};
// ll dy[4] = {0, 1, 0, -1};
// ll pas[5001][5002];

ld add(ld a, ld b) {
	if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0;
	return a + b;
}

struct Point {
	ld x, y;
	int num;
	Point() {}
	Point(ld x, ld y) : x(x), y(y) {}
	Point(ld x, ld y, int n) : x(x), y(y), num(n) {}
	Point operator+(const Point &p) { return Point(add(x, p.x), add(y, p.y)); }
	Point operator-(const Point &p) {
		return Point(add(x, -p.x), add(y, -p.y));
	}
	bool operator==(const Point &p) { return x == p.x && y == p.y; }
	bool operator!=(const Point &p) { return *this == p; }
	bool operator<(const Point &p) {
		if (x != p.x)
			return x < p.x;
		else
			return y < p.y;
	}
	friend Point operator*(ld k, const Point &p) {
		return Point(k * p.x, k * p.y);
	}
	ld dot(const Point &p) { return add(x * p.x, y * p.y); }
	ld det(const Point &p) { return add(x * p.y, -y * p.x); }
};

vector<Point> convex_hull(vector<Point> p) {
	sort(p.begin(), p.end());
	int n = p.size();
	int k = 0;
	vector<Point> q(2 * n);
	for (int i = 0; i < n; i++) {
		while (k > 1 && (q[k - 1] - q[k - 2]).det(p[i] - q[k - 1]) <= 0) k--;
		q[k++] = p[i];
	}
	for (int i = n - 2, t = k; i >= 0; i--) {
		while (k > t && (q[k - 1] - q[k - 2]).det(p[i] - q[k - 1]) <= 0) k--;
		q[k++] = p[i];
	}
	q.resize(k - 1);
	return q;
}

ld dist(Point p1, Point p2) { return (p1 - p2).dot(p1 - p2); }
ld dif(Point p1, Point p2) { return sqrt(dist(p1, p2)); }

ld longestdist(vector<Point> t) {
	int n = t.size();
	ld M  = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) { M = max(M, dist(t[i], t[j])); }
	}
	return M;
}

int main() {
	IOS();
	ll n;
	cin >> n;
	vector<Point> p(n);
	vector<ld> ans(n, 0);
	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		p[i] = Point(x, y, i);
	}

	vector<Point> q = convex_hull(p);

	int m = q.size();
	for (int j = 0; j < m; j++) {
		ld costh = (q[mod(j - 1, m)] - q[j]).dot(q[mod(j + 1, m)] - q[j]);
		costh /= dif(q[mod(j - 1, m)], q[j]) * dif(q[mod(j + 1, m)], q[j]);
		if (abs(costh - 1) <= EPS) costh = 1.0;
		if (abs(costh + 1) <= EPS) costh = -1.0;
		ans[q[j].num] = (M_PI - acos(costh)) / (2 * M_PI);
	}
	for (int i = 0; i < n; i++) { dumpf(ans[i], 8); }
	return 0;
}


#include<bits/stdc++.h>
#define DBG(x) cerr << #x << “ = ” << (x) << endl
#define Rep(i,a,b) for (int i = a, to = b; i < to; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

typedef long long Long;

using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	Long n,x,t;
    cin>>n>>x>>t;
    cout<<((n+x-1)/x)*t;

	return 0;
}

#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

int main()
{
	vector<string> a(3);

	string res = "YES";
	for (ll i = 0; i < 3; i++)
	{
		cin >> a[i];

		if (i > 0)
		{
			if (a[i - 1].back() != a[i].front())
			{
				res = "NO";
			}
		}
	}

	println(res);
}


#include <bits/stdc++.h>

using namespace std;

#ifdef KACTL
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
#endif
#define all(x) begin(x), end(x)

using ll = int64_t;
using ld = long double;
using pii = pair<int32_t, int32_t>;
using vi = vector<int32_t>;

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

    int h, w, a, b;
    cin >> h >> w >> a >> b;

    /* h = w, a = b
     * do the jagged thing
     *
     * XX.
     * .XX
     * X.X
     *
     * XX..
     * .XX.
     * ..XX
     * X..X
     *
     *
     * some can be 1s and some can be zeros
     * 2 4 1 1
     *
     * 3 3 1 1
     */

    vector<string> res(h, string(w, '0'));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            res[i][j] ^= (i < b) ^ (j < a);
        }
    }

    for (auto& row : res)
        cout << row << '\n';

    return 0;
}

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

int main(void) {
  int H, W, A, B;
  cin >> H >> W >> A >> B;

  if ((H / 2) < B || (W / 2) < A) {
    cout << -1 << endl;
    return 0;
  }

  rep(i, H) {
    char out = '0';
    if (i >= B) out = '1';
    rep(j, W) {
      if (j < A)
        cout << out;
      else
        cout << ((out == '0') ? '1' : '0');
    }
    cout << endl;
  }
  return 0;
}


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

int main() {
	int A, B, C, D, E, F;
	cin >> A >> B >> C >> D >> E >> F;
	cout << max(E, F) + max({ A + B + C, A + B + D, A + C + D, B + C + D }) << endl;
}

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

int main(void){
    int h, w, a, b;
    cin>>h>>w>>a>>b;
    vector<vector<int> > v(h, vector<int>(w));
    if(w/2>=a && h/2>=b){
        vector<int> s1(w);
        vector<int> s2(w);
        rep(i, a){
            s1[i] = 1;
        }
        for(int i = a;i<=w-1;i++){
            s2[i] = 1;
        }
        rep(i, b){
            v[i] = s1;
        }
        for(int i = b;i<=h-1;i++){
            v[i] = s2;
        }
        rep(i, h){
            rep(j, w) cout<<v[i][j];
            cout<<endl;
        }
    }
    else cout<<"No"<<endl;
    return 0;
}


// -*- mode:c++; coding:utf-8; indent-tabs-mode:nil; -*-
// ALDS1_2-D

#include <algorithm>
#include <cstdio>

static void print_ints(const int as[], const unsigned int n,
                       const char* sep = " ") {
  const char* out_sep = "";
  for (unsigned int i = 0; i < n; ++i) {
    std::printf("%s%d", out_sep, as[i]);
    out_sep = sep;
  }
  std::puts("");
}

static void print_int(const unsigned int n) {
  std::printf("%d\n", n);
}

static unsigned int cnt = 0;

inline
static void insert(int as[], const unsigned int i, const unsigned int d) {
  const int asi = as[i];
  unsigned int j = i;
  while (j >= d && asi < as[j-d]) {
    as[j] = as[j-d];
    j -= d;
    ++cnt;
  }
  as[j] = asi;
}

void insertion_sort(int as[], const unsigned int n, const unsigned int d) {
  for (unsigned int i = d; i < n; ++i) {
    insert(as, i, d);
  }
}

inline
unsigned int next_g(const unsigned int g) { return 3*g + 1; }

unsigned int gen_m(const unsigned int n) {
  unsigned int m = 1;
  for (unsigned int g = 1;;) {
    g = next_g(g);
    if (n < g) break;
    ++m;
  }
  return m;
}

void shell_sort(int as[], const unsigned int n) {
  cnt = 0;

  unsigned int m = gen_m(n);
  int gs[m];
  for (unsigned int g = 1, i = 0; i < m; ++i) {
    gs[m-1-i] = g;
    g = next_g(g);
  }
  print_int(m);
  print_ints(gs, m);

  for(unsigned int i = 0; i < m; ++i) {
    insertion_sort(as, n, gs[i]);
  }

  print_int(cnt);
}

int main(void) {
  unsigned int n;
  std::scanf("%d", &n);
  int as[n];
  for (unsigned int i = 0; i < n; ++i) {
    std::scanf("%d", &as[i]);
  }

  shell_sort(as, n);
  print_ints(as, n, "\n");

  return 0;
}
// end

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

typedef long long ll;

ll maxSubarrayXOR(ll a[], int n) {
    int index = 0;
    for(int i = 60; i >= 0; i--) {
        int pos = index;
        int f = 0;
        for(int j = index; j < n; j++) {
            if(a[j] & (1LL << i)) f = 1, pos = j;
        }
        if(!f) continue;
        swap(a[index], a[pos]);
        for(int j = 0; j < n; j++) {
            if(j != index && (a[j] & (1LL << i))) a[j] ^= a[index];
        }
        index++;
    }
    ll res = 0;
    for(int i = 0; i < n; i++) res ^= a[i];
    return res;
}

int n;
ll a[100010], ans = 0;
int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        scanf("%lld", a+i);
        ans ^= a[i];
    }
    for(int i = 0; i < n; i++) a[i] &= ~ans;
    cout << ans + 2*maxSubarrayXOR(a, n);
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

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

char mp[2222][2222];
bool ng[2222][2222];
int high[2222][2222];

int seica(ll h,ll w){
	ll ret = 0;
	REP(i,h){
		stack<pair<ll,ll>> st;
		REP(j,w){
			ll tmp = j;
			while(!st.empty() && st.top().FI > high[i][j]){
				auto now = st.top();
				st.pop();
				ret = max(ret,(j - now.SE) * now.FI);
				tmp = min(tmp,now.SE);
				//cout << i << " " << j << " " << now.FI << " " << now.SE << " " << (j - now.SE) * now.FI << endl;
			}
			if(st.empty() || st.top().FI < high[i][j])st.push(MP(high[i][j],tmp));
			if(st.empty() || st.top().FI < i + 1)st.push(MP(i + 1,j));
		}
		while(!st.empty()){
			auto now = st.top();
			st.pop();
			ret = max(ret, (w - now.SE) * now.FI);
			//cout << i << " " << w << " " << now.FI << " " << now.SE << " " << (w - now.SE) * now.FI << endl;
		}
	}
	return ret;
}

int main(){

	int h,w;cin >> h >> w;
	REP(i,h){
		REP(j,w){
			cin >> mp[i][j];
		}
	}

	REP(i,h-1){
		REP(j,w-1){
			int y = i + 1;
			int x = j + 1;
			int cou = 0;
			if(mp[y][x] == '#')cou++;
			if(mp[y][x-1] == '#')cou++;
			if(mp[y-1][x] == '#')cou++;
			if(mp[y-1][x-1] == '#')cou++;
			if(cou % 2)ng[y][x] = true;
		}
	}

	REP(i,h){
		REP(j,w){
			if(i == 0)high[i][j] = 1;
			else {
				if(ng[i][j])high[i][j] = 1;
				else high[i][j] = high[i-1][j] + 1;
			}
		}
	}

	cout << seica(h,w) << endl;

	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <cstdint>
#include <tuple>
#include <numeric>
#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)

const int N = 2005;
char board[N][N];
int val[N][N];

class UnionFind {
public:
	vector<int> data;
	UnionFind(int n) : data(n, -1) {}
	int find(int x) {
		if (data[x] < 0)return x;
		return data[x] = find(data[x]);
	}
	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return false;
		if (data[x] > data[y])swap(x, y);
		data[x] += data[y];
		data[y] = x;
		return true;
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	int size(int x) {
		return -data[find(x)];
	}
};

int main() {
	int h, w;
	scanf("%d%d", &h, &w);
	REP(i, h)scanf("%s", board[i]);
	REP(i, w)val[0][i] = w - i;
	FOR(i, 1, h) {
		val[i][w - 1] = 1;
		bool same = (board[i - 1][w - 1] == board[i][w - 1]);
		for (int j = w - 2; j >= 0; j--) {
			if (same ^ (board[i - 1][j] == board[i][j])) {
				val[i][j] = 1;
				same = !same;
			}
			else val[i][j] = val[i][j + 1] + 1;
		}
	}
	int ans = max(h, w);
	REP(j, w) {
		UnionFind uf(h);
		vector<PII> vpii(h - 1);
		FOR(i, 1, h)vpii[i - 1] = MP(val[i][j], i);
		sort(ALL(vpii), greater<PII>());
		REP(i, h - 1) {
			if (uf.unite(vpii[i].second - 1, vpii[i].second)) {
				ans = max(ans, uf.size(vpii[i].second) * vpii[i].first);
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int h, w, ah, aw;
  while(cin >> h >> w, h || w){
    ah = aw = 1000;
    for(int i = 1; i <= 150; i++){
      for(int j = i+1; j <= 150; j++){
	if(i*i + j*j > h*h + w*w || i*i + j*j == h*h + w*w && i > h){
	  if(i*i + j*j < ah*ah + aw*aw || i*i + j*j == ah*ah + aw*aw && i < ah) ah = i, aw = j;
	}
      }
    }
    cout << ah << " " << aw << endl;
  }
  return 0;
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define mp(a,b) make_pair((a),(b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

struct S{
	int h,w,l;

	S(int h,int w):h(h),w(w){
		l=h*h+w*w;
	}

	bool operator<(const S &s)const{
		if(l==s.l) return h<s.h;
		return l<s.l;
	}
};

void Solve(){
	vector<S> rects;
	rep(i,1,151) rep(j,i+1,151) rects.push_back(S{i,j});
	sort(rects.begin(),rects.end());

	int h,w;
	while(true){
		cin >> h >> w;
		if(h==0&&w==0) break;

		auto it=++lower_bound(rects.begin(),rects.end(),S{h,w});
		cout << it->h << ' ' << it->w << endl;
	}
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	Solve();
	return 0;
}


#include<cstdio>

int main(){
	int x,S;
	1<=x>=100;
	scanf("%d", &x);
	S=x*x*x;
	printf("%d\n", S);

	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits.h>
#include <string>
#include <string.h>
#include <sstream>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;

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

  printf("%d\n", (int)pow( num, 3 ));
  return 0;
}


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

int main(){
  long double n,m,d;
  cin>>n>>m>>d;
  if(d==0){
    cout<<fixed<<setprecision(10);
    cout<<(m-1)/n<<endl;
    return 0;
  }
  cout<<fixed<<setprecision(10);
  cout<<(2*(n-d)*(m-1))/(n*n)<<endl;
}

//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<string>
#include<cstdlib>
#include<bitset>
#include<sstream>
#include<queue>
#include<stack>
#include<list>
#include<limits>
#include<iomanip>

using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pt;
typedef pair<ll,ll> pll;

#define Max 1000001
#define intinf 2147483647
#define longlonginf 9223372036854775807
#define MOD 998244353
#define MOD2 1000000007
#define fastio ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define vint vector<int>
#define vll vector<long long>
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define F(i,n) for(int i = 0;i<n;i++)
#define Fr(i,l,r) for(int i = l;i <= r;i++)
#define rF(i,n) for(int i = n-1;i>=0;i--)

//#define min(a,b) a>b?b:a
//#define max(a,b) a>b?a:b
#define all(arr) arr.begin(),arr.end()
//#define sort(vec) sort(vec.begin(),vec.end())
//#define reverse(vec) reverse(vec.begin(),vec.end())

const ll m = 1e9 + 9;

ll Lpow(ll a,ll b)
{
    ll ans = 1;
    while(b > 0)
    {
        if(b%2)
            ans = (ans*a)%m;
        a = (a*a)%m;
        b = b/2;
    }
    return ans;
}

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

signed main()
{
    fastio;

    int h1,m1,h2,m2,k;

    cin >> h1 >> m1 >> h2 >> m2 >> k;

    int ans = (h2-h1)*60 + m2 - m1;

    if(ans <= 0)
        ans += 24*60;

    ans = max(ans-k,0);

    cout << ans << endl;

    return 0;
}


// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

template<typename M>
void warshallFloyd(M &m, const int &n) {
  rep(k, n) rep(i, n) rep(j, n) cmin(m[i][j], m[i][k] + m[k][j]);
}

void solve() {
  int n;
  while (cin >> n, n) {
    vvi t(n, vi(n));
    int m;
    cin >> m;
    rep(i, n) rep(j, n) t[i][j] = i == j ? 0 : INF;
    while (m--) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      t[a][b] = t[b][a] = 1;
    }
    warshallFloyd(t, n);
    int s = 0;
    rep(i, n) s += 1 <= t[0][i] && t[0][i] <= 2;
    cout << s << endl;
  }
}

#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<vector<int> > edges(n);
        for(int i=0; i<m; ++i){
            int a, b;
            cin >> a >> b;
            edges[a-1].push_back(b-1);
            edges[b-1].push_back(a-1);
        }

        vector<bool> check(n, false);
        check[0] = true;
        for(int i=0; i<2; ++i){
            vector<bool> next = check;
            for(int j=0; j<n; ++j){
                if(check[j]){
                    for(unsigned k=0; k<edges[j].size(); ++k)
                        next[edges[j][k]] = true;
                }
            }
            check = next;
        }

        int ret = -1;
        for(int i=0; i<n; ++i){
            if(check[i])
                ++ ret;
        }
        cout << ret << endl;
    }
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define P pair<int,int>
#define all(vec) vec.begin(),vec.end()

int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x / gcd(x, y)*y;
}
int kai(int x, int y, int m) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

int comb(int x, int y, int m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

int n, m, q;
string s[2010];
int cum1[2010][2010], cum2[2010][2010], cum3[2010][2010];

signed main() {
	cin >> n >> m >> q;
	for (int i = 0; i < n; i++)cin >> s[i];
	for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {
		if (s[i][j] == '1')cum1[i][j]++;
		if (i)cum1[i][j] += cum1[i - 1][j];
		if (j)cum1[i][j] += cum1[i][j - 1];
		if (i&&j)cum1[i][j] -= cum1[i - 1][j - 1];
	}
	for (int i = 0; i < n - 1; i++)for (int j = 0; j < m; j++) {
		if (s[i][j] == '1'&&s[i + 1][j] == '1')cum2[i + 1][j]++;
	}
	for (int i = 0; i < n; i++)for (int j = 0; j < m - 1; j++) {
		if (s[i][j] == '1'&&s[i][j + 1] == '1')cum3[i][j + 1]++;
	}
	for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {
		if (i)cum2[i][j] += cum2[i - 1][j];
		if (j)cum2[i][j] += cum2[i][j - 1];
		if (i&&j)cum2[i][j] -= cum2[i - 1][j - 1];
		if (i)cum3[i][j] += cum3[i - 1][j];
		if (j)cum3[i][j] += cum3[i][j - 1];
		if (i&&j)cum3[i][j] -= cum3[i - 1][j - 1];
	}
	for (int i = 0; i < q; i++) {
		int x, y, xx, yy; cin >> x >> y >> xx >> yy; x--; y--; xx--; yy--;
		int p = cum1[xx][yy];
		if (x)p -= cum1[x - 1][yy];
		if (y)p -= cum1[xx][y - 1];
		if (x&&y)p += cum1[x - 1][y - 1];
		int q = cum2[xx][yy] - cum2[x][yy] + cum3[xx][yy] - cum3[xx][y];
		if (y)q -= cum2[xx][y - 1];
		if (y)q += cum2[x][y - 1];
		if (x)q -= cum3[x - 1][yy];
		if (x)q += cum3[x - 1][y];
		cout << p - q << endl;
	}
}

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 2e3 + 5;

char readch() {
    char ch = getchar();
    while (ch != '1' && ch != '0') ch = getchar();
    return ch;
}
int s[maxn][maxn], n, m;
int sn[maxn][maxn], sve[maxn][maxn], she[maxn][maxn];

inline int Sum(int s[][maxn], int x1, int y1, int x2, int y2) {
    return s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
}

int main() {
    int T; scanf("%d%d%d", &n, &m, &T);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            char ch = readch();
            if (ch == '1') s[i][j] = 1;
            else s[i][j] = 0;
        }
    //calc sn, sve, she
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            sn[i][j] = sn[i-1][j] + sn[i][j-1] - sn[i-1][j-1] + s[i][j];
            sve[i][j] = sve[i-1][j] + sve[i][j-1] - sve[i-1][j-1] + (s[i][j] && s[i+1][j]);
            she[i][j] = she[i-1][j] + she[i][j-1] - she[i-1][j-1] + (s[i][j] && s[i][j+1]);
        }
    while (T--) {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        printf("%d\n", Sum(sn, x1, y1, x2, y2) - Sum(sve, x1, y1, x2-1, y2) - Sum(she, x1, y1, x2, y2-1));
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

struct Edge {
  int from, to;
};

vector<vector<Edge>> ReadDirectedGraph(
    int n, int m, bool is_one_indexed=false) {
  vector<vector<Edge>> graph(n);
  for (int i = 0; i < m; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (is_one_indexed) {
      v1--;
      v2--;
    }
    graph[v1].push_back(Edge({v1, v2}));
  }
  return graph;
}

vector<int> StronglyConnectedComponents(const vector<vector<Edge>>& graph) {
  int n = graph.size();
  vector<int> vs;
  vector<int> visited1(n);
  function<void(int)> dfs1 = [&](int v) {
    if (visited1[v]) return;
    visited1[v] = 1;
    for (const auto& e : graph[v]) dfs1(e.to);
    vs.push_back(v);
  };
  for (int v = 0; v < n; v++) dfs1(v);
  reverse(vs.begin(), vs.end());

  vector<vector<Edge>> r_graph(n);
  for (int v = 0; v < n; v++) {
    for (const auto& e : graph[v]) {
      r_graph[e.to].push_back(Edge({e.to, e.from}));
    }
  }

  vector<int> to_id(n);
  int current_id = 0;
  vector<int> visited2(n);
  function<void(int)> dfs2 = [&](int v) {
    if (visited2[v]) return;
    visited2[v] = 1;
    to_id[v] = current_id;
    for (const auto& e : r_graph[v]) dfs2(e.to);
  };
  for (int v : vs) {
    if (!visited2[v]) {
      dfs2(v);
      current_id++;
    }
  }
  return to_id;
}

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

  int n, m;
  cin >> n >> m;
  auto graph = ReadDirectedGraph(n, m, false);
  auto to_scc_id = StronglyConnectedComponents(graph);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (to_scc_id[v1] == to_scc_id[v2]) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x)*(x))
#define mp make_pair
#define uint unsigned
#define ld long double
#define PI pair<int,int>
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=100005;
int dq,n,s,tot,q[N];
ll sum,ans;
int x[N],p[N];
signed main(){
	n=read(); s=read();
	for(int i=1;i<=n;i++){
		x[i]=read(); p[i]=read();
	}
	int l=1,r=n;
	int pos=lower_bound(&x[1],&x[n+1],s)-x;
	dq=0;
	if(pos==1){
		cout<<x[n]-s<<endl; return 0;
	}
	if(pos>n){
		cout<<s-x[1]<<endl; return 0;
	}
	if(p[1]<p[n]){dq=1; sum=p[1]; q[++tot]=x[1]; l++;}else {sum=p[n]; q[++tot]=x[n]; r--;}
	while(1){
		if(dq){
			if(r<pos)break;
			if(p[r]>sum){
				dq^=1; q[++tot]=x[r];
			}
			sum+=p[r];
			r--;
		}else{
			if(l>=pos)break;
			if(p[l]>=sum){
				dq^=1; q[++tot]=x[l];
			}
			sum+=p[l];
			l++;
		}
	}
	reverse(&q[1],&q[tot+1]);
	q[0]=s; for(int i=1;i<=tot;i++)ans+=abs(q[i]-q[i-1]);
	cout<<ans<<endl;
}


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

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

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

class TaskU {
public:
	int N;
	vector<vector<int>> a;
	vector<ll> dp, scores;
	vector<int> rem;

	void solve(std::istream& in, std::ostream& out) {
		in >> N;
		a.resize(N, vector<int> (N));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				in >> a[i][j];
			}
		}
		scores.resize(1 << N, 0);
		dp.resize(1 << N, 0);
		pre_calc();
		for (int mask = 0; mask < (1 << N); mask++) {
			rem.clear();
			for (int i = 0; i < N; i++) {
				if (!(mask & (1 << i))) rem.push_back(i);
			}
			rec(0, dp[mask], mask, 0);
		}
		out << dp[(1 << N) - 1];
	}

	void pre_calc() {
		for (int mask = 0; mask < (1 << N); mask++) {
			for (int i = 0; i < N; i++) {
				for (int j = i + 1; j < N; j++) {
					if ((mask & (1 << i)) && (mask & (1 << j)))
						scores[mask] += a[i][j];
				}
			}
		}
	}

	void rec(int pos, ll score, int mask, int group) {
		if (pos == (int) rem.size()) {
			s_max(dp[mask], score + scores[group]);
			return;
		}
		rec(pos + 1, score, mask ^ (1 << rem[pos]), group ^ (1 << rem[pos]));
		rec(pos + 1, score, mask, group);
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	TaskU solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}

#include<bits/stdc++.h>
#define MOD (long long)(1e9+7)
using namespace std;
long long n,a[20][20],dp[1<<17],sum[1<<17];
int main()
{
	cin >> n;
	for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin >> a[i][j];
	for(int i=0; i<(1<<n); i++) for(int j=0; j<n; j++) for(int k=j+1; k<n; k++)
		if((i&(1<<j)) && (i&(1<<k))) sum[i]+=a[j][k];
	for(int i=0; i<(1<<n); i++)
	{
		vector<int> pos;
		for(int j=0; j<n; j++) if(i&(1<<j)) pos.push_back(1<<j);
		int m=pos.size();
		for(int j=0; j<(1<<m); j++)
		{
			int c=0;
			for(int k=0; k<m; k++) if(j&(1<<k)) c+=pos[k];
			dp[i]=max(dp[i],dp[i-c]+sum[c]);
		}
	}
	long long ans=0;
	for(int i=0; i<(1<<n); i++) ans=max(ans,dp[i]);
	cout << ans;
	return 0;
}


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

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());

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

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

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using vi = vector<int>;
using pii = pair<int,int>;
#define ST first
#define ND second
#define PB push_back

const int nax = 301;
const int mod = 998244353;
int n,a[nax];
int dp[nax][nax*nax];
int ans,sum,sub;

int main() {
	scanf("%d",&n);
	for(int i=1; i<=n;i++) {
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	ans=1;
	for(int i=1; i<n;i++) {
		ans=((LL)ans*3)%mod;
	}
	dp[0][0] = 1;
	for(int i=1; i<=n;i++) {
		for(int s=0; s<=sum; s++) {
			dp[i][s] = (2*dp[i-1][s])%mod;
			if(s-a[i]>=0) {
				dp[i][s]= (dp[i][s]+dp[i-1][s-a[i]])%mod;
			}
		}
	}
	for(int i=1; i<=sum;i++) {
		if(i>=(sum+1)/2) {
			sub=(sub+dp[n][i])%mod;
		}
	}
	dp[0][0] = 1;
	for(int i=1; i<=n;i++) {
		for(int s=0; s<=sum;s++) {
			dp[i][s] = dp[i-1][s];
			if(s-a[i]>=0) {
				dp[i][s]= (dp[i][s]+dp[i-1][s-a[i]])%mod;
			}
		}
	}
	if(sum%2==0) {
		sub=(sub-dp[n][sum/2]+mod)%mod;
	}
	int res = (((ans-sub)%mod)+mod)%mod;
	res = ((LL)res*3)%mod;
	printf("%d",res);
}


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

const long long mod = 1e9 + 7;

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

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

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

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

int main(){
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    int len = 0;
    rep(i, n) {
        if (s.substr(n-1-i, i+1) == t.substr(0, i+1)) len = i+1;
    }
    cout << (2*n-len) << endl;
    return 0;
}

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

ll n,a[maxn],b[maxn],c[maxn],d[maxn];
ull h[maxn],tmp,B[maxn];

vector <int> ans;

int main()
{
	n=read(); B[0]=1; rep(i,1,n) a[i]=read(),a[i+n]=a[i]; rep(i,1,n) b[i]=read();
	rep(i,1,2*n-1) c[i]=a[i]^a[i+1]; c[n]=a[1]^a[n];
	rep(i,1,n-1) d[i]=b[i]^b[i+1]; d[n]=b[1]^b[n];
	rep(i,1,n) tmp=tmp*233+d[i],B[i]=B[i-1]*233;
	rep(i,1,2*n) h[i]=h[i-1]*233+c[i];

	//rep(i,1,2*n) cout<<c[i]<<" "; cout<<endl;
	//rep(i,1,n) cout<<d[i]<<" "; cout<<endl<<endl;

	for(int i=1;i<=n;i++)
	{
		ull p=h[i+n-1]-h[i-1]*B[n];
		if(p==tmp) ans.pb(i-1);
	}
	for(int i=0;i<ans.size();i++) printf("%lld %lld\n",ans[i],b[1]^a[1+ans[i]]);
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array)/sizeof(array[0]))
using ll =long long;
using namespace std;
using Graph=vector<vector<int>>;
using Field=vector<vector<int>>;

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

int main(){
    ll n;
    cin>>n;
    ll a[5]={};
    rep(i,5)cin>>a[i];
    ll b[5]={};
    rep(i,5){
        ll s=n%a[i];
        ll t=n/a[i];
        if(s!=0)b[i]=t+1+4;
        else b[i]=t+4;
    }
    ll ans=0;
    rep(i,5){
        ans=max(ans,b[i]);
    }
    cout<<ans<<endl;
}

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

int main() {
  long N,A,B,C,D,E;;
  cin >> N >> A >> B >> C >> D >> E;
  long slow = min(min(A,B),min(C,min(D,E)));
  cout << (N + slow - 1) / slow + 4 << endl;
}


#include <iostream>
#include <algorithm>

using namespace std;
int main(){
    int a,b;
    cin >> a >> b;

    if(a == b){
        cout << a+b << endl;
    }else{
        int m = max(a,b);
        cout << m + (m-1) << endl;
    }

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for(int i=0; i<(int)(a); i++)
typedef long long ll;
const ll INF = 1LL<<60;
const ll mod = 998244353;

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

  bool flg = false;
  for (int i=1; i<=m; i++) {
    int l = i;
    int r = n-i;
    if (n%2 == 0 && !flg && r-l <= n/2) flg = true;
    if (flg) r--;
    printf("%d %d\n", l, r);
  }

  return 0;
}


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
using ld = long double;
const int MOD = 1e9+7;
const ll INF = 1LL << 60;

int main() {
    int x, y; cin >> x >> y;
    if(x + y == 2) cout << 1000000 << endl;
    else if(x + y == 3) cout << 500000 << endl;
    else if(x + y == 4) cout << 400000 << endl;
    else if(x + y == 5) cout << 300000 << endl;
    else if(x + y == 6) cout << 200000 << endl;
    else if(x == 3 || y == 3) cout << 100000 << endl;
    else cout << 0 << endl;

    return 0;
}

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

using namespace std;

long long mod = 1000000007;

long long pow(long long x, long long k)
{
    if (k == 0)
    {
        return 1LL;
    }
    long long temp = pow(x, k / 2);
    if (k % 2 == 1)
    {
        return (((temp * temp) % mod) * x) % mod;
    }
    else
    {
        return (temp * temp) % mod;
    }
}

long long modular_inverse(long long x)
{
    return pow(x, mod - 2);
}

long long binomial(long long n, long long k)
{
    long long rez = 1;
    for (int i = n - k + 1; i <= n; i++)
    {
        rez *= i;
        rez %= mod;
    }
    long long temp = 1;
    for (int i = 1; i <= k; i++)
    {
        temp *= i;
        temp %= mod;
    }
    rez *= modular_inverse(temp);
    rez %= mod;
    return rez;
}

int score(int x)
{
    return max(100000 * (4 - x), 0);
}

int main()
{
    int x, y;
    cin >> x >> y;
    cout << score(x) + score(y) + ((x == y && x == 1) ? 400000 : 0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
////////////////////////////

#define N 100004
ll d[N+1];
ll a[N+1];
vector<ll>c[N+1];
void dfs(ll k){
    for(auto x:c[k]){
        d[x]=d[k]+1;
        dfs(x);
    }
}
int main(){fastio
    ll n,k;cin>>n>>k;

    rep(i,1,n){
        cin>>a[i];
        if(i!=1)c[a[i]].pb(i);
    }
    ll ans=0;
    if(k==1){
        rep(i,1,n)if(a[i]!=1)ans++;
        cout<<ans<<endl;
        return 0;
    }
    if(a[1]!=1)ans++;
    dfs(1);d[1]=0;
    vector<ll>dep[N];
    rep(i,1,n)dep[d[i]].pb(i);
    l_l dp[n+1];
    for(ll i=N-1;i>=0;i--){
        if(i==0){
            for(auto x:c[1]){
                ans+=dp[x].first;
            }
        }
        for(auto x:dep[i]){
            ll ma=0,sum=0;
            for(auto y:c[x]){
                sum+=dp[y].first;
                if(dp[y].second==k){
                    sum++;
                }else{
                    Max(ma,dp[y].second);
                }
            }
            dp[x]=l_l(sum,ma+1);
        }
    }
    //rep(i,1,n)cout<<dp[i].first<<" "<<dp[i].second<<endl;
    cout<<ans<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define maxn 100001
int n,k,he[maxn],ver[2*maxn],nxt[2*maxn],tot,cnt;
void add(int x,int y){
	ver[++tot]=y;nxt[tot]=he[x];he[x]=tot;
}
int dfs(int x,int fa){
	int maxx=0;
	for(int i=he[x];i;i=nxt[i]){
		if(ver[i]==fa)continue;
		maxx=max(maxx,dfs(ver[i],x));
	}if(x==1)return 0;
	if(maxx==k-1&&fa!=1){
		//cout<<x<<endl;
		cnt++;return 0;
	}else return maxx+1;
}
int main(){
	scanf("%d%d",&n,&k);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(i!=1)add(i,x),add(x,i);
		else if(x!=1)cnt++;
	}dfs(1,0);
	printf("%d",cnt);
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using vi=vector<int>;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }

vector<int> Eratosthenes(int N){
	vector<int> arr(N,1);
    vector<int> res;
	for(int i = 2; i < sqrt(N); i++){
		if(arr[i]){
			for(int j = 0; i * (j + 2) < N; j++){
				arr[i *(j + 2)] = 0;
			}
		}
	}

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

const ll INF = 1LL << 60;

ll N;
vector<ll> a;
int main() {
    cin >> N;
    a.resize(N + 1);
    rep(i, N) cin >> a[i + 1];
    vector<ll> ans(N + 1, -1);
    for (int i = N; i > 0; i--) {
        int loop_cnt = N / i;
        if (loop_cnt >= 2) {
            ll tmp = 0;
            for (int j = i + i; j <= N; j += i) {
                tmp += ans[j];
            }
            tmp = tmp % 2;
            ans[i] = tmp ^ a[i];
        } else {
            ans[i] = a[i];
        }
    }
    set<ll> st;
    for (int i = 1; i <= N; i++) {
        if (ans[i] == 1) {
            st.insert(i);
        };
    }

    cout << st.size() << endl;

    for (auto v : st) {
        cout << v << " ";
    }
    cout << "\n";
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define uu unsigned
#define io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define MOD 1000000007
#define pl pair<ll,ll>
#define mkp make_pair
#define ss second
#define ff first
#define mit(a,b) map<a,b>::iterator
#define sit(a) set<a>::iterator
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define printcas cout<<"Case "<<cas<<": "
#define _ continue;
#define endl '\n'
#define __ cout<<endl;continue;
const ll oo=(1ll<<60);
int gi(){
	int x=0,w=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')w=0,ch=getchar();
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
vector<vector<vector<double>>> dp(305,vector<vector<double>>(305,vector<double>(305,-1.0)));
double n;
double e(double t1,double t2,double t3)
{
    if(dp[(int)t1][(int)t2][(int)t3]!=-1) return dp[(int)t1][(int)t2][(int)t3];
    return dp[(int)t1][(int)t2][(int)t3]=(1.0/(t1+t2+t3))*(n+((!t1)?0:(t1*e(t1-1.0,t2,t3)))+((!t2)?0:(t2*e(t1+1.0,t2-1.0,t3)))+((!t3)?0:(t3*e(t1,t2+1.0,t3-1.0))));
}
int main()
{
     //io;
    //string hudai(1000000,'a');
    int T=1;
    //T=gi();
     for(int cas=1;cas<=T;cas++)
    {
        double t1=0.0,t2=0.0,t3=0.0,x;
       cin>>n;
       for(int i=1;i<=n;i++)
       {
           cin>>x;
           if(x==1)t1+=1.0;
           else if(x==2) t2+=1.0;
           else t3+=1.0;
       }
//       int mx=max(t1,max(t2,t3));
//       for(int i=0;i<=n+2;i++)for(int j=0;j<=n+2;j++)for(int k=0;k<=n+2;k++)dp[i][j][k]=-1;
       dp[0][0][0]=0;
       printf("%.15lf",e(t1,t2,t3));

    }
    return 0;
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldb = long double;

//BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree, chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun

#define REP(i,a,b) for(ll i = a; i < b; ++i)
#define PRI(s) cout << s << endl
#define PRIF(v, n) printf("%."#n"f\n", v)
#define PRIY PRI("Yes")
#define PRIN PRI("No")
#define MINS(a,b) a = min(a,(decltype(a))(b))
#define MAXS(a,b) a = max(a,(decltype(a))(b))

using arr = array<ll, 3>;
int main() {
	auto proc = [](string str) {
		int bra = 0;
		int cket = 0;
		for (char c : str) {
			if (c == '(') {
				++bra;
			}
			else {
				if (bra > 0) --bra;
				else ++cket;
			}
		}
		return arr{cket, bra, bra - cket};
	};
	auto cmp = [](arr a, arr b) {
		if (a[0] == b[0])return a[1] > b[1];
		return a[0] < b[0];
	};
	auto cmp2 = [](arr a, arr b) {
		if (a[0] == b[0])return a[1] > b[1];
		return a[0] > b[0];
	};
	ll N;
	cin >> N;
	vector<arr> plus,minus;
	REP(i, 0, N) {
		string str;
		cin >> str;
		auto a = proc(str);
		if (a[2] >= 0) plus.push_back(a);
		else minus.push_back(a);
	}
	sort(plus.begin(), plus.end(), cmp);
	if (plus.size() == 0 || plus[0][0] > 0) {
		PRIN;
		return 0;
	}
	sort(minus.begin(), minus.end(), cmp2);
	int last = -1;
	REP(i, 0, minus.size()) {
		if (minus[i][1] == 0) {
			last = i;
			break;
		}
	}
	if (last == -1) {
		if (minus.size() != 0)PRIN;
		else {
			for (auto p : plus) {
				if (p[0] != 0 || p[1] != 0) {
					PRIN;
					return 0;
				}
			}
			PRIY;
		}
		return 0;
	}
	int cur = 0;
	for (auto p : plus) {
		cur -= p[0];
		if (cur < 0) {
			PRIN;
			return 0;
		}
		cur += p[1];
	}
	REP(i, 0, minus.size()) {
		if (i == last)continue;
		auto p = minus[i];
		cur -= p[0];
		if (cur < 0) {
			PRIN;
			return 0;
		}
		cur += p[1];
	}
	cur -= minus[last][0];
	if (cur == 0)PRIY;
	else PRIN;
	return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <iomanip>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
wGraph InputWGraph(ll N,ll M){
    wGraph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b,c;
        cin >> a >> b >> c;
        a--;b--;
        G[a].push_back(make_pair(b,c));
        G[b].push_back(make_pair(a,c));
    }
    return G;
}
wGraph InputDWGraph(ll N, ll M){
    wGraph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b,c;
        cin >> a >> b >> c;
        a--;b--;
        G[a].push_back(make_pair(b,c));
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}

void OutputGraph(wGraph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j].first <<"/" << G[i][j].second << ",";
        }
        cout << endl;
    }
}

void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    string N;
    cin >> N;
    ll digit = N.size();
    vector<vector<ll>>memo(2);
    for (int yi = 0; yi < 2; ++yi) {
        memo[yi] = vector<ll>(digit);
        for (int xi = 0; xi < digit; ++xi) {
            memo[yi][xi] = (1ll << 50);
        }
    }
    ll first = N[0] - '0';
    memo[0][0] = min(11 - first,first);
    memo[1][0] = min(11 - first - 1,first + 1);

    for (int i = 1; i < digit; ++i) {
        ll num = N[i] - '0';
        memo[0][i] = min(memo[0][i-1] + num,memo[1][i-1] + (10 - num));
        memo[1][i] = min(memo[0][i-1] + num + 1,memo[1][i-1] + (10 - num - 1));
    }
    cout << (memo[0][digit-1]) << endl;
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
    R(N,X);
    LL need=0;
    priority_queue<PLL>h0,h1,h2;
    REP(i,N){
        R(b[i],l[i],u[i]);
        need+=b[i]*l[i];
        h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
    }
    LL an=0;
    while(need>=h0.top().F){
        an+=X;
        used[h0.top().S]=1;
        need-=h0.top().F;
        h0.pop();
    }
    if(need<=0){
        W(an);
        return 0;
    }
    int add_id=h0.top().S;
    LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
    LL mi=1e18;
    REP(i,N){
        if(used[i]){
            LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);

            if(l[i]*b[i]>=need2){
                mi=min(mi,(need2+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
        else{
            if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
            if(l[i]*b[i]>=need){
                mi=min(mi,(need+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
    }
    an+=mi;
    W(an);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1000000007;
const int BASE = 27;
const int oo = 1e9;
const double EPS = 1e-9;
const int MX = 2e5 + 1;

int n;
ll  x;
struct exam {
  ll b, u, v;
};
exam e[MX];

struct line {
  ll a, b, idx;
  line() {
    a = b = idx = 0;
  }
  line(ll _a, ll _b, int _idx) {
    a = _a, b = _b, idx = _idx;
  }
  ll eval(ll x) {
    return a * x + b;
  }
  bool operator<( line &rhs) {
    if(eval(x) == rhs.eval(x))
      return a < rhs.a;
    return eval(x) < rhs.eval(x);
  }
};

bool check(ll rem) {
  vector<line> all;
  ll sc = 0;
  for (int i = 1; i <= n; ++i) {
    sc -= e[i].u * e[i].b;
    ll a1 = e[i].v;
    ll b1 = -e[i].b * (e[i].v - e[i].u);
    all.push_back(line(a1, b1, i));
  }
  if(sc == 0)return true;
  if(rem == 0)return false;
  sort(all.begin(),all.end());
  ll g1 = rem/x,g2;
  if(rem%x == 0)
    g2 = x,g1--;
  else g2 = rem%x;
  ll sm = 0;
  for(int j = all.size()-1;j>= all.size()-g1;--j)
    sm += all[j].eval(x);

  for(int j = all.size()-g1; j < all.size();++j){
    ll nwSm = sm-all[j].eval(x)+all[all.size()-g1-1].eval(x);
    ll mx = max(all[j].eval(g2),line(e[all[j].idx].u,0,all[j].idx).eval(g2));
    if(mx+nwSm+sc >= 0)
      return true;

  }
  for(int j = 0 ; j+g1 < all.size();++j){
    ll mx = max(all[j].eval(g2),
      line(e[all[j].idx].u,0,all[j].idx).eval(g2));
    if(mx+sm+sc >= 0)return true;
  }
  return false;
}

int main() {
  cin >> n >> x;
  for (int b, l, u, i = 1; i <= n; ++i) {
    scanf("%lld%lld%lld", &e[i].b, &e[i].u, &e[i].v);
  }

  ll lo = 0 , hi = 1ll * n * x, best = -1;
  while (lo <= hi) {
    ll md = (lo + hi) / 2;
    if (check(md)) {
      best = md;
      hi = md - 1;
    } else {
      lo = md + 1;
    }
  }
  cout << best << endl;
  return 0;
}


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

int main() {
	vector<int>a(5);
	for (int i = 0; i < 5; i++)cin >> a.at(i);
	sort(a.begin(), a.end());
	int k;
	cin >> k;
	for (int i = 0; i < 4; i++) {
		for (int j = i; j < 5; j++) {
			if (a.at(j) - a.at(i) <= k)continue;
			else {
				cout << ":(" << endl;
				return 0;
			}
		}
	}
	cout << "Yay!" << endl;
}

#include<iostream>
using namespace std;
class dice
{
  private:
  int status[6];
  public:
  dice();
  int showfront(void);
  bool is(int,int);
  bool isfront(int);
  void rotate(char);
  bool rtfront(int);
  bool operator==(dice);
  bool eqv(dice);
};

bool dice::eqv(dice di2)
{
  if(!di2.rtfront((*this).showfront()))
    return false;
  else
  {
    int i;
    for(i=0;i<4;i++)
    {
      if ((*this)==di2)
      {
        return true;
      }
      di2.rotate('E');
    }
    if(i==4) return false;
  }
}

bool dice::rtfront(int a)
{
  int j;
  for(j=0;j<=3;j++)
  {
    if((*this).isfront(a)) break;
    (*this).rotate('N');
  }
  if(j==4)
  {
    (*this).rotate('E');
    (*this).rotate('S');
  }
  if(!(*this).isfront(a))
  {
    (*this).rotate('N');
    (*this).rotate('N');
  }
  if(!(*this).isfront(a))
    return false;
  else
    return true;
}

bool dice::operator==(dice d)
{
  int i=0;
  for(i=0;i<6;i++)
    if((*this).status[i]!=d.status[i]) break;
  if(i==6) return true;
  else return false;
}

dice::dice()
{
  for(int i=0;i<6;i++)
    cin>>status[i];
}

bool dice::isfront(int a)
{
  if(status[1]==a) return true;
  else return false;
}

bool dice::is(int a,int b)
{
  if((status[0]==a)&&(status[1]==b)) return true;
  else return false;
}

int dice::showfront()
{
  return status[1];
}

void dice::rotate(char s)
{
  switch(s)
  {
    int tmp;
    case 'N':
      tmp=status[0];
      status[0]=status[1];
      status[1]=status[5];
      status[5]=status[4];
      status[4]=tmp;
      break;
    case 'E':
      tmp=status[0];
      status[0]=status[3];
      status[3]=status[5];
      status[5]=status[2];
      status[2]=tmp;
      break;
    case 'W':
      tmp=status[0];
      status[0]=status[2];
      status[2]=status[5];
      status[5]=status[3];
      status[3]=tmp;
      break;
    case 'S':
      tmp=status[0];
      status[0]=status[4];
      status[4]=status[5];
      status[5]=status[1];
      status[1]=tmp;
      break;
  }
}

int main()
{
  int n;
  cin>>n;
  dice di[n];
  bool ans=true;
  for(int i=0;i<n;i++)
    for(int j=i+1;j<n;j++)
      if(di[i].eqv(di[j]))
      {
        ans=false;
        break;
      }
  if(ans==true)
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;

}


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

//upper_bound, lower_boundを用いた
//のになぜか通らない・・・

int main(){
	int n;
	long long a;
	if(scanf("%d", &n)==EOF){return 0;}

	long long top[n];
	long long center[n];
	long long down[n];

	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} top[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} center[i] = a;}
	for(int i = 0; i<n; i++){if(scanf("%lld", &a)==EOF){return 0;} down[i] = a;}

	// top, downをソート
	sort(top, top+n);
	sort(down, down+n);
	//for(int i = 0; i<n; i++){cout << top[i] << ", "<<endl;}
	//for(int i = 0; i<n; i++){cout << down[i] << ", "<<endl;}

	long long sum = 0;
	//int ok1, ng1, ok2, ng2, mid;
	int ok1, ok2;

	for(int i = 0; i< n; i++){
		//center[i]より小さいtopの数を探す
		ok1 = lower_bound(top, top+n, center[i]) - top;
		ok2 = upper_bound(down, down+n, center[i]) - down;

		//cout << "top[ok1-1]" <<  top[ok1-1] << ", center:"<< center[i] <<", top[ok1]:"<< top[ok1] << endl;
		//cout << "down[ok2-1]:" << down[ok2-1] <<", center:"<< center[i] <<", down[ok2]:" << down[ok2] << endl;
		sum += (long long)ok1 * (long long)(n - ok2);
	}
	cout << sum << endl;
	return 0;
}

//include,using,define等
#pragma region header
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
#include <cstring>

// ===============================================================
//using系
#pragma region header
using namespace std;
using ll = long long;
using lint = long long;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
using ui = unsigned int;
using qul = queue<ll>;
using pql = priority_queue<ll>;
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,1,0,-1 };
constexpr ll mod = 1000000007;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
//define
#pragma region header
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, n) for(ll i = 1; i <= n; i++)
#define INF (ll)10000000000000000
#define mod (ll)1000000007
#pragma endregion
#pragma endregion

int main() {
	int n; cin >> n;
	vector<int> a(n), b(n), c(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];
	for (int i = 0; i < n; i++)
		cin >> c[i];
	vector<int> d(n);
	vector<int> e(n);
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	sort(c.begin(), c.end());
	lint Finalans = 0;
	for (int i = 0; i < n; i++) {
		Finalans += (lower_bound(a.begin(), a.end(), b[i]) - a.begin()) * (n - (upper_bound(c.begin(),c.end(),b[i])-c.begin()));
	}
	printf("%lld\n", Finalans);

	return 0;
}