Ishaan has been given a task by his teacher. He needs to find the Nth term of a series. His teacher gives him some examples to help him out (Refer examples below). He is a bit weak in pattern searching so to help him his teacher told him that the Nth term is related to prime numbers. The Nth term is the difference of N and the closest prime number to N. Help him find the Nth term for a given N.
 
Example 1:
Input: N = 10
Output: 1
Explanation: Closest prime to 10 is 11.
Example 2:
Input: N = 7
Output: 0
Explanation: Closest prime to 7 is 7.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function NthTerm() which takes N as input paramater and returns Nth term.
 
Expected Time Complexity: O(N* √ N)
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 100000

Difficulty: EASY
Expected Time Complexity: O(N* √ N)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



class Solution:

	def NthTerm(self, N):

		# Code here

Solutions:
class Solution:

	def NthTerm(self, N):

		def bo(x):
			if x < 2:
				return False
			i = 2
			while i * i <= x:
				if x % i == 0:
					return False
				i = i + 1
			return True
		z = N
		y = N
		if N == 1:
			return 1
		while True:
			if bo(y):
				break
			y = y + 1
		while z > 1:
			if bo(z):
				break
			z = z - 1
		if y - N > N - z:
			return N - z
		else:
			return y - N


class Solution:

	def is_prime(self, n):
		if n < 4:
			return True
		i = 2
		while i * i <= n:
			if n % i == 0:
				return False
			i += 1
		return True

	def NthTerm(self, N):
		if N == 1:
			return N
		c = 1
		if self.is_prime(N):
			return 0
		for i in range(1, N):
			if self.is_prime(N - i):
				return c
			elif self.is_prime(N + i):
				return c
			else:
				c += 1


class Solution:

	def isPrime(self, N):
		if N == 1:
			return False
		if N == 2 or N == 3:
			return True
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				return False
		return True

	def NthTerm(self, N):
		i = 0
		while True:
			if self.isPrime(N + i):
				return i
			if N - i > 0 and self.isPrime(N - i):
				return i
			i += 1
		return -1


import math

def isprime(num):
	a = 2
	while a <= math.sqrt(num):
		if num % a < 1:
			return False
		a = a + 1
	return num > 1

class Solution:

	def NthTerm(self, N):
		i = N
		j = i
		while 1:
			if isprime(i):
				return i - N
			elif isprime(j):
				return N - j
			i += 1
			j -= 1


class Solution:

	def NthTerm(self, N):

		def p(x):
			if x == 1:
				return 0
			for i in range(2, int(x ** 0.5) + 1):
				if x % i == 0:
					return 0
			return 1

		def np(x):
			while p(x) != 1:
				x = x + 1
			return x

		def pp(x):
			while p(x) != 1:
				x = x - 1
			return x
		a = np(N)
		b = pp(N)
		if p(N) == 1:
			return 0
		else:
			return min(abs(N - a), abs(N - b))


class Solution:

	def NthTerm(self, N):
		ans = self.near_prime(N)
		if ans >= N:
			ans -= N
		else:
			ans = N - ans
		return ans

	def is_prime(self, N):
		if N < 2:
			return False
		else:
			for i in range(2, int(N ** (1 / 2) + 1)):
				if N % i == 0:
					return False
		return True

	def near_prime(self, N):
		if self.is_prime(N):
			return N
		else:
			lowr_num = N - 1
			upper_num = N + 1
			while True:
				if self.is_prime(lowr_num) == True:
					return lowr_num
				elif self.is_prime(upper_num) == True:
					return upper_num
				else:
					lowr_num -= 1
					upper_num += 1


import math

class Solution:

	def NthTerm(self, N):

		def Prime(n):
			i = 2
			if n < 2:
				return False
			while i <= math.sqrt(n):
				if n % i == 0:
					return False
				i = i + 1
			return True
		if Prime(N):
			return 0
		i = 1
		while True:
			if Prime(N - i) or Prime(N + i):
				break
			i = i + 1
		return i


