Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.

Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.

What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?


-----Input-----

The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.


-----Output-----

Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].


-----Examples-----
Input
6 3 2 4

Output
5

Input
6 3 1 3

Output
1

Input
5 2 1 5

Output
0



-----Note-----

In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.

In the second test she only needs to close all the tabs to the right of the current position of the cursor.

In the third test Luba doesn't need to do anything.

Difficulty: EASY
Tags: Implementation
Expected Time Complexity: O(1)

Starter code:

Solutions:
(n, pos, l, r) = [int(i) for i in input().split()]
ans = 0
if l == 1 and r == n:
	ans = 0
elif l == 1:
	ans = abs(r - pos) + 1
elif r == n:
	ans = abs(pos - l) + 1
else:
	ans = r - l + 2
	if pos < l:
		ans += l - pos
	elif l <= pos and pos <= r:
		if abs(pos - l) < abs(r - pos):
			ans += pos - l
		else:
			ans += r - pos
	else:
		ans += pos - r
print(ans)


(n, pos, l, r) = map(int, input().split())
if l == 1 and r == n:
	print(0)
elif l == 1:
	print(abs(pos - r) + 1)
elif r == n:
	print(abs(l - pos) + 1)
elif pos <= l:
	print(r - l + l - pos + 2)
elif pos >= r:
	print(r - l + pos - r + 2)
else:
	print(r - l + min(pos - l, r - pos) + 2)


(n, p, l, r) = map(int, input().split())
if l == 1:
	if r < n:
		print(abs(r - p) + 1)
	else:
		print(0)
elif r < n:
	print(min(abs(p - l), abs(p - r)) + (r - l) + 2)
else:
	print(abs(l - p) + 1)


res = 0
str = input().split()
if int(str[2]) > 1 or int(str[3]) < int(str[0]):
	if int(str[2]) == 1:
		res = abs(int(str[1]) - int(str[3])) + 1
	if int(str[3]) >= int(str[0]):
		res = abs(int(str[1]) - int(str[2])) + 1
if int(str[2]) > 1 and int(str[3]) < int(str[0]):
	res = min(abs(int(str[1]) - int(str[3])), abs(int(str[1]) - int(str[2]))) + abs(int(str[2]) - int(str[3])) + 2
print(res)


(n, pos, l, r) = map(int, input().split())
pos -= 1
l -= 1
r -= 1
v = [0] * n
for i in range(l, r + 1):
	v[i] = 1
t = 0
while v.count(0) != 0:
	if l == 0:
		t += abs(pos - r) + 1
		for i in range(r + 1, n):
			v[i] = 1
	elif r == n - 1:
		t += abs(pos - l) + 1
		for i in range(0, l):
			v[i] = 1
	elif abs(pos - l) <= abs(pos - r):
		t += abs(pos - l)
		pos = l
		for i in range(0, l):
			v[i] = 1
		t += 1
		t += abs(pos - r)
		for i in range(r + 1, n):
			v[i] = 1
		t += 1
	else:
		t += abs(pos - r)
		pos = r
		for i in range(r + 1, n):
			v[i] = 1
		t += 1
		t += abs(pos - l)
		for i in range(0, l):
			v[i] = 1
		t += 1
print(t)


def k(a, b, c, d):
	ok = 0
	if c != 1:
		ok += abs(c - b) + 1
		b = c
	if d != a:
		ok += abs(d - b) + 1
	return ok

def gh(a, b, c, d):
	ok = 0
	if d != a:
		ok += abs(d - b) + 1
		b = d
	if c != 1:
		ok += abs(c - b) + 1
	return ok
(a, b, c, d) = map(int, input().split())
print(min(k(a, b, c, d), gh(a, b, c, d)))


import math
(n, pos, l, r) = map(int, input().split())
if l == 1 and r == n:
	print(0)
elif l == 1:
	print(int(math.fabs(r - pos)) + 1)
elif r == n:
	print(int(math.fabs(pos - l)) + 1)
elif pos >= l and pos <= r:
	m = max(r - pos, pos - l)
	n = min(r - pos, pos - l)
	print(n * 2 + m + 2)
elif pos < l:
	print(r - pos + 2)
else:
	print(pos - l + 2)


(n, pos, l, r) = map(int, input().split())
lr = 0
cur = pos
if 1 < l:
	lr += abs(cur - l) + 1
	cur = l
if r < n:
	lr += abs(cur - r) + 1
rl = 0
cur = pos
if r < n:
	rl += abs(cur - r) + 1
	cur = r
if 1 < l:
	rl += abs(cur - l) + 1
result = min(lr, rl)
print(result)


import itertools as it, math, functools as ft
(n, pos, l, r) = map(int, input().split())
res = 0
if l == 1:
	if r == n:
		res = 0
	else:
		res = abs(pos - r) + 1
elif r == n:
	res = abs(pos - l) + 1
else:
	xl = abs(pos - l)
	xr = abs(r - pos)
	if xl <= xr:
		res = xl + 1
		if l > 1:
			res += r - l + 1
	else:
		res = xr + 1
		if r < n:
			res += r - l + 1
print(res)


import math as mt
import sys, string, bisect
input = sys.stdin.readline
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
(n, p, l, r) = M()
ans = 0
if r - l + 1 == n:
	print(0)
elif l == 1:
	print(abs(r - p) + 1)
elif r == n:
	print(abs(l - p) + 1)
else:
	print(min(abs(p - l), abs(r - p)) + r - l + 2)


(n, p, l, r) = list(map(int, input().split()))
if l > 1 and r < n:
	t1 = abs(p - l) + (r - l)
	t2 = abs(p - r) + (r - l)
	print(min(t1, t2) + 2)
elif l > 1 and r == n:
	print(abs(p - l) + 1)
elif l == 1 and r < n:
	print(abs(p - r) + 1)
else:
	print(0)


import math
(n, pos, l, r) = map(int, input().strip().split(' '))
if l == 1 and r == n:
	print(0)
elif l == 1:
	if pos == r:
		print(1)
	elif pos > r:
		print(pos - r + 1)
	elif pos < r:
		print(r - pos + 1)
elif r == n:
	if pos == l:
		print(1)
	elif pos < l:
		print(l - pos + 1)
	else:
		print(pos - l + 1)
elif pos >= l and pos <= r:
	if pos - l < r - pos:
		print(2 + pos - l + r - l)
	else:
		print(2 + r - l + r - pos)
elif pos > r:
	print(pos - r + 2 + r - l)
else:
	print(l - pos + 2 + r - l)


(n, pos, l, r) = [int(i) for i in input().split()]
if l == 1 and r == n:
	print(0)
elif l == 1:
	if pos > r:
		print(pos - r + 1)
	else:
		print(r - pos + 1)
elif r == n:
	if pos < l:
		print(l - pos + 1)
	else:
		print(pos - l + 1)
elif pos < l:
	print(l - pos + 1 + r - l + 1)
elif pos > r:
	print(pos - r + 1 + r - l + 1)
else:
	print(min(pos - l, r - pos) * 2 + max(pos - l, r - pos) + 2)


(n, pos, l, r) = map(int, input().split(' '))
if r - l + 1 == n:
	sum = 0
elif l == 1:
	sum = abs(r - pos) + 1
elif r == n:
	sum = abs(l - pos) + 1
elif abs(pos - r) >= abs(pos - l):
	sum = abs(pos - l) + (r - l) + 2
else:
	sum = abs(pos - r) + (r - l) + 2
print(sum)


(n, pos, l, r) = map(int, input().split())
d1 = l - 1
d2 = n - r
ans = 0
if pos < l:
	ans += l - pos
	pos = l
if pos > r:
	ans += pos - r
	pos = r
pos1 = pos - l
pos2 = r - pos
m = min(pos1, pos2)
if m == pos1:
	if l != 1:
		ans += m + 1
		if r != n:
			ans += 1 + r - l
	elif r != n:
		ans += 1 + pos2
elif m == pos2:
	if r != n:
		ans += m + 1
		if l != 1:
			ans += 1 + r - l
	elif l != 1:
		ans += 1 + pos1
print(ans)


q = input()
(n, pos, l, r) = q.split()
n = int(n)
pos = int(pos)
l = int(l)
r = int(r)
if l == 1 and r == n:
	res = 0
elif l == 1 and r != n:
	res = abs(pos - r) + 1
elif l != 1 and r == n:
	res = abs(pos - l) + 1
elif abs(pos - l) > abs(pos - r):
	res = abs(pos - r) + 1 + (r - l) + 1
else:
	res = abs(pos - l) + (r - l) + 2
print(res)


(n, pos, l, r) = map(int, input().split())

def dist(x, y):
	return abs(x - y)
tasks = []
if l != 1:
	tasks.append(l)
if r != n:
	tasks.append(r)
if len(tasks) == 2 and dist(pos, r) < dist(pos, l):
	tasks = [r, l]
out = 0
while tasks:
	t = tasks.pop(0)
	out += dist(pos, t) + 1
	pos = t
print(out)


(n, c, s, e) = list(map(int, input().split()))
countl = 0
countr = 0
flag = 0
if s != 1:
	flag += 1
	countl += abs(c - s) + 1
	if e != n:
		countl += abs(e - s) + 1
if e != n:
	flag += 2
	countr += abs(e - c) + 1
	if s != 1:
		countr += abs(e - s) + 1
if flag > 2:
	print(min(countl, countr))
elif flag == 2:
	print(countr)
elif flag == 1:
	print(countl)
else:
	print(0)


s = input()
(n, pos, l, r) = list(map(int, s.split()))
time = 0
if l <= pos <= r:
	time_1 = 0
	if l > 1:
		time_1 += pos - l + 1
	if r < n:
		time_1 += r - l + 1
	time_2 = 0
	if r < n:
		time_2 += r - pos + 1
	if l > 1:
		time_2 += r - l + 1
	time = time_1 if time_1 < time_2 else time_2
if pos < l:
	time = l - pos + 1
	if r < n:
		time += r - l + 1
if pos > r:
	time = -r + pos + 1
	if l > 1:
		time += r - l + 1
print(time)


inp = input().split(' ')
n = int(inp[0])
pos = int(inp[1])
l = int(inp[2])
r = int(inp[3])
t = 0
if r - l + 1 >= n:
	t = 0
