// see https://gist.github.com/LumaKernel/ff55d49ee1af69b7388f15b707e75c15
const bool DEBUG = 0;
#include <iostream>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <complex>
#include <random>
#include <iomanip>
#include <cassert>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tuple<ll, ll> P;
typedef tuple<ll, ll, ll> P3;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<P> VP;

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

#define FOR(i,a,b) for(int i=(a);i<int(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(b)-1;i>=int(a);--i)
#define RREP(i,n) RFOR(i,0,n)

#define LFOR(i,a,b) for(ll i=(a);i<(b);++i)
#define LREP(i,n) LFOR(i,0,n)
#define LRFOR(i,a,b) for(ll i=(b)-1;i>=(a);--i)
#define LRREP(i,b,a) LRFOR(i,0,n)

#define ALL(a) (a).begin(),(a).end()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define MP make_pair
#define PB push_back
#define EACH(i,c) REP(i,(int)(c).size())
#define REACH(i,c) RREP(i,(int)(c).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define BR cout << "\n"

#define dump(x) if(DEBUG)cerr<<"[" << __LINE__ << "] "<<#x<<"="<<(x)<<"\n"
#define dump2(x,y) if(DEBUG)cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)<<"\n"
#define dump3(x,y,z) if(DEBUG)cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)<<"\n"\
  <<" , "<<#z<<"="<<(z)<<"\n"

#define SAY(x) if(DEBUG) cerr << "[" << __LINE__ << "] " << (x) << "\n"

#define YES(x) cout<<((x)?"YES":"NO")<<"\n"
#define Yes(x) cout<<((x)?"Yes":"No")<<"\n"
#define yes(x) cout<<((x)?"yes":"no")<<"\n"

int int_in() {
	int n;
	cin >> n;
	return n;
}
ll ll_in() {
	ll n;
	cin >> n;
	return n;
}
string string_in() {
	string n;
	cin >> n;
	return n;
}
#define ini int_in()
#define inl ll_in()
#define ins string_in()

#define inside(x,y) (0<=(x)&&(x)<w&&0<=(y)&&(y)<h)

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int _N = 2e5;

ll ans;

struct RMQ {
	ll data[_N * 4];
	int m;
	void init(int n) {
		m = 1;
		while (m < n)
			m *= 2;
		REP(i,m*2-1)
			data[i] = LINF;
	}
	void update(int i, ll x) {
		i += m - 1;
		data[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			data[i] = min(data[i * 2 + 1], data[i * 2 + 2]);
		}
	}
	ll query(int a, int b) {
		return _query(a,b,0,0,m);
	}
	ll _query(int a, int b, int k, int l, int r) {
		if (b <= l || r <= a) return LINF;
		if (a <= l && r <= b) return data[k];
		ll da = _query(a, b, k * 2 + 1, l, (l + r) / 2);
		ll db = _query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(da, db);
	}
	ll get(int i) {
		return data[i + m - 1];
	}
};

RMQ ldp, rdp;
int main() {
	omajinai;
	//#####//
	int n=ini,q=ini,a[]={ini-1,ini-1};
	ldp.init(n);rdp.init(n);
	ll sum=0;

	int last=ini-1;

	REP(i,2){
		dump(a[i]);
		ldp.update(a[i], abs(last - a[!i]) - a[i]);
		rdp.update(a[i], abs(last - a[!i]) + a[i]);
	}
	REP(i,q-1) {
		int x=ini-1;
		ll dif=abs((ll)x-last);
		sum += dif;
		ll da = min(ldp.query(0, x)+x, rdp.query(x,n)-x);
		dump(x);
		dump(ldp.query(0, x+1));
		dump(rdp.query(x, n));
		dump(da);
		ldp.update(last, da-last-dif);
		rdp.update(last, da+last-dif);
		last = x;
	}
	ans = LINF;
	REP(i,n){
		ans = min(ans,ldp.get(i) + i + sum);
	}
	cout << ans << endl;
}


Measured memory_bytes: 12544