You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.

In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.

Let's calculate $c_r$ ($0 \le r \le m-1)$ — the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.

Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.

Find the minimum number of moves to satisfy the above requirement.


-----Input-----

The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.

The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.


-----Output-----

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$.


-----Examples-----
Input
6 3
3 2 0 6 10 12

Output
3
3 2 0 7 10 14 

Input
4 2
0 1 2 3

Output
0
0 1 2 3

Difficulty: HARD

Solutions:
import sys
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
ori_arr = arr[:]
store_m = [[] for i in range(m)]
for i in range(n):
	store_m[arr[i] % m].append(i)
limit = n // m
count = 0
free = []
for i in range(2 * m):
	while len(store_m[i % m]) > limit:
		free.append(store_m[i % m].pop())
	while free and len(store_m[i % m]) < limit:
		store_m[i % m].append(free.pop())
for i in range(m):
	for j in range(limit):
		r = store_m[i][j]
		arr[r] = arr[r] + (i - arr[r] % m) % m
for i in range(n):
	count += arr[i] - ori_arr[i]
print(count)
print(*arr)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
idx = [[] for i in range(m)]
for i in range(n):
	idx[a[i] % m].append(i)
k = n // m
free = []
for i in range(m):
	while len(idx[i]) > k:
		v = idx[i].pop()
		free.append(v)
	while free and len(idx[i]) < k:
		v = free.pop()
		idx[i].append(v)
for i in range(m):
	while len(idx[i]) > k:
		v = idx[i].pop()
		free.append(v)
	while free and len(idx[i]) < k:
		v = free.pop()
		idx[i].append(v)
cost = 0
for i in range(m):
	for j in idx[i]:
		dx = (i - a[j] % m) % m
		cost += dx
		a[j] += dx
print(cost)
print(*a)


R = lambda : map(int, input().split())
(n, m) = R()
L = list(R())
d = [[] for i in range(m)]
for (j, i) in enumerate(L):
	d[i % m].append(j)
k = n // m
a = []
res = 0
j = 0
for i in range(2 * m):
	c = i % m
	while len(d[c]) > k:
		ind = d[c].pop()
		a.append([ind, i])
	while len(d[c]) < k and len(a) > 0:
		(ind, ele) = a.pop()
		d[c].append(ind)
		res += i - ele
		L[ind] += i - ele
