#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
bool t1=0,t2=0;
int n,x;
vector<int> v;
cin>>n>>x;
v.resize(n);
for(int i=0;i<n;i++)
cin>>v[i];
v.push_back(x);
sort(v.begin(),v.end());
t1=v[0]==v[1];
t2=v[v.size()-1]==v[v.size()-2];
v.resize(unique(v.begin(),v.end())-v.begin());
if(n==1)
cout<<0<<endl;
else if(v[0]==x)
cout<<v[v.size()-2]-x+t2*(v[v.size()-1]-v[v.size()-2])<<endl;
else if(v[v.size()-1]==x)
cout<<v[v.size()-1]-v[1]+t1*(v[1]-v[0])<<endl;
else if((v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))>
(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x)))
cout<<(x-v[0]+(find(v.begin(),v.end(),v[v.size()-1])-find(v.begin(),v.end(),x)>1)*(x-v[0]+v[v.size()-2]-x))<<endl;
else
cout<<(v[v.size()-1]-x+(find(v.begin(),v.end(),x)-find(v.begin(),v.end(),v[0])>1)*(v[v.size()-1]-x+x-v[1]))<<endl;
return 0;
}


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

const int Max=100005;
int ar[Max];

int main()
{
	int n,a;
	while(scanf("%d%d",&n,&a)!=EOF) {
		for(int i=1;i<=n;i++) scanf("%d",&ar[i]);
		sort(ar+1,ar+1+n);
		if(n==1) {printf("0\n");continue;}
		else {
			int ok=ar[n-1]-ar[1]+min(abs(a-ar[1]),abs(ar[n-1]-a));
			int ans=min(ok,min(ar[n]-ar[2]+abs(ar[n]-a),abs(a-ar[2])+ar[n]-ar[2]));
			printf("%d\n",ans);
		}
	}
	return 0;
}


#include<algorithm>
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdio>
#include<cmath>
using namespace std;
//--container
typedef long long ll;
typedef pair<int,int> pi;
const ll up=2000000000;
//--
#define clr(a) memset(a,0,sizeof(a))
ll ar[100010];int n;
void cl(){
	int i,j,t;ll r,a;scanf("%d %I64d",&n,&a);for(i=1;i<=n;scanf("%I64d",&ar[i++]));
	if(n==1){printf("0\n");return;};
	sort(ar+1,ar+1+n);r=ar[n-1]-ar[1]+min(abs(a-ar[1]),abs(a-ar[n-1]));
	r=min(r,ar[n]-ar[2]+min(abs(a-ar[2]),abs(a-ar[n])));
	printf("%I64d\n",r);
};
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	cl();
	return 0;
};


#include<algorithm>
#include<iostream>
#include<cstring>
#include<climits>
#include<cstdio>
#include<cmath>
using namespace std;
//--container
typedef long long ll;
typedef pair<int,int> pi;
const ll up=2000000000;
//--
#define clr(a) memset(a,0,sizeof(a))
ll ar[100010];int n;
void cl(){
	int i,j,t;ll r,a;scanf("%d %I64d",&n,&a);for(i=1;i<=n;scanf("%I64d",&ar[i++]));
	if(n==1){printf("0\n");return;};
	r=ar[n-1]-ar[1]+min(abs(a-ar[1]),abs(a-ar[n-1]));
	r=min(r,ar[n]-ar[2]+min(abs(a-ar[2]),abs(a-ar[n])));
	printf("%I64d\n",r);
};
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
#endif
	cl();
	return 0;
};


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

const int maxn = 1e5 + 10;

int n, a;
int x[maxn];
map<int, int> flag;

int main()
{
	cin >> n >> a;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i];
		flag[x[i]]++;
	}
	if (n == 1 || flag[a] >= n - 1)
	{
		cout << 0;
		return 0;
	}
	sort(x + 1, x + n + 1);
	int ans = 0x3f3f3f3f;
	if (a <= x[1])
	{
		if (flag[x[n]] > 1) ans = min(x[n] - a, ans);
		else ans = min(x[n - 1] - a, ans);
	}
	else
	{
		if (a >= x[n])
		{
			if (flag[x[1]] > 1) ans = min(a - x[1], ans);
			else ans = min(a - x[2], ans);
		}
		else
		{
			if (flag[x[1]] > 1) ans = min(a - x[1] + 2 * (x[n] - a), ans);
			else
			{
				if (x[2] < a) ans = min(min(2 * (a - x[2]) + x[n] - a, a - x[2] + 2 * (x[n] - a)), ans);
				else  ans = min((x[n] - a), ans);
			}
			if (flag[x[n]] > 1) ans = min(x[n] - a + 2 * (a - x[1]), ans);
			else
			{
				if (x[n - 1] > a) ans = min(min(2 * (x[n - 1] - a) + a - x[1], x[n - 1] - a + 2 * (a - x[1])), ans);
				else ans = min(a - x[1], ans);
			}
		}

	}
	cout << ans;
	return 0;
}


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

const int maxn = 1e5 + 10;

int n, a;
int x[maxn];
map<int, int> flag;

int main()
{
	cin >> n >> a;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i];
		flag[x[i]]++;
	}
	if (n == 1 || flag[a] >= n - 1)
	{
		cout << 0;
		return 0;
	}
	sort(x + 1, x + n + 1);
	int ans = 0x3f3f3f3f;
	if (a <= x[1])
	{
		if (flag[x[n]] > 1) ans = min(x[n] - a, ans);
		else ans = min(x[n - 1] - a, ans);
	}
	else
	{
		if (a >= x[n])
		{
			if (flag[x[1]] > 1) ans = min(a - x[1], ans);
			else ans = min(a - x[2], ans);
		}
		else
		{
			if (flag[x[1]] > 1) ans = min(a - x[1] + 2 * (x[n] - a), ans);
			else
			{
				if (x[2] < a) ans = min(a - x[2] + 2 * (x[n] - a), ans);
				else  ans = min((x[n] - a), ans);
			}
			if (flag[x[n]] > 1) ans = min(x[n] - a + 2 * (a - x[1]), ans);
			else
			{
				if (x[n - 1] > a) ans = min(x[n - 1] - a + 2 * (a - x[1]), ans);
				else ans = min(a - x[1], ans);
			}
		}
	}
	cout << ans;
	return 0;
}


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

const int maxn = 1e5 + 10;

int n, a, minp=0x3f3f3f3f, maxp;
int x[maxn];
map<int, int> flag;

int main()
{
	cin >> n >> a;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i];
		flag[x[i]]++;
		minp = min(minp, x[i]);
		maxp = max(maxp, x[i]);
	}
	if (n == 1)
	{
		cout << 0;
		return 0;
	}
	sort(x + 1, x + n + 1);
	int ans = 0x3f3f3f3f;
	if (a <= x[1])
	{
		if (flag[x[n]] > 1) ans = min(x[n] - a, ans);
		else ans = min(x[n - 1] - a, ans);
	}
	else
	{
		if (a >= x[n])
		{
			if (flag[x[1]] > 1) ans = min(a - x[1], ans);
			else ans = min(a - x[2], ans);
		}
		else
		{
			if (flag[x[1]] > 1) ans = min(a - x[1] + 2 * (x[n] - a), ans);
			else
			{
				if (x[2] <= a) ans = min(a - x[2] + 2 * (x[n] - a), ans);
				else  ans = min((x[n] - a), ans);
			}
			if (flag[x[n]] > 1) ans = min(x[n] - a + 2 * (a - x[1]), ans);
			else
			{
				if (x[n - 1] >= a) ans = min(x[n - 1] - a + 2 * (a - x[1]), ans);
				else ans = min(a - x[1], ans);
			}
		}
	}
	cout << ans;
	return 0;
}


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

const int maxn = 1e5 + 10;

int n, a;
int x[maxn];
map<int, int> flag;

int main()
{
	cin >> n >> a;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i];
		flag[x[i]]++;
	}
	if (n == 1)
	{
		cout << 0;
		return 0;
	}
	sort(x + 1, x + n + 1);
	int ans = 0x3f3f3f3f;
	if (a <= x[1])
	{
		if (flag[x[n]] > 1) ans = min(x[n] - a, ans);
		else ans = min(x[n - 1] - a, ans);
	}
	else
	{
		if (a >= x[n])
		{
			if (flag[x[1]] > 1) ans = min(a - x[1], ans);
			else ans = min(a - x[2], ans);
		}
		else
		{
			if (flag[x[1]] > 1) ans = min(a - x[1] + 2 * (x[n] - a), ans);
			else
			{
				if (x[2] < a) ans = min(a - x[2] + 2 * (x[n] - a), ans);
				else  ans = min((x[n] - a), ans);
			}
			if (flag[x[n]] > 1) ans = min(x[n] - a + 2 * (a - x[1]), ans);
			else
			{
				if (x[n - 1] > a) ans = min(x[n - 1] - a + 2 * (a - x[1]), ans);
				else ans = min(a - x[1], ans);
			}
		}
	}
	cout << ans;
	return 0;
}


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

const int maxn = 1e5 + 10;

int n, a, minp=0x3f3f3f3f, maxp;
int x[maxn];
map<int, int> flag;