class Solution:

	def NthTerm(self, N):
		i = 0
		while True:
			if self.isprime(N + i) or self.isprime(N - i):
				return i
			i += 1
		return i

	def isprime(self, n):
		if n == 1:
			return 0
		elif n <= 3:
			return 1
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return 0
		return 1


import math

class Solution:

	def NthTerm(self, N):
		if self.is_prime(N):
			return 0
		else:
			i = 1
			while True:
				if self.is_prime(N - i) or self.is_prime(N + i):
					return i
				else:
					i += 1

	def is_prime(self, num):
		if num <= 1:
			return False
		upper_limit = math.floor(math.sqrt(num)) + 1
		for i in range(2, upper_limit):
			if num % i == 0:
				return False
		return True


def prime(n):
	if n < 2:
		return 0
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return 0
	return 1

class Solution:

	def NthTerm(self, N):
		b = N
		N = b + 1
		c = b - 1
		if prime(b):
			return 0
		while N:
			if prime(N):
				break
			N += 1
		while c:
			if prime(c):
				break
			c -= 1
		p = N - b
		q = b - c
		r = min(p, q)
		return r


import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

class Solution:

	def NthTerm(self, N):
		if N <= 1:
			h = abs(2 - N)
			return h
		p2 = N
		p1 = N
		f = False
		d = False
		while not f:
			if isPrime(p2) == True:
				f = True
			else:
				p2 = p2 + 1
		while not d:
			if isPrime(p1) == True:
				d = True
			else:
				p1 = p1 - 1
		g1 = abs(p1 - N)
		g = abs(p2 - N)
		return min(g, g1)


class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1

		def prime(x):
			if x == 2:
				return True
			for i in range(2, int(x ** 0.5) + 1):
				if x % i == 0:
					return False
			return True
		if prime(N):
			return 0
		a = N
		b = N
		while not prime(a):
			a -= 1
		while not prime(b):
			b += 1
		return min(abs(N - a), abs(N - b))


import math

def isprime(num):
	a = 2
	while a <= math.sqrt(num):
		if num % a < 1:
			return False
		a += 1
	return num > 1

class Solution:

	def NthTerm(self, N):
		les = N
		c = N
		uni = 0
		l = 0
		while c:
			if isprime(c) == True:
				l = c
				break
			c -= 1
		while N:
			if isprime(N) == True:
				uni = N
				break
			N += 1
		if les - l < uni - les:
			return les - l
		else:
			return uni - les


class Solution:

	def isprime(self, x):
		c = 0
		p = int(x ** 0.5) + 1
		for i in range(2, p):
			if x % i == 0:
				return False
		return True

	def NthTerm(self, N):
		j = N + 1
		i = N
		while True:
			if i > 1 and self.isprime(i):
				return N - i
			if self.isprime(j):
				return j - N
			i -= 1
			j += 1


import math

class Solution:

	def NthTerm(self, N):

		def nextPrimeNumber(n):
			(p1, p2) = (n, n)

			def isPrime(num):
				if num <= 1:
					return False
				for i in range(2, int(math.sqrt(num)) + 1):
					if num % i == 0:
						return False
				return True
			while p1 > 0 and (not isPrime(p1)):
				p1 += 1
			while p2 > 0 and (not isPrime(p2)):
				p2 -= 1
			return (p1, p2)
		(p1, p2) = nextPrimeNumber(N)
		return min(abs(p1 - N), abs(p2 - N))


class Solution:

	def NthTerm(self, N):
		if N == 0:
			return 2
		if N == 1:
			return 1
		import math
		a = 0
		f = N
		p = 0
		while p == 0:
			t = 0
			for j in range(2, int(math.sqrt(f)) + 1):
				if f % j == 0:
					t = 1
					break
			if t == 0:
				p = 1
				a = f
			else:
				f -= 1
		p = 0
		f = N + 1
		b = 0
		while p == 0:
			t = 0
			for j in range(2, int(math.sqrt(f)) + 1):
				if f % j == 0:
					t = 1
					break
			if t == 0:
				p = 1
				b = f
			else:
				f += 1
		return min(b - N, N - a)