print(res)
print(*L)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(k, c, free) = (n // m, [[] for _ in range(m)], [])
for (i, ai) in enumerate(a):
	c[ai % m].append(i)
ans = 0
for _ in range(2 * m):
	i = _ % m
	lci = len(c[i])
	for j in range(k, lci)[::-1]:
		free.append((c[i][j], i))
		c[i].pop()
	for j in range(k - lci):
		if free:
			to_add = (i - free[-1][1]) % m
			ans += to_add
			a[free[-1][0]] += to_add
			c[i].append(free[-1][0])
			free.pop()
print(ans)
print(*a)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
s = [[] for _ in range(m)]
for i in range(n):
	s[a[i] % m].append(i)
f = []
c = 0
for _ in range(2):
	for i in range(m):
		while len(s[i]) > n // m:
			f.append((s[i].pop(), i))
		while len(s[i]) < n // m and f:
			(v, p) = f.pop()
			s[i].append(v)
			if i > p:
				d = i - p
			else:
				d = i + m - p
			a[v] += d
			c += d
print(c)
print(*a)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(m):
	d[i] = []
for i in range(len(a)):
	r = a[i] % m
	d[r].append((a[i], i))
step = 0
x = n // m
stack = []
for i in range(2 * m):
	i = i % m
	while len(d[i]) > x:
		stack.append(d[i].pop())
	while len(d[i]) < x and len(stack):
		z = list(stack.pop())
		delta = (i + m - z[0] % m) % m
		z[0] += delta
		d[i].append((z[0], z[1]))
		step += delta
arr = []
for i in d:
	for j in d[i]:
		arr.append(j)
arr.sort(key=lambda i: i[1])
print(step)
for i in arr:
	print(i[0], end=' ')
print()


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
(N, M) = MAP()
A = LIST()
modA = [a % M for a in A]
idxs = [[] for i in range(M)]
for (i, a) in enumerate(modA):
	idxs[a].append(i)
K = N // M
cnt = 0
tmp = []
for m in range(M * 2):
	while tmp and len(idxs[m % M]) < K:
		idxs[m % M].append(tmp.pop())
	while len(idxs[m % M]) > K:
		tmp.append(idxs[m % M].pop())
	cnt += len(tmp)
modans = [0] * N
for (m, li) in enumerate(idxs):
	for i in li:
		modans[i] = m
ans = [0] * N
for i in range(N):
	if modA[i] < modans[i]:
		ans[i] = A[i] + (modans[i] - modA[i])
	elif modA[i] > modans[i]:
		ans[i] = A[i] + (modans[i] + M - modA[i])
	else:
		ans[i] = A[i]
print(cnt)
print(*ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
k = int(n / m)
lis = [[] for i in range(m + 1)]
free = list()
for i in range(n):
	ele = a[i] % m
	lis[ele].append(i)
ans = 0
for i in range(2 * m):
	cur = i % m
	while len(lis[cur]) > k:
		ele = lis[cur].pop()
		free.append([ele, i])
	while len(lis[cur]) < k and free != []:
		(ele, mm) = free.pop()
		lis[cur].append(ele)
		a[ele] += i - mm
		ans += i - mm
print(ans)
for item in a:
	print(item, end=' ')


import collections

class Solution:

	def solve(self, n, m, arr):
		k = n // m
		free = []
		remToIndexDict = collections.defaultdict(list)
		for (index, elem) in enumerate(arr):
			remToIndexDict[elem % m].append(index)
		ans = 0
		for rem in range(2 * m):
			curr = rem % m
			while len(remToIndexDict[curr]) > k:
				index = remToIndexDict[curr].pop()
				free.append((index, rem))
			while len(remToIndexDict[curr]) < k and free:
				(index, mmod) = free.pop()
				remToIndexDict[curr].append(index)
				arr[index] += rem - mmod
				ans += rem - mmod
		return (ans, arr)
sol = Solution()
[n, m] = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
(ans, arr) = sol.solve(n, m, arr)
print(ans)
print(' '.join(map(str, arr)))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = n // m
remain = [[] for i in range(m)]
for i in range(n):
	x = a[i] % m
	remain[x].append(i)
ans = 0
f = []
for i in range(2 * m):
	cur = i % m
	while len(remain[cur]) > t:
		elm = remain[cur].pop()
		f.append([elm, i])
	while len(remain[cur]) < t and len(f) != 0:
		(elm, j) = f.pop()
		remain[cur].append(elm)
		a[elm] += abs(i - j)
		ans += abs(i - j)
print(ans)
print(*a)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
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')

class SegmentTree:

	def __init__(self, data, default=-10 ** 6, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(200001)]
pp = [0] * 200001

def SieveOfEratosthenes(n=200000):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = n // m
remain = [[] for i in range(m)]
for i in range(n):
	x = a[i] % m
	remain[x].append(i)
ans = 0
f = []
for i in range(2 * m):
	cur = i % m
	while len(remain[cur]) > t:
		elm = remain[cur].pop()
		f.append([elm, i])
	while len(remain[cur]) < t and len(f) != 0:
		(elm, j) = f.pop()
		remain[cur].append(elm)
		a[elm] += abs(i - j)
		ans += abs(i - j)
print(ans)
print(*a)


(n, m) = map(int, input().split())
k = n // m
a = list(map(int, input().split()))
cc = [[] for i in range(m)]
for i in range(n):
	cc[a[i] % m].append(i)
f = []
s = 0
for t in range(2):
	for i in range(m):
		while len(cc[i]) > k:
			f.append(cc[i].pop())
		while len(cc[i]) < k and len(f) > 0:
			p = f.pop()
			cc[i].append(p)
			d = (i - a[p]) % m
			a[p] += d
			s += d
print(s)
print(' '.join(map(str, a)))


from sys import stdin
input = stdin.buffer.readline
(n, m) = map(int, input().split())
arr = [int(x) for x in input().split()]
dp = [[] for i in range(m)]
for i in range(n):
	dp[arr[i] % m].append(i)
res = 0
k = n // m
ans = arr.copy()
s = []
for t in range(2):
	for i in range(m):
		if len(dp[i]) < k:
			while len(s) != 0 and len(dp[i]) < k:
				x = s.pop()
				y = arr[x] % m
				if i > y:
					ans[x] = ans[x] + (i - y)
					res = res + (i - y)
				else:
					ans[x] = ans[x] + (m - 1 - y) + (i + 1)
					res = res + (m - 1 - y) + (i + 1)
				dp[i].append('xxx')
		if len(dp[i]) > k:
			while len(dp[i]) > k:
				s.append(dp[i].pop())
print(res)
print(*ans)


l = input().split()
n = int(l[0])
m = int(l[1])
arr = [[] for i in range(m)]
l = input().split()
li = [int(i) for i in l]
for i in range(n):
	arr[li[i] % m].append(i)
req = n // m
count = 0
stack = []
for i in range(2 * m):
	curr = len(arr[i % m])
	if curr < req:
		z = req - curr
		did = 0
		while stack != [] and did < z:
			wtf = i % m - stack[-1][0]
			if wtf < 0:
				wtf += m
			count += wtf
			li[stack[-1][1]] += wtf
			arr[i % m].append(stack[-1][1])
			stack.pop(-1)
			did += 1
	else:
		y = i % m
		for j in range(curr - req):
			stack.append((y, arr[y][-1]))
			arr[y].pop(-1)
print(count)
for i in li:
	print(i, end=' ')


(n, m) = map(int, input().split())
Ccount = n // m
moves = 0
freeSet = []
arr = list(map(int, input().split()))
C = [[] for i in range(m)]
for i in range(n):
	C[arr[i] % m].append(i)
for j in range(2 * m):
	Cindex = j % m
	while len(C[Cindex]) > Ccount:
		currelem = C[Cindex].pop(-1)
		freeSet.append((currelem, j))
	while len(C[Cindex]) < Ccount and freeSet:
		getFree = freeSet.pop(-1)
		C[Cindex].append(getFree[0])
		arr[getFree[0]] += j - getFree[1]
		moves += j - getFree[1]
print(moves)
print(*arr)