else:
	if pos < l:
		t += l - pos + 1
		if r < n:
			t += r - l + 1
	if pos > r:
		t += pos - r + 1
		if l > 1:
			t += r - l + 1
	if pos >= l and pos <= r:
		if pos - l <= r - pos and l > 1:
			t += pos - l + 1
			if r < n:
				t += r - l + 1
		if pos - l <= r - pos and l == 1:
			t += r - pos + 1
		if pos - l > r - pos and r < n:
			t += r - pos + 1
			if l > 1:
				t += r - l + 1
		if pos - l > r - pos and r == n:
			t += pos - l + 1
print(t)


(n, p, l, r) = map(int, input().split())
s = 0
if l <= p <= r:
	if l > 1 and n - r > 0:
		if r - p <= p - l:
			s = r - p + 1 + r - l + 1
		else:
			s = p - l + 1 + r - l + 1
		print(s)
	elif l == 1 and r != n:
		print(r - p + 1)
	elif l == 1 and r == n:
		print(0)
	else:
		print(p - l + 1)
elif p < l:
	s = l - p + 1
	if r == n:
		print(s)
	else:
		print(s + r - l + 1)
else:
	s = s + p - r + 1
	if l == 1:
		print(s)
	else:
		print(s + r - l + 1)


(n, pos, l, r) = map(int, input().split())
if r == n and l == 1:
	print(0)
else:
	ans0 = abs(pos - r) + 1
	ans1 = abs(pos - l) + 1
	if r < n:
		ans1 += abs(r - l) + 1
	if l > 1:
		ans0 += abs(r - l) + 1
	print(min(ans1, ans0))


(n, pos, a, b) = map(int, input().split())
if a == 1 and b == n:
	ans = 0
if a == 1 and b != n:
	ans = abs(pos - b) + 1
if a != 1 and b == n:
	ans = abs(pos - a) + 1
if a != 1 and b != n:
	ans = b - a + min(abs(pos - a), abs(pos - b)) + 2
print(ans)


(n, pos, l, r) = [int(i) for i in input().split()]
if r == n and l == 1:
	print(0)
elif r == n:
	print(abs(pos - l) + 1)
elif l == 1:
	print(abs(r - pos) + 1)
else:
	s1 = abs(r - pos)
	s2 = abs(l - pos)
	print(min(s1, s2) + (r - l) + 2)


(n, pos, l, r) = [int(v) for v in input().split()]
needleft = l > 1
needright = r < n
if needleft:
	if needright:
		dl = abs(pos - l)
		dr = abs(pos - r)
		print(min(dl, dr) + 1 + r - l + 1)
	else:
		print(abs(pos - l) + 1)
elif needright:
	print(abs(pos - r) + 1)
else:
	print(0)


(a, b, c, d) = map(int, input().split())
if c == 1 and d == a:
	print(0)
elif c == 1:
	print(abs(d - b) + 1)
elif d == a:
	print(abs(b - c) + 1)
else:
	print(2 + d - c + min(abs(b - c), abs(d - b)))


R = lambda x: map(x, input().split())
(n, pos, l, r) = R(int)
(to_l, to_r) = (abs(pos - l), abs(pos - r))
l_to_r = r - l
if l == 1:
	to_l = 100000000000.0
if r == n:
	to_r = 100000000000.0
if to_l < to_r:
	print(to_l + 1 + (l_to_r + 1) * (r != n))
elif to_l != 100000000000.0 or to_r != 100000000000.0:
	print(to_r + 1 + (l_to_r + 1) * (l != 1))
else:
	print(0)


(n, pos, l, r) = list(map(int, input().split()))
if r - l + 1 == n:
	print(0)
elif l == 1:
	print(max(pos - r, r - pos) + 1)
elif r == n:
	print(max(pos - l, l - pos) + 1)
else:
	a = max(pos - l, l - pos)
	b = max(pos - r, r - pos)
	if a > b:
		print(b + 2 + r - l)
	else:
		print(a + 2 + r - l)


(n, pos, a, b) = map(int, input().split())
if a == 1:
	if pos <= b:
		if pos == 1 and b != n:
			print(b - a + 1)
		elif pos == 1 and b == n:
			print(0)
		elif pos != 1 and b != n:
			print(b - pos + 1)
		elif pos != 1 and b == n:
			print(0)
	else:
		print(pos - b + 1)
elif b == n:
	if a == 1:
		print(0)
		exit()
	if pos >= a:
		print(pos - a + 1)
		exit()
	if pos < a:
		print(a - pos + 1)
elif pos >= a and pos <= b:
	print(min(pos - a + b - a + 2, b - pos + b - a + 2))
elif pos < a:
	print(a - pos + 1 + b - a + 1)
elif pos > b:
	print(pos - b + 1 + b - a + 1)


def minimum_action(number_of_tabs, pos, left, right):
	if left > 1 and right < number_of_tabs:
		return min(abs(pos - left), abs(pos - right)) + (right - left) + 2
	if left > 1:
		return abs(pos - left) + 1
	if right < number_of_tabs:
		return abs(pos - right) + 1
	return 0

def test():
	assert minimum_action(6, 3, 2, 4) == 5
	assert minimum_action(6, 3, 1, 3) == 1
	assert minimum_action(5, 2, 1, 5) == 0
(n, pos, l, r) = [int(i) for i in input().split()]
print(minimum_action(n, pos, l, r))


import sys

def sanvandong():
	(m, n, a) = map(int, input().split())
	u = m // a if m % a == 0 else m // a + 1
	v = n // a if n % a == 0 else n // a + 1
	print(u * v)

def coccoc():
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		print(0)
		exit(0)
	if l == 1:
		print(abs(r - pos) + 1)
		exit(0)
	if r == n:
		print(abs(l - pos) + 1)
		exit(0)
	if pos < l:
		print(l - pos + 1 + (r - l) + 1)
		exit(0)
	if pos > r:
		print(pos - r + 1 + r - l + 1)
		exit(0)
	print(2 + min(pos - l, r - pos) * 2 + max(pos - l, r - pos))

def chiavachia():
	y = 0

def leftandRight():
	z = 0
coccoc()


(n, pos, l, r) = map(int, input().split())
if l == 1:
	if r == n:
		print(0)
	else:
		print(max(r - pos, pos - r) + 1)
elif r == n:
	print(max(pos - l, l - pos) + 1)
else:
	print(min(max(pos - l, l - pos), max(pos - r, r - pos)) + r - l + 2)


(n, p, l, r) = tuple(map(int, input().split()))
result = 0
flag = 0
if p < l:
	result += l - p + 1
	if r < n:
		result += r - l + 1
	print(result)
elif p > r:
	result += p - r + 1
	if l > 1:
		result += r - l + 1
	print(result)
else:
	dif = r - p
	if dif <= p - l and n - r != 0:
		result += r - p + 1
		if l - 1 != 0:
			result += r - l + 1
	elif dif > p - 1 and l - 1 != 0:
		result += p - l + 1
		if n - r != 0:
			result += r - l + 1
	else:
		if l - 1 != 0:
			result += p - l + 1
			flag = 1
		if n - r != 0:
			if flag:
				result += r - l + 1
			else:
				result += r - p + 1
	print(result)


(n, p, l, r) = map(int, input().split())
res = 0
if p < l:
	res = l - p + 1
	if r < n:
		res += r - l + 1
elif p > r:
	res = p - r + 1
	if l > 1:
		res += r - l + 1
elif l == 1 and r == n:
	res = 0
elif l == 1:
	res = 1 + r - p
elif r == n:
	res = 1 + p - l
else:
	res = 2 * min(r - p, p - l) + max(r - p, p - l) + 2
print(res)


def main():
	(n, pos, l, r) = map(int, input().split())
	ans = 0
	if l == 1 and r == n:
		print(0)
		return
	if l == 1:
		if pos > r:
			ans = pos - r + 1
		else:
			ans = r - pos + 1
		print(ans)
		return
	if r == n:
		if pos > l:
			ans = pos - l + 1
		else:
			ans = l - pos + 1
		print(ans)
		return
	if l == r:
		if pos > l:
			ans += pos - l + 2
		elif pos == l:
			ans = 2
		else:
			ans += l - pos + 2
		print(ans)
		return
	if l <= pos and pos <= r:
		if pos - l < r - pos:
			ans += pos - l + 1
			if r != n:
				ans += r - l + 1
		else:
			ans += r - pos + 1
			if l != 1:
				ans += r - l + 1
	elif pos < l:
		ans += l - pos + 1
		if r != n:
			ans += r - l + 1
	else:
		ans += pos - r + 1
		if l != 1:
			ans += r - l + 1
	print(ans)
main()


(n, pos, l, r) = map(int, input().split())
res1 = 0
res2 = 0
if l != 1:
	res1 += abs(l - pos) + 1
	if r != n:
		res1 += r - l + 1
elif r != n:
	res1 = abs(r - pos) + 1
if r != n:
	res2 += abs(pos - r) + 1
	if l != 1:
		res2 += r - l + 1
elif l != 1:
	res2 += abs(l - pos) + 1
print(min(res1, res2))


(n, p, l, r) = map(int, input().split())
(x, y) = (abs(p - l) + 1, abs(r - p) + 1)
print(y * (r < n) if l == 1 else x if r == n else min(x, y) + r - l + 1)


(n, pos, l, r) = map(int, input().split())
diff = r - l
if l > 1:
	lb = 1
else:
	lb = 0
if r < n:
	rb = 1
else:
	rb = 0
ans = 0
if lb and rb:
	ans = min(abs(pos - l), abs(pos - r)) + diff
	ans += 2
elif lb:
	ans = abs(pos - l) + 1
elif rb:
	ans = abs(pos - r) + 1
else:
	pass
print(ans)


(n, pos, l, r) = map(int, input().split())
if pos >= l and pos <= r:
	if l > 1 and r < n:
		ans = r - l + 1 + min(pos - l, r - pos) + 1
	elif l == 1 and r == n:
		ans = 0
	elif l == 1:
		ans = r - pos + 1
	elif r == n:
		ans = pos - l + 1
elif pos < l:
	if l > 1 and r < n:
		ans = r - pos + 2
	elif r == n:
		ans = l - pos + 1
elif pos > r:
	if l > 1 and r < n:
		ans = pos - l + 2
	elif l == 1:
		ans = pos - r + 1
print(ans)