def isPrime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1
		k = 0
		while k < N:
			if isPrime(N - k):
				break
			elif isPrime(N + k):
				break
			else:
				k += 1
		return k


from bisect import *
primes = [2]
prime = [1] * 100001
for i in range(4, 100001, 2):
	prime[i] = 0
for i in range(3, 100001, 2):
	if prime[i]:
		for j in range(i * i, 100001, i):
			prime[j] = 0
		primes.append(i)

class Solution:

	def NthTerm(self, n):
		i = bisect_right(primes, n)
		if primes[i] == n:
			return 0
		elif i == 0:
			return primes[i] - n
		elif primes[i] - n > n - primes[i - 1]:
			return n - primes[i - 1]
		else:
			return primes[i] - n


class Solution:

	def NthTerm(self, N):
		if self.is_prime(N):
			return int(0)
		else:
			k = N - 1
			while k > 1:
				if self.is_prime(k):
					break
				k = k - 1
			j = N + 1
			while True:
				if self.is_prime(j):
					break
				j = j + 1
			k_diff = N - k
			j_diff = j - N
			return min(k_diff, j_diff)

	def is_prime(self, num):
		if num > 1:
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					return False


def isPrime(N):
	if N < 2:
		return False
	i = 2
	while i * i <= N:
		if N % i == 0:
			return False
		i += 1
	return True

class Solution:

	def NthTerm(self, N):
		x = N
		y = N
		while x > 1:
			if isPrime(x):
				break
			x -= 1
		while True:
			if isPrime(y):
				break
			y += 1
		if not isPrime(x):
			return y - N
		return min(abs(N - x), abs(N - y))


import math

class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1
		i = N
		j = N
		while not self.is_prime(i) and (not self.is_prime(j)):
			i -= 1
			j += 1
		if self.is_prime(i):
			return N - i
		return j - N

	def is_prime(self, n):
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				return False
		return True


import bisect

class Solution:

	def NthTerm(self, n):
		if n == 1:
			return 1

		def fun(m):
			for i in range(2, m):
				if i * i > m:
					break
				if m % i == 0:
					return False
			return True
		for i in range(n):
			if fun(n - i) or fun(n + i):
				return i
		return n


import math

class Solution:

	def NthTerm(self, N):
		gap = 1
		if self.isPrime(N):
			return 0
		while True:
			if self.isPrime(N + gap) or self.isPrime(N - gap):
				break
			gap += 1
		return gap

	def isPrime(self, n):
		if n < 2:
			return False
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				return False
		return True


import math

class Solution:

	def isprime(self, N):
		if N == 1:
			return False
		if N == 2:
			return True
		if N % 2 == 0:
			return False
		num = math.floor(math.sqrt(N))
		for i in range(3, num + 1, 2):
			if N % i == 0:
				return False
		return True

	def NthTerm(self, N):
		ans = True
		num = None
		i = 0
		while ans and N - i > 1:
			if self.isprime(N - i):
				ans = False
				num = N - i
			elif self.isprime(N + i):
				ans = False
				num = N + i
			i += 1
		if ans:
			while ans:
				if self.isprime(N + i):
					ans = False
					num = N + i
				i += 1
		return abs(num - N)


class Solution:

	def NthTerm(self, N):

		def is_prime(n):
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if N < 2:
			return N
		if is_prime(N):
			return 0
		i = 1
		while True:
			if is_prime(N + i) or is_prime(N - i):
				return i
			i += 1


class Solution:

	def NthTerm(self, N):

		def isprime(n):
			if n <= 1:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if isprime(N):
			return 0
		p = 0
		for i in range(N):
			if isprime(N - i):
				p = N - i
				break
			if isprime(N + i):
				p = N + i
				break
		return abs(N - p)


