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: interview

Starter code:

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

if l > 1 and r < n:
    if l <= pos and pos <= r:
        if pos - l < r - pos:
            print(pos - l + 1 + r - l + 1)
        else:
            print(r - pos + 1 + r - l + 1)
    elif pos > r:
        print(pos - r + 1 + r - l + 1)
    else:
        print(l - pos + 1 + r - l + 1)
elif l == 1 and r < n:
    print(int(abs(pos - r)) + 1)
elif l > 1 and r == n:
    print(int(abs(pos - l)) + 1)
else:
    print(0)

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

def main():
    n, pos, l, r = list(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, 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, l, r = map(int, input().split())
if (l <= pos <= r):
	if (l == 1 and r == n):
		print(0)
	elif (l == 1 and r < n):
		print(r - pos + 1)
	elif (r == n and l > 1):
		print(pos - l + 1)
	else:
		print(r - l + min(r - pos, pos - l) + 2)
elif (pos < l):
	if (r == n):
		print(l - pos + 1)
	else:
		print(r - pos + 2)
elif (pos > r):
	if (l == 1):
		print(pos - r + 1)
	else:
		print(pos - l + 2)

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

if l == 1 and r == n:
    print(0)

else:
    if l == 1 and r != n:
        print(abs(pos - r) + 1)

    elif l != 1 and r == n:
        print(abs(pos - l) + 1)

    else:
        if l <= pos <= r:
            print(r - l + 2 + min(abs(pos - l), abs(pos - r)))

        elif pos < l:
            print(r - l + 2 + abs(pos - l))

        else:
            print(r - l + 2 + abs(pos - r))


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

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

n, pos, l, r = list(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)
else:
    print(min(abs(l - pos) + 1 + r - l + 1, abs(r - pos) + 1 + r - l + 1))


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

def solve(n,pos,l,r):
    if l == 1 and r == n:
        return 0
    elif l == 1:
        return abs(pos-r)+1
    elif r == n:
        return abs(pos-l)+1
    else:
        if l <= pos and pos <= r:
            return abs(r-l) + min(abs(pos-l),abs(pos-r))+2
        elif pos < l:
            return abs(pos-l) + abs(r-l) + 2
        else:
            return abs(pos-r) + abs(r-l) + 2

print(solve(n,pos,l,r))

n, pos, l, r = map(int, 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)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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, pos, l, r = list(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]))


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)
else:
    if needright:
        print(abs(pos - r) + 1)
    else:
        print(0)


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)


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()

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))


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)

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)
		


# B

import math

n, pos, l, r = list(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(l - pos) + 1))
else:
    if pos <= l:
        print(r - pos + 2)
    elif r <= pos:
        print(pos - l + 2)
    else:
        print(min(pos + r - 2*l, 2*r - l - pos) + 2)


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)
else:
  print(min(abs(l-pos),abs(r-pos)) + r-l + 2)


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)
else:
    if r==n:
        print(la+1)
    else:
        if la<ra:
            print(r-l+2+la)
        else:
            print(r-l+2+ra)

n,pos,l,r  = [int(i) for i in input().split()]

time_l = 0;
if l != 1:
    time_l += abs(pos - l) + 1 # move to l and delete
    pos1 = l
else:
    pos1 = pos
if r != n: time_l += abs(r-pos1) + 1 # move to r and delete

time_r = 0;
if r != n:
    time_r += abs(pos - r) + 1 # move to l and delete
    pos1 = r
else:
    pos1 = pos
if l != 1: time_r += abs(pos1-l) + 1 # move to r and delete

#print(time_l, time_r)
print(min(time_l, time_r))