(n, pos, l, r) = map(int, input().split())
if pos >= l and pos <= r:
	if l == 1 and r != n:
		print(r - pos + 1)
	elif r == n and l != 1:
		print(pos - l + 1)
	elif l == 1 and r == n:
		print(0)
	else:
		x = pos - l
		y = r - pos
		a = max(x, y)
		b = min(x, y)
		print(2 * b + a + 2)
elif pos < l:
	if r != n:
		print(r - pos + 2)
	else:
		print(l - pos + 1)
elif pos > r:
	if l != 1:
		print(pos - l + 2)
	else:
		print(pos - r + 1)


(n, pos, l, r) = map(int, input().split())
total = 0
if pos >= l and pos <= r:
	if pos - l <= r - pos:
		if l != 1:
			total += pos - l + 1
			if r != n:
				total += r - l + 1
		elif r != n:
			total += r - pos + 1
	elif r != n:
		total += r - pos + 1
		if l != 1:
			total += r - l + 1
	elif l != 1:
		total += pos - l + 1
elif pos < l:
	total += l - pos + 1
	if r != n:
		total += r - l + 1
else:
	total += pos - r + 1
	if l != 1:
		total += r - l + 1
print(total)


(n, pos, l, r) = [int(x) for x in input().split(' ')]
ans = 0
ra = abs(pos - r)
la = abs(pos - l)
if l == 1:
	if r == n:
		print(0)
	else:
		print(ra + 1)
elif r == n:
	print(la + 1)
elif la < ra:
	print(r - l + 2 + la)
else:
	print(r - l + 2 + ra)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from math import ceil
for _ in range(int(input()) if not True else 1):
	(n, pos, l, r) = map(int, input().split())
	if l == 1:
		if r == n:
			print(0)
			continue
		else:
			print(1 + abs(pos - r))
			continue
	if r == n:
		print(1 + abs(l - pos))
		continue
	print(2 + min(abs(pos - l), abs(r - pos)) + r - l)


(n, pos, l, r) = map(int, input().split())
tol = abs(pos - l) + 1
tor = abs(pos - r) + 1
if l == 1 and r == n:
	ans = 0
elif l == 1:
	ans = tor
elif r == n:
	ans = tol
elif pos >= l and pos <= r:
	ans = min(tol, tor) + r - l + 1
else:
	ans = min(tol, tor) + r - l + 1
print(ans)


(n, p, l, r) = map(int, input().split())
ans = 10000
if l == 1 and r == n:
	ans = 0
else:
	if l == 1:
		ans = min(ans, abs(r - p) + 1)
	if r == n:
		ans = min(ans, abs(l - p) + 1)
	ans = min(ans, abs(l - p) + r - l + 2)
	ans = min(ans, abs(r - p) + r - l + 2)
print(ans)


(n, p, l, r) = map(int, input().split())
ans = 0
if p <= l:
	ans += l - p + 1
	if l == 1:
		ans -= 1
	if r != n:
		ans += r - l + 1
elif p >= r:
	ans += p - r + 1
	if r == n:
		ans -= 1
	if l != 1:
		ans += r - l + 1
elif p - l < r - p:
	if l != 1:
		ans += p - l + 1
	else:
		l = p
	if r != n:
		ans += r - l + 1
else:
	if r != n:
		ans += r - p + 1
	else:
		r = p
	if l != 1:
		ans += r - l + 1
print(ans)


a = input()
a = a.split(' ')
n = int(a[0])
pos = int(a[1])
l = int(a[2])
r = int(a[3])
if l == 1 and r == n:
	print(0)
elif l == 1 and r != n or (l != 1 and r == n):
	if l == 1:
		print(abs(pos - r) + 1)
	else:
		print(abs(pos - l) + 1)
else:
	print(r - l + min(abs(pos - l), abs(pos - r)) + 2)


(n, pos, l, r) = list(map(int, input().split()))
l_close = l == 1
r_close = r == n
ans = 0
if l_close and r_close:
	pass
elif l_close:
	ans += abs(pos - r) + 1
elif r_close:
	ans += abs(pos - l) + 1
else:
	ans += min(abs(pos - r), abs(pos - l)) + 1 + abs(l - r) + 1
print(ans)


import math as m
(n, pos, l, r) = [int(x) for x in input().split()]
a = [x + 1 for x in range(n)]
count = 0
cp = pos
if pos - l < r - pos:
	if l > 1:
		count += m.fabs(pos - l) + 1
		cp = l
	if r < n:
		count += m.fabs(r - cp) + 1
else:
	if r < n:
		count += m.fabs(r - pos) + 1
		cp = r
	if l > 1:
		count += m.fabs(cp - l) + 1
print(int(count))


(n, pos, l, r) = map(int, input().split())
if pos in range(l, r + 1):
	if l != 1 and r != n:
		print(min(r - pos, pos - l) + 1 + (r - l + 1))
	elif l != 1:
		print(pos - l + 1)
	elif r != n:
		print(r - pos + 1)
	else:
		print(0)
elif l != 1 and r != n:
	if pos >= r:
		print(pos - r + 1 + (r - l + 1))
	else:
		print(l - pos + 1 + (r - l + 1))
elif l != 1:
	print(l - pos + 1)
elif r != n:
	print(pos - r + 1)
else:
	print(0)


(n, pos, l, r) = map(int, input().split())
cur = pos
fs = 0
ss = 0
if l > 1:
	fs += abs(cur - l) + 1
	cur = l
if r < n:
	fs += abs(cur - r) + 1
cur = pos
if r < n:
	ss += abs(cur - r) + 1
	cur = r
if l > 1:
	ss += abs(cur - l) + 1
print(min(fs, ss))


x = input().split()
n = int(x[0])
pos = int(x[1])
l = int(x[2])
r = int(x[3])
if l == 1 and r < n:
	ans = abs(r - pos) + 1
elif r == n and l > 1:
	ans = abs(l - pos) + 1
elif l > 1 and r < n:
	ans = min(abs(r - pos), abs(l - pos)) + r - l + 2
else:
	ans = 0
print(ans)


(n, pos, l, r) = map(int, input().split())
if pos < l:
	x = 0
	if r < n:
		x = r - l + 1
	print(l - pos + 1 + x)
elif pos > r:
	x = 0
	if l > 1:
		x = r - l + 1
	print(pos - r + 1 + x)
elif l == 1 and r == n:
	print(0)
elif l == 1:
	print(r - pos + 1)
elif r == n:
	print(pos - l + 1)
else:
	a = pos - l + 1 + r - l + 1
	b = r - pos + 1 + r - l + 1
	if a < b:
		print(a)
	else:
		print(b)


def main():
	(n, pos, l, r) = map(int, input().split())
	ans = 0
	if l <= pos <= r:
		if l == 1:
			if r == n:
				print(0)
				return
			ans += r - pos + 1
			print(ans)
			return
		if r == n:
			ans = pos - l + 1
			print(ans)
			return
		ans = min(pos - l, r - pos) + r - l + 2
		print(ans)
		return
	if pos > r:
		ans += pos - r + 1
		if l > 1:
			ans += r - l + 1
		print(ans)
		return
	ans += l - pos + 1
	if r < n:
		ans += r - l + 1
	print(ans)
	return
main()


(n, pos, l, r) = map(int, input().split())
ans = 0
if l > 1:
	ans += 1
if r < n:
	ans += 1
if l <= pos <= r:
	if l > 1:
		if r < n:
			ans += min(pos - l, r - pos) + r - l
		else:
			ans += pos - l
	elif r < n:
		ans += r - pos
if pos < l:
	if r < n:
		ans += r - pos
	else:
		ans += l - pos
if pos > r:
	if l > 1:
		ans += pos - l
	else:
		ans += pos - r
print(ans)


(n, pos, l, r) = [int(x) for x in input().split()]
left_closed = l == 1
right_closed = r == n
moves = 0
while not left_closed or not right_closed:
	if r < pos:
		pos -= 1
	elif pos < l:
		pos += 1
	elif pos < r and (not right_closed) and (left_closed or r - pos <= pos - l):
		pos += 1
	elif pos > l and (not left_closed) and (not (not right_closed and pos == r)):
		pos -= 1
	elif l == pos and (not left_closed):
		left_closed = True
	elif r == pos:
		right_closed = True
	moves += 1
print(moves)


string = input().split(' ')
n = int(string[0])
pos = int(string[1])
a = int(string[2])
b = int(string[3])
if b == n and a == 1:
	print(0)
elif b == n and a != 1:
	if pos >= a:
		print(pos - a + 1)
	else:
		print(a - pos + 1)
elif a == 1 and b != n:
	if pos <= b:
		print(b - pos + 1)
	else:
		print(pos - b + 1)
elif pos <= a:
	print(b - pos + 2)
elif pos >= b:
	print(pos + 2 - a)
else:
	res_left = pos - 2 * a + 2 + b
	res_right = 2 * b - pos + 2 - a
	print(min(res_left, res_right))


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(n, p, l, r) = map(int, input().split())
if l == 1 and r == n:
	print(0)
elif l == 1:
	print(abs(p - r) + 1)
elif r == n:
	print(abs(p - l) + 1)
elif r < p:
	print(2 + p - l)
elif l <= p <= r:
	print(min(p - l, r - p) + r - l + 2)
else:
	print(2 + r - p)


import math

def main():
	(n, p, l, r) = map(int, input().split())
	res = 0
	if l == 1 and r == n:
		pass
	elif l == 1:
		res += 1 + math.fabs(p - r)
	elif r == n:
		res += 1 + math.fabs(p - l)
	else:
		res += 2 + r - l
		res += min(math.fabs(p - r), math.fabs(p - l))
	print(int(res))
main()


(n, pos, l, r) = [int(x) for x in input().split()]
if n == 1 or (r == n and l == 1):
	print(0)
elif l == 1:
	print(abs(pos - r) + 1)
elif r == n:
	print(abs(pos - l) + 1)
else:
	print(min(abs(pos - l), abs(pos - r)) + 2 + r - l)


(n, pos, l, r) = map(int, input().split())
if l == 1 and r == n:
	print(0)
	exit()
if l == 1:
	print(abs(pos - r) + 1)
	exit()
if r == n:
	print(abs(pos - l) + 1)
	exit()
if abs(pos - l) > abs(pos - r):
	(l, r) = (r, l)