class Solution:

	def isPrime(self, n):
		if n == 2 or n == 3:
			return True
		elif n == 1:
			return False
		else:
			i = 2
			while i * i <= n:
				if n % i == 0:
					return False
				i = i + 1
			return True

	def NthTerm(self, N):
		if self.isPrime(N):
			return 0
		else:
			i = N - 1
			j = N + 1
			while 1:
				if i > 0 and self.isPrime(i):
					return N - i
				elif self.isPrime(j):
					return j - N
				else:
					i = i - 1
					j = j + 1


import math

class Solution:

	def isPrime(self, n):
		if n == 2:
			return True
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				return False
		return True

	def NthTerm(self, n):
		if n == 1:
			return 1
		ans = 1000000000.0
		if self.isPrime(n):
			return 0
		flag = False
		l = n
		r = n
		count = 0
		while True:
			if self.isPrime(l) or self.isPrime(r):
				return count
			count += 1
			l -= 1
			r += 1
			if l == 0:
				flag = True
				break
		if flag:
			while True:
				if self.isPrime(r):
					return count
				r += 1
				count += 1
		return ans


import math

class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1
		j = N
		sqr = int(math.sqrt(N))
		while j:
			count = 0
			for i in range(2, int(math.sqrt(j)) + 1):
				if j % i == 0:
					count = count + 1
			if count == 0:
				break
			j = j + 1
		k = N
		while k:
			count1 = 0
			for i in range(2, int(math.sqrt(k)) + 1):
				if k % i == 0:
					count1 = count1 + 1
			if count1 == 0:
				break
			k = k - 1
		n = N - j
		n1 = N - k
		if n < 0:
			n = -n
		if n1 < 0:
			n1 = -n
		if n > n1:
			return n1
		else:
			return n


class Solution:

	def NthTerm(self, N):
		if N == 2 or N == 3 or N == 5 or (N == 7):
			return 0
		if N == 1:
			return 1
		if N == 0:
			return 2
		prev = N
		nxt = N + 1
		(fn, fp) = ('y', 'y')
		prprime = 0
		nprime = 0
		while True:
			if fp == 'y':
				for i in range(2, int(prev ** 0.5) + 1):
					if prev % i == 0:
						fp = 'n'
						break
				if fp == 'n':
					prev -= 1
					fp = 'y'
				else:
					prprime = prev
					fp = 'g'
			if fn == 'y':
				for j in range(2, int(nxt ** 0.5) + 1):
					if nxt % j == 0:
						fn = 'n'
						break
				if fn == 'n':
					nxt += 1
					fn = 'y'
				else:
					nprime = nxt
					fn = 'g'
			if fn == 'g' and fp == 'g':
				break
		return min(N - prprime, nprime - N)


class Solution:

	def NthTerm(self, N):

		def isPrime(n):
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if N == 1:
			return 1
		c = 0
		(l, r) = (N, N)
		while True:
			if isPrime(l) or isPrime(r):
				return c
			c += 1
			l -= 1
			r += 1
			if l == 0:
				break
		while True:
			if isPrime(r):
				return c
			r += 1
			c += 1


import math

class Solution:

	def prime(self, n):
		if n <= 1:
			return False
		else:
			for i in range(2, int(math.sqrt(n)) + 1):
				if n % i == 0:
					return False
		return True

	def NthTerm(self, N):
		i = 0
		while True:
			res = self.prime(N + i)
			if res:
				return i
			res2 = self.prime(N - i)
			if res2:
				return i
			i += 1


class Solution:

	def is_Prime(self, N):
		if N > 1:
			for i in range(2, int(pow(N, 0.5)) + 1):
				if N % i == 0:
					return False
			return True

	def NthTerm(self, N):
		if self.is_Prime(N):
			return int(0)
		else:
			k = N - 1
			while k > 1:
				if self.is_Prime(k):
					break
				k = k - 1
			j = N + 1
			while True:
				if self.is_Prime(j):
					break
				j = j + 1
			k_diff = N - k
			j_diff = N - j
			return min(abs(k_diff), abs(j_diff))


