stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<cmath>

#include<algorithm>

#include<cstdio>

#include<stack>

#include<queue>

#include<map>

#include<cstring>



using namespace std;



const int MAX=1e5+10;



int a[MAX];

int p[MAX];



long long mod_exp(int b){

	long long res,t;

	res=1;

	t=2;

	while(b){

		if(b&1){

			res=res*t%1000000007;

		}

		t=t*t%1000000007;

		b>>=1;

	}

	return res;

}



int main(){

	int n;

	cin>>n;

	memset(p,0,sizeof(p));

	for(int i=0;i<n;i++){

		scanf("%d",&a[i]);

		p[a[i]]++;

	}

	int cnt=0;

	bool flag=true;

	for(int i=0;i<n;i++){

		if((n+a[i]+1)%2!=0||(n-a[i]+1)%2!=0||(p[0]!=1&&n%2==1)||(p[a[i]]!=2&&a[i]!=0)){

			cout<<"0"<<endl;

			flag=false;

			break;

		}

		if(p[i]==2)

		cnt++;

	}

	if(flag)

	cout<<mod_exp(cnt)<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4249037941040623x):
// ====================================

//   author: M_sea

//   website: https://m-sea-blog.com/

// ====================================

#include <bits/stdc++.h>

#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)

#define debug(...) fprintf(stderr,__VA_ARGS__)

using namespace std;

typedef long long ll;



int read() {

	int X=0,w=1; char c=getchar();

	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }

	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();

	return X*w;

}



const int N=100000+10;

const int mod=1e9+7;

int qpow(int a,int b) { int c=1;

	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;

	return c;

}



int n,a[N];