print(abs(pos - l) + 1 + abs(r - l) + 1)


(n, pos, l, r) = input('').strip().split(' ')
(n, pos, l, r) = (int(n), int(pos), int(l), int(r))
t = 0
if pos <= r and pos >= l:
	if l == 1 and r == n:
		t = 0
	elif l == 1:
		t += r - pos
		t += 1
	elif r == n:
		t += pos - l
		t += 1
	elif l > 1 and r < n:
		t += min(abs(pos - l), abs(r - pos))
		t += 1
		t += r - l
		t += 1
else:
	if pos < l:
		t += l - pos
		t += 1
		if r != n:
			t += r - l
			t += 1
	if pos > r:
		t += pos - r
		t += 1
		if l != 1:
			t += r - l
			t += 1
print(t)


R = lambda : list(map(int, input().split()))
(n, pos, l, r) = R()
res = 0
if pos < l:
	res = res + (l - pos + 1)
	if r != n:
		res = res + (r - l + 1)
elif pos > r:
	res = res + (pos - r + 1)
	if l != 1:
		res += r - l + 1
elif pos == l:
	if l != 1:
		res = res + 1
	if r != n:
		res = res + (r - l + 1)
elif pos == r:
	if r != n:
		res = res + 1
	if l != 1:
		res = res + (r - l + 1)
else:
	if l != 1:
		if r != n:
			res = res + (min(r - pos, pos - l) + 1 + r - l + 1)
	if l != 1:
		if r == n:
			res = res + (pos - l + 1)
	if l == 1:
		if r != n:
			res = res + (r - pos + 1)
print(res)


import sys

def sanvandong():
	(m, n, a) = map(int, input().split())
	u = m // a if m % a == 0 else m // a + 1
	v = n // a if n % a == 0 else n // a + 1
	print(u * v)

def coccoc():
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		print(0)
	elif l == 1:
		print(abs(r - pos) + 1)
	elif r == n:
		print(abs(l - pos) + 1)
	elif pos < l:
		print(l - pos + 1 + (r - l) + 1)
	elif pos > r:
		print(pos - r + 1 + r - l + 1)
	else:
		print(2 + min(pos - l, r - pos) * 2 + max(pos - l, r - pos))

def chiavachia():
	for _ in range(int(input())):
		n = int(input())
		(u, cnt) = (n, 0)
		while u > 0:
			dig = u % 10
			u = u // 10
			if dig > 0 and n % dig == 0:
				cnt += 1
		print(cnt)

def leftandRight():
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if k < min(a):
		print(0)
	elif k >= max(a):
		print(n)
	else:
		(i, j) = (-1, n)
		while i < n and a[i + 1] <= k:
			i += 1
		while j > 0 and a[j - 1] <= k:
			j -= 1
		if i > j:
			print(n)
		else:
			print(max(0, n - (j - i - 1)))
coccoc()


x = input().split()
n = int(x[0])
pos = int(x[1])
l = int(x[2])
r = int(x[3])

def ans(l, r, pos):
	if l == 1 and r == n:
		return 0
	if pos > r:
		if l == 1:
			return pos - r + 1
		return pos - l + 2
	elif pos < l:
		if r == n:
			return l - pos + 1
		return r - pos + 2
	elif l == 1:
		return r - pos + 1
	elif r == n:
		return pos - l + 1
	elif pos - l <= r - pos:
		return pos - l + r - l + 2
	else:
		return r - pos + r - l + 2
print(ans(l, r, pos))


(n, pos, l, r) = map(int, input().split())

def solve():
	if pos <= l:
		if l == 1:
			if r == n:
				return 0
			if r != n:
				return r - pos + 1
		if l != 1:
			if r == n:
				return l - pos + 1
			if r != n:
				return l - pos + 1 + r - l + 1
	if pos >= r:
		if r == n:
			if l == 1:
				return 0
			if l != 1:
				return pos - l + 1
		if r != n:
			if l == 1:
				return pos - r + 1
			if l != 1:
				return pos - r + 1 + r - l + 1
	if pos > l and pos < r:
		if l == 1 and r == n:
			return 0
		if l == 1 and r != n:
			return r - pos + 1
		if r == n and l != 1:
			return pos - l + 1
		if l > 1 and r < n:
			t1 = pos - l + 1 + r - l + 1
			t2 = r - pos + 1 + r - l + 1
			return min(t1, t2)
	if pos == l and pos == r:
		if n == 1:
			return 0
		elif l == 1 or l == n:
			return 1
		else:
			return 2
	return -1
print(solve())


import sys
import math

def isIn(position, left, right):
	return position >= left and position <= right

def main():
	(n, pos, l, r) = map(int, input().split(' '))
	steps = 0
	if isIn(pos, l, r):
		d1 = pos - l
		d2 = r - pos
		if d1 == 0:
			if l > 1:
				steps = 1
			if r < n:
				steps += r - l + 1
		elif d2 == 0:
			if r < n:
				steps = 1
			if l > 1:
				steps += r - l + 1
		elif d1 < d2:
			if l > 1:
				steps += pos - l + 1
				pos = l
			if r < n:
				steps += r - pos + 1
		elif d2 <= d1:
			if r < n:
				steps += r - pos + 1
				pos = r
			if l > 1:
				steps += pos - l + 1
	elif pos < l:
		if l > 1:
			steps += l - pos + 1
		if r < n:
			steps += r - l + 1
	elif pos == l:
		if l > 1:
			steps += 1
		if r < n:
			steps += r - l + 1
	elif pos > r:
		if r < n:
			steps += pos - r + 1
		if l > 1:
			steps += r - l + 1
	elif pos == r:
		if r < n:
			steps += 1
		if l > 1:
			steps += r - l + 1
	print(steps)
main()


(n, pos, l, r) = map(int, input().split())
(dl, dr) = (abs(pos - l) + 1, abs(pos - r) + 1)
print(dr * (r < n) if l == 1 else dl if r == n else min(dl, dr) + r - l + 1)


(n, pos, l, r) = [int(t) for t in input().split()]
if l == 1 and r == n:
	print(0)
if l == 1 and r != n:
	print(abs(r - pos) + 1)
if r == n and l != 1:
	print(abs(l - pos) + 1)
if r != n and l != 1:
	print(r - l + min(abs(r - pos), abs(l - pos)) + 2)


(m, lel, l, z) = map(int, input().split())
c = 0
if l == 1 and z == m:
	c = 0
elif l == 1:
	c = abs(lel - z) + 1
elif z == m:
	c = abs(lel - l) + 1
else:
	c += min(abs(lel - l), abs(z - lel))
	c += abs(z - l)
	c += 2
print(c)


(n, pos, l, r) = map(int, input().split())
res = 0
if pos - l < r - pos:
	if l != 1:
		res += abs(pos - l) + 1
		pos = l
	if r != n:
		res += abs(r - pos) + 1
else:
	if r != n:
		res += abs(r - pos) + 1
		pos = r
	if l != 1:
		res += abs(pos - l) + 1
print(res)


(n, pos, l, r) = [int(i) for i in input().split()]
a = l - 1
b = n - r
if a > 0 and b > 0:
	x1 = min(abs(l - pos), abs(r - pos))
	x2 = abs(l - r)
	x = x1 + x2 + 2
if a > 0 and b == 0:
	x1 = abs(l - pos)
	x = x1 + 1
if a == 0 and b > 0:
	x1 = abs(r - pos)
	x = x1 + 1
if a == 0 and b == 0:
	x = 0
print(x)


(n, pos, l, r) = map(int, input().split())
time = 0
if l != 1 or r != n:
	if r == n or (l != 1 and abs(pos - l) < abs(pos - r)):
		time += abs(pos - l) + 1
		if r != n:
			time += r - l + 1
	else:
		time += abs(pos - r) + 1
		if l != 1:
			time += r - l + 1
print(time)


(n, pos, l, r) = [int(x) for x in input().split()]
count = 0
if pos in range(l, r + 1):
	if pos - l < r - pos:
		if len(range(1, l + 1)) != 1:
			count += pos - l + 1
			pos = l
		if len(range(r, n + 1)) != 1:
			count += r - pos + 1
			pos = r
	else:
		if len(range(r, n + 1)) != 1:
			count += r - pos + 1
			pos = r
		if len(range(1, l + 1)) != 1:
			count += pos - l + 1
			pos = l
elif l == 1 and r == n:
	count = 0
elif l == 1 or r == n:
	if l > pos:
		count += l - pos + 1
	elif pos > r:
		count += pos - r + 1
elif l > pos:
	count += l - pos + 1
	count += r - l + 1
elif pos > r:
	count += pos - r + 1
	count += r - l + 1
print(count)


(n, pos, l, r) = [int(i) for i in input().split()]
tot = 0
a = 0
b = 0
if pos > r:
	tot += pos - r + 1
	if l > 1:
		tot += r - l + 1
elif pos < l:
	tot += l - pos + 1
	if r < n:
		tot += r - l + 1
else:
	if n > r:
		a = abs(r - pos)
		tot += 1
	if l > 1:
		b = abs(l - pos)
		tot += 1
	tot += a + b + min(a, b)
print(tot)


(n, pos, l, r) = map(int, input().split())
ans = 0
p = pos
if l != 1:
	ans += abs(pos - l) + 1
	pos = l
if r != n:
	ans += abs(pos - r) + 1
	pos = r
temp = 0
pos = p
if r != n:
	temp += abs(pos - r) + 1
	pos = r
if l != 1:
	temp += abs(pos - l) + 1
	pos = l
print(min(temp, ans))


def main():
	(N, POS, L, R) = map(int, input().split())
	if L == 1 and R == N:
		step = 0
	elif L == 1:
		step = abs(POS - R) + 1
	elif R == N:
		step = abs(POS - L) + 1
	else:
		step = abs(min(POS - L, R - POS)) + R - L + 2
	print(step)
main()


import sys
import math
input = sys.stdin.readline
for _ in range(1):
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		print(0)
		continue
	if l != 1 and r != n:
		ans = min(abs(l - pos), abs(r - pos)) + 2 + abs(r - l)
	elif l == 1:
		ans = abs(pos - r) + 1
	else:
		ans = abs(pos - l) + 1
	print(ans)


(n, p, left, right) = map(int, input().split())
summ = 0
if p < left:
	summ = left - p + 1 + (right - left + 1) * (right < n)
