#include <bits/stdc++.h>
using namespace std;
long long x,n;

int main()
{
long long S[n+5];
long long minimum=100000000;
long long maksimum=-10000000;

 for (int i=1; i<=n; i++)
 {
 cin>>S[i];   
 }
 for (int i=1; i<=n; i++)
 {
  if (S[i]<minimum) minimum=S[i];
  if (S[i]>maksimum) maksimum=S[i];
 }
  if (n==1)
  {
  if (x>maksimum)
  cout<<x-maksimum<<endl;

  else cout y-x<<endl;
  }
  else if (x>maksimum) 
  cout<<x-minimum<<endl;

  else if (x<minimum)
  cout<<maksimum-x<<endl;

  else if (x>minimum && x<maksimum)
  cout<<x-minimum+maksimum-minimum<<endl;

 return 0;
}