int main() {

	n=read();

	for (int i=1;i<=n;++i) a[i]=read();

	sort(a+1,a+n+1);

	if (n&1) {

		int flag=(a[1]==0);

		for (int i=2;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	} else {

		int flag=1;

		for (int i=1;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	}

	return 0;

}
// Slower C++ solution:


#include<bits/stdc++.h>

using namespace std;



long long con=1000000007,a[100070],cnt[100070],fac[100050];



long long fact(long long n)

{

    if(n==0)

    {

        return 1;

    }

    long long mul=1,i;



    mul=fac[n-1]*n;

    mul=mul%con;



    return mul;

}



int main()

{



    long long n,i,ans=1;



    for(i=0; i<100050; i++)

    {

        fac[i]=fact(i);

    }

    scanf("%lld",&n);



    for(i=0; i<n; i++)

    {

        scanf("%lld",&a[i]);

        cnt[a[i]]++;

    }



    for(i=1;i<100050; i++)

    {

        if(cnt[i]%2!=0)

        {

            printf("0");

            return 0;

        }





        ans*=fac[cnt[i]];

        ans=ans%con;

    }



    printf("%lld",ans);

    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.4091718178857287x):
// ====================================

//   author: M_sea

//   website: https://m-sea-blog.com/

// ====================================

#include <bits/stdc++.h>

#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)

#define debug(...) fprintf(stderr,__VA_ARGS__)

using namespace std;

typedef long long ll;



int read() {

	int X=0,w=1; char c=getchar();

	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }

	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();

	return X*w;

}



const int N=100000+10;

const int mod=1e9+7;

int qpow(int a,int b) { int c=1;

	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;

	return c;

}



int n,a[N];



int main() {

	n=read();

	for (int i=1;i<=n;++i) a[i]=read();

	sort(a+1,a+n+1);

	if (n&1) {

		int flag=(a[1]==0);

		for (int i=2;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	} else {

		int flag=1;

		for (int i=1;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

#include <cstdlib>

#include <cstdio>

#include <algorithm>

#include <cmath>

#include <iomanip>

#include <vector>

using namespace std;

const int maxn = 1e5+100;

const int MOD = 1e9+7;

const double Pi = 3.141592654;

typedef long long ll;

typedef unsigned long long ull;





int main()

{

    int n;

    cin >> n;

    int a[maxn];

    int vis[maxn];

    int st;

    ll ans = 1;

    memset(vis, 0, sizeof(vis));

    for(int i=1 ; i<=n ; ++i)

    {

        cin >> a[i];

        vis[a[i]] ++;

    }

    if(!(n%2))//even

    {

        if(vis[0])

        {

            cout << 0 << endl;

            return 0;

        }

        for(int i=1 ; i<=n ; i+=2)

        {

            ans = (ans%MOD)*2%MOD;

            if(vis[i] != 2)

        {

            cout << 0 << endl;

            return 0;

        }

        }

    }

    else

    {

        if(vis[0] != 1)

        {

            cout << 0 << endl;

            return 0;

        }

        for(int i=2 ; i<=n ; i+=2)

        {

            ans = (ans%MOD)*2%MOD;

            if(vis[i] != 2)

        {

            cout << 0 << endl;

            return 0;

        }

        }

    }

    cout << ans << endl;



}



//9(4(7(1)(2))(3(6)(8)))(5(10)(11(12)))


// Faster C++ solution for the same problem (measured speedup: 1.4288638114897814x):
// ====================================

//   author: M_sea

//   website: https://m-sea-blog.com/

// ====================================

#include <bits/stdc++.h>

#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)

#define debug(...) fprintf(stderr,__VA_ARGS__)

using namespace std;

typedef long long ll;



int read() {

	int X=0,w=1; char c=getchar();

	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }

	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();

	return X*w;

}



const int N=100000+10;

const int mod=1e9+7;

int qpow(int a,int b) { int c=1;

	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;

	return c;

}



int n,a[N];



int main() {

	n=read();

	for (int i=1;i<=n;++i) a[i]=read();

	sort(a+1,a+n+1);

	if (n&1) {

		int flag=(a[1]==0);

		for (int i=2;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	} else {

		int flag=1;

		for (int i=1;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <bits/stdc++.h>

using namespace std;



int main() {

	//cout<<"GfG!";

	long long int n, a[1000000], i, j, k, f = 1 ;

	unsigned long long int tot = 1, M = 1000000007;

	cin>>n;

	for (i = 0; i < n; i++)

	{

	    cin>>a[i]; 

	}

	sort(a, a + n);

	i = 0;  j = 0; 

	while (i < n)

	{

	    if (n == 1)

	    {   break;  }

	    else if (a[i] == 0 && a[i+1] != 0)

	    {

	        i++;   

	    }

	    else if (a[i] == 0 && a[i+1] == 0)

	    {       f = 0;  break;  }

	    else

	    {

	        if (i == n - 1)

	        {       f = 0;  break;  }

	        else if(a[i+1] == a[i] && (a[j+2] - a[j] == 2))

	        {

	            tot = (tot * 2) % M;

	            i += 2;   j += 2;   

	            if (j == n - 2)

	                j = 0;

	                

	        }

	        else

	        {

	            f = 0;  break;

	        }

	    }

	     

	}

	if (f)

	    cout<<tot;

	else

	    cout<<"0";

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.199217983097722x):
// ====================================

//   author: M_sea

//   website: https://m-sea-blog.com/

// ====================================

#include <bits/stdc++.h>

#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)

#define debug(...) fprintf(stderr,__VA_ARGS__)

using namespace std;

typedef long long ll;



int read() {

	int X=0,w=1; char c=getchar();

	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }

	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();

	return X*w;

}



const int N=100000+10;

const int mod=1e9+7;

int qpow(int a,int b) { int c=1;

	for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;

	return c;

}



int n,a[N];



int main() {

	n=read();

	for (int i=1;i<=n;++i) a[i]=read();

	sort(a+1,a+n+1);

	if (n&1) {

		int flag=(a[1]==0);

		for (int i=2;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	} else {

		int flag=1;

		for (int i=1;i<=n;i+=2)

			if (a[i]!=i||a[i+1]!=i) { flag=0; break; }

		if (flag) printf("%d\n",qpow(2,n/2));

		else puts("0");

	}

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

    double a,b,c;

    cin>>a>>b>>c;

    c=min(c,b-1);

    cout<<floor(a*c/b)-a*floor(c/b)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.367780649554507x):
#include <stdio.h>

#include <math.h>

#define min(a,b) ((a) > (b) ? (b) : (a))

int main() {

    long long int x,ans, a, b,n;

    scanf("%lld %lld %lld",&a, &b, &n);



     x = min(n, b-1);

    ans = floor(a*x/b) - a * floor(x/b);

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



}
// Slower C++ solution:
#include<iostream>

#include<cmath>

using namespace std;

#define ll long long

ll a,b,n;

ll x;

ll gg;

int main(){

	cin>>a>>b>>n;

	x=min(b-1,n);

	gg=a*x/b;

	cout<<gg<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.250507725377486x):
#include <stdio.h>

#include <math.h>

#define min(a,b) ((a) > (b) ? (b) : (a))

int main() {

    long long int x,ans, a, b,n;

    scanf("%lld %lld %lld",&a, &b, &n);



     x = min(n, b-1);

    ans = floor(a*x/b) - a * floor(x/b);

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



}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int main()

{

    long int a,b,c;

    cin>>a>>b>>c;

    c=min(c,b-1);

    cout<<floor(a*c/b)<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.310979335353453x):
#include <stdio.h>

#include <math.h>

#define min(a,b) ((a) > (b) ? (b) : (a))

int main() {

    long long int x,ans, a, b,n;

    scanf("%lld %lld %lld",&a, &b, &n);



     x = min(n, b-1);

    ans = floor(a*x/b) - a * floor(x/b);

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



}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define ll long long

const int maxn = 2e5 + 100; 

const double pi = acos(-1.0);



inline ll read()

{

    ll kk=0,f=1;

    char cc=getchar();

    while(cc<'0'||cc>'9'){if(cc=='-')f=-1;cc=getchar();}

    while(cc>='0'&&cc<='9'){kk=(kk<<1)+(kk<<3)+cc-'0';cc=getchar();}

    return kk*f;

}



ll floor(ll a,ll b);

int main(void)

{

	ll a,b,n;

	a=read();b=read();n=read();

	

	ll x=min(n,b-1);

	ll ans = floor(a*x/b) - a*floor(x/b); 

	printf("%lld",ans);

	return 0;

}



ll floor(ll a,ll b)

{

	ll c=a/b;

	return a/b;

}

// Faster C++ solution for the same problem (measured speedup: 5.173066560712518x):
#include <stdio.h>

#include <math.h>

#define min(a,b) ((a) > (b) ? (b) : (a))

int main() {

    long long int x,ans, a, b,n;

    scanf("%lld %lld %lld",&a, &b, &n);



     x = min(n, b-1);

    ans = floor(a*x/b) - a * floor(x/b);

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



}
// Slower C++ solution:
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



ll g(ll x) {

  ll l = 0, r = n + 1;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    f(c1) < f(c2) ? l = c1 : r = c2;

  }

  return f(l);

}



int main() {

  cin >> a >> b >> n;



  ll l = 0, r = n + 1;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    g(c1) < g(c2) ? l = c1 : r = c2;

  }

  if (f(0) > f(r)) r = 0;

  else if (f(n) > f(r)) r = n;

  cout <<  f(r) << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.6553463962562243x):
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



int main() {

  cin >> a >> b >> n;



  cout << f(min(b - 1, n)) << endl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



ll g(ll x) {

  ll l = 0, r = n + 1;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    f(c1) < f(c2) ? l = c1 : r = c2;

  }

  return f(l);

}



int main() {

  cin >> a >> b >> n;



  ll l = 0, r = n + 1;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    g(c1) < g(c2) ? l = c1 : r = c2;

  }

  if (f(0) > f(r)) r = 0;

  else if (f(n) > f(r)) r = n;

  cout <<  f(r) << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.6553463962562243x):
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



int main() {

  cin >> a >> b >> n;

  cout << f(min(b - 1, n)) << endl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



ll g(ll x) {

  ll l = 0, r = n;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    f(c1) < f(c2) ? l = c1 : r = c2;

  }

  return f(l);

}



int main() {

  cin >> a >> b >> n;



  ll l = 0, r = n;

  rep(i, 100) {

    c1 = (l * 2 + r) / 3;

    c2 = (r * 2 + l) / 3;

    g(c1) < g(c2) ? l = c1 : r = c2;

  }

  if (f(0) > f(r)) r = 0;

  else if (f(n) > f(r)) r = n;

  cout <<  f(r) << endl;

  return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.654796243232944x):
#include <bits/stdc++.h>



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

using namespace std;

typedef long long ll;

const int INF = 1e9;



ll a, b, n, c1, c2, ans = 0;



ll f(ll x) { return floor(a * x / b) - a * floor(x / b); }



int main() {

  cin >> a >> b >> n;

  cout << f(min(b - 1, n)) << endl;

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

#define MAXN (1 << 18)

#define ll long long

 

int n, q, A, B;

int x[MAXN];

ll seg1[MAXN << 1], seg2[MAXN << 1];

 

ll query(ll *seg, int u, int l, int r, int ql, int qr){

	if(ql <= l && r <= qr) return seg[u];

	int m = l + r >> 1, ls = u << 1, rs = ls | 1;

	ll ret = LLONG_MAX;

	if(!(m < ql)) ret = std::min(ret, query(seg, ls, l, m, ql, qr));

	if(!(qr < m + 1)) ret = std::min(ret, query(seg, rs, m + 1, r, ql, qr));

	return ret;

}

 

void insert(ll *seg, int u, int l, int r, int pos, ll val){

	if(l == r){

		seg[u] = val;

		return;

	}

	int m = l + r >> 1, ls = u << 1, rs = ls | 1;

	if(pos <= m) insert(seg, ls, l, m, pos, val);

	else insert(seg, rs, m + 1, r, pos, val);

	seg[u] = std::min(seg[ls], seg[rs]);

}

 

int main(){

	scanf("%d%d%d%d", &n, &q, &A, &B);

	memset(seg1, 0x7f, sizeof seg1);

	memset(seg2, 0x7f, sizeof seg2);

	x[0] = B;

	insert(seg1, 1, 1, n, A, A);

	insert(seg2, 1, 1, n, A, -A);

	ll delta = 0;

	for(int i = 1; i <= q; ++ i){

		scanf("%d", x + i);

		

		ll tmp1 = query(seg1, 1, 1, n, x[i] + 1, n) - x[i];

		ll tmp2 = query(seg2, 1, 1, n, 1, x[i]) + x[i];

		tmp1 = std::min(tmp1, tmp2);

		

//		ll now = query(seg1, 1, 1, n, x[i - 1], x[i - 1]) - x[i - 1] + delta;

//		if(tmp1 < now){

			insert(seg1, 1, 1, n, x[i - 1], tmp1 + x[i - 1] - std::abs(x[i] - x[i - 1]));

			insert(seg2, 1, 1, n, x[i - 1], tmp1 - x[i - 1] - std::abs(x[i] - x[i - 1]));

//		}

		

		delta += std::abs(x[i] - x[i - 1]);

	}

	ll ans = LLONG_MAX;

	for(int i = 1; i <= n; ++ i){

		ans = std::min(ans, query(seg1, 1, 1, n, i, i) - i + delta);

	}

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.315528020047126x):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5 + 100;



int n, q, x[N];

ll sum[N], f[N], fen[2][N];



void update(int id, int p, ll val) {

  if (p <= 0) return;

  for (; p; p -= p & -p) fen[id][p] = min(fen[id][p], val);

}



ll query(int id, int p) {

  ll res = 1e18;

  if (p <= 0) return res;

  for (; p <= n; p += p & -p) res = min(res, fen[id][p]);

  return res;

}



int main() {

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

  cin >> n >> q; q += 2;

  for (int i = 1; i <= q; ++i) {

    cin >> x[i];

    if (i > 1) sum[i] = sum[i - 1] + abs(x[i] - x[i - 1]);

  }

  memset(fen, 0x3f, sizeof(fen));

  for (int i = q - 1; i; --i) {

    f[i] = 1e18;

    f[i] = min(f[i], sum[q] - sum[i + 1] + f[q]);

    f[i] = min(f[i], query(0, x[i]) - x[i] - sum[i + 1]);

    f[i] = min(f[i], query(1, n + 1 - x[i]) + x[i] - sum[i + 1]);

    update(0, x[i + 1], f[i] + sum[i] + x[i + 1]);

    update(1, n + 1 - x[i + 1], f[i] + sum[i] - x[i + 1]);

  }

  cout << f[1];

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

 

#define MAXN (1 << 18)

#define ll long long

 

int n, q, A, B;

int x[MAXN];

ll seg1[MAXN << 1], seg2[MAXN << 1];

 

ll query(ll *seg, int u, int l, int r, int ql, int qr){

	if(ql <= l && r <= qr) return seg[u];

	int m = l + r >> 1, ls = u << 1, rs = ls | 1;

	ll ret = LLONG_MAX;

	if(!(m < ql)) ret = std::min(ret, query(seg, ls, l, m, ql, qr));

	if(!(qr < m + 1)) ret = std::min(ret, query(seg, rs, m + 1, r, ql, qr));

	return ret;

}

 

void insert(ll *seg, int u, int l, int r, int pos, ll val){

	if(l == r){

		seg[u] = val;

		return;

	}

	int m = l + r >> 1, ls = u << 1, rs = ls | 1;

	if(pos <= m) insert(seg, ls, l, m, pos, val);

	else insert(seg, rs, m + 1, r, pos, val);

	seg[u] = std::min(seg[ls], seg[rs]);

}

 

int main(){

	scanf("%d%d%d%d", &n, &q, &A, &B);

	memset(seg1, 0x7f, sizeof seg1);

	memset(seg2, 0x7f, sizeof seg2);

	x[0] = B;

	insert(seg1, 1, 1, n, A, A);

	insert(seg2, 1, 1, n, A, -A);

	ll delta = 0;

	for(int i = 1; i <= q; ++ i){

		scanf("%d", x + i);

		

		ll tmp1 = query(seg1, 1, 1, n, x[i] + 1, n) - x[i];

		ll tmp2 = query(seg2, 1, 1, n, 1, x[i]) + x[i];

		tmp1 = std::min(tmp1, tmp2);

		

//		ll now = query(seg1, 1, 1, n, x[i - 1], x[i - 1]) - x[i - 1] + delta;

//		if(tmp1 < now){

			insert(seg1, 1, 1, n, x[i - 1], tmp1 + x[i - 1] - std::abs(x[i] - x[i - 1]));

			insert(seg2, 1, 1, n, x[i - 1], tmp1 - x[i - 1] - std::abs(x[i] - x[i - 1]));

//		}

		

		delta += std::abs(x[i] - x[i - 1]);

	}

	ll ans = LLONG_MAX;

	for(int i = 1; i <= n; ++ i){

		ans = std::min(ans, query(seg1, 1, 1, n, i, i) - i + delta);

	}

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 9.675303593524344x):
#include<bits/stdc++.h>



#define oo ll(1e16)

#define maxn 200005

#define block 400



using namespace std;

typedef long long ll;



int n, q, a, b;

ll d[maxn];

ll L[maxn], R[maxn];



ll ret = 0;



ll get(int st, int en, int ok) {

  ll ret = oo;



  for (int i = st; i <= en;)

    if (i % block == 1 && i + block <= en + 1) {

      ret = min(ret, (ok ? L[(i - 1) / block] : R[(i - 1) / block]));

      i += block;

    }

    else {

      ret = min(ret, d[i] + (ok ? -i : i));

      ++i;

    }



  return ret;

}



int main(){

  ios_base::sync_with_stdio(0);



  cin >> n >> q >> a >> b;



  fill(d + 1, d + n + 1, oo);

  fill(L, L + n + 1, oo);

  fill(R, R + n + 1, oo);



  d[b] = 0;

  L[(b - 1) / block] = -b;

  R[(b - 1) / block] = b;



  while (q--) {

    int x;

    cin >> x;



    ll tem = min(get(1, x, 1) + x, get(x, n, 0) - x);



    d[a] = min(d[a], tem - abs(x - a));



    int bl = (a - 1) / block;



    L[bl] = oo;

    R[bl] = oo;



    for (int i = max(bl * block + 1, 1); i <= bl * block + block && i <= n; ++i) {

      L[bl] = min(L[bl], d[i] - i);

      R[bl] = min(R[bl], d[i] + i);

    }



    ret += abs(x - a);



   // for (int i = 1; i <= n; ++i) cerr << d[i] + ret << ' '; cerr << "\n\n";



    a = x;

  }



  ll ma = 0;

  for (int i = 1; i <= n; ++i)

    ma = min(ma, d[i]);



  cout << ma + ret;



  return 0;



}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5 + 100;



int n, q, x[N];

ll sum[N], f[N], fen[2][N];



void update(int id, int p, ll val) {

  if (p <= 0) return;

  for (; p; p -= p & -p) fen[id][p] = min(fen[id][p], val);

}



ll query(int id, int p) {

  ll res = 1e18;

  if (p <= 0) return res;

  for (; p <= n; p += p & -p) res = min(res, fen[id][p]);

  return res;

}



int main() {

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

  cin >> n >> q; q += 2;

  for (int i = 1; i <= q; ++i) {

    cin >> x[i];

    if (i > 1) sum[i] = sum[i - 1] + abs(x[i] - x[i - 1]);

  }

  memset(fen, 0x3f, sizeof(fen));

  for (int i = q - 1; i; --i) {

    f[i] = 1e18;

    f[i] = min(f[i], sum[q] - sum[i + 1] + f[q]);

    f[i] = min(f[i], query(0, x[i]) - x[i] - sum[i + 1]);

    f[i] = min(f[i], query(1, n + 1 - x[i]) + x[i] - sum[i + 1]);

    update(0, x[i + 1], f[i] + sum[i] + x[i + 1]);

    update(1, n + 1 - x[i + 1], f[i] + sum[i] - x[i + 1]);

  }

  cout << f[1];

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.178443754408738x):
#include<bits/stdc++.h>



#define oo ll(1e16)

#define maxn 200005

#define block 400



using namespace std;

typedef long long ll;



int n, q, a, b;

ll d[maxn];

ll L[maxn], R[maxn];



ll ret = 0;



ll get(int st, int en, int ok) {

  ll ret = oo;



  for (int i = st; i <= en;)

    if (i % block == 1 && i + block <= en + 1) {

      ret = min(ret, (ok ? L[(i - 1) / block] : R[(i - 1) / block]));

      i += block;

    }

    else {

      ret = min(ret, d[i] + (ok ? -i : i));

      ++i;

    }



  return ret;

}



int main(){

  ios_base::sync_with_stdio(0);



  cin >> n >> q >> a >> b;



  fill(d + 1, d + n + 1, oo);

  fill(L, L + n + 1, oo);

  fill(R, R + n + 1, oo);



  d[b] = 0;

  L[(b - 1) / block] = -b;

  R[(b - 1) / block] = b;



  while (q--) {

    int x;

    cin >> x;



    ll tem = min(get(1, x, 1) + x, get(x, n, 0) - x);



    d[a] = min(d[a], tem - abs(x - a));



    int bl = (a - 1) / block;



    L[bl] = oo;

    R[bl] = oo;



    for (int i = max(bl * block + 1, 1); i <= bl * block + block && i <= n; ++i) {

      L[bl] = min(L[bl], d[i] - i);

      R[bl] = min(R[bl], d[i] + i);

    }



    ret += abs(x - a);



   // for (int i = 1; i <= n; ++i) cerr << d[i] + ret << ' '; cerr << "\n\n";



    a = x;

  }



  ll ma = 0;

  for (int i = 1; i <= n; ++i)

    ma = min(ma, d[i]);



  cout << ma + ret;



  return 0;



}