class Solution:

	def NthTerm(self, N):

		def isPrime(n):
			if n < 2:
				return False
			if n == 2:
				return True
			i = 2
			while i * i <= n:
				if n % i == 0:
					return False
				i += 1
			return True
		i = N
		while not isPrime(i):
			i += 1
		l = N
		if N > 2:
			while not isPrime(l):
				l -= 1
			return min(N - l, i - N)
		return i - N


class Solution:

	def NthTerm(self, N):

		def not_prime(n):
			if n < 2:
				return True
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return True
			return False
		i = 1
		if not not_prime(N):
			return 0
		while not_prime(i + N) and not_prime(N - i):
			i += 1
		return i


import math

class Solution:

	def NthTerm(self, N):

		def isPrime(num):
			if num <= 1:
				return False
			for i in range(2, int(math.sqrt(num)) + 1):
				if num % i == 0:
					return False
			return True
		diff = 0
		while True:
			if isPrime(N + diff) or isPrime(N - diff):
				return diff
			diff += 1
		return -1


import math

class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1

		def isPrime(n):
			for i in range(2, int(math.sqrt(n)) + 1):
				if n % i == 0:
					return False
			return True
		m = N
		while not isPrime(m):
			m -= 1
		l = N
		while not isPrime(l):
			l += 1
		return min(N - m, l - N)


class Solution:

	def NthTerm(self, N):

		def isprime(k):
			f = 0
			while True:
				for i in range(2, int(k ** 0.5) + 1):
					if k % i == 0:
						k += 1
						break
				else:
					return k

		def bprime(l):
			fc = 0
			while True:
				for i in range(2, int(l ** 0.5) + 1):
					if l % i == 0:
						l -= 1
						break
				else:
					return l
		if N == 1:
			return 1
		elif N == 0:
			return 2
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				s = isprime(N)
				j = bprime(N)
				e = abs(N - s)
				d = abs(N - j)
				if e > d:
					return d
				else:
					return e
		else:
			return 0


class Solution:

	def NthTerm(self, N):
		from math import sqrt

		def isPrime(n):
			if n <= 1:
				return False
			for i in range(2, int(sqrt(n)) + 1):
				if n % i == 0:
					return False
			return True
		x = N
		y = N
		while x > 1:
			if isPrime(x):
				break
			x -= 1
		while True:
			if isPrime(y):
				break
			y += 1
		if not isPrime(x):
			return y - N
		return min(abs(N - x), abs(N - y))


class Solution:

	def prime_num(self, num):
		for i in range(2, int(num ** 0.5) + 1):
			if num % i == 0:
				return (False, num)
		return (True, num)

	def NthTerm(self, N):
		if N == 1:
			return 1
		if N == 2:
			return 0
		m = N
		lf = False
		rt = False
		(s, n) = self.prime_num(N)
		if s == True:
			return 0
		N1 = N
		while rt != True:
			if N1 == 1:
				break
			(rt, num1) = self.prime_num(N1)
			if rt == True:
				break
			N1 = N1 + 1
		N2 = N
		while lf != True:
			if N2 == 1:
				break
			(lf, num2) = self.prime_num(N2)
			if lf == True:
				break
			N2 = N2 - 1
		d1 = abs(num1 - m)
		d2 = abs(num2 - m)
		return d1 if d1 < d2 else d2


from math import sqrt

class Solution:

	def NthTerm(self, N):
		if N == 0 or N == 1:
			return 2 - N
		num = 0
		num2 = 0
		while True:
			prime_flag = True
			new_n = N + num
			for i in range(2, int(sqrt(new_n) + 1)):
				if new_n % i == 0:
					prime_flag = False
					break
			if prime_flag:
				return abs(num)
			num2 += 1
			num += num2 * pow(-1, num2 + 1)


