using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = System.Diagnostics.Trace;
using SB = System.Text.StringBuilder;
using static System.Math;
using Number = System.Int32;
using System.Numerics;
using static System.Numerics.BigInteger;
namespace Program {
	using N = Int32;
	public class Solver {
		Random rnd = new Random(0);
		public void Solve() {
			var n = ri;
			var P = new StarrySkyTree(Enumerate(n, x => INF));
			var Q = new StarrySkyTree(Enumerate(n, x => INF));
			var q = ri;
			Action<int, long> update = (i, v) => {
				Debug.WriteLine($"{i} {v}");
				P.Push(i, i + 1, v + i - P.Query(i, i + 1));
				Q.Push(i, i + 1, v - i - Q.Query(i, i + 1));
			};
			for (int i = 0; i < n; i++)
				update(i, INF);
			update(ri - 1, 0);
			var last = ri - 1;
			for (int _ = 0; _ < q; _++) {
				var next = ri - 1;
				Debug.WriteLine($"{last} -> {next}");
				var v = Min(next + Q.Query(0, next + 1), P.Query(next, n) - next);
				P.Push(0, n, Abs(last - next));
				Q.Push(0, n, Abs(last - next));
				update(last, Min(v, P.Query(last, last + 1) - last));
				last = next;
			}
			var ans = INF;
			for (int i = 0; i < n; i++)
				ans = Min(ans, P.Query(i, i + 1) - i);
			Console.WriteLine(ans);
		}
		const long INF = 1L << 60;
		static int[] dx = { -1, 0, 1, 0 };
		static int[] dy = { 0, 1, 0, -1 };
		int ri { get { return sc.Integer(); } }
		long rl { get { return sc.Long(); } }
		double rd { get { return sc.Double(); } }
		string rs { get { return sc.Scan(); } }
		public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());

		static T[] Enumerate<T>(int n, Func<int, T> f) {
			var a = new T[n];
			for (int i = 0; i < a.Length; ++i) a[i] = f(i);
			return a;
		}
		static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
	}
}

#region main
static class Ex {
	static public string AsString(this IEnumerable<char> ie) { return new string(ie.ToArray()); }
	static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") {
		return string.Join(st, ie);
	}
	static public void Main() {
		Console.SetOut(new Program.IO.Printer(Console.OpenStandardOutput()) { AutoFlush = true });
		var solver = new Program.Solver();
		//* 
		var t = new System.Threading.Thread(solver.Solve, 50000000);
		//t.Start();
		//t.Join();
		//*/
		solver.Solve();
		Console.Out.Flush();
	}
}
#endregion
#region Ex
namespace Program.IO {
	using System.IO;
	using System.Text;
	using System.Globalization;

	public class Printer : StreamWriter {
		public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
		public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	}

	public class StreamScanner {
		public StreamScanner(Stream stream) { str = stream; }

		public readonly Stream str;
		private readonly byte[] buf = new byte[1024];
		private int len, ptr;
		public bool isEof = false;
		public bool IsEndOfStream { get { return isEof; } }

		private byte read() {
			if (isEof) return 0;
			if (ptr >= len) {
				ptr = 0;
				if ((len = str.Read(buf, 0, 1024)) <= 0) {
					isEof = true;
					return 0;
				}
			}
			return buf[ptr++];
		}

		public char Char() {
			byte b = 0;
			do b = read(); while ((b < 33 || 126 < b) && !isEof);
			return (char)b;
		}
		public string Scan() {
			var sb = new StringBuilder();
			for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b);
			return sb.ToString();
		}
		public string ScanLine() {
			var sb = new StringBuilder();
			for (var b = Char(); b != '\n' && b != 0; b = (char)read()) if (b != '\r') sb.Append(b);
			return sb.ToString();
		}
		public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); }
		public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); }
		public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); }
	}
}

#endregion