int main()
{
	cin >> n >> a;
	for (int i = 1; i <= n; i++)
	{
		cin >> x[i];
		flag[x[i]]++;
		minp = min(minp, x[i]);
		maxp = max(maxp, x[i]);
	}
	if (n == 1)
	{
		cout << 0;
		return 0;
	}
	sort(x + 1, x + n + 1);
	int ans = 0x3f3f3f3f;
	if (a <= x[1])
	{
		if (flag[x[n]] > 1) ans = min(x[n] - a, ans);
		else ans = min(x[n - 1] - a, ans);
	}
	else
	{
		if (a >= x[n])
		{
			if (flag[x[1]] > 1) ans = min(a - x[1], ans);
			else ans = min(a - x[2], ans);
		}
		else
		{
			if (flag[x[1]] > 1) ans = min(a - x[1] + 2 * (x[n] - a), ans);
			else ans = min(a - x[2] + 2 * (x[n] - a), ans);
			if (flag[x[n]] > 1) ans = min(x[n] - a + 2 * (a - x[1]), ans);
			else ans = min(x[n - 1] - a + 2 * (a - x[1]), ans);
		}
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> v;
int main () {
	int n, a, b;
	long long s = 0;
	cin >> n >> a;
	for ( int i = 0; i < n; i ++) {
		cin >> b;
		v.push_back (b);
	}
	sort ( v.begin (), v.end ());
	int r1 = abs (a-v[0]);  //11
	int r2 = abs (a-v[n-1]);	//10
	if ( n == 2) {
		if (r1 > r2)
			cout << r2;
		else
			cout << r1;
		return 0;
	}
	if (n == 1) {
		cout << "0";
		return 0;
	}
	if (r1 > r2)
		if ( abs (a-v[1]) > r2)
			s = r2*2 + abs (a-v[1]);
		else
			s = abs (a-v[1])*2 + r2;
	else
		if ( abs (a-v[n-2]) > r1)
			s = r1*2 + abs (a-v[n-2]);
		else
			s = abs (a-v[n-2])*2 + r1;
	cout << s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> v;
int main () {
	int n, a, b;
	int s = 0;
	cin >> n >> a;
	for ( int i = 0; i < n; i ++) {
		cin >> b;
		v.push_back (b);
	}
	sort ( v.begin (), v.end ());
	int r1 = abs (a-v[0]);  //11
	int r2 = abs (a-v[n-1]);	//10
        int whole = abs (v[0]-v[n-1]);
	if ( n == 2) {
		if (r1 > r2)
			cout << r2;
		else
			cout << r1;
		return 0;
	}
	if (n == 1) {
		cout << "0";
		return 0;
	}
	s = r2+whole;
        s = min (s, r1 + whole);
        s = min (s, abs (v[1]-a) + abs (v[1]-v[n-1]));
        s = min (s, abs (v[n-2]-a) + abs (v[0]-v[n-2]));
        cout << s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> v;
int main () {
	int n, a, b;
	int s = 0;
	cin >> n >> a;
	for ( int i = 0; i < n; i ++) {
		cin >> b;
		v.push_back (b);
	}
	sort ( v.begin (), v.end ());
	if ( n == 2) {
		cout <<  min (abs (v[0]-a), abs(v[1]-a));
		return 0;
	}
	if (n == 1) {
		cout << "0";
		return 0;
	}
    s = abs (v[n-1]-a) + abs (v[n-1]-v[1]);
    s = min (s, abs(v[n-2]-a) + abs (v[n-2]-v[0]));
    s = min (s, abs (v[0]-a) + abs (v[0]-v[n-2]));
    s = min (s, abs (v[1]-a) + abs (v[1]-v[n-1]));
    cout << s;
	return 0;
}


#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
set <int> s;
set <int> :: iterator it;
int main () {
	int n, a, b, small, big;
	small = 0;
	big = 0;
	cin >> n >> a;
	for ( int i = 0; i < n; i ++) {
		cin >> b;
		s.insert (b);
	}
	int t = s.size ();
	int r1 = abs ( *s.begin () - a);
	int r2 = abs ( *s.rbegin () - a);
		if (r1 > r2)
			s.erase (*s.begin ());
		else
			s.erase (*s.rbegin ());
	for ( it = s.begin(); it != s.end (); it ++)
		if ( *it < a)
			small ++;
		else
			big ++;
	if ( small == 0) {
		cout << abs ( *s.rbegin () - a);
		return 0;
	}
	if ( big == 0) {
		cout << abs ( *s.rbegin () - a);
		return 0;
	}
	r1 = abs ( *s.begin () - a);
	r2 = abs ( *s.rbegin () - a);
	if ( r1 > r2)
		cout << r2*2 + r1;
	else
		cout << r1*2 + r2;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}
		cnt = 0;
		int dis = 0;

		while (cnt < n-1)
		{
			if (s == 0)
			{
				dis += v[s + 1] - v[s];
				v.erase(v.bgn + s);
			}
			else if (s == v.sz - 1)
			{
				dis += v[s] - v[s - 1];
				int temp = s--;
				v.erase(v.bgn + temp);
			}
			else
			{
				if (v[s] - v[s - 1] == v[s + 1] - v[s])
				{
					if (v.sz - s > s)
					{
						dis += v[s+1] - v[s];
						v.erase(v.bgn + s);
					}
					else
					{
						dis += v[s] - v[s - 1];
						int temp = s--;
						v.erase(v.bgn + temp);
					}
				}
				else if (v[s] - v[s - 1] < v[s + 1] - v[s])
				{
					dis += v[s] - v[s - 1];
					int temp = s--;
					v.erase(v.bgn + temp);
				}
				else
				{
					dis += v[s + 1] - v[s];
					v.erase(v.bgn + s);
				}
			}

			cnt++;
		}

		PF("%d\n", dis);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}

		int dis1, dis2, dis3, dis4;

		dis1 = dis2 = dis3 = dis4 = 0;

		for (int i = s - 1; i >= 1; i--)
		{
			dis1 += v[i + 1] - v[i];
		}

		if (s != v.sz - 1)
		{
			dis1 += v[s + 1] - v[1];

			for (int i = s + 2; i <= v.sz - 1; i++)
			{
				dis1 += v[i] - v[i - 1];
			}
		}

		for (int i = s + 1; i <= v.sz-1; i++)
		{
			dis2 += v[i] - v[i-1];
		}

		if (s != 0)
		{
			dis2 += v[v.sz - 1] - v[s - 1];

			for (int i = s - 2; i >= 1; i--)
			{
				dis2 += v[i + 1] - v[i];
			}
		}
		//
		for (int i = s - 1; i >= 0; i--)
		{
			dis3 += v[i + 1] - v[i];
		}

		if (s != v.sz - 1)
		{
			dis3 += v[s + 1] - v[0];

			for (int i = s + 2; i <= v.sz - 2; i++)
			{
				dis3 += v[i] - v[i - 1];
			}

		}

		for (int i = s + 1; i <= v.sz - 2; i++)
		{
			dis4 += v[i] - v[i - 1];
		}

		if (s != 0)
		{
			dis4 += v[v.sz - 2] - v[s - 1];

			for (int i = s - 2; i >= 0; i--)
			{
				dis4 += v[i + 1] - v[i];
			}
		}

		PF("%d\n", min4(dis1, dis2, dis3, dis4));
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}

		int dis1, dis2, dis3, dis4;

		dis1 = dis2 = dis3 = dis4 = 0;

		for (int i = s - 1; i >= 1; i--)
		{
			dis1 += v[i + 1] - v[i];
		}

		dis1 += v[s + 1] - v[1];

		for (int i = s + 2; i <= v.sz-1; i++)
		{
			dis1 += v[i] - v[i-1];
		}

		for (int i = s + 1; i <= v.sz-1; i++)
		{
			dis2 += v[i] - v[i-1];
		}

		dis2 += v[v.sz-1] - v[s-1];

		for (int i = s - 2; i >= 1 ; i--)
		{
			dis2 += v[i+1] - v[i];
		}
		//
		for (int i = s - 1; i >= 0; i--)
		{
			dis3 += v[i + 1] - v[i];
		}

		dis3 += v[s + 1] - v[0];

		for (int i = s + 2; i <= v.sz - 2; i++)
		{
			dis3 += v[i] - v[i - 1];
		}

		for (int i = s + 1; i <= v.sz - 2; i++)
		{
			dis4 += v[i] - v[i - 1];
		}

		dis4 += v[v.sz - 2] - v[s - 1];

		for (int i = s - 2; i >= 0; i--)
		{
			dis4 += v[i + 1] - v[i];
		}

		PF("%d\n", min4(dis1, dis2, dis3, dis4));
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}
		cnt = 0;
		int dis = 0;

		while (cnt < n-1)
		{
			if (s == 0)
			{
				dis += v[s + 1] - v[s];
				v.erase(v.bgn + s);
			}
			else if (s == v.sz - 1)
			{
				dis += v[s] - v[s - 1];
				int temp = s--;
				v.erase(v.bgn + temp);
			}
			else
			{
				if (v[s] - v[s - 1] == v[s + 1] - v[s])
				{
					if ((v.sz-1) - s > s)
					{
						dis += v[s+1] - v[s];
						v.erase(v.bgn + s);
					}
					else
					{
						dis += v[s] - v[s - 1];
						int temp = s--;
						v.erase(v.bgn + temp);
					}
				}
				else if (v[s] - v[s - 1] < v[s + 1] - v[s])
				{
					dis += v[s] - v[s - 1];
					int temp = s--;
					v.erase(v.bgn + temp);
				}
				else
				{
					dis += v[s + 1] - v[s];
					v.erase(v.bgn + s);
				}
			}

			cnt++;
		}

		PF("%d\n", dis);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}

		int dis1, dis2, dis3, dis4;

		dis1 = dis2 = dis3 = dis4 = 0;

		if (s == 0)
		{
			FORL(i, v.sz - 2)
			{
				dis1 += v[i + 1] - v[i];
			}

			PF("%d\n", dis1);
		}
		else if (s == v.sz - 1)
		{
			for (int i = v.sz - 1;i >=2;i--)
			{
				dis1 += v[i] - v[i-1];
			}

			PF("%d\n", dis1);
		}
		else
		{
			for (int i = s - 1; i >= 1; i--)
			{
				dis1 += v[i + 1] - v[i];
			}

			dis1 += v[s + 1] - v[1];

			for (int i = s + 2; i <= v.sz - 1; i++)
			{
				dis1 += v[i] - v[i - 1];
			}

			for (int i = s + 1; i <= v.sz - 1; i++)
			{
				dis2 += v[i] - v[i - 1];
			}

			dis2 += v[v.sz - 1] - v[s - 1];

			for (int i = s - 2; i >= 1; i--)
			{
				dis2 += v[i + 1] - v[i];
			}
			//
			for (int i = s - 1; i >= 0; i--)
			{
				dis3 += v[i + 1] - v[i];
			}

			dis3 += v[s + 1] - v[0];

			for (int i = s + 2; i <= v.sz - 2; i++)
			{
				dis3 += v[i] - v[i - 1];
			}

			for (int i = s + 1; i <= v.sz - 2; i++)
			{
				dis4 += v[i] - v[i - 1];
			}

			dis4 += v[v.sz - 2] - v[s - 1];

			for (int i = s - 2; i >= 0; i--)
			{
				dis4 += v[i + 1] - v[i];
			}

			PF("%d\n", min4(dis1, dis2, dis3, dis4));
		}

	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<utility>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef list<int> li;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef pair<int,int> pii;