import math

class Solution:

	def check(self, n):
		a = 2
		while a <= math.sqrt(n):
			if n % a == 0:
				return False
			a = a + 1
		return True

	def NthTerm(self, N):
		if N == 1:
			return 1
		k = N
		front = N + 1
		back = N - 1
		if self.check(N):
			return 0
		while True:
			ans1 = self.check(front)
			if ans1 == True:
				return abs(k - front)
			ans2 = self.check(back)
			if ans2 == True:
				return abs(k - back)
			front = front + 1
			back = back - 1


from math import sqrt

class Solution:

	def isprime(self, N):
		return all((N % i != 0 for i in range(2, int(sqrt(N) + 1))))

	def NthTerm(self, N):
		if N > 1:
			prime = False
			res = N
			i = 1
			if self.isprime(N):
				return 0
			else:
				while not prime:
					if self.isprime(N - i):
						N = N - i
						prime = True
					elif self.isprime(N + i):
						N = N + i
						prime = True
					i += 1
				res = N - res
				return abs(res)
		else:
			return N


from math import sqrt

class Solution:

	def prime(self, n):
		if n <= 1:
			return False
		for i in range(2, int(sqrt(n)) + 1):
			if n % i == 0:
				return False
		return True

	def NthTerm(self, n):
		mini = 0
		maxi = 0
		if self.prime(n):
			return n - n
		for i in range(n, 1, -1):
			if self.prime(i):
				mini = i
				break
		for i in range(n + 1, 100000):
			if self.prime(i):
				maxi = i
				break
		if n - mini >= maxi - n:
			return maxi - n
		elif maxi - n > n - mini:
			return n - mini


import math

class Solution:

	def NthTerm(self, N):

		def isPrime(n):
			if n <= 1:
				return False
			for i in range(2, int(math.sqrt(n)) + 1):
				if n % i == 0:
					return False
			return True
		(x, y) = (N, N)
		count = 0
		while 1:
			if isPrime(x) or isPrime(y):
				return count
			x -= 1
			y += 1
			count += 1
		return count


from math import sqrt

class Solution:

	def NthTerm(self, N):
		diff = []
		loop = True
		i = N + 1
		if N == 1:
			return 1
		while i >= 2:
			i = i - 1
			flag = 0
			for j in range(2, int(sqrt(i)) + 1):
				if i % j == 0:
					flag = 1
			if flag == 0:
				diff = abs(N - i)
				break
		i = N
		while True:
			i = i + 1
			flag = 0
			for j in range(2, int(sqrt(i)) + 1):
				if i % j == 0:
					flag = 1
			if flag == 0:
				diff2 = abs(N - i)
				break
		out = min(diff, diff2)
		return out