elif p > right:
	summ = p - right + 1 + (right - left + 1) * (left > 1)
elif p - left < right - p:
	summ = (p - left + 1 + (p - left) * (right < n)) * (left > 1)
	summ += (right - p + 1) * (right < n)
else:
	summ = (right - p + 1 + (right - p) * (left > 1)) * (right < n)
	summ += (p - left + 1) * (left > 1)
print(summ)


import sys

def solution(tabs, pointer, l, r):
	if l == 1 and r == tabs:
		return 0
	if l == 1:
		return abs(pointer - r) + 1
	if r == tabs:
		return abs(pointer - l) + 1
	dist_r = abs(pointer - r)
	dist_l = abs(pointer - l)
	return min(dist_l, dist_r) + r - l + 2
(tabs, pointer, l, r) = input().strip().split(' ')
(tabs, pointer, l, r) = [int(tabs), int(pointer), int(l), int(r)]
print(solution(tabs, pointer, l, r))


def main():
	(n, pos, l, r) = map(lambda x: int(x), input().split(' '))
	move = 0
	a = 1
	b = n
	if l == 1 and r == n:
		move = 0
	elif l == 1:
		(move, pos) = moveto(move, pos, r)
		move = delete(move)
	elif r == n:
		(move, pos) = moveto(move, pos, l)
		move = delete(move)
	elif pos < l:
		(move, pos) = moveto(move, pos, l)
		move = delete(move)
		(move, pos) = moveto(move, pos, r)
		move = delete(move)
	elif l <= pos <= r:
		if closertoL(l, r, pos):
			(move, pos) = moveto(move, pos, l)
			move = delete(move)
			(move, pos) = moveto(move, pos, r)
			move = delete(move)
		else:
			(move, pos) = moveto(move, pos, r)
			move = delete(move)
			(move, pos) = moveto(move, pos, l)
			move = delete(move)
	else:
		(move, pos) = moveto(move, pos, r)
		move = delete(move)
		(move, pos) = moveto(move, pos, l)
		move = delete(move)
	print(move)

def delete(move):
	return move + 1

def moveto(move, loc, des):
	move = move + abs(des - loc)
	loc = des
	return (move, loc)

def closertoL(l, r, pos):
	return pos - l <= r - pos
main()


from sys import stdin
from io import StringIO
(n, pos, l, r) = map(int, stdin.readline().strip().split())
if l == 1 and r == n:
	print(0)
elif l == 1:
	print(abs(pos - r) + 1)
elif r == n:
	print(abs(pos - l) + 1)
elif pos < l:
	print(r - pos + 2)
elif pos > r:
	print(pos - l + 2)
else:
	print(min(pos - l + r - l + 2, r - pos + r - l + 2))


import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque

def LM(t, r):
	return list(map(t, r))

def R():
	return stdin.readline()

def RS():
	return R().split()

def I():
	return int(R())

def F():
	return float(R())

def LI():
	return LM(int, RS())

def LF():
	return LM(float, RS())

def ONE_SL():
	return list(input())

def ONE_IL():
	return LM(int, ONE_SL())

def ALL_IL():
	return LM(int, stdin)

def ap(f):
	return f.append

def pll(li):
	print('\n'.join(LM(str, li)))

def pljoin(li, s):
	print(s.join(li))

def main():
	(n, pos, L, R) = LI()
	ans = 0
	l = -1
	r = 1
	if L <= pos <= R:
		if R != n:
			ans += R - pos + 1
			r = 0
		if L != 1:
			ans += pos - L + 1
			l = 0
		if r == l:
			ans += min(pos - L, R - pos)
	elif L > pos:
		ans += L - pos + 1
		if R != n:
			ans += R - L + 1
	elif pos > R:
		ans += pos - R + 1
		if L != 1:
			ans += R - L + 1
	print(ans)
main()


(n, pos, l, r) = map(int, input().split())
seconds = 0
if pos < l:
	seconds += l - pos
	pos = l
if pos > r:
	seconds += pos - r
	pos = r
(left, right) = (l > 1, r < n)
if left and right:
	seconds += +(min(pos - l, r - pos) + (r - l) + 2)
elif left:
	seconds += 1 + (pos - l)
elif right:
	seconds += 1 + (r - pos)
print(seconds)


(n, p, l, r) = map(int, input().split())
if l == 1 and r == n:
	a = 0
elif l == 1:
	a = abs(r - p) + 1
elif r == n:
	a = abs(p - l) + 1
elif p <= l:
	a = r - p + 2
elif p > l and p < r and (p - l <= r - p):
	a = p - l + 2 + (r - l)
elif p > l and p < r and (p - l > r - p):
	a = r - p + 2 + (r - l)
else:
	a = p - l + 2
print(a)


class CodeforcesTask915BSolution:

	def __init__(self):
		self.result = ''
		self.n_pos_l_r = []

	def read_input(self):
		self.n_pos_l_r = [int(x) for x in input().split(' ')]

	def process_task(self):
		position = self.n_pos_l_r[1]
		targets = []
		time = 0
		if self.n_pos_l_r[2] > 1:
			targets.append(self.n_pos_l_r[2])
			time += 1
		if self.n_pos_l_r[3] < self.n_pos_l_r[0]:
			targets.append(self.n_pos_l_r[3])
			time += 1
		if self.n_pos_l_r[3] < position:
			targets.sort(reverse=True)
		if self.n_pos_l_r[2] <= position <= self.n_pos_l_r[3]:
			if len(targets) == 2:
				if abs(targets[0] - position) > abs(targets[1] - position):
					(targets[0], targets[1]) = (targets[1], targets[0])
		while targets:
			if targets[0] > position:
				position += 1
				time += 1
			elif targets[0] < position:
				position -= 1
				time += 1
			else:
				targets.pop(0)
		self.result = str(time)

	def get_result(self):
		return self.result
Solution = CodeforcesTask915BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


data = list(map(int, input().split(' ')))
n = data[0]
pos = data[1]
l = data[2]
r = data[3]
res = 0
if pos < l:
	res += l - pos + 1
	if r < n:
		res += r - l + 1
elif pos > r:
	res += pos - r + 1
	if l > 1:
		res += r - l + 1
elif r < n and l > 1:
	res += min(pos - l, r - pos) + r - l + 2
elif l > 1:
	res += pos - l + 1
elif r < n:
	res += r - pos + 1
print(res)


(n, pos, l, r) = [int(i) for i in input().split()]
seconds = 0
if l > 1:
	seconds += 1
	if abs(pos - l) < abs(pos - r) or r == n:
		seconds += abs(pos - l)
	else:
		seconds += r - l
if r < n:
	seconds += 1
	if abs(pos - l) >= abs(pos - r) or l == 1:
		seconds += abs(pos - r)
	else:
		seconds += r - l
print(seconds)


vals = input()
vals = vals.strip()
n = int(vals.split()[0])
pos = int(vals.split()[1])
l = int(vals.split()[2])
r = int(vals.split()[3])
minMoves = 0
if l > 1 and r < n:
	closeL = max(pos - l, l - pos)
	closeR = max(pos - r, r - pos)
	if closeR <= closeL:
		if pos >= r:
			minMoves += pos - r + 1
		else:
			minMoves += r - pos + 1
		minMoves += r - l + 1
	else:
		if pos >= l:
			minMoves += pos - l + 1
		else:
			minMoves += l - pos + 1
		minMoves += r - l + 1
elif l > 1:
	if pos >= l:
		minMoves += pos - l + 1
	else:
		minMoves += l - pos + 1
elif r < n:
	if pos >= r:
		minMoves += pos - r + 1
	else:
		minMoves += r - pos + 1
else:
	minMoves = 0
print(minMoves)


from sys import stdin as cin
from sys import stdout as cout

def main():
	(n, pos, l, r) = map(int, cin.readline().split())
	if l == 1 and r == n:
		print(0)
		return
	if l == 1:
		print(1 + abs(r - pos))
		return
	if r == n:
		print(1 + abs(pos - l))
		return
	if l == r:
		print(2 + abs(pos - l))
		return
	print(2 + min(abs(r - pos), abs(l - pos)) + r - l)
main()


(n, pos, l, r) = map(int, input().split())
if r - l + 1 == n:
	print(0)
	exit(0)
if pos > l and pos < r:
	if n > r and l > 1:
		x = pos - l + 1 + r - l + 1
		y = r - pos + 1 + r - l + 1
		ans = min(x, y)
	elif n == r:
		ans = pos - l + 1
	elif l == 1:
		ans = r - pos + 1
elif pos >= r:
	if n > r:
		ans = pos - r + 1
	else:
		ans = 0
	if l > 1:
		ans += r - l + 1
elif pos <= l:
	if l > 1:
		ans = l - pos + 1
	else:
		ans = 0
	if n > r:
		ans += r - l + 1
print(ans)


(n, pos, l, r) = map(int, input().split())
has_left = l > 1
has_right = r < n
if not has_left and (not has_right):
	print(0)
elif has_left and (not has_right):
	print(1 + abs(l - pos))
elif not has_left and has_right:
	print(1 + abs(r - pos))
else:
	a = abs(l - pos) + abs(l - r)
	b = abs(r - pos) + abs(r - l)
	print(2 + min(a, b))


def solve(n, pos, l, r):
	if l == 1 and r == n:
		return 0
	if l == 1:
		return abs(r - pos) + 1
	if r == n:
		return abs(pos - l) + 1
	res = 0
	if abs(pos - l) < abs(pos - r):
		res += abs(pos - l) + 1
	else:
		res += abs(pos - r) + 1
	return res + abs(l - r) + 1
lst = list(map(int, input().split()))
print(solve(lst[0], lst[1], lst[2], lst[3]))


def b():
	(n, pos, l, r) = map(int, input().split(' '))
	if l == 1 and r == n:
		return 0
	if r == n:
		return abs(pos - l) + 1
	if l == 1:
		return abs(pos - r) + 1
	return r - l + 2 + min(abs(r - pos), abs(pos - l))
print(b())


(n, p, i, r) = list(map(int, input().split()))
ta = 0
if i != 1 and r != n:
	op = abs(p - i)
	sa = abs(p - r)
	ta += min(op, sa)
	ta += r - i
	ta += 1
	print(ta + 1)