#define psb(x) push_back(x)
#define psf(x) push_front(x)
#define ppb pop_back()
#define ppf pop_front()
#define pop pop()
#define front front()
#define back back()
#define bgn begin()
#define end end()
#define emp empty()
#define clr clear()
#define sz size()
#define sp setprecision
#define fx fixed
#define ff first
#define ss second

#define reset(a) memset(a,0,sizeof(a))
#define assign(a,b) memset(a,b,sizeof(a))
#define assignmx(a) memset(a,127,sizeof(a))
#define assignmn(a) memset(a,128,sizeof(a))

#define max3(x, y, z) max(x, max(y, z))
#define min3(x, y, z) min(x, min(y, z))
#define max4(w, x, y, z) max(w, max(x, max(y,z)))
#define min4(w, x, y, z) min(w, min(x, min(y, z)))

#define range(r,c) ((r >=0 && r <row) && (c >=0 && c <column))

#define _3SFI(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define _3SFLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define _3SFD(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)

#define _2SFI(a,b) scanf("%d%d",&a,&b)
#define _2SFLL(a,b) scanf("%lld%lld",&a,&b)
#define _2SFD(a,b) scanf("%lf%lf",&a,&b)

#define _SFI(a) scanf("%d",&a)
#define _SFLL(a) scanf("%lld",&a)
#define _SFD(a) scanf("%lf",&a)

#define _SFCH(a) scanf("%c",&a)

#define PF printf
#define SF scanf
#define getch ch=getchar()

#define FORE(i,n) for(int i = 1; i <= n; i++)
#define FORL(i,n) for(int i = 0; i < n; i++)
#define FORI(i,r,n,v) for(int i = r ; i <= n ; i+=v)
#define FORT(t) for(int o = 1 ; o <= t ; o++)

