A teacher asks the class to open their books to a page number. A student can either start turning pages from the front of the book or from the back of the book. They always turn pages one at a time.  When they open the book, page $1$ is always on the right side:

When they flip page $1$, they see pages $2$ and $3$.  Each page except the last page will always be printed on both sides.  The last page may only be printed on the front, given the length of the book.  If the book is $n$ pages long, and a student wants to turn to page $\boldsymbol{p}$, what is the minimum number of pages to turn?  They can start at the beginning or the end of the book. 

Given $n$ and $\boldsymbol{p}$, find and print the minimum number of pages that must be turned in order to arrive at page $\boldsymbol{p}$. 

Example  

$n=5$ 

$p=3$  

Using the diagram above, if the student wants to get to page $3$, they open the book to page $1$, flip $1$ page and they are on the correct page.  If they open the book to the last page, page $5$, they turn $1$ page and are at the correct page.  Return $1$. 

Function Description  

Complete the pageCount function in the editor below.  

pageCount has the following parameter(s):  

int n: the number of pages in the book   
int p: the page number to turn to  

Returns  

int: the minimum number of pages to turn

Input Format

The first line contains an integer $n$, the number of pages in the book. 

The second line contains an integer, $\boldsymbol{p}$, the page to turn to.

Constraints

$1\leq n\leq10^5$
$1\leq p\leq n$

Sample Input 0
6
2

Sample Output 0
1

Explanation 0

If the student starts turning from page $1$, they only need to turn $1$ page:

If a student starts turning from page $\boldsymbol{6}$, they need to turn $2$ pages:

Return the minimum value, $1$.

Sample Input 1
5
4

Sample Output 1
0

Explanation 1

If the student starts turning from page $1$, they need to turn $2$ pages:

If they start turning from page $5$, they do not need to turn any pages:

Return the minimum value, $0$.

Source: hackerrank | Difficulty: EASY
Tags: Implementation


