This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.

Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly $\frac{n}{k}$ times consecutively. In other words, array a is k-periodic, if it has period of length k.

For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.

For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.


-----Input-----

The first line of the input contains a pair of integers n, k (1 ≤ k ≤ n ≤ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 2), a_{i} is the i-th element of the array.


-----Output-----

Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.


-----Examples-----
Input
6 2
2 1 2 2 2 1

Output
1

Input
8 4
1 1 2 1 1 1 2 1

Output
0

Input
9 3
2 1 1 1 2 1 1 1 2

Output
3



-----Note-----

In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].

In the second sample, the given array already is 4-periodic.

In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] — this array is simultaneously 1-, 3- and 9-periodic.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation

Greedy algorithms


Solutions:
(n, k) = map(int, input().split())
v = [int(x) for x in input().split()]
l = [0] * k
for i in range(k):
	l[i] = v[i::k].count(1)
ans = 0
n //= k
for x in l:
	ans += min(x, n - x)
print(ans)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
z = n // k
for i in range(k):
	b = l[i::k]
	co = b.count(1)
	ans += min(z - co, co)
print(ans)


R = lambda : map(int, input().split())
(n, m) = R()
s = list(R())
r = 0
a = n // m
for i in range(m):
	(b1, b2) = (0, 0)
	for j in range(a):
		if s[i + j * m] == 1:
			b1 += 1
		else:
			b2 += 1
	r += min(b1, b2)
print(r)


(n, k) = map(int, input().split())
l = [*map(int, input().split())]
print(sum((min(l[i::k].count(1), l[i::k].count(2)) for i in range(k))))


(n, k) = map(int, input().split())
a = input().split()
l = [''.join(a[i:i + k]) for i in range(0, n, k)]
if len(set(l)) == 1:
	print(0)