elif i == 1 and r != n:
	sa = abs(p - r)
	ta += sa
	ta += 1
	print(ta)
elif i != 1 and r == n:
	op = abs(p - i)
	ta += op
	ta += 1
	print(ta)
else:
	print('0')


(n, pos, l, r) = [int(k) for k in input().split(' ') if k]
if r - l + 1 == n:
	print(0)
elif r != n and l != 1:
	print(min(abs(pos - r), abs(pos - l)) + 1 + abs(r - l) + 1)
elif r == n:
	print(abs(pos - l) + 1)
elif l == 1:
	print(abs(pos - r) + 1)
else:
	raise Exception


(n, pos, l, r) = map(int, input().split())
left_first = 10 ** 6
right_first = 10 ** 6
if l == 1 and r == n:
	left_first = 0
elif l == 1:
	if pos < r:
		right_first = r - pos + 1
	else:
		right_first = pos - r + 1
elif r == n:
	if pos < l:
		left_first = l - pos + 1
	else:
		left_first = pos - l + 1
elif pos < l:
	left_first = l - pos + 1 + r - l + 1
elif l <= pos <= r:
	left_first = pos - l + r - l + 2
	right_first = r - pos + r - l + 2
else:
	right_first = pos - r + r - l + 2
print(min([left_first, right_first]))


import sys

def sol(n, pos, l, r):
	total = 0
	if l == 1 and r == n:
		return 0
	if l == 1:
		return abs(pos - r) + 1
	if r == n:
		return abs(pos - l) + 1
	if pos < l:
		return r + 2 - pos
	if l <= pos <= r:
		left = pos - l + r - l + 2
		right = r - pos + r - l + 2
		return min(left, right)
	if pos > r:
		return pos - l + 2
[n, pos, l, r] = [int(x) for x in input().strip().split()]
print(sol(n, pos, l, r))


(n, pos, l, r) = [int(x) for x in input().split()]
cl = l > 1
cr = r < n
if not cl and (not cr):
	print(0)
elif cl and (not cr):
	print(abs(pos - l) + 1)
elif not cl and cr:
	print(abs(pos - r) + 1)
elif pos <= l:
	print(abs(pos - r) + 2)
elif pos >= r:
	print(abs(pos - l) + 2)
else:
	print(min(abs(pos - l), abs(pos - r)) + 1 + (r - l) + 1)


(n, pos, l, r) = list(map(int, input().split()))
if l == 1 and n == r:
	print(0)
elif l == 1:
	print(max(r, pos) - min(r, pos) + 1)
elif r == n:
	print(max(l, pos) - min(l, pos) + 1)
else:
	where_l = max(l, pos) - min(l, pos) + 1
	where_r = max(r, pos) - min(r, pos) + 1
	if where_l < where_r:
		print(max(l, pos) - min(l, pos) + 1 + r - l + 1)
	else:
		print(max(r, pos) - min(r, pos) + 1 + r - l + 1)


npt = input().split()
(n, pos, l, r) = (int(npt[0]), int(npt[1]), int(npt[2]), int(npt[3]))
a = 1
b = n
result = 0
if l == 1 and r == n:
	result = 0
elif l == 1:
	result = abs(pos - r) + 1
elif r == n:
	result = abs(pos - l) + 1
else:
	result = min(abs(pos - l), abs(pos - r)) + r - l + 2
print(result)


(n, pos, l, r) = map(int, input().split())
res = 0
if l != 1:
	res += 1
if r != n:
	res += 1
if pos <= l:
	res += l - pos
	if r != n:
		res += r - l
	print(res)
	exit()
if pos >= r:
	res += pos - r
	if l != 1:
		res += r - l
	print(res)
	exit()
if (pos > l) & (pos < r):
	if (l == 1) & (r != n):
		res += r - pos
	elif (l != 1) & (r == n):
		res += pos - l
	elif (l == 1) & (r == n):
		res = 0
	else:
		res += min(r - pos, pos - l)
		res += r - l
	print(res)
	exit()


def main():
	(n, pos, l, r) = [int(_) for _ in input().split(' ')]
	if l is 1 and r == n:
		print(0)
		return
	if l is 1:
		print(abs(r - pos) + 1)
		return
	if r == n:
		print(abs(l - pos) + 1)
		return
	print(min(abs(l - pos), abs(r - pos)) + (r - l) + 2)
main()


(n, curr, l, r) = map(int, input().split())
pos = 0
if l == 1 and r == n:
	print(0)
	exit()
if l == 1:
	if curr >= r:
		print(curr - r + 1)
	else:
		print(r - curr + 1)
	exit()
if r == n:
	if curr >= l:
		print(curr - l + 1)
	else:
		print(l - curr + 1)
	exit()
if curr == l:
	print(1 + (r - l) + 1)
	exit()
if curr == r:
	print(1 + (r - l) + 1)
	exit()
if l <= curr <= r:
	mini = min(curr - l, r - curr)
	print(mini + (r - l) + 2)
	exit()
if curr < l:
	print(l - curr + 1 + r - l + 1)
	exit()
if curr > r:
	print(curr - r + 1 + (r - l) + 1)
	exit()


(vsego, pozicia, nachinaia, konchaia) = map(int, input().split())
vlevo = nachinaia - 1
vpravo = vsego - konchaia
dolevo = abs(pozicia - nachinaia)
dopravo = abs(konchaia - pozicia)
counter = 0
if dolevo > dopravo:
	if konchaia != vsego:
		counter += dopravo
		counter += 1
		if nachinaia != 1:
			counter += konchaia - nachinaia
			counter += 1
	elif nachinaia != 1:
		counter += dolevo
		counter += 1
elif dolevo <= dopravo:
	if nachinaia != 1:
		counter += dolevo
		counter += 1
		if konchaia != vsego:
			counter += konchaia - nachinaia
			counter += 1
	elif konchaia != vsego:
		counter += dopravo
		counter += 1
print(counter)


(n, pos, l, r) = map(int, input().split())
res = 0
f = 0
if pos > r:
	res += abs(pos - r) + 1
	if l != 1:
		res += 1 + abs(l - r)
elif l <= pos <= r:
	if l == 1 and r == n:
		pass
	elif l == 1:
		res += 1 + abs(pos - r)
	elif r == n:
		res += 1 + abs(pos - l)
	elif abs(pos - l) < abs(pos - r):
		res += 2 + abs(pos - l) + abs(r - l)
	else:
		res += 2 + abs(pos - r) + abs(r - l)
else:
	res += abs(pos - l) + 1
	if r != n:
		res += 1 + abs(l - r)
print(res)


(n, pos, l, r) = list(map(int, input().split()))
answer = 0
if pos == l:
	if pos != 1:
		answer += 1
	if r != n:
		answer += 1 + r - l
elif pos == r:
	if pos != n:
		answer += 1
	if l != 1:
		answer += r - l + 1
elif pos > r:
	answer = pos - r + 1
	if l != 1:
		answer += 1 + r - l
elif pos < l:
	answer = l - pos + 1
	if r != n:
		answer += 1 + r - l
elif l == 1 and r != n:
	answer = r - pos + 1
elif l != 1 and r == n:
	answer = pos - l + 1
elif l != 1 and r != n:
	answer = min(r - pos + 1 + r - l + 1, pos - l + 1 + r - l + 1)
print(answer)


(n, pos, l, r) = map(int, input().split())
ans = 0
if l <= pos <= r:
	if l == 1:
		if r == n:
			print(0)
			exit(0)
		print(r - pos + 1)
		exit(0)
	if r == n:
		print(pos - l + 1)
		exit(0)
	if pos - l <= r - pos:
		print(2 + pos - l + r - l)
	else:
		print(2 + r - pos + r - l)
elif pos < l:
	if r == n:
		print(l - pos + 1)
		exit(0)
	print(r - pos + 2)
else:
	if l == 1:
		print(pos - r + 1)
		exit(0)
	print(pos - l + 2)


def mindis(pos, l, r):
	return min(abs(pos - l), abs(pos - r))
v = [int(x) for x in input().split(' ')]
n = v[0]
pos = v[1]
l = v[2]
r = v[3]
if l != 1 and r != n:
	print(mindis(pos, l, r) + 1 + abs(l - r) + 1)
elif l == 1 and r != n:
	print(abs(r - pos) + 1)
elif r == n and l != 1:
	print(abs(l - pos) + 1)
else:
	print(0)


(n, pos, l, r) = map(int, input().split(' '))
if l == 1 and r == n:
	count = 0
elif l == 1 and pos > r:
	count = pos - r + 1
elif l == 1:
	count = r - pos + 1
elif r == n and pos < l:
	count = l - pos + 1
elif r == n:
	count = pos - l + 1
elif pos < l:
	count = l - pos + 1 + r - l + 1
elif pos > r:
	count = pos - l + 2
else:
	count1 = pos - l + 1 + r - l + 1
	count2 = r - pos + 1 + r - l + 1
	count = min(count1, count2)
print(count)


T = 1
for i in range(1, T + 1):
	(n, pos, l, r) = map(int, input().split())
	if r - l + 1 == n:
		print(0)
		continue
	if l == 1:
		print(abs(r - pos) + 1)
		continue
	if r == n:
		print(abs(pos - l) + 1)
		continue
	min_res = abs(l - pos) + 1 + (r - l) + 1
	min_res = min(min_res, abs(r - pos) + 1 + (r - l) + 1)
	print(min_res)


(n, pos, l, r) = [int(i) for i in input().split()]
time_l = 0
if l != 1:
	time_l += abs(pos - l) + 1
	pos1 = l
else:
	pos1 = pos
if r != n:
	time_l += abs(r - pos1) + 1
time_r = 0
if r != n:
	time_r += abs(pos - r) + 1
	pos1 = r
else:
	pos1 = pos
if l != 1:
	time_r += abs(pos1 - l) + 1
print(min(time_l, time_r))


import math
from sys import stdin
string = stdin.readline().strip().split()
tabs = int(string[0])
pos = int(string[1])
l = int(string[2])
r = int(string[3])
output = 0
if l == 1 and r == tabs:
	output = 0
elif l == 1:
	output = abs(r - pos) + 1
elif r == tabs:
	output = abs(pos - l) + 1
else:
	output = min(abs(r - pos), abs(l - pos)) + (r - l) + 2
print(output)