Solutions:
import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min((n - p) // 2, p // 2))


import sys
n = int(input().strip())
p = int(input().strip())
answer = min(p // 2, (n - p) // 2)
print(answer)


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys
n = int(input().strip())
p = int(input().strip())
page_in_book = p // 2
total_pages = n // 2
from_front = page_in_book
from_back = total_pages - page_in_book
print(min(from_front, from_back))


import sys
n = int(input().strip())
p = int(input().strip())
l = p // 2
r = ((n | 1) - p) // 2
print(min(l, r))


import sys
n = int(input().strip())
p = int(input().strip())
front = p // 2
n = n // 2 * 2
back = max(0, (n - p + 1) // 2)
print(min(front, back))


import sys
n = int(input().strip())
p = int(input().strip())
front = p // 2
if n % 2 == 0:
	back = (n - p + 1) // 2
else:
	back = (n - p) // 2
print(min(front, back))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
q = n - p
np = p // 2
nq = None
if n % 2 == 0:
	nq = (q + 1) // 2
else:
	nq = q // 2
print(min([np, nq]))


import sys
n = int(input().strip())
p = int(input().strip())
left = p // 2
right = 0
if n % 2 == 0:
	import math
	right = math.ceil((n - p + 0.0) / 2)
else:
	right = (n - p) // 2
print(min(left, right))


import sys
n = int(input().strip())
p = int(input().strip())
count = int((p - 1) / 2)
if p % 2 == 0:
	count += 1
countEnd = int((n - p) / 2)
if n % 2 == 0 and p % 2 != 0:
	countEnd += 1
print(min(count, countEnd))


import sys
n = int(input().strip())
p = int(input().strip())
front = p // 2
back = n // 2 - front
if back >= front:
	print(front)
else:
	print(back)


import sys
n = int(input().strip())
p = int(input().strip())
p1 = p
if p % 2 > 0:
	p1 = p - 1
n1 = n
if n % 2 > 0:
	n1 = n - 1
print(min(p1 // 2, (n1 - p1) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 != 0:
	n = n - 1
if p % 2 != 0:
	p = p - 1
print(int(min(p / 2, (n - p) / 2)))


import sys
n = int(input().strip())
p = int(input().strip())
if p == n and n % 2 == 0 or p == 1 or (p == n - 1 and n % 2 == 1):
	print('0')
else:
	print(min((n - p) // 2, p // 2))


import sys
n = int(input().strip())
p = int(input().strip())

def turn_from_front(p):
	counter = 0
	if p % 2 == 0:
		p += 1
	for i in range(1, p, 2):
		counter += 1
	return counter

def turn_from_back(n, p):
	counter = 0
	if n % 2 == 0:
		if p % 2 == 0:
			for i in range(n, p, -2):
				counter += 1
		else:
			for i in range(n + 1, p, -2):
				counter += 1
	elif p % 2 == 0:
		p += 1
		for i in range(n, p, -2):
			counter += 1
	else:
		for i in range(n, p, -2):
			counter += 1
	return counter
arr = []
arr.append(turn_from_front(p))
arr.append(turn_from_back(n, p))
arr.sort()
print(arr[0])


import sys
n = int(input().strip())
p = int(input().strip())
if n >= 1 and n <= 10 ** 5 and (p <= n) and (p >= 1):
	if p % 2 != 0:
		t1 = (p - 1) / 2
		if n % 2 == 0:
			t2 = (n - p + 1) / 2
		else:
			t2 = (n - p) / 2
	else:
		t1 = p / 2
		if n % 2 == 0:
			t2 = (n - p) / 2
		else:
			t2 = (n - p - 1) / 2
	if t1 > t2:
		print(int(t2))
	elif t2 >= t1:
		print(int(t1))


import sys

def solve(n, p):
	from_start = p // 2
	from_end = (n - p) // 2
	return min(from_start, from_end)

def main(inp=sys.stdin):
	n = int(inp.readline().strip())
	p = int(inp.readline().strip())
	print(solve(n, p))
main()


import sys
import math
n = float(input().strip())
p = float(input().strip())
distFromStart = 0
distFromEnd = 0
distFromStart = int(math.ceil((p - 1.0) / 2))
if n % 2 == 0:
	distFromEnd = int(math.ceil((n - p) / 2))
else:
	distFromEnd = int(math.floor((n - p) / 2))
print(min(distFromStart, distFromEnd))


import sys
n = int(input().strip())
p = int(input().strip())
if p <= int(n / 2):
	print(int(p / 2))
else:
	print(int(n / 2) - int(p / 2))


import sys
n = int(input().strip())
p = int(input().strip())
if p == 1 or n == p:
	print('0')
else:
	if p > int(n / 2):
		c = int((n - p) / 2)
	else:
		c = int(p / 2)
	print(c)


import sys
import unittest
import math

class DrawingBook:

	def __init__(self, n, p):
		self.n = self._get_next_odd(n)
		self.p = self._get_next_odd(p)

	def _get_next_odd(self, x):
		if x % 2:
			return x
		else:
			return x + 1

	def calculate(self):
		from_left = self.p / 2
		from_right = (self.n - self.p) / 2
		if from_left < from_right:
			return math.floor(from_left)
		else:
			return math.floor(from_right)

class TestDrawingBook(unittest.TestCase):

	def test1(self):
		db = DrawingBook(6, 2)
		self.assertEquals(db.calculate(), 1)

	def test2(self):
		db = DrawingBook(5, 4)
		self.assertEquals(db.calculate(), 0)
n = int(input().strip())
p = int(input().strip())
db = DrawingBook(n, p)
print(db.calculate())


import sys
n = int(input().strip())
p = int(input().strip())
if p <= int(n / 2):
	print(int(p / 2))
else:
	print(int((n - p) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys
import math
n = int(input().strip())
p = int(input().strip())
print(math.floor((n - p) / 2) if math.floor(p / 2) > math.floor((n - p) / 2) else math.floor(p / 2))


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	n += 1
if p % 2 == 0:
	p += 1
d1 = (n - p) / 2
d2 = (p - 1) / 2
if d1 < d2:
	print(int(d1))
else:
	print(int(d2))


import sys
n = int(input().strip())
p = int(input().strip())
k = int(p / 2)
l = int((n - p) / 2)
if k < l:
	print(k)
else:
	print(l)


N = int(input().rstrip())
P = int(input().rstrip())
from_front = P // 2
from_back = (N - P) // 2
print(min(from_back, from_front))


import sys
n = int(input().strip())
p = int(input().strip())
a = p // 2
b = (n - p) // 2
if b < a:
	print(b)
else:
	print(a)


import math
n = int(input().strip())
p = int(input().strip())
if p <= n / 2:
	print(p // 2)
elif n % 2 == 0:
	print(math.ceil((n - p) / 2))
else:
	print(math.floor((n - p) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
if p <= n / 2:
	print(p // 2)
else:
	if n % 2 == 0:
		n += 1
	print((n - p) // 2)


import sys
n = int(input().strip())
p = int(input().strip())
if p / n > 0.5:
	turns = int(n / 2) - int(p / 2)
else:
	turns = int(p / 2)
print(turns)


import sys
n = int(input().strip())
p = int(input().strip())

def do(n, p):
	if p == 1 or p == n:
		return 0
	if n % 2 == 1:
		n -= 1
	from1 = p // 2
	fromN = (n + 1 - p) // 2
	return min(from1, fromN)
print(do(n, p))


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	n = n + 1
mid = (n + 1) / 2
if mid % 2 == 0:
	if p < mid:
		s = p / 2
	else:
		s = n / 2 - p / 2
else:
	mid = mid - 1
	if p <= mid:
		s = p / 2
	else:
		s = n / 2 - p / 2
print(int(s))


import sys
import math
n = int(input().strip())
p = int(input().strip())
middle = n / 2
if p <= middle:
	print(math.floor(p / 2))
else:
	print(math.floor((n - p) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
from_start = p // 2
from_end = (n // 2 * 2 + 1 - p) // 2
print(min(from_start, from_end))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys
n = int(input().strip())
p = int(input().strip())
total_number = 0
page_number = 0
if n % 2 == 1:
	total_number = n - 1
else:
	total_number = n
if p % 2 == 1:
	page_number = p - 1
else:
	page_number = p
from_front = page_number / 2
from_back = (total_number - page_number) / 2
print(int(min(from_front, from_back)))


import sys
n = int(input().strip())
p = int(input().strip())
forward = p // 2
n = n + 1 if n % 2 == 0 else n
backward = (n - p) // 2
print(forward if forward < backward else backward)


import sys

def solve(n, p):
	xl = p // 2
	xr = (n + (1 - n % 2) - p) // 2
	return min(xl, xr)
n = int(input().strip())
p = int(input().strip())
print(solve(n, p))


import math
n = int(input())
p = int(input())
if n & 1:
	n -= 1
dif1 = p - 1
dif2 = n - p
ans1 = math.ceil(dif1 / 2)
ans2 = math.ceil(dif2 / 2)
if ans1 >= ans2:
	print(ans2)
else:
	print(ans1)


import sys
n = int(input().strip())
p = int(input().strip())
left = 0
right = 1
npages = 0
nstart = 0
while left != n and right != n:
	if left == p or right == p:
		nstart = npages
	left = right + 1
	right = left + 1
	npages = npages + 1
nend = 0
while left != p and right != p:
	right = left - 1
	left = right - 1
	nend = nend + 1
print(min(nstart, nend))


import sys
n = int(input().strip())
p = int(input().strip())
val = None
if p == n:
	print(0)
else:
	is_even = True if n % 2 == 0 else False
	arr = []
	arr.append(1)
	for i in range(2, n, 2):
		arr.append((i, i + 1))
	if is_even:
		arr.append(n)
	c1 = 0
	c2 = 0
	for i in range(len(arr)):
		if type(arr[i]) is tuple:
			if p not in arr[i]:
				c1 += 1
			else:
				val = c1
				print(val)
				break
		elif arr[i] == p:
			val = c1
			print(val)
			break
		else:
			c1 += 1
		if type(arr[len(arr) - i - 1]) is tuple:
			if p not in arr[len(arr) - i - 1]:
				c2 += 1
			else:
				val = c2
				print(val)
				break
		elif arr[len(arr) - i - 1] == p:
			val = c2
			print(val)
			break
		else:
			c2 += 1


import sys
n = int(input().strip())
p = int(input().strip())
k = min(int((n - p) / 2), int(p / 2))
print(k)


import sys
n = int(input().strip())
p = int(input().strip())
from_first = p // 2
from_last = (n - p) // 2
print(min(from_first, from_last))


import sys
n = int(input().strip())
p = int(input().strip())
if p <= n / 2:
	ref = 1
else:
	ref = -1
i = 0
if ref == -1:
	if p == n:
		print(0)
		exit()
	if n % 2 == 0:
		if p % 2 == 0:
			print(int((n - p) / 2))
			exit()
		if p % 2 != 0:
			print(int((n - (p - 1)) / 2))
			exit()
	else:
		print(int((n - p) / 2))
		exit()
if ref == 1:
	if p == 1:
		print(0)
		exit()
	elif p % 2 == 0:
		print(int(p / 2))
		exit()
	else:
		print(int((p - 1) / 2))
		exit()


import sys
import math
n = int(input().strip())
p = int(input().strip())
output = min(math.floor(p / 2), math.floor((n - p) / 2))
print(output)


import sys
import math
n = int(input().strip())
p = int(input().strip())
if p == 1:
	print(0)
elif p <= n // 2:
	print(int(math.ceil((p - 1) / 2)))
elif n % 2 == 1:
	print((n - p) // 2)
elif n == p:
	print(0)
else:
	print((n - 1 - p) // 2 + 1)


import sys
n = int(input().strip())
p = int(input().strip())
pages = []
x = min(p // 2, abs(p // 2 - n // 2))
print(x)


import sys
n = int(input().strip())
p = int(input().strip())
n = list((int(x) for x in range(n)))
if p == 0:
	print('0')
elif p == n[-1]:
	print('0')
else:
	mid = len(n) / 2
	if p % 2 == 0:
		if p == len(n) - 1:
			print('0')
		elif p <= mid:
			print(int(p / 2))
		elif p > mid:
			print(int((len(n) - p) / 2))
	elif p % 2 == 1:
		if p <= mid:
			print(int((p - 1) / 2))
		elif p > mid:
			print(int((len(n) - (p - 1)) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
n = list((int(x) for x in range(n)))
if p == 0:
	print('0')
elif p == n[-1]:
	print('0')
else:
	mid = len(n) / 2
	if p % 2 == 0:
		if p == len(n) - 1:
			print('0')
		elif p <= mid:
			print(int(p / 2))
		elif p > mid:
			print(int((len(n) - p) / 2))
	elif p % 2 == 1:
		if p <= mid:
			print(int((p - 1) / 2))
		elif p > mid:
			print(int((len(n) - (p - 1)) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
r = 0
if p <= n / 2:
	if p % 2 == 0:
		r = p / 2
	else:
		r = (p - 1) / 2
elif n % 2 == 0:
	if (n - p + 1) % 2 == 0:
		r = (n - p + 1) / 2
	else:
		r = (n - p) / 2
elif (n - p) % 2 == 0:
	r = (n - p) / 2
else:
	r = (n - p - 1) / 2
r = int(r)
print(r)


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p + abs(n % 2 - 1)) // 2))


n = int(input())
p = int(input())
m = 1 + n // 2
k = p // 2
print(min(k, m - k - 1))


import sys
n = int(input().strip())
p = int(input().strip())
normalize = lambda x: x if x % 2 == 0 else x - 1
normalized_n = normalize(n)
normalized_p = normalize(p)
print(min((normalized_p // 2, (normalized_n - normalized_p) // 2)))


import sys
from math import *
n = int(input().strip())
p = int(input().strip())
if n == p:
	print(0)
else:
	l = 2
	c_l = 1
	c_l += (p - l) // 2
	r = n
	c_r = 0
	if n % 2 == 0:
		r = n - 1
		c_r += (r - p) // 2
		c_r += 1
	else:
		c_r += (r - p) // 2
	print(min(c_l, c_r))


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	print(min(int(p / 2), int((n - p + 1) / 2)))
else:
	print(min(int(p / 2), int((n - p) / 2)))


import sys

def fromFront(tot, dest):
	return dest // 2

def fromBack(tot, dest):
	if tot % 2 == 0:
		return (tot - dest + 1) // 2
	else:
		return (tot - dest) // 2
n = int(input().strip())
p = int(input().strip())
front = fromFront(n, p)
back = fromBack(n, p)
if front <= back:
	print(front)
else:
	print(back)


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
if p == 1 or p == n or p == n - 1:
	print(0)
else:
	x = 2
	pageList = []
	while x < n - 1:
		pageList.append([x, x + 1])
		x += 2
	forward = 1
	for i in pageList:
		if p in i:
			break
		forward += 1
	if forward <= len(pageList) // 2:
		print(forward)
	else:
		print(len(pageList) - forward + 1)


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 == 1:
	n -= 1
ans = (p - 1 if p % 2 == 1 else p) // 2
ans = min(ans, (n - (p - 1 if p % 2 == 1 else p)) // 2)
print(ans)


n = int(input())
p = int(input())
if n % 2 == 0:
	n = n + 1
if p % 2 != 0:
	p = p - 1
r = (n - p - 1) // 2
l = (p - 2) // 2 + 1
if r < l:
	print(r)
else:
	print(l)


import sys
n = int(input().strip())
p = int(input().strip())
i = 0
while i <= n // 2:
	if 2 * i + 1 >= p or 2 * (n // 2 - i) <= p:
		break
	else:
		i += 1
print(i)


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys

def getNumOfPagesFrom(start, page):
	count = 0
	if start == 1:
		while page - start > 0:
			start += 2
			count += 1
	else:
		while start - page > 1:
			start -= 2
			count += 1
	return count
n = int(input().strip())
p = int(input().strip())
if p < float((n + 1) / 2):
	print(getNumOfPagesFrom(1, p))
else:
	print(getNumOfPagesFrom(n, p))


import sys
n = int(input().strip())
p = int(input().strip())
if n >= 1 and n <= 10 ** 5:
	if p >= 1 and p <= n:
		if p <= n / 2:
			print(p // 2)
		else:
			print((n - p) // 2)


(n, m) = (int(input()), int(input()))
print(min(m // 2, n // 2 - m // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys
import math
PRINT = False
n = int(input().strip())
p = int(input().strip())
if p == 1:
	print(0)
else:
	n_middle = n - 1
	p_middle = p - 2
	turns_middle = p_middle // 2
	double_pages_middle = math.ceil(n_middle / 2)
	reverse_turns_middle = double_pages_middle - 1 - turns_middle
	turns = turns_middle + 1
	if PRINT:
		print('N middle: ' + str(n_middle))
		print('P middle: ' + str(p_middle))
		print('Double_pages_middle: ' + str(double_pages_middle))
		print('Turns middle: ' + str(turns_middle))
		print('Turns: ' + str(turns))
		print('Reverse: ' + str(reverse_turns_middle))
	print(min(turns, reverse_turns_middle))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
front = int(p / 2)
back = n - p + 1 if n % 2 == 0 else n - p
back = int(back / 2)
print(min(front, back))


import sys
import math
n = int(input().strip())
p = int(input().strip())
if n / 2 > p:
	t = math.ceil((p - 1) / 2)
else:
	t = math.floor((n - p) / 2)
print(t)


import math
import sys
n = int(input().strip())
p = int(input().strip())
turns = int(n / 2)
if int(turns / 2) >= int(p / 2):
	print(int(p / 2))
else:
	print(int((n - p) / 2))


import sys
n = int(input().strip())
p = int(input().strip())
l = n // 2
lp = p // 2
print(min(lp, l - lp))


import sys
import math
n = int(input().strip())
p = int(input().strip())
print(min((n - p) // 2, (p - 1) // 2 + ((p - 1) % 2 > 0)))


import sys
n = int(input().strip())
p = int(input().strip())
a = p // 2
b = n // 2
print('{}'.format(min(a, b - a)))


import sys
n = int(input().strip())
p = int(input().strip())

def min_pages_num(n, p):
	if p % 2:
		k1 = (p - 1) / 2
		if n % 2:
			k2 = (n - p) / 2
		else:
			k2 = (n - p + 1) / 2
	else:
		k1 = p / 2
		if n % 2:
			k2 = (n - p - 1) / 2
		else:
			k2 = (n - p) / 2
	return int(min(k1, k2))
print(min_pages_num(n, p))


import sys
n = int(input().strip())
p = int(input().strip())
if p > n // 2:
	c = (n - p) // 2
else:
	c = p // 2
print(c)


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	n = n + 1
t1 = int(p / 2)
t2 = int((n - p) / 2)
if t1 < t2:
	print(t1)
else:
	print(t2)


import sys
import math
n = int(input().strip())
p = int(input().strip())
turn_front = math.ceil((p - 1) / 2)
turn_back = math.ceil((n - p) / 2) if n % 2 == 0 else math.floor((n - p) / 2)
print(min(turn_front, turn_back))


import sys
n = int(input().strip())
p = int(input().strip())
if n % 2 != 0:
	n = n - 1
if p % 2 != 0:
	p = p - 1
if p - 1 <= n - p:
	print((p - 1) // 2 + 1)
else:
	print((n - p) // 2)


import sys
n = int(input().strip())
p = int(input().strip())
n2 = int(n / 2)
p2 = int(p / 2)
if p2 > int(n2 / 2):
	print(int(n2 - p2))
else:
	print(p2)


import sys
n = int(input().strip())
p = int(input().strip())
ftob = int(p / 2)
n += 1 - n % 2
btof = int((n - p) / 2)
print(min(ftob, btof))


import sys
n = int(input().strip())
p = int(input().strip())
p = int(p / 2)
n = int(n / 2)
print(int(min(p, n - p)))


n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	if p % 2 == 0:
		print(min(p // 2, (n - p) // 2))
	else:
		print(min((p - 1) // 2, (n - p + 1) // 2))
elif p % 2 == 0:
	print(min(p // 2, (n - p) // 2))
else:
	print(min((p - 1) // 2, (n - p) // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(int(min(int(p / 2), int((n - p) / 2))))


import sys
n = int(input().strip())
p = int(input().strip())
turns = 0
max_page = 1
if n % 2 == 1:
	min_page = n - 1
else:
	min_page = n
while max_page < p and min_page > p:
	max_page += 2
	min_page -= 2
	turns += 1
print(turns)


import sys
n = int(input().strip())
p = int(input().strip())
print(min(int(p / 2), int((n - p) / 2)))


import sys
import math
n = int(input().strip())
p = int(input().strip())
if n % 2 == 0:
	turn_from_back = math.ceil((n - p) / 2)
else:
	turn_from_back = math.floor((n - p) / 2)
turn_from_front = math.ceil((p - 1) / 2)
if turn_from_front < turn_from_back:
	print(turn_from_front)
else:
	print(turn_from_back)


import sys
import math
n = int(input().strip())
p = int(input().strip())
if n - p >= p:
	if p == 1:
		turns = 0
	else:
		turns = math.ceil((p - 1) / 2)
elif p == n:
	turns = 0
elif (n - p) % 2 == 0:
	turns = math.ceil((n - p) / 2)
else:
	turns = math.ceil((n - p - 1) / 2)
print(turns)


import sys
n = int(input().strip())
p = int(input().strip())
front = p // 2
back = ((n | 1) - p) // 2
print(min(front, back))


import sys
n = int(input().strip())
p = int(input().strip())
if p == 1 or (p == n and p % 2 == 0):
	print(0)
else:
	print(min(p // 2, n // 2 - p // 2))


import sys
n = int(input().strip())
p = int(input().strip())
print(min(p // 2, n // 2 - p // 2))


import sys
nop = int(input().strip())
pd = int(input().strip())
page_fwd = 0
if pd % 2 == 0:
	page_fwd = pd / 2
else:
	page_fwd = (pd - 1) / 2
page_bwd = 0
if nop % 2 != 0:
	nop = nop - 1
if pd % 2 != 0:
	pd -= 1
page_bwd = (nop - pd) / 2
print(int(min(page_fwd, page_bwd)))


import sys
n = int(input().strip())
p = int(input().strip())
if p in [1, n]:
	print(0)
else:
	front_flips = p // 2
	p = n - p
	if n % 2:
		back_flips = p // 2
	else:
		back_flips = (p + 1) // 2
	print(min(front_flips, back_flips))


import sys
n = int(input().strip())
p = int(input().strip())
if p > int(n / 2):
	print(int((n - p) / 2))
else:
	print(int(p / 2))