#region Lazy Propagation
public interface ILazyData<T1, T2> {
	T1 Merge(T1 l, T1 r);
	T1 Identity { get; }
	T1 InitValue { get; }
	T1 Apply(int l, int r, T1 val, T2 x);
	T2 Push(T2 val, T2 x);
	T2 Zero { get; }
}
#endregion
#region LazyPropagation Segment Tree
public class LazyPropagationSegmentTree<T1, T2, U>
	where U : struct, ILazyData<T1, T2> {
	int sz;
	int n;
	T1[] data;
	T2[] lazy;
	bool[] flag;
	U op;
	public LazyPropagationSegmentTree(int size) {
		sz = size;
		n = 1;
		while (n < sz)
			n <<= 1;
		data = new T1[n * 2];
		lazy = new T2[n * 2];
		flag = new bool[n * 2];
		for (int i = 1; i < n * 2; i++) {
			data[i] = op.InitValue;
			lazy[i] = op.Zero;
		}
	}
	public LazyPropagationSegmentTree(T1[] a) {

		sz = a.Length;
		n = 1;
		while (n < sz)
			n <<= 1;
		data = new T1[n * 2];
		lazy = new T2[n * 2];
		flag = new bool[n * 2];
		for (int i = 0; i < n; i++) {
			if (i < sz)
				data[i + n] = a[i];
			else data[i + n] = op.InitValue;
			lazy[i + n] = op.Zero;
		}
		for (int i = n - 1; i >= 1; i--) {
			data[i] = op.Merge(data[i * 2], data[i * 2 + 1]);
			lazy[i] = op.Zero;

		}
	}
	private void lazyEval(int k, int a, int b) {
		if (!flag[k])
			return;
		push(a, (a + b) / 2, lazy[k], k * 2, a, (a + b) / 2);
		push((a + b) / 2, b, lazy[k], k * 2 + 1, (a + b) / 2, b);
		lazy[k] = op.Zero;
		flag[k] = false;
	}
	private void eval(int k) {
		data[k] = op.Merge(data[k * 2], data[k * 2 + 1]);
	}

	public void Push(int a, int b, T2 v) {
		push(a, b, v, 1, 0, n);
	}
	private void push(int a, int b, T2 x, int k, int l, int r) {
		if (r <= a || b <= l)
			return;
		else if (a <= l && r <= b) {
			flag[k] = true;
			lazy[k] = op.Push(lazy[k], x);
			data[k] = op.Apply(l, r, data[k], x);
		}
		else {
			lazyEval(k, l, r);
			push(a, b, x, k << 1, l, (l + r) >> 1);
			push(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
			eval(k);
		}
	}
	public T1 Query(int a, int b) { return query(a, b, 1, 0, n); }
	private T1 query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l)
			return op.Identity;
		if (a <= l && r <= b)
			return data[k];
		else {
			lazyEval(k, l, r);
			var vl = query(a, b, k << 1, l, (l + r) >> 1);
			var vr = query(a, b, (k << 1) + 1, (l + r) >> 1, r);
			eval(k);
			return op.Merge(vl, vr);
		}
	}
	public T1[] Items {
		get {
			var ret = new T1[sz];
			for (int i = 0; i < ret.Length; i++)
				ret[i] = Query(i, i + 1);
			return ret;
		}
	}
}
#endregion

#region StarrySkyTree
public class StarrySkyTree : LazyPropagationSegmentTree<long, long, StarySky> {
	public StarrySkyTree(long[] a) : base(a) { }
}
public struct StarySky : ILazyData<long, long> {
	public long Identity { get { return long.MaxValue / 4; } }
	public long InitValue { get { return Identity; } }
	public long Zero { get { return 0; } }
	public long Apply(int l, int r, long val, long x) { return val + x; }
	public long Merge(long l, long r) { return Math.Min(l, r); }
	public long Push(long val, long x) { return val + x; }
}
#endregion


Measured memory_bytes: 34108