Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere.

One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo m, or not.

Arithmetical progression modulo m of length n with first element x and difference d is sequence of integers x, x + d, x + 2d, ..., x + (n - 1)·d, each taken modulo m.

Input

The first line contains two integers m and n (2 ≤ m ≤ 109 + 7, 1 ≤ n ≤ 105, m is prime) — Timofey's favorite prime module and the length of the sequence.

The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai < m) — the elements of the sequence.

Output

Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo m.

Otherwise, print two integers — the first element of the obtained progression x (0 ≤ x < m) and its difference d (0 ≤ d < m).

If there are multiple answers, print any of them.

Examples

Input

17 5
0 2 4 13 15


Output

13 2


Input

17 5
0 2 4 13 14


Output

-1


Input

5 3
1 2 3


Output

3 4

Source: codeforces | Difficulty: VERY_HARD
Tags: Mathematics

Number theory

Implementation

Complete search


Solutions:
def solve(n, m, a):
	if n == 0:
		return (0, 1)
	if n == 1:
		return (a[0], 1)
	d = (a[1] - a[0]) % m
	if d < 0:
		d += m
	st = set(a)
	cnt = 0
	for v in a:
		cnt += (v + d) % m in st
	cnt = n - cnt
	d = d * pow(cnt, m - 2, m) % m
	now = a[0]
	while (now + m - d) % m in st:
		now = (now + m - d) % m
	for i in range(n):
		if (now + i * d) % m not in st:
			return (-1, -1)
	return (now, d)
(m, n) = map(int, input().split())
a = list(map(int, input().split()))
if n * 2 > m:
	st = set(a)
	b = [i for i in range(m) if i not in st]
	(f, d) = solve(len(b), m, b)
	f = (f + d * (m - n)) % m
else:
	(f, d) = solve(n, m, a)
if f < 0 or d < 0:
	print(-1)
else:
	print(f, d)


import sys

def modInverse(x, y, mod):
	res = 1
	x = x % mod
	while y > 0:
		if y & 1:
			res = res * x % mod
		y = y // 2
		x = x * x % mod
	return res

def isEqual(a, b):
	for i in range(len(a)):
		if a[i] != b[i]:
			return False
	return True
(m, n) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
s = [0, 0]
for x in arr:
	s[0] = s[0] + x
	s[1] = s[1] + x * x
if n == 1:
	print(arr[0], 0)
	sys.exit()
if n == m:
	print('0 1')
	sys.exit()