else:
	s = 0
	x = [0] * k
	for i in l:
		for j in range(k):
			if i[j] == '1':
				x[j] += 1
	m = ['1' if y > n // k - y else '2' for y in x]
	for p in l:
		for q in range(k):
			if p[q] != m[q]:
				s += 1
	print(s)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
print(sum((min(x.count(1), x.count(2)) for x in (a[i::k] for i in range(k)))))


def solve(n, k, arr):
	count = 0
	for i in range(k):
		d = {1: 0, 2: 0}
		for j in range(0, n, k):
			d[arr[i + j]] += 1
		ones = d[1]
		twos = d[2]
		if ones and twos:
			count += min(ones, twos)
	return count

def main():
	(n, k) = list(map(int, input().split(' ')))
	arr = list(map(int, input().split(' ')))
	print(solve(n, k, arr))
main()


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(k):
	c = [0] * 2
	for j in range(i, n, k):
		c[a[j] - 1] += 1
	ans += min(c)
print(ans)


def main():
	(n, k) = map(int, input().split(' '))
	A = [int(i) for i in input().split(' ')]
	ans = 0
	one = 0
	two = 0
	for i in range(k):
		one = 0
		two = 0
		for j in range(n // k):
			pos = i + j * k
			if A[pos] == 1:
				one = one + 1
			else:
				two = two + 1
		if one < two:
			ans = ans + one
		else:
			ans = ans + two
	print(ans)
main()


(n, k) = map(int, input().split())
(s, d) = (0, n // k)
t = input()[::2]
for i in range(k):
	p = t[i::k].count('1')
	s += min(d - p, p)
print(s)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
c = 0
z = n // k
for i in range(k):
	a1 = a[i::k]
	r1 = a1.count(1)
	r2 = a1.count(2)
	c = c + min(r1, r2)
print(c)


print((lambda n: (lambda data, k: sum((min(data[i::k].count(1), data[i::k].count(2)) for i in range(k))))(list(map(int, input().split())), int(n[1])))(input().split()))


(n, k) = map(int, input().split())
a = input().split()
print(sum([min([a[i::k].count(d) for d in '12']) for i in range(k)]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(k):
	(t1, t2) = (0, 0)
	for j in range(i, n, k):
		if a[j] == 1:
			t1 += 1
		else:
			t2 += 1
	ans += min(t1, t2)
print(ans)


(n, k) = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 0
for i in range(0, k):
	n1 = l[i::k].count(1)
	n2 = l[i::k].count(2)
	ans += min(n1, n2)
print(ans)


(n, k) = map(int, input().split())
a = [0] * k
for (i, j) in enumerate(map(int, input().split())):
	a[i % k] += j - 1
print(sum([min(i, n // k - i) for i in a]))


def main():
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	got = [0] * (k + 1)
	for i in range(0, k):
		for j in range(n // k):
			if arr[j * k + i] == 1:
				got[i] += 1
	answer = 0
	for i in range(k):
		answer += min(got[i], n // k - got[i])
	print(answer)
main()


(n, k) = map(int, input().split())
a = input().split()
res = 0
for i in range(k):
	one = 0
	two = 0
	for j in range(i, n, k):
		if a[j] == '2':
			two += 1
		else:
			one += 1
	res += min(one, two)
print(res)


k = int(input().split()[1])
b = list(map(int, input().split()))
c = 0
for i in range(k):
	d = b[i::k]
	c += min(d.count(1), d.count(2))
print(c)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(k):
	ones = 0
	twos = 0
	for j in range(n // k):
		if l[i + j * k] == 1:
			ones += 1
		else:
			twos += 1
	ans += min(ones, twos)
print(ans)


(n, k) = map(int, input().split())
line = list(map(int, input().split()))
count = 0
for i in range(k):
	t = 0
	s = 0
	for j in range(i, n, k):
		if line[j] == 1:
			t += 1
		else:
			s += 1
	count += min(s, t)
print(count)


from collections import Counter
I = lambda : map(int, input().split())
(n, k) = I()
A = list(I())
print(sum((n // k - max(Counter(A[i::k]).values()) for i in range(k))))


from collections import Counter
(n, k) = list(map(int, input().split()))
ar = list(map(int, input().split()))
kc = []
ris = 0
for cont in range(0, n - k + 1, k):
	kc.append(ar[cont:cont + k])
for cont in range(0, k):
	l = []
	for cont2 in range(0, len(kc)):
		l.append(kc[cont2][cont])
		a = Counter(l).most_common(1)[0][1]
	ris += len(kc) - a
print(ris)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
s = 0
x = n // k
for i in range(k):
	cnt = 0
	for j in range(i, n, k):
		if arr[j] == 1:
			cnt += 1
	s += min(cnt, x - cnt)
print(s)


(n, m) = map(int, input().split())
has = [[0, 0] for i in range(m)]
lis = list(map(int, input().split()))
for i in range(n):
	if lis[i] == 2:
		has[i % m][1] += 1
	else:
		has[i % m][0] += 1
ans = 0
for i in range(m):
	ans += min(has[i][0], has[i][1])
print(ans)


(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ans = 0
for i in range(k):
	t = 0
	for j in a[i::k]:
		s = a[i::k].count(j)
		if s > t:
			t = s
			o = j
	for j in a[i::k]:
		if j != o:
			ans += 1
print(ans)


(n, k) = map(int, input().split())
s = [int(i) for i in input().split()]
d = []
cnt = 0
for i in range(k):
	r = []
	for j in range(n):
		if i + j * k < n:
			r.append(s[i + j * k])
	d.append(r)
for i in d:
	a = i.count(1)
	b = i.count(2)
	if a and b:
		cnt += min(a, b)
print(cnt)


3

def readln():
	return tuple(map(int, input().split()))
(n, k) = readln()
a = readln()
ans = 0
for i in range(k):
	cnt = len([1 for j in range(i, n, k) if a[j] == 1])
	ans += min(cnt, n // k - cnt)
print(ans)


inputs = [int(string) for string in input().split()]
n = inputs[0]
k = inputs[1]
count = int(n / k)
array = [int(string) for string in input().split()]
summ = int(0)
various = []
for i in range(int(0), int(k)):
	various.append(0)
for i in range(len(array)):
	if array[i] == 1:
		various[i % k] += 1
	else:
		various[i % k] -= 1
for i in range(int(0), int(k)):
	summ += (count - abs(various[i])) / 2
print(int(summ))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
l = []
j = 0
for i in range(n // k):
	l.append(a[j:j + k])
	j += k
c = 0
for i in range(k):
	d = {}
	for j in range(n // k):
		v = d.get(l[j][i], 0)
		if v == 0:
			d[l[j][i]] = 1
		else:
			d[l[j][i]] = d[l[j][i]] + 1
	if d.get(1, 0) == 0 or d.get(2, 0) == 0:
		continue
	c1 = d.get(1)
	c2 = d.get(2)
	c += c1 if c1 < c2 else c2
print(c)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
i = 0
t = [0] * m
while i < m:
	a = 0
	b = 0
	for j in range(i, n, m):
		if l[j] == 1:
			a += 1
		else:
			b += 1
	if a >= b:
		t[i] = 1
	else:
		t[i] = 2
	i += 1
k = 0
for i in range(n):
	if l[i] != t[i % m]:
		k += 1
	else:
		pass
print(k)


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations
from math import gcd, floor, inf, log2, sqrt, log10
from bisect import bisect_right, bisect_left
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
matrix = []
row = []
for num in arr:
	row.append(num)
	if len(row) == k:
		matrix.append(row)
		row = []
transpose = [[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))]
ans = 0
for row in transpose:
	ans += min(row.count(1), row.count(2))
print(ans)


I = lambda : map(int, input().split())
(n, k) = I()
z = n // k
a = (*I(),)
r = 0
for i in range(k):
	b = a[i::k]
	x = b.count(1)
	r += min(x, z - x)
print(r)


def _count(list, item):
	count = 0
	for c in list:
		if c == item:
			count += 1
	return count

def common(list):
	counts = []
	maxcount = 0
	for stuff in list:
		if _count(list, stuff) > maxcount:
			maxitem = stuff
			maxcount = _count(list, stuff)
	return maxitem
aa = input().split()
n = int(aa[0])
k = int(aa[1])
a = input().split()
matrix = []
new = []
for i in range(1, n + 1):
	if i % k == 0:
		new.append(a[i - 1])
		matrix.append(new)
		new = []
	else:
		new.append(a[i - 1])
colums = []
answer = 0
for j in range(len(matrix[0])):
	colum = []
	for i in range(len(matrix)):
		colum.append(matrix[i][j])
	colums.append(colum)
for rows in matrix:
	for i in range(len(rows)):
		if rows[i] != common(colums[i]):
			answer += 1
print(answer)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
poc = 0
for i in range(k):
	(p1, p2) = (0, 0)
	for j in range(i, n, k):
		if a[j] == 1:
			p1 += 1
		else:
			p2 += 1
	poc += min(p1, p2)
print(poc)


(n, k) = [int(x) for x in input().split()]
l = list(map(int, input().split()))
s = 0
for i in range(k):
	t = 0
	d = 0
	for j in range(n // k):
		if l[j * k + i] == 1:
			t += 1
		else:
			d += 1
	s += min(t, d)
print(s)


read = lambda : map(int, input().split())
(n, k) = read()
m = n // k
a = list(read())
result = 0
for i in range(k):
	t = a[i:n:k]
	s = t.count(1)
	result += min(s, m - s)
print(result)


(n, k) = map(int, input().split())
daf = list(map(int, input().split()))
daf2 = [0] * k
for i in range(n):
	daf2[i % k] += daf[i]
done = n // k
total = 0
for i in range(k):
	if daf2[i] == done or daf2[i] == 2 * done:
		continue
	else:
		total += min(daf2[i] - done, 2 * done - daf2[i])
print(total)


def cal(s, k):
	q = len(s) // k
	sm = 0
	for r in range(k):
		one = two = 0
		for x in range(q):
			if s[k * x + r] == 1:
				one += 1
			else:
				two += 1
		sm += min(one, two)
	return sm
(n, k) = [int(x) for x in input().split()]
s = [int(x) for x in input().split()]
w = cal(s, k)
print(w)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in range(0, k):
	one = two = 0
	for j in range(i, n, k):
		if arr[j] == 1:
			one += 1
		else:
			two += 1
	ans += min(one, two)
print(ans)


(n, k) = list(map(int, input().split()))
L = list(map(int, input().split()))
A = []
for i in range(0, n, k):
	A.append(list(L[i:i + k]))
z = n // k
ans = 0
for i in range(k):
	one = 0
	two = 0
	for j in range(z):
		if A[j][i] == 1:
			one += 1
		else:
			two += 1
	ans += min(one, two)
print(ans)


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
input = stdin.readline
from random import sample
t = 1
from collections import Counter, deque
from math import sqrt, ceil, log2, gcd

class DisjSet:

	def __init__(self, n):
		self.rank = [1] * n
		self.parent = [i for i in range(n)]

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1

def f(arr, i, j, d, dist):
	if i == j:
		return
	nn = max(arr[i:j])
	for tl in range(i, j):
		if arr[tl] == nn:
			dist[tl] = d
			f(arr, i, tl, d + 1, dist)
			f(arr, tl + 1, j, d + 1, dist)

def ps(n):
	cp = 0
	while n % 2 == 0:
		n = n // 2
		cp += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		while n % ps == 0:
			n = n // ps
			cp += 1
	if n != 1:
		return False
	return True

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n + 1):
		qlr = qlr * ip % mod
	for ij in range(1, r + 1):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
tt = [xa ** 3 for xa in range(0, 10 ** 4 + 1)]
qq = set(tt)
for jj in range(t):
	(n, k) = [int(x) for x in input().strip().split()]
	arr = [int(x) for x in input().strip().split()]
	ans = 0
	for i in range(k):
		count1 = 0
		count2 = 0
		for j in range(i, n, k):
			if arr[j] == 1:
				count1 += 1
			else:
				count2 += 1
		ans += min(count1, count2)
	print(ans)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
for i in range(k):
	ans += min(arr[i::k].count(1), arr[i::k].count(2))
print(ans)


(n, k) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
print(sum(((lambda x: min(x - n // k, 2 * n // k - x))(sum(a[i::k])) for i in range(k))))


(n, k) = list(map(int, input().split()))
a = input().split()
c = 0
for i in range(k):
	(x, y) = (0, 0)
	for j in range(i, n, k):
		if a[j] == '1':
			x += 1
		else:
			y += 1
	c += min(x, y)
print(c)


import math
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(k):
	r = a[i]
	m = 0
	for j in range(n // k):
		if a[i + j * k] != r:
			m += 1
	ans += min(m, n // k - m)
print(ans)


(n, k) = map(int, input().split(' '))
li = list(map(int, input().split(' ', n)[:n]))
hs = [[0 for i in range(k)] for j in range(n // k)]
p = 0
for i in range(n // k):
	for j in range(k):
		hs[i][j] = li[p]
		p += 1
ans = 0
for i in range(k):
	an = 0
	for j in range(n // k):
		if hs[j][i] == 1:
			an += 1
	if an <= n // k // 2:
		ans += an
	else:
		ans += n // k - an
print(ans)


line = input().split()
n = int(line[0])
k = int(line[1])
line = input().split()
arr = [int(i) for i in line]
matrix = []
new_arr = []
for i in range(n + 1):
	if i % k == 0 and new_arr:
		matrix.append(new_arr)
		new_arr = []
	if i == n:
		break
	new_arr.append(arr[i])
changes = 0
for c in range(k):
	one_cnt = 0
	two_cnt = 0
	for r in range(n // k):
		if matrix[r][c] == 2:
			two_cnt += 1
		else:
			one_cnt += 1
	if one_cnt != 0 and two_cnt != 0:
		changes += min(one_cnt, two_cnt)
print(changes)


(n, k) = map(int, input().split())
nums = list(map(int, input().split()))
ans = 0
for i in range(k):
	cnt = [0, 0, 0]
	for j in range(n // k):
		cnt[nums[j * k + i]] += 1
	ans += min(cnt[1], cnt[2])
print(ans)


def main():
	R = lambda : map(int, input().split())
	(n, k) = R()
	a = list(R())
	m = n // k
	ans = 0
	for i in range(k):
		c = [n, 0, 0]
		for j in range(m):
			c[a[j * k + i]] += 1
		ans += min(c)
	print(ans)
main()


(n, k) = map(int, input().split())
a = [int(x) for x in input().split()]
b = []
ans = 0
res = []
i = 0
while i < len(a):
	res.append(a[i:i + k])
	i += k
i = 0
while i < k:
	cntTwo = 0
	cntOne = 0
	j = 0
	while j < len(res):
		if res[j][i] == 1:
			cntOne += 1
		else:
			cntTwo += 1
		j += 1
	if cntTwo > cntOne:
		b.append(2)
	else:
		b.append(1)
	i += 1
x = 0
for i in a:
	if i != b[x]:
		ans += 1
	if x < k - 1:
		x += 1
	else:
		x = 0
print(ans)


from math import *
[n, k] = [int(i) for i in input().split()]
arr = [int(i) - 1 for i in input().split()]
arr2 = [[0 for i in range(2)] for j in range(k)]
r = 0
for i in range(n // k):
	for j in range(k):
		arr2[j][arr[i * k + j]] += 1
for j in range(k):
	r += min(arr2[j][0], arr2[j][1])
print(r)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
count = 0
index = 0
while index < k:
	new = [arr[index]]
	temp = index
	for i in range(n // k - 1):
		new.append(arr[temp + k])
		temp += k
	if new.count(1) >= new.count(2):
		count += new.count(2)
	else:
		count += new.count(1)
	index += 1
print(count)


from sys import stdin, stdout
from collections import deque
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	(n, k) = mp()
	l = li()
	ans = 0
	for i in range(k):
		x = []
		for j in range(i, n, k):
			x.append(l[j])
		a = x.count(1)
		b = len(x) - a
		ans += min(a, b)
	pr(ans)
for _ in range(1):
	solve()


from collections import Counter
X = list(map(int, input().split()))
Numbers = list(map(int, input().split()))
print(sum((X[0] // X[1] - max(Counter(Numbers[i::X[1]]).values()) for i in range(X[1]))))


(n, k) = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
ans = 0
for base in range(k):
	ones = twos = 0
	for x in range(n // k):
		if a[x * k + base] == 1:
			ones += 1
		else:
			twos += 1
	ans += min(ones, twos)
print(ans)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(k):
	(s1, s2) = (0, 0)
	for j in range(i, n, k):
		if A[j] == 1:
			s1 += 1
		else:
			s2 += 1
	ans = ans + min(s1, s2)
print(ans)


def f(l1, l2):
	(n, k) = l1
	nol = n // k
	sl = [sum(l2[i:n:k]) for i in range(k)]
	return sum([min(nol * 2 - s, s - nol) for s in sl])
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
print(f(l1, l2))


(n, k) = map(int, input().split())
t = list(map(int, input().split()))
sum1 = float('inf')
l = [[0, 0] for i in range(k)]
for i in range(0, k):
	for j in range(i, n, k):
		l[i][t[j] - 1] = l[i][t[j] - 1] + 1
act_ = []
for i in range(k):
	if l[i][0] > l[i][1]:
		act_.append(1)
	else:
		act_.append(2)
count = 0
for i in range(0, n, k):
	for j in range(i, i + k):
		if t[j] != act_[j - i]:
			count = count + 1
print(count)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(k):
	c1 = c2 = 0
	for j in range(i, n, k):
		if A[j] == 1:
			c1 += 1
		else:
			c2 += 1
	ans += min(c1, c2)
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
m = 0
for i in range(k):
	s = a[i::k]
	c = [s.count(x) for x in set(s)]
	m += len(s) - max(c)
print(m)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ones = [0] * k
twos = [0] * k
for i in range(n):
	if a[i] == 1:
		ones[i % k] += 1
	else:
		twos[i % k] += 1
ans = 0
for i in range(k):
	ans += n // k - max(ones[i], twos[i])
print(ans)


I = lambda : map(int, input().split())
(n, k) = I()
a = [0] * n
i = 0
for v in I():
	a[i] += v - 1
	i = (i + 1) % k
print(sum(map(lambda x: min(x, n // k - x), a)))


def main():
	(n, k) = map(int, input().split())
	l = input().split()
	print(sum((min(nn.count('1'), nn.count('2')) for nn in zip(*[l[i:i + k] for i in range(0, n, k)]))))

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
(cnt1, cnt2) = ([0] * (m + 1), [0] * (m + 1))
for i in range(len(arr)):
	if arr[i] == 1:
		cnt1[i % m + 1] += 1
	if arr[i] == 2:
		cnt2[i % m + 1] += 1
res = 0
for i in range(m + 1):
	res += min(cnt1[i], cnt2[i])
print(res)


(n, k) = map(int, input().split())
answer = [[0, 0] for _ in range(k)]
k1 = 0
for x in input().split():
	if k1 == k:
		k1 = 0
	answer[k1][int(x) - 1] += 1
	k1 += 1
res = 0
for x in answer:
	res += min(x)
print(res)


from io import BytesIO, IOBase
import sys, os
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = print
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys, operator, math, operator
from collections import Counter
(n, k) = inpm()
li = inpl()
omap = {}
ans = min(li.count(2), li.count(1))
add = 0
for i in ra(k):
	(a, b) = (0, 0)
	for j in ra(i, n, k):
		if li[j] == 2:
			b += 1
		else:
			a += 1
	add += min(a, b)
P(min(ans, add))


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
c = 0
for i in range(0, k):
	one = a[i::k].count(1)
	two = a[i::k].count(2)
	c += min(one, two)
print(c)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
for i in range(k):
	c.append([0, 0])
for i in range(k):
	b.append(a[i])
par = True
ans = 0
for i in range(n):
	if a[i] != b[i % k]:
		par = False
	c[i % k][a[i] - 1] += 1
if par:
	print(0)
else:
	for i in range(k):
		ans += min(c[i])
	print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for j in range(k):
	count = {}
	for i in range(n // k):
		num = a[i * k + j]
		count[num] = count.get(num, 0) + 1
	ans += min(count.get(1, 0), count.get(2, 0))
print(ans)


(n, k) = map(int, input().split())
s = list(input().split())
a = n // k
d = 0
for x in range(k):
	b = ''
	c = x
	for y in range(a):
		b += s[c]
		c += k
	if b.count('1') >= b.count('2'):
		d += b.count('2')
	else:
		d += b.count('1')
print(d)


line = input('')
x = line.split(' ')
n = eval(x[0])
k = eval(x[1])
line = input('')
arr = line.split(' ')
total = 0
for i in range(k):
	one = two = 0
	for j in range(i, len(arr), k):
		if arr[j] == '1':
			one += 1
		elif arr[j] == '2':
			two += 1
	total += min(one, two)
print(total)


(n, k) = map(int, input().split())
lst = list(map(int, input().split()))
x = []
c = 0
for j in range(k):
	x = lst[j::k]
	x1 = x.count(1)
	x2 = x.count(2)
	c += min(x1, x2)
print(c)


import sys
import math
(n, k) = [int(x) for x in sys.stdin.readline().split()]
an = [int(x) for x in sys.stdin.readline().split()]
res = 0
for i in range(k):
	k1 = 0
	k2 = 0
	for j in range(i, n, k):
		if an[j] == 1:
			k1 += 1
		else:
			k2 += 1
	res += int(n / k) - max(k2, k1)
print(res)


(n, k) = map(int, input().split())
(a, val) = (list(map(int, input().split())), 0)
for i in range(k):
	c = sum((a[i + j * k] == 1 for j in range(n // k)))
	val += min(c, n // k - c)
print(val)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
x = list(zip(*[a[k * (i - 1):k * i] for i in range(1, n // k + 1)]))
ans = 0
for i in range(k):
	t = x[i].count(2)
	ans += min(t, n // k - t)
print(ans)


bot = True
R = lambda : map(int, input().split())
(n, k) = R()
a = list(R())
print(sum((min(x.count(1), x.count(2)) for x in (a[i::k] for i in range(k)))))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
l = [[0, 0] for _ in range(k)]
for i in range(n):
	if a[i] == 1:
		l[i % k][0] += 1
	else:
		l[i % k][1] += 1
ans = 0
for i in l:
	ans += min(i)
print(ans)


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

def arr_inp():
	return [int(x) for x in input().split()]
(n, k) = inp()
(a, out) = (arr_inp(), 0)
for i in range(k):
	arr = a[i::k]
	out += min(arr.count(1), arr.count(2))
print(out)