def f(a, b, l, r, i):
	if a == l and b == r:
		return 0
	elif a == l and b > r:
		return 1 + abs(i - r)
	elif a < l and b == r:
		return 1 + abs(i - l)
	elif a < l and b > r:
		return 2 + abs(l - r) + min(abs(i - l), abs(i - r))
(n, p, l, r) = list(map(int, input().split()))
(a, b) = (1, n)
t = 0
print(f(a, b, l, r, p))


Read = lambda : list(map(int, input().split()))
(n, pos, l, r) = Read()
if l == 1 and r == n:
	print(0)
elif l <= pos <= r:
	if l == 1:
		print(r - pos + 1)
	elif r == n:
		print(pos - l + 1)
	else:
		print(min(r - pos, pos - l) + r - l + 2)
elif pos < l:
	print(l - pos + 1 + (r - l + 1) * (r < n))
else:
	print(pos - r + 1 + (r - l + 1) * (l > 1))


import sys
line = sys.stdin.readline()
vals = line.split()
n = int(vals[0])
pos = int(vals[1])
l = int(vals[2])
r = int(vals[3])
result = 0
middle = (l + r) / 2.0
if l == 1 and r == n:
	print(result)
else:
	if pos < l:
		result += l - pos
		pos = l
		result += 1
	elif pos > r:
		result += pos - r
		pos = r
		result += 1
	elif l != 1 and pos < middle:
		result += pos - l
		pos = l
		result += 1
	elif r != n:
		result += r - pos
		pos = r
		result += 1
	else:
		result += pos - l
		pos = l
		result += 1
	if l == r == 1 or l == r == n:
		pass
	elif pos == l and r != n or (pos == r and l != 1):
		result += r - l
		result += 1
	print(result)
line = sys.stdin.readline()
vals = line.split()
vsizes = []
for val in vals:
	vsizes.append(int(val))