arr.sort()
for i in range(1, n):
	d = arr[i] - arr[0]
	x = (s[0] - n * (n - 1) // 2 * d + m) % m * modInverse(n, m - 2, m) % m
	Sum = (n * x * x + n * (n - 1) * d * x + n * (n - 1) * (2 * n - 1) // 6 * d * d) % m
	if Sum == s[1] % m:
		b = [x]
		for j in range(1, n):
			b.append((b[j - 1] + d) % m)
		b.sort()
		if isEqual(arr, b):
			print(x, d)
			sys.exit()
print('-1')


from functools import reduce

def gcd_extended(bigger, less):
	if less == 0:
		return (bigger, 1, 0)
	mod = bigger % less
	div = bigger // less
	(gcd, c_less, c_mod) = gcd_extended(less, mod)
	c_bigger = c_mod
	c_less = c_less - c_mod * div
	return (gcd, c_bigger, c_less)

def mlp_inverse(x, p):
	(one, c_p, c_x) = gcd_extended(p, x)
	return (c_x + p) % p

def tests(x, d, p, row):
	_row = set(row)
	n = len(row)
	if d == 0:
		return False
	for i in range(n):
		elem = x + i * d
		elem = (elem % p + p) % p
		if elem not in _row:
			return False
	return True
(p, n) = (int(x) for x in input().split())
row = [int(x) for x in input().split()]
if p == n:
	print(1, 1)
	exit()
if n == 1:
	print(row[0], 0)
	exit()
c1 = reduce(lambda x, y: (x + y) % p, row, 0)
c2 = reduce(lambda x, y: (x + y * y) % p, row, 0)
sum_i = reduce(lambda x, y: (x + y) % p, range(0, n), 0)
sum_i_sq = reduce(lambda x, y: (x + y * y) % p, range(0, n), 0)
inv_sum_i = mlp_inverse(sum_i, p)
inv_sum_i_sq = mlp_inverse(sum_i_sq, p)
for x in row:
	d = (c1 - n * x) * inv_sum_i
	d = (d % p + p) % p
	equasion = n * x * x + 2 * d * x * sum_i + d * d * sum_i_sq
	equasion = (equasion % p + p) % p
	if equasion == c2 and tests(x, d, p, row):
		print(x, d)
		exit()
print(-1)


import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter, deque, defaultdict
from functools import lru_cache, reduce
from heapq import heappush, heappop, heapify, heappushpop, _heappop_max, _heapify_max

def _heappush_max(heap, item):
	heap.append(item)
	heapq._siftdown_max(heap, 0, len(heap) - 1)

def _heappushpop_max(heap, item):
	if heap and item < heap[0]:
		(item, heap[0]) = (heap[0], item)
		heapq._siftup_max(heap, 0)
	return item
from math import e, gcd as GCD, modf
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def Extended_Euclid(n, m):
	stack = []
	while m:
		stack.append((n, m))
		(n, m) = (m, n % m)
	if n >= 0:
		(x, y) = (1, 0)
	else:
		(x, y) = (-1, 0)
	for i in range(len(stack) - 1, -1, -1):
		(n, m) = stack[i]
		(x, y) = (y, x - n // m * y)
	return (x, y)

class MOD:

	def __init__(self, mod):
		self.mod = mod

	def Pow(self, a, n):
		a %= self.mod
		if n >= 0:
			return pow(a, n, self.mod)
		else:
			assert math.gcd(a, self.mod) == 1
			x = Extended_Euclid(a, self.mod)[0]
			return pow(x, -n, self.mod)

	def Build_Fact(self, N):
		assert N >= 0
		self.factorial = [1]
		for i in range(1, N + 1):
			self.factorial.append(self.factorial[-1] * i % self.mod)
		self.factorial_inv = [None] * (N + 1)
		self.factorial_inv[-1] = self.Pow(self.factorial[-1], -1)
		for i in range(N - 1, -1, -1):
			self.factorial_inv[i] = self.factorial_inv[i + 1] * (i + 1) % self.mod
		return self.factorial_inv

	def Fact(self, N):
		return self.factorial[N]

	def Fact_Inv(self, N):
		return self.factorial_inv[N]

	def Comb(self, N, K):
		if K < 0 or K > N:
			return 0
		s = self.factorial[N]
		s = s * self.factorial_inv[K] % self.mod
		s = s * self.factorial_inv[N - K] % self.mod
		return s
(M, N) = map(int, readline().split())
A = list(map(int, readline().split()))
set_A = set(A)
MD = MOD(M)
if N == M:
	print(0, 1)
	exit()
if 2 * N < M:
	if N == 1:
		print(A[0], 0)
		exit()
	a = (A[0] - A[1]) % M
	cnt = 0
	for i in range(N):
		if (A[i] + a) % M in set_A:
			cnt += 1
	d = a * MD.Pow(N - cnt, -1) % M
	cnt = 0
	for i in range(N):
		if (A[i] - d) % M in set_A:
			cnt += 1
		else:
			ans = A[i]
	if cnt == N - 1:
		print(ans, d)
		exit()
else:
	A = [a for a in range(M) if not a in set_A]
	N = len(A)
	if N == 1:
		print((A[0] + 1) % M, 1)
		exit()
	set_A = set(A)
	a = (A[0] - A[1]) % M
	cnt = 0
	for i in range(N):
		if (A[i] + a) % M in set_A:
			cnt += 1
	d = a * MD.Pow(N - cnt, -1) % M
	cnt = 0
	for i in range(N):
		if (A[i] + d) % M in set_A:
			cnt += 1
		else:
			ans = A[i]
	if cnt == N - 1:
		print((ans + d) % M, d)
		exit()
print(-1)


from collections import defaultdict
dic = defaultdict(int)

def pow_mod(a, b, c):
	n = 1
	m = a
	v = b
	if a == 1:
		return 1
	while v > 0:
		if v % 2 == 1:
			n *= m % c
			n %= c
		m *= m
		m %= c
		v //= 2
	return n % c

def pow(a, b):
	n = 1
	m = a
	v = b
	if a == 1:
		return 1
	while v > 0:
		if v % 2 == 1:
			n *= m
		m *= m
		v //= 2
	return n

def tonelli_shanks(a, p):
	if a == 0:
		return 0
	q = p - 1
	s = 0
	while q % 2 == 0:
		s += 1
		q //= 2
	z = 2
	while pow_mod(z, (p - 1) // 2, p) != p - 1:
		z += 1
	m = s
	c = pow_mod(z, q, p)
	t = pow_mod(a, q, p)
	r = pow_mod(a, (q + 1) // 2, p)
	while m > 1:
		if pow_mod(t, pow(2, m - 2), p) == 1:
			c = c ** 2 % p
			m -= 1
		else:
			t = c ** 2 % p * t % p
			r = c * r % p
			c = c ** 2 % p
			m -= 1
	return r % p
(m, n) = map(int, input().split())
a = list(map(int, input().split()))
for i in a:
	dic[i] += 1
if n == 1:
	d = 0
	x = a[0]
	print(x, d)
elif n == m - 1:
	d = 1
	a.sort()
	if a[0] != (a[-1] + d) % m:
		print(a[0], d)
	for i in range(1, n):
		if a[i] != (a[i - 1] + d) % m:
			print(a[i], d)
			quit()
elif n == m:
	d = 1
	x = 0
	print(x, d)
elif m == 2:
	a.sort()
	print(a[0], a[1] - a[0])
elif m == 3:
	a.sort()
	d = a[1] - a[0]
	for i in range(1, n):
		if a[i] != a[i - 1] + d:
			print(-1)
			quit()
	print(a[0], d)
else:
	ave = sum(a) * pow_mod(n, m - 2, m) % m
	b = [(a[i] - ave) ** 2 % m for i in range(n)]
	s = sum(b) * pow_mod(n, m - 2, m) % m
	k = n ** 2 - 1
	d2 = s * 12 * pow_mod(k, m - 2, m) % m
	d = tonelli_shanks(d2, m)
	x = (ave - d * (n - 1) * pow_mod(2, m - 2, m) % m) % m
	if x != int(x):
		print(-1)
		quit()
	x = int(x)
	for i in range(n):
		if not dic[(x + i * d) % m]:
			print(-1)
			quit()
	print(x, d)