class Solution:

	def check(self, x):
		import math
		p = 0
		if x > 1:
			for i in range(2, int(math.sqrt(x)) + 1):
				if x % i == 0:
					p = 1
					break
			if p == 0:
				return True
			else:
				return False
		else:
			return False

	def NthTerm(self, N):
		m = 10000
		if N == 1:
			return 1
		for i in range(N, N * 2):
			if self.check(i):
				m = min(m, abs(i - N))
				break
		for i in range(N, N // 2, -1):
			if self.check(i):
				m = min(m, abs(i - N))
				break
		return m


class Solution:

	def NthTerm(self, N):

		def primeNum(n):
			if n <= 1:
				return False
			for i in range(2, int(n ** 0.5) + 1):
				if n % i == 0:
					return False
			return True
		if primeNum(N):
			return 0
		L = N
		R = N
		while not primeNum(L) and (not primeNum(R)):
			L -= 1
			R += 1
		return min(abs(L - N), abs(R - N))


import math

class Solution:

	def isPrime(self, N):
		k = int(math.sqrt(N)) + 1
		for i in range(2, k, 1):
			if N % i == 0:
				return False
		return True

	def NthTerm(self, N):
		if N == 0:
			return 2
		elif N == 1:
			return 1
		elif ob.isPrime(N):
			return 0
		aboveN = -1
		belowN = -1
		n1 = N + 1
		while True:
			if ob.isPrime(n1):
				aboveN = n1
				break
			else:
				n1 += 1
		n1 = N - 1
		while True:
			if ob.isPrime(n1):
				belowN = n1
				break
			else:
				n1 -= 1
		diff1 = aboveN - N
		diff2 = N - belowN
		return min(diff1, diff2)


import collections

class Solution:

	def NthTerm(self, N):

		def is_prime(n: int) -> bool:
			if n <= 3:
				return n > 1
			i = 2
			while i * i <= n:
				if n % i == 0:
					return False
				i += 1
			return True
		if N == 0:
			return 2
		if N == 1:
			return 1
		if is_prime(N):
			return 0
		n = N + 1
		p = N - 1
		while not is_prime(n):
			n += 1
		while not is_prime(p):
			p -= 1
		return min(N - p, n - N)


import collections

class Solution:

	def NthTerm(self, N):
		import re

		def isPrime(n):
			return re.compile('^1?$|^(11+)\\1+$').match('1' * n) is None

		def is_prime(n: int) -> bool:
			if n <= 3:
				return n > 1
			if n % 2 == 0 or n % 3 == 0:
				return False
			i = 5
			while i ** 2 <= n:
				if n % i == 0 or n % (i + 2) == 0:
					return False
				i += 6
			return True
		if N == 0:
			return 2
		if N == 1:
			return 1
		if is_prime(N):
			return 0
		n = N + 1
		p = N - 1
		while not is_prime(n):
			n += 1
		while not is_prime(p):
			p -= 1
		return min(N - p, n - N)


class Solution:

	def NthTerm(self, N):

		def isPrime(n):
			if n <= 1:
				return False
			else:
				i = 2
				sn = int(n ** 0.5)
				while i <= sn:
					if n % i == 0:
						return False
					i += 1
				else:
					return True
		for d in range(0, N + 1):
			(i, j) = (N - d, N + d)
			if isPrime(i) or isPrime(j):
				return d


import math

class Solution:

	def NthTerm(self, n):
		if n < 2:
			return 1
		nextPrime = 0
		while nextPrime < n:
			if self.isPrime(n - nextPrime) or self.isPrime(n + nextPrime):
				break
			nextPrime += 1
		return nextPrime

	def isPrime(self, num):
		n = 2
		while n <= math.sqrt(num):
			if num % n == 0:
				return False
			n += 1
		return True


import math

class Solution:

	def is_prime(self, x):
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				return False
		return True

	def NthTerm(self, N):
		if N == 0:
			return 2
		if N == 1:
			return 1
		if N == 2:
			return 0
		if N == 3:
			return 0
		min_diff = float('inf')
		for i in range(N, 2 * N):
			if self.is_prime(i):
				min_diff = abs(i - N)
				break
		for i in range(N + 1)[::-1]:
			if self.is_prime(i):
				min_diff = min(min_diff, abs(i - N))
				break
		return min_diff


import math

def isprime(n):
	ran = int(math.sqrt(n))
	for i in range(2, ran + 1):
		if n % i == 0:
			return False
	return True

class Solution:

	def NthTerm(self, N):
		if N == 1:
			return 1
		if N == 0:
			return 2
		if isprime(N):
			return 0
		num = N
		for i in range(1, N):
			if N - i >= 1 and isprime(N - i):
				return i
			if isprime(N + i):
				return i


import math

class Solution:

	def primecheck(self, x):
		if x == 1:
			return 0
		sta = 1
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				sta = 0
				break
		return sta

	def NthTerm(self, N):
		i = 0
		while True:
			if self.primecheck(N - i) == 1 or self.primecheck(N + i) == 1:
				return i
			i = i + 1