int X4[] = { 0, -1, 0, 1 };
int Y4[] = { -1, 0, 1, 0 };
int X8[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int Y8[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int X3D6[] = { 0, 0, -1, 1, 0, 0 };
int Y3D6[] = { -1, 1, 0, 0, 0, 0 };
int Z3D6[] = { 0, 0, 0, 0, -1, 1 };

#define INFMX 2139062143
#define INFMN -2139062144
#define pi acos(-1.0)
#define N 100001

bool flag, flag1, flag2, flag3;

int row, column;

int main()
{
	int n, a, temp, cnt;
	vector<int> v;

	while (_2SFI(n, a) == 2)
	{
		v.clr;
		v.psb(a);

		FORE(i, n)
		{
			_SFI(temp);
			v.psb(temp);
		}

		sort(v.bgn, v.end);

		int s;

		FORL(i, v.sz)
		{
			if (v[i] == a)
			{
				s = i;
				break;
			}
		}
		cnt = 0;
		int dis = 0;

		while (cnt < n-1)
		{
			if (s == 0)
			{
				dis += v[s + 1] - v[s];
				v.erase(v.bgn + s);
			}
			else if (s == v.sz - 1)
			{
				dis += v[s] - v[s - 1];
				int temp = s--;
				v.erase(v.bgn + temp);
			}
			else
			{
				if (v[s] - v[s - 1] == v[s + 1] - v[s])
				{
					if (v[s] - v[0] >= v[v.sz-1] - v[s])
					{
						dis += v[s+1] - v[s];
						v.erase(v.bgn + s);
					}
					else
					{
						dis += v[s] - v[s - 1];
						int temp = s--;
						v.erase(v.bgn + temp);
					}
				}
				else if (v[s] - v[s - 1] < v[s + 1] - v[s])
				{
					dis += v[s] - v[s - 1];
					int temp = s--;
					v.erase(v.bgn + temp);
				}
				else
				{
					dis += v[s + 1] - v[s];
					v.erase(v.bgn + s);
				}
			}

			cnt++;
		}

		PF("%d\n", dis);
	}
	return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#define pb push_back
#define mp make_pair

using namespace std;

const int maxn = (int) 1e5 + 100;

int n;
long long x;

long long a[maxn];

long long go_right() {
  long long res = 0;
  int summ = 0;

  int st = n;

  for (int i = 0; i < n; ++i) {
    if (x <= a[i]) {
      st = i;
      break;
    }
  }

  res += a[st] - x;
  summ++;

  for (int i = st + 1; i < n; ++i) {
    if (summ >= n - 1) {
      return res;
    }

    res += (a[i] - a[i - 1]);
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }

  res *= 2;
  st--;

  res += x - a[st];
  summ++;

  for (int i = st - 1; i >= 0; i--) {
    if (summ >= n - 1) {
      return res;
    }

    res += (a[i + 1] - a[i]);
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

long long go_left() {
  long long res = 0;
  int summ = 0;

  int st = 0;

  for (int i = n - 1; i >= 0; i--) {
    if (a[i] <= x) {
      st = i;
      break;
    }
  }

  res += x - a[st];
  summ++;

  for (int i = st - 1; i >= 0; i--) {
    if (summ >= n - 1) {
      return res;
    }

    res += a[i + 1] - a[i];
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }

  res *= 2;
  st++;

  res += a[st] - x;
  summ++;

  for (int i = st + 1; i < n; ++i) {
    if (summ >= n - 1) {
      return res;
    }

    res += a[i] - a[i - 1];
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

int main() {
  scanf("%d %I64d", &n, &x);

  for (int i = 0; i < n; ++i)
    scanf("%I64d", &a[i]);

  sort(a, a + n);

  cout << min(go_right(), go_left()) << endl;

  return 0;
}


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#define pb push_back
#define mp make_pair

using namespace std;

const int maxn = (int) 1e5 + 100;

int n;
long long x;

long long a[maxn];

long long go_right() {
  long long res = 0;
  int summ = 0;

  int st = n;

  for (int i = 0; i < n; ++i) {
    if (x <= a[i]) {
      st = i;
      break;
    }
  }

  res += a[st] - x;
  summ++;

  for (int i = st + 1; i < n; ++i) {
    if (summ >= n - 1) {
      return res;
    }

    res += (a[i] - a[i - 1]);
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }

  res *= 2;

  if (st == n) {
    res = 0;
    summ = 0;
  }

  st--;

  res += x - a[st];
  summ++;

  for (int i = st - 1; i >= 0; i--) {
    if (summ >= n - 1) {
      return res;
    }

    res += (a[i + 1] - a[i]);
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

long long go_left() {
  long long res = 0;
  int summ = 0;

  int st = -1;

  for (int i = n - 1; i >= 0; i--) {
    if (a[i] <= x) {
      st = i;
      break;
    }
  }

  if (st != -1) {
    res += x - a[st];
  }

  summ++;

  for (int i = st - 1; i >= 0; i--) {
    if (summ >= n - 1) {
      return res;
    }

    res += a[i + 1] - a[i];
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }

  res *= 2;

  if (st == -1) {
    res = 0;
    summ = 0;
  }

  st++;

  res += a[st] - x;
  summ++;

  for (int i = st + 1; i < n; ++i) {
    if (summ >= n - 1) {
      return res;
    }

    res += a[i] - a[i - 1];
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

int main() {
  scanf("%d %I64d", &n, &x);

  for (int i = 0; i < n; ++i)
    scanf("%I64d", &a[i]);

  sort(a, a + n);

  //cout << go_left() << ' ' << go_right() << endl;

  cout << min(go_right(), go_left()) << endl;

  return 0;
}
/*
3 100
1 2 3
*/


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#define pb push_back
#define mp make_pair

using namespace std;

const long long maxn = (long long) 1e5 + 100;

long long n;
long long x;

long long a[maxn];

long long go_right() {
  long long res = 0;
  long long summ = 0;

  long long st = n;

  for (long long i = 0; i < n; ++i) {
    if (x <= a[i]) {
      st = i;
      break;
    }
  }

  res += a[st] - x;
  summ++;

  for (long long i = st + 1; i < n; ++i) {
    if (summ >= n - 1 && st != n) {
      return res;
    }

    res += (a[i] - a[i - 1]);
    summ++;
  }

  if (summ >= n - 1 && st != n) {
    return res;
  }

  res *= 2;

  if (st == n) {
    res = 0;
    summ = 0;
  }

  st--;

  res += x - a[st];
  summ++;

  for (int i = st - 1; i >= 0; i--) {
    if (summ >= n - 1) {
      return res;
    }

    res += (a[i + 1] - a[i]);
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

long long go_left() {
  long long res = 0;
  long long summ = 0;

  long long st = -1;

  for (long long i = n - 1; i >= 0; i--) {
    if (a[i] <= x) {
      st = i;
      break;
    }
  }

  if (st != -1) {
    res += x - a[st];
  }

  summ++;

  for (long long i = st - 1; i >= 0; i--) {
    if (summ >= n - 1 && st != -1) {
      return res;
    }

    res += a[i + 1] - a[i];
    summ++;
  }

  if (summ >= n - 1 && st != -1) {
    return res;
  }

  res *= 2;

  if (st == -1) {
    res = 0;
    summ = 0;
  }

  st++;

  res += a[st] - x;
  summ++;

  for (long long i = st + 1; i < n; ++i) {
    if (summ >= n - 1) {
      return res;
    }

    res += a[i] - a[i - 1];
    summ++;
  }

  if (summ >= n - 1) {
    return res;
  }
}

int main() {
  scanf("%I64d %I64d", &n, &x);

  for (long long i = 0; i < n; ++i)
    scanf("%I64d", &a[i]);

  sort(a, a + n);

  //cout << go_left() << ' ' << go_right() << endl;

  cout << min(go_right(), go_left()) << endl;

  return 0;
}
/*
3 100
1 2 3
*/


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#define pb push_back
#define mp make_pair

using namespace std;

const int maxn = (int) 1e5 + 100;

int n, x;

int a[maxn];

int go(int l, int r) {
  int len_right, len_left;

  if (x > a[r]) {
    len_right = x - a[l];
  } else {
    len_right = 2 * (a[r] - x) + (x - a[l]);
  }

  if (x < a[l]) {
    len_left = a[r] - x;
  } else {
    len_left = 2 * (x - a[l]) + (a[r] - x);
  }

  return min(len_right, len_left);
}

int main() {
  scanf("%d %d", &n, &x);

  if (n == 1) {
    cout << "0";
    return 0;
  }

  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

  sort(a, a + n);

  cout << min(go(0, n - 2), go(1, n - 1));

  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	if (n == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	sort(ALL(v));
	int x1 = abs(a - v[0]);
	int x2 = abs(a - v[1]);
	if (n == 2)
	{
		cout << min(x1, x2) << endl;
		return 0;
	}
	int x3 = abs(a - v[n - 1]);
	int x4 = abs(a - v[n - 2]);
	int y1 = v[n - 2] - v[0];
	int y2 = v[n - 1] - v[1];
	if (y1 < y2) cout << y1 + min(x1, x4) << endl;
	else if (y1 > y2) cout << y2 + min(x3, x2) << endl;
	else cout << y1 + min(min(x1, x4), min(x3, x2)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	if (n == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	sort(ALL(v));
	int x1 = abs(a - v[0]);
	int x2 = abs(a - v[1]);
	if (n == 2)
	{
		cout << min(x1, x2) << endl;
		return 0;
	}
	int x3 = abs(a - v[n - 1]);
	int x4 = abs(a - v[n - 2]);
	int y1 = v[n - 2] - v[0];
	int y2 = v[n - 1] - v[1];
	cout << min(min(x1, x4) + y1, min(x2, x3) + y2);

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	if (n == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	sort(ALL(v));
	int x1 = abs(a - v[0]);
	int x2 = abs(a - v[1]);
	if (n == 2)
	{
		cout << min(x1, x2) << endl;
	}
	int x3 = abs(a - v[n - 1]);
	int x4 = abs(a - v[n - 2]);
	int y1 = v[n - 2] - v[0];
	int y2 = v[n - 1] - v[1];
	if (y1 < y2) cout << y1 + min(x1, x4) << endl;
	else if (y1 > y2) cout << y2 + min(x3, x2) << endl;
	else cout << y1 + min(min(x1, x4), min(x3, x2)) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	sort(ALL(v));
	cout << min(min((abs(a-v[0]) + v[n-2]-v[0]), (abs(a - v[1]) + v[n - 1] - v[1])), min((abs(a - v[n-1]) + v[n - 1] - v[1]), (abs(a - v[n-2]) + v[n - 2] - v[0]))) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	sort(ALL(v));
	bool f1 = v[0] == v[1];
	bool f2 = v[n - 1] == v[n - 2];
	if (f1 && !f2) cout << min(abs(a - v[0]) + v[n - 2] - v[0], abs(a - v[n - 2]) + v[n - 2] - v[0]) << endl;
	else if (f2 && !f1) cout << min(abs(a - v[n-1]) + v[n - 1] - v[1], abs(a - v[1]) + v[n - 1] - v[0]) << endl;
	else cout << min(min((abs(a - v[0]) + v[n - 2] - v[0]), (abs(a - v[1]) + v[n - 1] - v[1])), min((abs(a - v[n - 1]) + v[n - 1] - v[1]), (abs(a - v[n - 2]) + v[n - 2] - v[0]))) << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	sort(ALL(v));
	int x1 = abs(a - v[0]);
	int x2 = abs(a - v[1]);
	int x3 = abs(a - v[n - 1]);
	int x4 = abs(a - v[n - 2]);
	int y1 = v[n - 2] - v[0];
	int y2 = v[n - 1] - v[1];
	if (y1 < y2) cout << y1 + min(x1, x4);
	else if (y1 > y2) cout << y2 + min(x3, x2);
	else cout << y1 + min(min(x1, x4), min(x3, x2));

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <cctype>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

#define mp make_pair
#define forn(i, n) for(int64 i = 0; i < int64(n); ++i)
#define forr(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define forit(i,c) for(i=(c).begin(); i!=(c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef vector<int> vint;
typedef vector<bool> vbool;
typedef vector<int64> vint64;
typedef pair<int, int> pii;
typedef pair<int64, int64> pii64;

typedef map<int, int> mii;
typedef map<char, int> mci;

int main()
{
#ifdef ONLINE_JUDGE
	//freopen("abc.in", "r", stdin);
	//freopen("abc.out", "w", stdout);
#else
	freopen("ReadMe.txt", "r", stdin);
#endif

	int n, a;
	cin >> n >> a;
	vint v(n);
	forn(i, n)
	{
		cin >> v[i];
	}
	if (n == 1)
	{
		cout << abs(a - v[0]) << endl;
		return 0;
	}
	sort(ALL(v));
	int x1 = abs(a - v[0]);
	int x2 = abs(a - v[1]);
	int x3 = abs(a - v[n - 1]);
	int x4 = abs(a - v[n - 2]);
	int y1 = v[n - 2] - v[0];
	int y2 = v[n - 1] - v[1];
	if (y1 < y2) cout << y1 + min(x1, x4) << endl;
	else if (y1 > y2) cout << y2 + min(x3, x2) << endl;
	else cout << y1 + min(min(x1, x4), min(x3, x2)) << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define mod 1000000007
int x[100005];
int main()
{
	int a,n;
	int i,j,k,l,m,ans;
	cin>>n;
	cin>>a;
	for(i=0;i<n;i++)
	cin>>x[i];
	if(n==1)
	{
		cout<<"0"<<"\n";
		return 0;
	}
	if(n==2)
	{
		k=a-x[0];l=x[1]-a;
		if(l<0) l=-l;
		if(k<0) k=-k;
		ans=min(k,l);
		cout<<ans<<"\n";
		return 0;
	}
	sort(x,x+n);
	if(a<=x[0]) ans=x[n-2]-a;
	else if(a>=x[n-1]) ans=a-x[1];
	else if(a>x[0]&&a<x[1])
	{
		ans=min(x[n-1]-a,a-x[0]+x[n-2]-x[0]);
		ans=min(ans,x[n-2]-a+x[n-2]-x[0]);
	}
	else if(a>x[n-2]&&a<x[n-1])
	{

		ans=min(a-x[1]+x[n-1]-x[1],a-x[0]);
		ans=min(ans,x[n-1]-a+x[n-1]-x[1]);
	}
	else
	{
		ans=min(a-x[1]+x[n-1]-x[1],a-x[0]+x[n-2]-x[0]);
		ans=min(ans,x[n-2]-a+x[n-2]-x[0]);
		ans=min(ans,x[n-1]-a+x[n-1]-x[1]);
	}
	cout<<ans<<"\n";
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
using namespace std;

const int Nmax = 1e5 + 17;
int t[Nmax];

int main() {
	//freopen("input.txt", "r", stdin);

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

	for(int i = 0; i< n; i++) scanf("%d", &t[i]);

	sort(t, t+n);
	long long ans = 3e9 + 7;

	if(n == 1) ans = 0;
	else if(n == 2){
		ans = min(abs(a - t[0]), abs(a - t[1]));
	}
	else  {
		if(t[0] <= a && a <= t[n-2]) ans = min(2 * 1ll * (t[n-2] - a) + a - t[0], 2 * 1ll * (a - t[0]) + t[n - 2] - a);
		if(t[1] <= a && a <= t[n-1]) ans = min(ans, min(2 * 1ll * (t[n-1] - a) + a - t[1], 2 * 1ll * (a - t[1]) + t[n - 1] - a));
	}

	printf("%I64d", ans);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
using namespace std;

const int Nmax = 1e5 + 17;
int t[Nmax];

int num1 (int ind, int count){
	int q = 0;
	while(ind - count - q >=0 && t[ind - count - q] == t[ind - count]) q++;
	return q;
}

int num2 (int ind, int count, int n) {
	int q = 0;
	while(ind + count + q < n  && t[ind + count + q] == t[ind + count]) q++;
	return q;
}

int main() {
	//freopen("input.txt", "r", stdin);

	int n, a;
	int ans = 2e9 + 7;
	scanf("%d%d", &n, &a);

	int left = n - 1;

	for(int i = 0; i< n; i++) {
		scanf("%d", &t[i]);
		if(t[i] == a) left--;
	}

	t[n] = a;
	sort(t, t+n+1);
	int ind;
	for(int i = 0; i <= n; i++) if(t[i] == a) { ind = i; break; }

	int start = ind + n - 1 - left;
	if(left != n - 1) start--;

	for(int i = start; i <= n; i++) {
		if(i != n && t[i] == t[i-1]) continue;
		if(ind - left + i - start >= 0 ) ans = min(ans, 2 * (t[i] - a) + a - t[ind - left + i - start]);
	}

	for(int i = ind; i >= 0; i--){
		if(i != 0 && t[i] == t[i-1]) continue;
		if(ind + n - 1 - left + (left - (ind - i) ) <= n) ans = min(ans, 2 * (a - t[i]) + t[ind + n - 1 - left + (left - (ind - i) )] - a);
	}

	printf("%d", ans);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
using namespace std;

const int Nmax = 1e5 + 17;
int t[Nmax];

int main() {
	//freopen("input.txt", "r", stdin);

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

	for(int i = 0; i< n; i++) scanf("%d", &t[i]);

	sort(t, t+n);
	long long ans = 3e9 + 7;

	if(n == 1) ans = 0;
	else if(n == 2){
		ans = min(abs(a - t[0]), abs(a - t[1]));
	}
	else  {
		if(t[0] <= a && a <= t[n-2]) ans = min(2 * 1ll * (t[n-2] - a) + a - t[0], 2 * 1ll * (a - t[0]) + t[n - 2] - a);
		if(t[1] <= a && a <= t[n-1]) ans = min(ans, min(2 * 1ll * (t[n-1] - a) + a - t[1], 2 * 1ll * (a - t[1]) + t[n - 1] - a));
		if(t[0] >= a) ans = min(ans, t[n-2] * 1ll - a);
		if(t[1] >= a) ans = min(ans, t[n-1] * 1ll - a);
		if(t[n-1] <= a) ans = min(ans, a - t[1] * 1ll);
		if(t[n-2] <= a) ans = min(ans, a - t[0] * 1ll);
	}

	printf("%I64d", ans);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
using namespace std;

const int Nmax = 1e5 + 17;
int t[Nmax];

int num1 (int ind, int count){
	int q = 0;
	while(ind - count - q >=0 && t[ind - count - q] == t[ind - count]) q++;
	return q;
}

int num2 (int ind, int count, int n) {
	int q = 0;
	while(ind + count + q < n  && t[ind + count + q] == t[ind + count]) q++;
	return q;
}

int main() {
	freopen("input.txt", "r", stdin);

	int n, a;
	int ans = 2e9 + 7;
	scanf("%d%d", &n, &a);

	int left = n - 1;

	for(int i = 0; i< n; i++) {
		scanf("%d", &t[i]);
		if(t[i] == a) left--;
	}

	t[n] = a;
	sort(t, t+n+1);
	int ind;
	for(int i = 0; i <= n; i++) if(t[i] == a) { ind = i; break; }

	int start = ind + n - 1 - left;
	if(left != n - 1) start--;

	for(int i = start; i <= n; i++) {
		if(i != n && t[i] == t[i-1]) continue;
		if(ind - left + i - start >= 0 ) ans = min(ans, 2 * (t[i] - a) + a - t[ind - left + i - start]);
	}

	for(int i = ind; i >= 0; i--){
		if(i != 0 && t[i] == t[i-1]) continue;
		if(ind + n - 1 - left + (left - (ind - i) ) <= n) ans = min(ans, 2 * (a - t[i]) + t[ind + n - 1 - left + (left - (ind - i) )] - a);
	}

	printf("%d", ans);
}


#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

int main()
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	/*
	int n;
	cin >> n;

	vector<int> vec;
	int mi = INT_MAX;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		vec.push_back(x);
		mi = min(mi, vec[i]);
	}

	for (int i = 0; i < n; i++)
		vec.push_back(vec[i]);

	int l=0, r=n-1;

	while (vec[l] != mi)
		l++;
	while (vec[r] != mi)
		r--;

	long long ans = n*mi + n-(r-l+1);
	cout << ans;

	*/

	int n, a;
	cin >> n >> a;
	vector<int> vec(n);

	for (int i = 0; i < n; i++)
		cin >> vec[i];

	if (n == 1)

	{
		cout << 0;
		return 0;
	}

	if (n == 2)
	{
		cout << min(abs(a - vec[0]), abs(a - vec[1]));
		return 0;
	}

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

	if (a > vec[n - 1])
	{
		cout << abs(a - vec[1]);
		return 0;
	}

	if (a < vec[0])
	{
		cout << abs(a - vec[n-2]);
		return 0;
	}

	int x = INT_MAX;

	x = min(x, abs(a - vec[n - 1]) + abs(vec[n - 1] - vec[1]));
	x = min(x, abs(a - vec[1]) + abs(vec[n - 1] - vec[1]));

	x = min(x, abs(a - vec[0]) + abs(vec[n - 2] - vec[0]));
	x = min(x, abs(a - vec[n-2]) + abs(vec[n - 2] - vec[0]));

	cout << x;

	return 0;
}


#include <vector>
#include <string>
#include <map>
#include<cmath>
#include <iostream>
#include<algorithm>

using namespace std;

int main()
{

	long long n, a;
	cin >> n >> a;
	vector <long long> vec(4);
	if (n == 1)
	{
		cout << "0";
		return 0;
	}
	if (n == 2)
	{
		cin >> vec[0] >> vec[1];
		{

		}
		cout << min(abs(vec[0] - a), abs(vec[1] - a));
		return 0;
	}

	if (n == 3)
	{

		cin >> vec[0] >> vec[1] >> vec[2];
		{
			for (int i = 0;i<n - 1;i++)
				for (int j = i + 1;j<n;j++)
					if (vec[i]>vec[j])
						swap(vec[i], vec[j]);
		}

		if (a <= vec[0] || a >= vec[2])
		{
			cout << abs(vec[1] - a);return 0;
		}

		//long long m1,m2,m3,m4;
		if (a >= vec[1] && a<vec[2])
		{
			cout << min(min(2 * abs(vec[2] - a) + abs(vec[1] - a), 2 * abs(vec[1] - a) + abs(vec[2] - a)), abs(a - vec[0]));
			return 0;

		}

		if (a>vec[0] && a <= vec[1])
		{
			cout << min(min(2 * abs(vec[0] - a) + abs(vec[1] - a), 2 * abs(vec[1] - a) + abs(vec[0] - a)), abs(a - vec[2]));
			return 0;

		}

	}

	//vector<long long> ivec(n);
	vec[0] = 1000003;vec[1] = 1000003;vec[2] = -1000003;vec[3] = -1000003;
	for (long long i = 0;i<n;i++)
	{
		long long q;
		cin >> q;
		if (q<vec[0])
		{
			if (vec[0]<vec[1])
				vec[1] = vec[0];
			vec[0] = q;
		}
		if (q>vec[3])
		{
			if (vec[3]>vec[2])
				vec[2] = vec[3];
			vec[3] = q;

		}

	}

	if (a <= vec[0])
	{
		cout << abs(vec[2] - a);return 0;
	}
	if (a >= vec[3])
	{
		cout << abs(vec[1] - a);return 0;
	}

	// long long m1,m2,m3,m4;
	if (a >= vec[2] && a<vec[3])
	{
		cout << min(min(2 * abs(vec[3] - a) + abs(vec[2] - a), 2 * abs(vec[2] - a) + abs(vec[3] - a)), abs(a - vec[0]));
		return 0;

	}

	if (a>vec[0] && a <= vec[1])
	{
		cout << min(min(2 * abs(vec[0] - a) + abs(vec[1] - a), 2 * abs(vec[1] - a) + abs(vec[0] - a)), abs(a - vec[3]));
		return 0;

	}

	cout << min(min(2 * abs(vec[n - 2] - a) + abs(vec[0] - a), 2 * abs(vec[0] - a) + abs(vec[n - 2] - a)),        min(2 * abs(vec[n - 1] - a) + abs(vec[1] - a), 2 * abs(vec[1] - a) + abs(vec[n - 1] - a)));

	return 0;

}


#include<stdio.h>
#include<algorithm>
#define LL _int64
#define maxn 100001
using namespace std;

int main(){

	LL n,a;
	scanf("%I64d %I64d",&n,&a);

	int i;
	LL x[maxn];
	for(i = 0 ; i < n ; i++){
		scanf("%I64d", &x[i]);
	}
	sort(x,x+n);
	LL count ;

	LL y=min(abs(x[0]-a),abs(x[n-2]-a))+x[n-2]-x[0];
    LL z=min(abs(x[1]-a),abs(x[n-1]-a))+x[n-1]-x[1];
	count = y < z ? y : z;

	printf("%I64d\n",count);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#define LL _int64
#define maxn 100001
using namespace std;

int main(){

	LL n,a;
	scanf("%I64d %I64d",&n,&a);

	int i;
	LL x[maxn];
	for(i = 0 ; i < n ; i++){
		scanf("%I64d", &x[i]);
	}
	sort(x,x+n);
	LL count ;

	if( n == 1 ){
		printf("0\n");
		return 0;
	}

	LL y=min(abs(x[0]-a),abs(x[n-2]-a))+x[n-2]-x[0];
    LL z=min(abs(x[1]-a),abs(x[n-1]-a))+x[n-1]-x[1];
	count = y < z ? y : z;

	printf("%I64d\n",count);
	return 0;
}


#include<stdio.h>
#include<algorithm>
#define LL _int64
#define maxn 100001
using namespace std;

int main(){

	LL n,a;
	scanf("%I64d %I64d",&n,&a);

	int i;
	LL x[1000];
	for(i = 0 ; i < n ; i++){
		scanf("%I64d", &x[i]);
	}
	sort(x,x+n);
	LL count ;

	LL y=min(abs(x[0]-a),abs(x[n-2]-a))+x[n-2]-x[0];
    LL z=min(abs(x[1]-a),abs(x[n-1]-a))+x[n-1]-x[1];
	count = y < z ? y : z;

	printf("%I64d\n",count);
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int n, a;
int x[100003];
int mi, ma;
int mi1, ma1;
int ans;

int main()
{
	scanf("%d%d", &n, &a);

	for(int i = 0; i < n; ++i)
		scanf("%d", &x[i]);

	if(n == 0)
	{
		printf("0");
		return 0;
	}

	sort(x, x + n);

	mi = x[0];
	ma = x[n - 1];

	if(mi >= a)
	{
		printf("%d", x[n - 2] - a);
		return 0;
	}

	if(ma <= a)
	{
		printf("%d", a - x[1]);
		return 0;
	}

	mi1 = x[1];
	ma1 = x[n - 2];

	if(mi1 >= a)
		ans = ma - a;
	else
		ans = ma - mi1 + min(ma - a, a - mi1);

	if(ma1 <= a)
		ans = min(ans, a - mi);
	else
		ans = min(ans, ma1 - mi + min(ma1 - a, a - mi));

	printf("%d", ans);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int n, a;
int x[100003];
int mi, ma;
int mi1, ma1;
int ans;

int main()
{
	scanf("%d%d", &n, &a);

	for(int i = 0; i < n; ++i)
		scanf("%d", &x[i]);

	if(n == 1)
	{
		printf("0");
		return 0;
	}

	sort(x, x + n);

	mi = x[0];
	ma = x[n - 1];

	if(mi >= a)
	{
		printf("%d", x[n - 2] - a);
		return 0;
	}

	if(ma <= a)
	{
		printf("%d", a - x[1]);
		return 0;
	}

	mi1 = x[1];
	ma1 = x[n - 2];

	if(mi1 >= a)
		ans = ma - a;
	else
		ans = ma - mi1 + min(ma - a, a - mi1);

	if(ma1 <= a)
		ans = min(ans, a - mi);
	else
		ans = min(ans, ma1 - mi + min(ma1 - a, a - mi));

	printf("%d", ans);

	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int n, a;
int x[100003];
int mi, ma;
int mi1, ma1;
int ans;

int main()
{
	scanf("%d%d", &n, &a);

	for(int i = 0; i < n; ++i)
		scanf("%d", &x[i]);

	sort(x, x + n);

	mi = x[0];
	ma = x[n - 1];

	if(mi >= a)
	{
		printf("%d", x[n - 2] - a);
		return 0;
	}

	if(ma <= a)
	{
		printf("%d", a - x[1]);
		return 0;
	}

	mi1 = x[1];
	ma1 = x[n - 2];

	if(mi1 >= a)
		ans = ma - a;
	else
		ans = ma - mi1 + min(ma - a, a - mi1);

	if(ma1 <= a)
		ans = min(ans, a - mi);
	else
		ans = min(ans, ma1 - mi + min(ma1 - a, a - mi));

	printf("%d", ans);

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[1000010];
int main()
{
    int n, t;
    scanf("%d%d", &n, &t);
    for (int i = 0; i < n; i ++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    int ans = 0;
    if (n == 1)
    {
        printf("0");
        exit(0);
    }
    if (t <= a[0]) ans += abs (t - a[n-2]);
    else if (t >= a[n-1]) ans += abs (t - a[1]);
    else
    {
        int min1 = 0, min2 = 0;
        min1 = min(abs(a[0] - t), abs (a[n-2] - t)) + (a[n-2] - a[0]);
        min2 = min(abs(a[1] - t), abs (a[n-1]-t)) + (a[n-1] - a[1]);
        ans = min(min1, min2);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[1000010];
int main()
{
    int n, t;
    scanf("%d%d", &n, &t);
    for (int i = 0; i < n; i ++)
        scanf("%d", &a[i]);
    sort(a, a + n);
    int ans = 0;
    if (t <= a[0]) ans += abs (t - a[n-2]);
    else if (t >= a[n-1]) ans += abs (t - a[1]);
    else
    {
        int min1 = 0, min2 = 0;
        min1 = min(abs(a[0] - t), abs (a[n-2] - t)) + (a[n-2] - a[0]);
        min2 = min(abs(a[1] - t), abs (a[n-1]-t)) + (a[n-1] - a[1]);
        ans = min(min1, min2);
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    long result[4];
    int left=0;
    int right=0;
    long sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(long), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(long), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(long), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int *mas = new int[n];

    int *temp_mas = mas;
    int temp_n = n;
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }

    int result = 0;
    for (i = 0;i < n - 1;i++)
    {
        qsort(temp_mas, temp_n, sizeof(int), compare);
        result += abs(temp_mas[0]- curPos);
        curPos = temp_mas[0];
        temp_mas++;
        temp_n--;
    }
    cout << result;

    delete mas;
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    int result[4];
    int left=0;
    int right=0;
    int sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int mas[100000];

int compare(const void * a, const void * b)
{
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    int result[4];
    int numres;
    unsigned int number;
    int curPos;
    cin >> number >> curPos;
    int i = 0;
    while (i < number)
    {
        cin >> mas[i];
        i++;
    }

    if (number == 1)
    {
        cout << 0;
        return 0;
    }
    qsort(mas, number, sizeof(int), compare);
    if (curPos <= mas[0])
    {
        cout << -curPos + mas[number - 2];
        return 0;
    }
    if (curPos >= mas[number - 1])
    {
        cout << curPos - mas[1];
        return 0;
    }

    if (number == 2)
    {
        int left = (curPos - mas[0]);
        int right = (mas[1] - curPos);
        left = (left < right) ? left : right;
        cout << left;
        return 0;
    }

    if (curPos <= mas[1])
    {
        result[0] = mas[number - 1] - curPos;
        result[1] = (curPos - mas[0]) * 2 + (mas[number - 2] - curPos);
        numres = 2;
        goto end;
    }
    if (curPos >= mas[number - 2])
    {
        result[0] = curPos - mas[0];
        result[1] = (mas[number -1]- curPos) * 2 + (curPos-mas[1]);
        numres = 2;
        goto end;
    }

    result[0] = (curPos - mas[0]) * 2 + (mas[number -2]-curPos);
    result[1] = (curPos - mas[1]) * 2 + (mas[number - 1] - curPos);
    result[2] = (mas[number - 1] - curPos) * 2 + (curPos - mas[1]);
    result[3] = (mas[number - 2] - curPos) * 2 + (curPos - mas[0]);
    numres = 4;

end:
    qsort(result, numres, sizeof(int), compare);

    cout << result[0];
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    unsigned long result[4];
    int left=0;
    int right=0;
    unsigned long sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(unsigned long), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(unsigned long), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(unsigned long), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int mas[100000];

int compare(const void * a, const void * b)
{
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int number;
    int curPos;
    cin >> number >> curPos;
    int i = 0;
    while (i < number)
    {
        cin >> mas[i];
        i++;
    }

    if (number == 1)
    {
        cout << 0;
        return 0;
    }
    qsort(mas, number, sizeof(int), compare);
    if (curPos <= mas[0])
    {
        cout<< - curPos+mas[number-2];  //(mas[0] - curPos)+mas[number-2]-mas[0]
        return 0;
    }
    if (curPos >= mas[number - 1])
    {
        cout << curPos-mas[1];  //(curPos-mas[number - 1]) + mas[number - 1]-mas[1]
        return 0;
    }

    int result[4];
    int left=0;
    int right=0;
    int sum1=0, sum2=0;

    int index = -1;
    for (i = 0;i < number -1;i++)
    {
        if (mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            index = i;
            break;
        }
    }
    if (mas[index + 1] == curPos) left = 0;

    sum1 = mas[index] - mas[0];
    sum2 = mas[number -1] - mas[index+1];

    result[0] = (left + sum1) *2 + right + sum2 - (mas[number - 1]- mas[number - 2]);
    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;
    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);
    result[3] = (right + sum2 - (mas[number -2] - mas[number -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);
    if (result[0] < 0) result[0] = result[1];

    cout << result[0];
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    unsigned result[4];
    int left=0;
    int right=0;
    unsigned sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(unsigned), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(unsigned), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(unsigned), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    int result[4];
    int left=0;
    int right=0;
    unsigned long sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    int result[4];
    int left=0;
    int right=0;
    int sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;
        if (curPos == mas[n - 2])
            result[0] -= mas[1] - mas[0];

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;
        if (curPos == mas[0])
            result[0] -= mas[n - 1] - mas[n - 2];

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    long sec_res[4];

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);
    sec_res[0] = (left + sum1) * 2 + right + sum2;

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;
    sec_res[1]= (left + sum1) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);
    sec_res[2] = (right + sum2) * 2 + left + sum1;

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;
    sec_res[3]= (right + sum2) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);
    qsort(sec_res, 4, sizeof(int), compare);

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int mas[100000];

int compare(const void * a, const void * b)
{
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int number;
    int curPos;
    cin >> number >> curPos;
    int i = 0;
    while (i < number)
    {
        cin >> mas[i];
        i++;
    }

    if (number == 1)
    {
        cout << 0;
        return 0;
    }
    qsort(mas, number, sizeof(int), compare);
    if (curPos <= mas[0])
    {
        cout << -curPos + mas[number - 2];  //(mas[0] - curPos)+mas[number-2]-mas[0]
        return 0;
    }
    if (curPos >= mas[number - 1])
    {
        cout << curPos - mas[1];  //(curPos-mas[number - 1]) + mas[number - 1]-mas[1]
        return 0;
    }

    int result[4];
    int left = 0;
    int right = 0;
    int sum1 = 0, sum2 = 0;

    int index = -1;
    for (i = 0;i < number - 1;i++)
    {
        if (mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            index = i;
            break;
        }
    }
    if (mas[index + 1] == curPos) left = 0;

    sum1 = mas[index] - mas[0];
    sum2 = mas[number - 1] - mas[index + 1];

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[number - 2] - mas[number - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[number - 1] - mas[number - 2]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }
    if (index == (number - 2))
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    result[0] = (left + sum1) * 2 + right + sum2 - (mas[number - 1] - mas[number - 2]);
    result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right + sum2;
    result[2] = (right + sum2) * 2 + left + sum1 - (mas[1] - mas[0]);
    result[3] = (right + sum2 - (mas[number - 2] - mas[number - 1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);
    if (result[0] < 0) result[0] = result[1];

    cout << result[0];
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }
    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    int result[4];
    int left=0;
    int right=0;
    int sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    if (index == n - 2)
    {
        result[0] = left + sum1;

        result[1] = (left + sum1 - (mas[1] - mas[0])) * 2 + right;
        result[2] = right * 2 + (left + sum1 - (mas[1] - mas[0]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;
    }

    if (index == 0)
    {
        result[0] = right + sum2;

        result[1] = (right + sum2 - (mas[n - 2] - mas[n - 1])) * 2 + left;
        result[2] = left * 2 + (right + sum2 - (mas[n-1] - mas[n-2]));
        qsort(result, 3, sizeof(int), compare);
        if (result[0] < 0) result[0] = result[1];
        cout << result[0];
        return 0;

    }

    result[0] = (left + sum1) *2 + right + sum2 - (mas[n - 1]- mas[n - 2]);

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * 2 +  left + sum1 -(mas[1] - mas[0]);

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);
    if (result[0] < 0) result[0] = result[1];

    cout << result[0];

    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int mas[100000];

int compare(const void * a, const void * b)
{
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    int result[4];
    int numres;
    unsigned int number;
    int curPos;
    cin >> number >> curPos;
    int i = 0;
    while (i < number)
    {
        cin >> mas[i];
        i++;
    }

    if (number == 1)
    {
        cout << 0;
        return 0;
    }
    qsort(mas, number, sizeof(int), compare);
    if (curPos <= mas[0])
    {
        cout << -curPos + mas[number - 2];
        return 0;
    }
    if (curPos >= mas[number - 1])
    {
        cout << curPos - mas[1];
        return 0;
    }

    if (number == 2)
    {
        int left = (curPos - mas[0]);
        int right = (mas[1] - curPos);
        left = (left < right) ? left : right;
        cout << left;
        return 0;
    }

    if (curPos <= mas[1])
    {
        result[0] = mas[number - 1] - curPos;
        result[1] = (curPos - mas[0]) * 2 + (mas[number - 2] - curPos);
        result[2] = (mas[number - 2]-curPos) * 2 + (curPos- mas[0]);
        numres = 3;
        goto end;
    }
    if (curPos >= mas[number - 2])
    {
        result[0] = curPos - mas[0];
        result[1] = (mas[number -1]- curPos) * 2 + (curPos-mas[1]);
        result[2] = (curPos - mas[1]) * 2 + (mas[number - 1] - curPos);
        numres = 3;
        goto end;
    }

    result[0] = (curPos - mas[0]) * 2 + (mas[number -2]-curPos);
    result[1] = (curPos - mas[1]) * 2 + (mas[number - 1] - curPos);
    result[2] = (mas[number - 1] - curPos) * 2 + (curPos - mas[1]);
    result[3] = (mas[number - 2] - curPos) * 2 + (curPos - mas[0]);
    numres = 4;

end:
    qsort(result, numres, sizeof(int), compare);

    cout << result[0];
    return 0;
}


#include <iostream>
#include <stdlib.h>
using namespace std;
int curPos;

int compare(const void * a, const void * b)
{
    /*if (abs(*(int*)a- curPos) <  abs(*(int*)b - curPos)) return -1;
    if (abs(*(int*)a - curPos) == abs(*(int*)b - curPos)) return 0;
    if (abs(*(int*)a - curPos) > abs(*(int*)b - curPos)) return 1;*/
    return (*(int*)a - *(int*)b);
}

int main(int argc, char argv[])
{
    unsigned int n;
    int a;
    cin >> n >> a;
    curPos = a;
    int mas[100000];
    int i = 0;
    while (i < n)
    {
        cin >> mas[i];
        i++;
    }

    int result[4];
    int left=0;
    int right=0;
    int sum1=0, sum2=0;
    qsort(mas, n, sizeof(int), compare);
    bool razr = false;
    bool temp = false;
    int index = -1;
    for (i = 0;i < n-1;i++)
    {
        if (!razr&&mas[i] <= curPos&&curPos <= mas[i + 1])
        {
            left = curPos - mas[i];
            right = mas[i + 1] - curPos;
            razr = true;
            index = i;
            continue;
        }

        if (razr)
            sum2 += mas[i+1]- mas[i];
        else
            sum1 += mas[i+1]- mas[i];
    }

    if (!razr)
    {
        if (curPos <= mas[0])
        {
            right = mas[0] - curPos;
            result[0] = right + sum1 - (mas[n - 1] - mas[n - 2]);

        }
        if (curPos >= mas[n - 1])
        {
            left = curPos - mas[n - 1];
            result[0] = left + sum1 - (mas[1] - mas[0]);
        }
        cout << result[0];
        return 0;
    }

    result[0] = (left + sum1) *(index==n-2?1:2) + (index == n-2 ? 0 :right + sum2 - (mas[n - 1]- mas[n - 2]));

    result[1] = (left + sum1 - (mas[1]-mas[0])) * 2 + right + sum2;

    result[2] = (right + sum2) * (index == 0 ? 1 : 2) + (index == 0 ? 0 : left + sum1 -(mas[1] - mas[0]));

    result[3] = (right + sum2 - (mas[n -2] - mas[n -1])) * 2 + left + sum1;

    qsort(result, 4, sizeof(int), compare);
    if (result[0] < 0) result[0] = result[1];

    cout << result[0];

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,maxn=0;
char a[100004];
int b[100004];
int main(){
    cin>>n;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,maxn;
char s[100003];
int a[100003];
int main(){
    cin>>n;
    cin>>s+1;
    for(int i=1;i<=n;i++){
        if(s[i]=='G')a[i]=a[i-1]+1;
        maxn=max(maxn,a[i]);
    }
    for(int i=n-1;i>=1;i--){
        if(a[i]&&a[i+1])
           a[i]=a[i+1];

    }
   // for(int i=1;i<=n;i++)
       //cout<<a[i]<<" ";
    for(int i=1;i<=n;i++){
        if(a[i])continue;
        for(int j=1;j<=i-1-a[i-1];j++){//12301230123402
           if(!a[i])maxn=max(maxn,a[i-1]+a[i+1]+1);
        }
        bool flag=true;
        for(int j=i+1;j<=n;j++){

            if(!a[j]){flag=false;}
            else{
                if(flag==false){
                    //cout<<i<<" "<<j<<endl;
                    maxn=max(maxn,a[i-1]+a[i+1]+1);
                }
            }
        }
        maxn=max(maxn,a[i-1]+a[i+1]);
    }
    cout<<maxn;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string a;
int m,n,l,k,j[210000],p,gs,lk[210000];
int main()
{
	scanf("%d",&m);
	cin>>a;
	int ll=a.size();
	for(int x=0;x<ll;x++)
	{
		if(a[x]=='G')
		{if(x==0||a[x-1]!='G')
		{
			j[++p]=1;
		}
		else
		{
			j[p]++;
		}
		gs++;
		}
		else
		{
			if(a[x-1]=='G'&&a[x+1]=='G')
			{
			lk[k++]=p;
			}
		}
	}
	if(p==0)
	{
		cout<<"0"<<endl;
	}
	else if(p==1)
	{
		cout<<gs<<endl;
	}
	else if(p==2)
	{
		cout<<gs<<endl;
	}
	else
	{
		int ans=0;
		for(int x=1;x<p;x++)
		{
		ans=max(j[x]+j[x+1]+1,ans);
		}
		cout<<ans<<endl;
	}

}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string a;
int m,n,l,k,j[210000],p,gs,lk[210000];
int main()
{
	scanf("%d",&m);
	cin>>a;
	int ll=a.size();
	for(int x=0;x<ll;x++)
	{
		if(a[x]=='G')
		{if(x==0||a[x-1]!='G')
		{
			j[++p]=1;
		}
		else
		{
			j[p]++;
		}
		gs++;
		}
		else
		{
			if(x!=ll-1&&a[x-1]=='G'&&a[x+1]=='G')
			{
			lk[k++]=p;
			}
		}
	}
	if(p==0)
	{
		cout<<"0"<<endl;
	}
	else if(p==1)
	{
		cout<<gs<<endl;
	}
	else if(p==2)
	{
		if(k==0)
		{
			int ans=0;
			for(int x=1;x<=p;x++)
			{
				ans=max(ans,j[x]);
			}
			cout<<ans<<endl;
		}
		else
		cout<<gs<<endl;
	}
	else
	{
		int ans=0;
		for(int x=0;x<k;x++)
		{
		ans=max(ans,j[lk[x]]+j[lk[x]+1]+1);
		}
		cout<<ans<<endl;
	}
}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string a;
int m,n,l,k,j[210000],p,gs,lk[210000];
int main()
{
	scanf("%d",&m);
	cin>>a;
	int ll=a.size();
	for(int x=0;x<ll;x++)
	{
		if(a[x]=='G')
		{if(x==0||a[x-1]!='G')
		{
			j[++p]=1;
		}
		else
		{
			j[p]++;
		}
		gs++;
		}
		else
		{
			if(x!=ll-1&&a[x-1]=='G'&&a[x+1]=='G')
			{
			lk[k++]=p;
			}
		}
	}
	if(p==0)
	{
		cout<<"0"<<endl;
	}
	else if(p==1)
	{
		cout<<gs<<endl;
	}
	else if(p==2)
	{
		if(k==0)
		{
			int ans=0;
			for(int x=1;x<=p;x++)
			{
				ans=max(ans,j[x]);
			}
			cout<<ans<<endl;
		}
		else
		cout<<gs<<endl;
	}
	else
	{
		int ans=0;
		for(int x=0;x<k;x++)
		{
		ans=max(ans,j[lk[x]]+j[lk[x]+1]+1);
		}
		for(int x=0;x<p;x++)
		{
		ans=max(ans,j[x]+1);
		}
		cout<<ans<<endl;
	}
}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string a;
int m,n,l,k,j[210000],p,gs,lk[210000];
int main()
{
	scanf("%d",&m);
	cin>>a;
	int ll=a.size();
	for(int x=0;x<ll;x++)
	{
		if(a[x]=='G')
		{if(x==0||a[x-1]!='G')
		{
			j[++p]=1;
		}
		else
		{
			j[p]++;
		}
		gs++;
		}
		else
		{
			if(x!=ll-1&&a[x-1]=='G'&&a[x+1]=='G')
			{
			lk[k++]=p;
			}
		}
	}
	if(p==0)
	{
		cout<<"0"<<endl;
	}
	else if(p==1)
	{
		cout<<gs<<endl;
	}
	else if(p==2)
	{
		if(k==0)
		{
			int ans=0;
			for(int x=1;x<=p;x++)
			{
				ans=max(ans,j[x]+1);
			}
			cout<<ans<<endl;
		}
		else
		cout<<gs<<endl;
	}
	else
	{
		int ans=0;
		for(int x=0;x<k;x++)
		{
		ans=max(ans,j[lk[x]]+j[lk[x]+1]+1);
		}
		for(int x=0;x<p;x++)
		{
		ans=max(ans,j[x]+1);
		}
		cout<<ans<<endl;
	}
}


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string a;
int m,n,l,k,j[210000],p,gs,lk[210000];
int main()
{
	scanf("%d",&m);
	cin>>a;
	int ll=a.size();
	for(int x=0;x<ll;x++)
	{
		if(a[x]=='G')
		{if(x==0||a[x-1]!='G')
		{
			j[++p]=1;
		}
		else
		{
			j[p]++;
		}
		gs++;
		}
		else
		{
			if(x!=ll-1&&a[x-1]=='G'&&a[x+1]=='G')
			{
			lk[k++]=p;
			}
		}
	}
	if(p==0)
	{
		cout<<"0"<<endl;
	}
	else if(p==1)
	{
		cout<<gs<<endl;
	}
	else if(p==2)
	{
		if(k==0)
		{
			int ans=0;
			for(int x=1;x<=p;x++)
			{
				ans=max(ans,j[x]+1);
			}
			cout<<ans<<endl;
		}
		else
		cout<<gs<<endl;
	}
	else
	{
		int ans=0;
		for(int x=0;x<k;x++)
		{
		ans=max(ans,j[lk[x]]+j[lk[x]+1]+1);
		}
		for(int x=0;x<=p;x++)
		{
		ans=max(ans,j[x]+1);
		}
		cout<<ans<<endl;
	}
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010100
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int f[100001];
int main(){
	int t;
	int n,x,y,d,cnt=0;
	ios::sync_with_stdio(false);
	string a;
	cin>>n;
	cin>>a;
	int l=a.size();
	for(int i=0;i<l;i++){
		if(a[i]=='G')cnt++;
		else f[i]+=cnt,cnt=0;
	}
	cnt=0;
	for(int i=l-1;i>=0;i--){2
		if(a[i]=='G')cnt++;
		else f[i]+=cnt,cnt=0;
	}
	int ret=0,pos=0;
	for(int i=0;i<n;i++){
		if(f[i]>ret)pos=i,ret=f[i];
	}
	if(ret){
		for(int i=n-1;i>pos;i--){
			if(a[i]=='G'){swap(a[pos],a[i]);break;}
		}
	}
	cnt=0;
	int ans=0;
	for(int i=0;i<n;i++){
		if(a[i]=='G')cnt++;
		else ans=max(ans,cnt),cnt=0;
	}
	ans=max(ans,cnt);
	cout<<ans;
	return 0;
}


#include"pch.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int totalGCount = 0, totalSCount = 0;
	int currentGCount = 0, currentSCount = 0, lastGC = 0;
	int maxGC = 0;

	while (n--)
	{
		char ch;
		cin >> ch;
		if (ch == 'G')
		{
			totalGCount++;
			currentSCount = 0;
			currentGCount++;
			if (maxGC < currentGCount + lastGC) maxGC = currentGCount + lastGC;
		}
		else if (ch == 'S')
		{
			lastGC = currentGCount;
			currentSCount++;
			if (maxGC < currentGCount)
			{
				maxGC = currentGCount;
			}
			currentGCount = 0;
			totalSCount++;
		}
	}

	if (totalGCount > maxGC && totalSCount > 1)
		maxGC++;

	cout << maxGC <<endl;
	return 0;
}


//#include"pch.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	scanf("%d", &n);
	int totalGCount = 0, totalSCount = 0;
	int currentGCount = 0, currentSCount = 0, lastGC = 0;
	int maxGC = 0;
	char ch;
	while (n--)
	{

		scanf(" %c", &ch);
		if (ch == 'G')
		{
			totalGCount++;
			currentSCount = 0;
			currentGCount++;
			if (maxGC < currentGCount + lastGC) maxGC = currentGCount + lastGC;
		}
		else if (ch == 'S')
		{
			lastGC = currentGCount;
			currentSCount++;
			currentGCount = 0;
			totalSCount++;
		}
	}

	if (totalGCount > maxGC && totalSCount > 1)
		maxGC++;

	cout << maxGC <<endl;
	return 0;
}


//#include"pch.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	scanf("%d", &n);
	int totalGCount = 0, totalSCount = 0;
	int currentGCount = 0, currentSCount = 0, lastGC = 0;
	int maxGC = 0;

	while (n--)
	{
		char ch;
		scanf("%c", &ch);
		if (ch == 'G')
		{
			totalGCount++;
			currentSCount = 0;
			currentGCount++;
			if (maxGC < currentGCount + lastGC) maxGC = currentGCount + lastGC;
		}
		else if (ch == 'S')
		{
			lastGC = currentGCount;
			currentSCount++;
			if (maxGC < currentGCount)
			{
				maxGC = currentGCount;
			}
			currentGCount = 0;
			totalSCount++;
		}
	}

	if (totalGCount > maxGC && totalSCount > 1)
		maxGC++;

	cout << maxGC <<endl;
	return 0;
}


//#include"pch.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int totalGCount = 0, totalSCount = 0;
	int currentGCount = 0, currentSCount = 0, lastGC = 0;
	int maxGC = 0;

	while (n--)
	{
		char ch;
		cin >> ch;
		if (ch == 'G')
		{
			totalGCount++;
			currentSCount = 0;
			currentGCount++;
			if (maxGC < currentGCount + lastGC) maxGC = currentGCount + lastGC;
		}
		else if (ch == 'S')
		{
			lastGC = currentGCount;
			currentSCount++;
			if (maxGC < currentGCount)
			{
				maxGC = currentGCount;
			}
			currentGCount = 0;
			totalSCount++;
		}
	}

	if (totalGCount > maxGC && totalSCount > 1)
		maxGC++;

	cout << maxGC <<endl;
	return 0;
}