vsizes.sort(reverse=True)
for vsize in vsizes:
	if vsize != 0 and glength % vsize == 0:
		print(glength // vsize)
		break


(n, cur, left, right) = map(int, input().strip().split())
visit_left = left > 1
visit_right = right < n
span = right - left
ans = 0
if visit_left:
	if visit_right:
		ans = span + min(abs(cur - left), abs(cur - right)) + 2
	else:
		ans = abs(cur - left) + 1
elif visit_right:
	ans = abs(cur - right) + 1
else:
	ans = 0
print(ans)


import sys
a = list(map(int, input().split()))
(n, pos, l, r) = (a[0], a[1], a[2], a[3])
res = 0
if l != 1 and r != n and (l <= pos) and (pos <= r):
	res = min(pos - l + 1 + r - l + 1, r - pos + 1 + r - l + 1)
elif l != 1 and r != n and (pos < l or pos > r):
	res = min(abs(pos - l) + 1 + abs(r - l) + 1, abs(r - pos) + 1 + abs(r - l) + 1)
elif l == 1 and r != n:
	res = abs(r - pos) + 1
elif r == n and l != 1:
	res = abs(l - pos) + 1
print(res)


inp = lambda : map(int, input().split())
(n, pos, l, r) = inp()
a = abs(pos - l) + 1 + r - l + 1
b = abs(pos - r) + 1 + r - l + 1
c = 1000000
d = 1000000
if l == 1:
	c = abs(pos - r) + 1
if r == n:
	d = abs(pos - l) + 1
if l == 1 and r == n:
	c = 0
print(min(a, b, c, d))


(n, pos, l, r) = map(int, input().split())
left = l - 1
right = n - r
if right == 0 and left == 0:
	print(0)
elif left == 0:
	if pos <= r:
		print(r - pos + 1)
	else:
		print(pos - r + 1)
elif right == 0:
	if pos >= l:
		print(pos - l + 1)
	else:
		print(l - pos + 1)
else:
	to_left = abs(l - pos)
	to_right = abs(r - pos)
	between = abs(r - l)
	print(min(to_left + 1 + between + 1, to_right + 1 + between + 1))


(n, k, l, r) = (int(x) for x in input().strip().split())
ans = 0
if k < l:
	ans += l - k
	ans += 1
	if r < n:
		ans += r - l
		ans += 1
elif k > r:
	ans += k - r + 1
	if l > 1:
		ans += r - l
		ans += 1
elif l > 1:
	if r < n:
		ans += min(k - l, r - k) + 2 + (r - l)
	else:
		ans += k - l + 1
elif r < n:
	ans += r - k + 1
print(ans)


s = input().split()
n = int(s[0])
pos = int(s[1])
a = int(s[2])
b = int(s[3])
res = 0
if pos == a:
	if a != 1:
		res += 1
	if b != n:
		res += 1
		res += b - a
	else:
		res += 0
elif pos == b:
	if b != n:
		res += 1
	if a != 1:
		res += 1
		res += b - a
	else:
		res += 0
elif pos < a:
	res += a - pos
	res += 1
	if b != n:
		res += 1
		res += b - a
elif pos > b:
	res += pos - b
	res += 1
	if a != 1:
		res += 1
		res += b - a
elif a == 1 and b == n:
	res = 0
elif a == 1 and b != n:
	res += b - pos
	res += 1
elif b == n and a != 1:
	res += pos - a
	res += 1
else:
	res += min(pos - a, b - pos) * 2
	res += max(pos - a, b - pos)
	res += 2
print(res)


def main():
	(n, p, l, r) = map(int, input().split())
	if l - 1 == 0 == n - r:
		print(0)
		return
	(res, l, p) = (0, l - 1, p - 1)
	if p - l < r - p - 1:
		(p, l, r) = (n - p - 1, n - r, n - l)
	if r < n:
		res += max(r - p, p - r + 2)
		(n, p) = (r, r - 1)
	(p, l, r) = (n - p - 1, n - r, n - l)
	if r < n:
		res += max(r - p, p - r + 2)
	print(res)
main()


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
(n, pos, l, r) = map(int, input().split())
ans = float('inf')
q = [(1, n, pos, 0)]
vis = {(1, n, pos)}
while q:
	(a, b, p, c) = q.pop(0)
	if a == l and b == r:
		ans = min(ans, c)
	else:
		if p > l:
			if (a, b, p - 1) not in vis:
				q.append((a, b, p - 1, c + 1))
				vis.add((a, b, p - 1))
		if p < r:
			if (a, b, p + 1) not in vis:
				q.append((a, b, p + 1, c + 1))
				vis.add((a, b, p + 1))
		if p >= r and b > r:
			if (a, p, p) not in vis:
				q.append((a, p, p, c + 1))
				vis.add((a, p, p))
		if p >= l and a < l:
			if (p, b, p) not in vis:
				q.append((p, b, p, c + 1))
				vis.add((p, b, p))
print(ans)


(n, pos, l, r) = list(map(int, input().split()))
count = 0
if l <= pos <= r:
	if r < n and l > 1:
		x = min(pos - l, r - pos)
		count += x
		count += r - l
		count += 2
	elif r < n and l == 1:
		count += r - pos
		count += 1
	elif r == n and l > 1:
		count += pos - l
		count += 1
elif pos > r:
	count += pos - r
	count += 1
	if l > 1:
		count += r - l
		count += 1
elif pos < l:
	count += l - pos
	count += 1
	if r < n:
		count += r - l
		count += 1
print(count)


l = list(map(int, input().split()))
if l[2] == 1 and l[3] == l[0]:
	print(0)
elif l[2] != 1 and l[3] == l[0]:
	print(1 + abs(l[2] - l[1]))
elif l[2] == 1 and l[3] != l[0]:
	print(1 + abs(l[3] - l[1]))
else:
	print(min(abs(l[1] - l[2]), abs(l[1] - l[3])) + 2 + l[3] - l[2])


def solve():
	(n, pos, l, r) = list(map(int, input().split(' ')))
	if l == 1 and r == n:
		print(0)
		return
	elif l == 1:
		print(abs(pos - r) + 1)
	elif r == n:
		print(abs(pos - l) + 1)
	else:
		print(min(abs(pos - l) + (r - l) + 2, abs(pos - r) + (r - l) + 2))
solve()


(n, pos, l, r) = map(int, input().split())
ans = 0
c1 = 0
c2 = 0
if l > 1:
	c1 = 1
if r < n:
	c2 = 1
if c1 and c2:
	if pos <= r and pos >= l:
		ans = min(pos - l + r - l, r - pos + r - l) + c1 + c2
	elif pos < l:
		ans = l - pos + c1 + r - l + c2
	else:
		ans = pos - r + c1 + r - l + c2
elif c1 == 0 and c2 == 1:
	ans = abs(r - pos) + c1 + c2
elif c2 == 0 and c1 == 1:
	ans = abs(pos - l) + c1 + c2
else:
	ans = 0
print(ans)


(n, pos, l, r) = map(int, input().split())
result = abs(pos - l) + r - l + 2
if l == 1:
	if abs(pos - r) + 1 < result:
		result = abs(pos - r) + 1
if r == n:
	if abs(pos - l) + 1 < result:
		result = abs(pos - l) + 1
if l == 1 and r == n:
	result = 0
if abs(pos - r) + r - l + 2 < result:
	result = abs(pos - r) + r - l + 2
print(result)


from sys import stdin, stdout
Input = []
Input = [stdin.readlines()]
Input = Input[0]
Input = [x.split(' ') for x in Input]
n = int(Input[0][0])
pos = int(Input[0][1])
l = int(Input[0][2])
r = int(Input[0][3])
if n > r and l > 1:
	if pos >= l and pos <= r:
		stdout.write(str(min(pos - l, r - pos) + r - l + 2))
	elif pos > r:
		stdout.write(str(pos - l + 2))
	else:
		stdout.write(str(r - pos + 2))
elif n == r and l > 1:
	stdout.write(str(abs(l - pos) + 1))
elif n > r and l == 1:
	stdout.write(str(abs(r - pos) + 1))
else:
	stdout.write(str(0))


import sys
(n, pos, l, r) = map(int, input().split())
time = 0
if pos < l:
	if n > r:
		time = r - pos + 2
	else:
		time = l - pos + 1
elif pos > r:
	if l > 1:
		time = pos - l + 2
	else:
		time = pos - r + 1
elif l > 1 and r < n:
	time = min(pos - l, r - pos) + r - l + 2
elif r < n:
	time = r - pos + 1
elif l > 1:
	time = pos - l + 1
else:
	time = 0
print(time)


(n, pos, l, r) = input().split()
n = int(n)
pos = int(pos)
l = int(l)
r = int(r)
if r - l + 1 == n:
	print(0)
elif l <= pos <= r:
	if l == 1:
		print(r - pos + 1)
	elif r == n:
		print(pos - l + 1)
	else:
		ans1 = pos - l + 1
		a = pos - ans1 + 1
		ans1 += r - a + 1
		ans2 = r - pos + 1
		a = pos + ans2 - 1
		ans2 += a - l + 1
		print(min(ans1, ans2))
elif pos < l:
	if r == n:
		print(l - pos + 1)
	else:
		ans1 = l - pos + 1
		ans1 += r - l + 1
		print(ans1)
elif l == 1:
	print(pos - r + 1)
else:
	ans1 = pos - r + 1
	ans1 += r - l + 1
	print(ans1)


(n, pos, l, r) = [int(x) for x in input().split()]
t = 0
lm = 0
rm = 0
if l > 1:
	t += 1
	lm = abs(pos - l)
if r < n:
	t += 1
	rm = abs(r - pos)
	if lm > 0:
		xm = r - l
		t += xm + min(rm, lm)
	else:
		t += rm
elif lm > 0:
	t += lm
print(t)


x = list(map(int, input().split()))
pos = x[1]
n = x[0]
l = x[2]
r = x[3]
step = 0
if pos < l:
	step = l - pos + 1
	if r < n:
		step += r - l + 1
elif pos > r:
	step = pos - r + 1
	if l > 1:
		step += r - l + 1
elif l > 1 and n > r:
	step += min(pos - l, r - pos) + r - l + 2
elif l == 1 and n > r:
	step = r - pos + 1
elif l > 1 and n == r:
	step += pos - l + 1
else:
	step = 0
print(step)


import sys
(n, pos, l, r) = map(int, sys.stdin.readline().split())
if l == 1 and r == n:
	print(0)
	exit()
if pos > l and pos < r:
	if l != 1 and r != n:
		if pos - l <= r - pos:
			c = pos - l + (r - l) + 2
			print(c)
			exit()
		else:
			print(r - pos + r - l + 2)
			exit()
	if l == 1 and r != n:
		print(r - pos + 1)
		exit()
	if l != 1 and r == n:
		print(pos - l + 1)
		exit()
if pos < l:
	if r == n:
		print(l - pos + 1)
		exit()
	print(l - pos + (r - l) + 2)
	exit()
if pos > r:
	if l == 1:
		print(pos - r + 1)
		exit()
	print(pos - r + 1 + (r - l) + 1)
	exit()
if pos == l:
	if r != n:
		if pos == 1:
			print(r - l + 1)
			exit()
		print(r - l + 2)
		exit()
	if r == n:
		if pos == 1:
			print(0)
			exit()
		print(1)
		exit()
if pos == r:
	if l != 1:
		if pos == n:
			print(r - l + 1)
			exit()
		print(r - l + 2)
		exit()
	if l == 1:
		if pos == n:
			print(0)
			exit()
		print(1)
		exit()


(n, pos, l, r) = list(map(int, input().split()))
s = 0
if abs(pos - l) < abs(pos - r):
	if l != 1:
		if abs(pos - l) > 0:
			s += abs(pos - l)
			s += 1
		else:
			s += 1
		pos = l
	if r != n:
		if abs(pos - r) > 0:
			s += abs(pos - r)
			s += 1
		else:
			s += 1
else:
	if r != n:
		if abs(pos - r) > 0:
			s += abs(pos - r)
			s += 1
		else:
			s += 1
		pos = r
	if l != 1:
		if abs(pos - l) > 0:
			s += abs(pos - l)
			s += 1
		else:
			s += 1
print(s)


def main():
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		print(0)
		return
	moves = 0
	if pos < l:
		moves += l - pos
		if l > 1:
			moves += 1
		if n - r > 0:
			moves += r - l + 1
	elif pos > r:
		moves += pos - r
		if r < n:
			moves += 1
		if l > 1:
			moves += r - l + 1
	elif pos == l == 1:
		moves += r - l + 1
	elif pos == r == n:
		moves += r - l + 1
	else:
		right_moves = r - pos
		left_moves = pos - l
		is_left = l > 1
		is_right = r < n
		if not is_left:
			moves = right_moves + 1
		elif not is_right:
			moves = left_moves + 1
		else:
			moves = min(left_moves, right_moves) * 2 + max(left_moves, right_moves) + 2
	print(moves)
main()


(n, pos, l, r) = map(int, input().split())
if l == 1 and r == n:
	print(0)
elif l == 1 and r == pos or (l == pos and r == n):
	print(1)
elif pos == l and pos == r:
	print(2)
elif l == 1 and r < n and (pos >= l) and (r >= pos):
	print(r - pos + 1)
elif l > 1 and r == n and (pos >= l) and (r >= pos):
	print(pos - l + 1)
elif pos >= l and r >= pos:
	count = 0
	count = count + r - pos + 1
	count = count + r - l + 1
	a = count
	count = 0
	count = count + pos - l + 1
	count = count + r - l + 1
	print(min(a, count))
elif pos > r:
	count = 0
	count = count + pos - r + 1
	if l > 1:
		count = count + r - l + 1
	print(count)
elif pos < l:
	count = 0
	count = count + l - pos + 1
	if r < n:
		count = count + r - l + 1
	print(count)


from sys import maxsize as maxsize
(n, pos, l, r) = [int(i) for i in input().split()]
ans = 0
pos1 = pos
if pos < l or pos > r:
	if pos < l:
		ans += l - pos + 1
		if r < n:
			ans += r - l + 1
	if pos > r:
		ans += pos - r + 1
		if l > 1:
			ans += r - l + 1
	print(ans)
else:
	if r < n:
		ans += r - pos + 1
	else:
		r = pos
	if l > 1:
		ans += r - l + 1
	res = 0
	if l > 1:
		res += pos1 - l + 1
	else:
		l = pos1
	if r < n:
		res += r - l + 1
	print(min(ans, res))


from math import *
(n, pos, l, r) = map(int, input().split(' '))
(a, b) = (1, 1)
if l == 1:
	a = 0
if r == n:
	b = 0
if (a, b) == (0, 0):
	print(0)
elif b == 0:
	print(abs(pos - l) + 1)
elif a == 0:
	print(abs(pos - r) + 1)
elif abs(pos - l) < abs(pos - r):
	print(abs(pos - l) + 1 + (r - l + 1))
else:
	print(abs(pos - r) + 1 + (r - l + 1))


(n, pos, l, r) = map(int, input().strip().split())
res_a = 0
res_b = 0
right = int(n > r)
left = int(l > 1)
if pos < r and pos > l:
	res_a += (r - pos) * right
	pos_r = r
	res_b += (pos - l) * left
	pos_l = l
if pos >= r:
	pos_l = l
	res_b += pos - l
	pos_r = pos
if pos <= l:
	pos_r = r
	res_a += r - pos
	pos_l = pos
res_a += (pos_r - r) * int(left or right) + int(right) + (r - l + 1) * int(left)
res_b += (l - pos_l) * int(left or right) + int(left) + (r - l + 1) * int(right)
print(min(res_a, res_b))


(n, pos, l, r) = map(int, input().split())
s = 0
if abs(pos - l) <= abs(pos - r):
	if l >= 2:
		s += 1 + abs(pos - l)
		if r < n:
			s += r - l + 1
	elif r < n:
		s += abs(pos - r) + 1
elif r < n:
	s += abs(pos - r) + 1
	if l >= 2:
		s += abs(r - l) + 1
elif l >= 2:
	s += abs(pos - l) + 1
print(s)


def solve():
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		print(0)
	elif l == 1:
		print(abs(pos - r) + 1)
	elif r == n:
		print(abs(pos - l) + 1)
	else:
		print(r - l + min(abs(pos - l), abs(pos - r)) + 2)

def main():
	t = 1
	for _ in range(t):
		solve()
main()


def main():
	(n, pos, l, r) = map(int, input().split())
	if l == 1 and r == n:
		return 0
	if l != 1 and r != n:
		q = r - l + 2
	else:
		if l == 1:
			return abs(pos - r) + 1
		return abs(pos - l) + 1
	w = min(abs(pos - l), abs(pos - r))
	return q + w
print(main())


(n, pos, l, r) = map(int, input().split())
count = 0
if l == 1 and r != n:
	count += abs(pos - r) + 1
elif l != 1 and r == n:
	count += abs(pos - l) + 1
elif l == 1 and r == n:
	count = 0
elif abs(pos - l) < abs(pos - r):
	count += abs(pos - l) + 1 + abs(l - r) + 1
else:
	count += abs(pos - r) + 1 + abs(l - r) + 1
print(count)


(n, p, l, r) = map(int, input().split())
(s1, s2) = (0, 0)
(l1, l2, r1, r2) = (0, 0, 0, 0)
p1 = p
if l > 1:
	l1 += abs(p - l)
	l1 += 1
	p1 = l
if r < n:
	r1 += abs(r - p1)
	r1 += 1
s1 = l1 + r1
p2 = p
if r < n:
	r2 += abs(r - p2)
	r2 += 1
	p2 = r
if l > 1:
	l2 += abs(p2 - l)
	l2 += 1
s2 = l2 + r2
print(min(s1, s2))


(n, pos, left, right) = map(int, input().split())
if left == 1 and right == n:
	print(0)
elif left == 1:
	print(abs(right - pos) + 1)
elif right == n:
	print(abs(left - pos) + 1)
else:
	print(min(abs(pos - right), abs(pos - left)) + (right - left) + 2)


(n, p, l, r) = map(int, input().split())
x = (l + r) // 2
count = 0
if l <= p <= r:
	if l != 1 and r != n:
		if p <= x:
			count += p - l
		else:
			count += r - p
		count += r - l + 2
	elif l == 1 and r != n:
		count += r - p + 1
	elif l != 1 and r == n:
		count += p - l + 1
	print(count)
else:
	if p > r:
		x = p - r + 1
		if l != 1:
			y = r - l + 1
			count += x + y
		else:
			count += x
	elif p < l:
		x = l - p + 1
		if r != n:
			y = r - l + 1
			count += x + y
		else:
			count += x
	print(count)