Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from functools import lru_cache
h, w, d = map(int, input().split())
pos = [[] for j in range(h*w)]
for a in range(h):
    s = list(map(int, input().split()))
    for g in range(w):
        pos[s[g] -1] = [s[g], a+1, g+1]
q = int(input())

def soo(now, start):
    next = now + d
    prevy, prevx = pos[now - 1][1], pos[now - 1][2]
    nexty, nextx = pos[next - 1][1], pos[next - 1][2]
    return ((abs(nexty - prevy) + abs(nextx - prevx)))

def solve(start, end):
    now = 0
    ans = 0
    for i in range(int((end - start) / d)):
        now = start if not now else now + d
        ans += soo(now, start)
    return ans

time = [0 for h in range(h*w)]
for l in range(h*w - d):
    if not time[l + d]:
        time[l+d] = (abs(pos[l+d][1] - pos[l][1]) + abs(pos[l+d][2] - pos[l][2]))


@lru_cache(maxsize=None)
def ask(st, en):
    return sum(time[st-1+d: en: d])


for rr in range(q):
    dd, ff = map(int, input().split())
    print(ask(dd, ff))

### FIXED CODE ###

from functools import lru_cache
h, w, d = map(int, input().split())
pos = [[] for j in range(h*w)]
for a in range(h):
    s = list(map(int, input().split()))
    for g in range(w):
        pos[s[g] -1] = [s[g], a+1, g+1]
q = int(input())

def soo(now, start):
    next = now + d
    prevy, prevx = pos[now - 1][1], pos[now - 1][2]
    nexty, nextx = pos[next - 1][1], pos[next - 1][2]
    return ((abs(nexty - prevy) + abs(nextx - prevx)))

def solve(start, end):
    now = 0
    ans = 0
    for i in range(int((end - start) / d)):
        now = start if not now else now + d
        ans += soo(now, start)
    return ans

time = [0 for h in range(h*w)]
for l in range(h*w - d):
    if not time[l + d]:
        time[l+d] = time[l] + (abs(pos[l+d][1] - pos[l][1]) + abs(pos[l+d][2] - pos[l][2]))


@lru_cache(maxsize=None)
def ask(st, en):
    return time[en-1] - time[st-1]


for rr in range(q):
    dd, ff = map(int, input().split())
    print(ask(dd, ff))
### BUGGY CODE ###

n = int(input())
num = list(map(int, input().split()))
q = int(input())
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for i in range(len(change)):
    ans = 0
    if change[i][0] in dic:
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    for i, j in enumerate(dic):
        ans += j*dic[j]
    print(ans)

### FIXED CODE ###

n = int(input())
num = list(map(int, input().split()))
q = int(input())
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
ans = 0
for i, j in enumerate(dic):
    ans += j*dic[j]

for i in range(q):
    if change[i][0] in dic:
        ans += dic[change[i][0]] * (change[i][1] - change[i][0])
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    print(ans)
### BUGGY CODE ###

N = int(input())
S = input()

R, G, B = [], [], []
for i, s in enumerate(S):
    if s == "R":
        R.append(i)
    elif s == "G":
        G.append(i)
    else:
        B.append(i)

result = 0
for r in R:
    for g in G:
        for b in B:
            i = min(r, g, b)
            k = max(r, g, b)
            j = (r + g + b) - i - k
            if j - i != k - j:
                result += 1

print(result)


### FIXED CODE ###

def check_RGB(c1, c2, c3):
    l = [c1, c2, c3]
    return ("R" in l) and ("G" in l) and ("B" in l)
    
N = int(input())
S = input()

R, G, B = 0, 0, 0
for i, s in enumerate(S):
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    else:
        B += 1

result = R * G * B

width = 1
while width * 2 < N:
    i = 0
    first = i
    mid = i + width
    last = i + width*2

    while last < N:
        if check_RGB(S[first], S[mid], S[last]):
            result -= 1

        i += 1
        first = i
        mid = i + width
        last = i + width*2

    width += 1

print(result)
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()
for _ in range(q):
  l, r = map(int, input().split())
  sub_s = s[l-1:r]
  print(sum([sub_s[i:i+2]=='AC' for i in range(r-l)]))

### FIXED CODE ###

N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])
### BUGGY CODE ###

n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(len(p)):
    tmp = p[i]
    a = min(p[:i+1])
    if tmp == a:
        count+=1
print(count)

### FIXED CODE ###

n = int(input())
p = list(map(int, input().split()))
count = 1
tmp = p[0]
for i in p:
    a = min(tmp, i)
    if tmp != a:
        count+=1
        tmp = a
print(count)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
q=int(input())
suma=sum(a)
for i in range(q):
    ans=0
    b,c=map(int,input().split())
    for j in range(len(a)):
        if a[j] == b:
            a[j]=c
            ans+=c-b
    suma+=ans
    print(suma)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
import collections
coll=collections.Counter(a)
q=int(input())
suma=sum(a)
lis=[0]*(10**5+1)
for i in a:
    lis[i-1]=coll[i] #lisの「番め」の数字が、aに何個あるか

for i in range(q):
    ans=0
    b,c=map(int,input().split())
    sa=c-b
    lis[c-1]+=lis[b-1]
    ans+=sa*lis[b-1]
    lis[b-1]=0
    suma+=ans
    print(suma)
### BUGGY CODE ###

import sys


def max_heapify(a, i):
    h = len(a)
    l = 2 * i + 1
    r = 2 * i + 2
    if l < h and a[l] > a[i]:
        largest = l
    else:
        largest = i
    if r < h and a[r] > a[largest]:
        largest = r

    if largest != i:
        tmp = a[i]
        a[i] = a[largest]
        a[largest] = tmp
        max_heapify(a, largest)


def build_max_heap(a):
    h = len(a)
    for i in range(h // 2 - 1, -1, -1):
        max_heapify(a, i)


def extract_max(a):
    max = a[0]
    a[0] = a[-1]
    a.pop()
    build_max_heap(a)
    return max

def main():
    istr = sys.stdin.read()
    cmds = list(istr.splitlines())
    end = False
    heap = []
    for cmd in cmds:
        if cmd == "end":
            end = True
            break
        elif cmd == "extract":
            print(extract_max(heap))
        elif cmd[0] == "i":
            heap.append(int(cmd[7:]))
            build_max_heap(heap)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys


def extract_max(A):
    x = A[0]
    A[0] = A[-1]
    A.pop()
    H = len(A)
    i = 0
    while i < H:
        v = A[i]
        rv = 0
        lv = 0
        r = (i + 1) * 2
        l = r - 1
        if l < H:
            lv = A[l]
            if r < H:
                rv = A[r]

        if v < lv and lv > rv:
            A[l] = v
            A[i] = lv
            i = l
        elif v < rv:
            A[r] = v
            A[i] = rv
            i = r
        else:
            break
    return x


def insert(a, n):
    i = len(a)
    a.append(n)
    while i > 0:
        p = int((i + 1) / 2) - 1
        v = a[i]
        pv = a[p]
        if pv < v:
            a[p] = v
            a[i] = pv
            i = p
        else:
            break


def main():
    istr = sys.stdin.read()
    cmds = list(istr.splitlines())
    S = []
    for cmd in cmds:  # input()
        if cmd[0] == "i":
            insert(S, int(cmd[7:]))

        elif cmd == "extract":
            a = extract_max(S)
            print(a)

        elif cmd == "end":
            break


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import itertools


def check(a):
    return a[0] < a[1] + a[2] and a[1] < a[0] + a[2] and a[2] < a[0] + a[1]

N = int(input().strip())
L = [int(str) for str in input().split()]

print(sum([1 if check(comb) else 0 for comb in itertools.combinations(L, 3)]))

### FIXED CODE ###

N = int(input().strip())
L = [int(str) for str in input().split()]

L.sort()

ans = 0

for a in range(N - 2):
    c = a + 1
    for b in range(a + 1, N - 1):
        while c < N - 1 and L[c + 1] < L[a] + L[b]:
            c += 1
        ans += c - b

print(ans)
### BUGGY CODE ###

def main():
    n = int(input())
    hlis = list(map(int, input().split()))


    
    for i in range(0, n-1):
        saishou = min(hlis[i:])
        if hlis[i] - 1 > saishou:
            print('No')
            exit()
        elif hlis[i] - 1 == saishou:
            hlis[i] = saishou

    print("Yes")

if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    n = int(input())
    hlis = list(map(int, input().split()))

    for i in range(0, n-1):
        if hlis[i]-1 > hlis[i+1]:
            print("No")
            exit()
        elif hlis[i]-1 == hlis[i+1]:
            hlis[i] = hlis[i] - 1
            if i > 1 and hlis[i-1] > hlis[i]:
                print('No')
                exit()
        elif i > 1 and hlis[i-1] <= hlis[i] - 1:
            hlis[i] = hlis[i]-1
    print("Yes")

if __name__ == "__main__":
    main()
### BUGGY CODE ###

import math
k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        for c in range(1, k+1):
            ans += math.gcd(math.gcd(a, b), c)
print(ans)


### FIXED CODE ###

from math import gcd
k = int(input())
ans = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        d = gcd(a, b)
        for c in range(1, k+1):
            ans += gcd(d, c)
print(ans)
### BUGGY CODE ###

while 1:
	n = input()
	if n == 0: break
	A = sorted(map(int,raw_input().split()))
	for i in set(A):
		if (n-A[::-1].index(i)-1) - A.index(i) + 1 > n/2:
			print i
			break
	else:
		print "NO COLOR"

### FIXED CODE ###

while 1:
    n = input()
    if n == 0: break
    A = map(int,raw_input().split())
    A.sort()
    c,l = A[0],0
    for i in A:
        if i == c:
            l += 1
            if l > n/2:
                print c
                break
        else:
            c = i
            l = 1
    else:
        print "NO COLOR"
### BUGGY CODE ###

n, k = map(int,input().split())
a = list(map(int,input().split()))
d = []
c = []
for i in range(n):
    if not a[i] in d:
        d.append(a[i])
        c.append(a.count(a[i]))
c.sort()
print(sum(c[0:len(d)-k]))

### FIXED CODE ###

n, k = map(int,input().split())
a = list(map(int,input().split()))
d = {}
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
c = sorted(d.items(),key = lambda x:x[1], reverse=True)
ans = 0
for i in range(min(k,len(c))):
    ans += c[i][1]

print(n-ans)
### BUGGY CODE ###

from sys import stdin, setrecursionlimit


def main():
    n, k = map(int, stdin.readline().split())
    p = list(map(int, stdin.readline().split()))

    sp = [0] * 1001

    for i in range(1, 1001):
        sp[i] = sum(range(i + 1)) / i

    max = 0
    max_index = 0

    for i in range(n - k + 1):
        tmp = 0
        for pi in p[i: i + k]:
            tmp += pi
        if tmp > max:
            max = tmp
            max_index = i

    ans = 0
    for i in range(max_index, max_index + k):
        ans += sp[p[i]]

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

### FIXED CODE ###

from sys import stdin, setrecursionlimit


def main():
    n, k = map(int, stdin.readline().split())
    p = list(map(int, stdin.readline().split()))

    sp = [0] * 1001

    for i in range(1, 1001):
        sp[i] = sum(range(i + 1)) / i

    summary = [0] * n
    summary[0] = sp[p[0]]
    for i in range(1, n):
        summary[i] = summary[i - 1] + sp[p[i]]

    max = summary[k - 1]
    for i in range(k, n):
        tmp = summary[i] - summary[i - k]
        if tmp > max:
            max = tmp
    print(max)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
### BUGGY CODE ###

import sys

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')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

S = input()
N = len(S)
K = INT()

se = set()
ans = []
for i in range(N):
    if len(ans) >= 5:
        if S[i] > ans[4]:
            continue
    for j in range(i+1, N+1):
        T = S[i:j]
        se.add(T)
    ans = sorted(se)
    ans = ans[:5]
ans = sorted(se)
print(ans[K-1])


### FIXED CODE ###

import sys

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')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

S = input()
N = len(S)
K = INT()

se = set()
for i in range(N):
    for j in range(i+1, min(i+6, N+1)):
        T = S[i:j]
        se.add(T)
ans = sorted(se)
print(ans[K-1])
### BUGGY CODE ###

N=int(input())
listA=list(map(int,input().split()))
listA.sort()  
for i in range(1,N):
  ans=listA.count(i)
  print(ans)
print(0)

### FIXED CODE ###

import itertools
N=int(input())
listA=list(map(int,input().split()))
listA.sort()
listA2=[0]*N
for i in listA:
  listA2[i-1]+=1
listA=set(listA)  
for i in range(1,N+1):
  if i in listA:
    print(listA2[i-1])
  else:
    print(0)
### BUGGY CODE ###

N, K = map(int, input().split())
A_list = list(map(int, input().split()))
F_list = list(map(int, input().split()))

A_list_min = sorted(A_list)
F_list_max = sorted(F_list, reverse=True)

first_OK = 10**12

def is_ok(arg):
    check_cnt = 0
    return_flag = 0
    for i in range(N):
        temp = A_list_min[i] - arg//F_list_max[i]
        if temp > 0:
            check_cnt += A_list_min[i] - arg//F_list_max[i]
    if check_cnt <= K:
        return_flag = 1
    else:
        return_flag = 0
    return return_flag

def bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

print(bisect(-1, first_OK))

### FIXED CODE ###

N, K = map(int, input().split())
A_list = list(map(int, input().split()))
F_list = list(map(int, input().split()))

A_list_min = sorted(A_list)
F_list_max = sorted(F_list, reverse=True)

first_OK = 10**12

def is_ok(arg):
    check_cnt = 0
    return_flag = 0
    for i in range(N):
        temp = A_list_min[i] - arg//F_list_max[i]
        if temp > 0:
            check_cnt += A_list_min[i] - arg//F_list_max[i]
    if check_cnt <= K:
        return_flag = 1
    else:
        return_flag = 0
    return return_flag

def bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

print(bisect(-1, first_OK))
### BUGGY CODE ###

N,K = map(int,input().split())
S = list(map(int,input()))
num = 1
T = []
T_append = T.append
if S[0] == 0:
    T.append(0)
tmp = S[0]
for i,s in enumerate(S):
    if i == 0:
        continue
    if tmp != s:
        T_append(num)
        num = 0
    tmp = s
    if i == N-1:
        T_append(num+1)
        continue
    num += 1
if S[N-1] == 0:
    T.append(0)
#index 0,2,4...に1の連続数、1,3,5...に0の連続数
if K >= len(T)//2:
    print(N)
else:
    ans = 0
    for i in range(len(T)//2 -K+1):
        ans = max(ans,sum(T[2*i:2*i+2*K+1]))
    print(ans)

### FIXED CODE ###

N,K = map(int,input().split())
S = list(map(int,input()))
num = 1
T = []
if S[0] == 0:
    T.append(0)
for i in range(1,N):
    if S[i-1] != S[i]:
        T.append(num)
        num = 0
    num += 1
T.append(num)
if S[N-1] == 0:
    T.append(0)
#index 0,2,4...に1の連続数、1,3,5...に0の連続数
if K >= len(T)//2:
    print(N)
else:
    x = sum(T[0:2*K+1])
    ans = x
    for i in range(1,(len(T)//2)-K+1):
        x = x-(T[2*i-2]+T[2*i-1])+(T[2*i+2*K-1]+T[2*i+2*K])
        ans = max(ans,x)
    print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
b = []


for i in range(n):
    cnt = 0
    baisuu = [j for j in range(1,n+1) if j % (n-i) == 0]
    for j in baisuu:
            cnt += a[j-1]
    b.append(cnt%2)

c = 0
for i in b:
    if i != 0:
        c += 1

print(c)
str_b = []
for i in reversed(b):
    if i != 0:
        str_b.append(str(i))
print(' '.join(str_b))

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
b = [0]*n

for i in range(n)[::-1]:
    cnt = sum(b[i::i+1])
    if (cnt %2) != a[i]:
        b[i] = 1
    

print(sum(b))

for i, j in enumerate(b):
    if j == 1:
        print(i+1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
from bisect import bisect_left

def hantei(n, mid, s, suffix):
    # print(mid, s, suffix)
    tmps = "A" + s[:mid-1]
    for i in range(n - mid + 1):
        # tmp = (s[i:i+mid], i)
        tmps = tmps[1:] + s[i+mid-1]
        tmp = (tmps, i)
        idx = bisect_left(suffix, tmp)

        # if mid == 4:
        #     print(tmp, idx)
        flag = True
        while(flag):
            tgt = suffix[idx]
            # print(tgt)
            if tgt[0].startswith(tmp[0]):
                if tgt[1] >= i + mid:
                    return True
                else:
                    idx += 1
                    if idx >= n:
                        flag = False
            else:
                flag = False

n = getN()
s = input().strip()

suffix = []

for i in range(n):
    suffix.append((s[i:], i))

suffix.sort()

mn = 0
mx = n // 2 + 1
while(mx-mn>1):
    mid = (mx + mn) // 2
    is_succ = hantei(n, mid, s, suffix)
    if is_succ:
        mn = mid
    else:
        mx = mid

if hantei(n, mx, s, suffix):
    print(mx)
else:
    print(mn)

"""
17
aaaaaaaaaaaaaaaaa -> 8
10
absoabsoab -> 4
6
abcabc -> 3
10
abcdefghij -> 0
15
rangerangerange -> 5
2
xx -> 1
2
xy -> 0
12
abcdxxxxabcd -> 4

14
abcxxxxxxxxabc
3????????????????
"""

### FIXED CODE ###

import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

dic = dict()

def zalg(s):
    n = len(s)
    ret = [0] * n
    ret[0] = n
    i, j = 1, 0
    while i < n:
        while i + j < n and s[j] == s[i+j]:
            j += 1
        ret[i] = j

        if j == 0:
            i += 1
            continue

        k = 1
        while k < j and k + ret[k] < j:
            ret[i + k] = ret[k]
            k += 1

        i += k
        j -= k

    return ret

def solve(n=None, s1=None, s2=None):
    n = getN()
    s = list(getS())
    ans = 0
    for i in range(n):
        ps = s[i:]
        z = zalg(ps)
        for i, zz in enumerate(z):
            if i >= zz:
                ans = max(ans, zz)
    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()
### BUGGY CODE ###

from collections import Counter
N,K=map(int,input().split())
A=input().split()
B=Counter(A)
C=sorted(B.items(),key=lambda x: -x[1])
print(sum(C[K:][i][1] for i in range(len(C[K:]))))

### FIXED CODE ###

from collections import Counter
N,K=map(int,input().split())
A=input().split()
B=Counter(A)
C=sorted(B.values(),reverse=True)
print(sum(C[K:]))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = abs(A[0] - sum(A[1:]))
for i in range(1, N-1):
  ans = min(abs(sum(A[:i]) - sum(A[i:])), ans)
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
X = sum(A)
xi = A[0]
ans = abs(X - 2 * A[N-1])
for i in range(1, N-1):
  xi += A[i]
  ans = min(abs(X - 2 * xi), ans)
print(ans)
### BUGGY CODE ###

def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        h, w = w, h
        for i in range(1, w // 2 + 1):
            vals[0] = i * h
            if h % 2 == 0:
                vals[1] = (w - i) * (h // 2)
                vals[2] = vals[1]
            else:
                vals[1] = (w - i) * (h // 2 + 1)
                vals[2] = (w - i) * (h // 2)
            diff = max(vals) - min(vals)
            if diff < min_diff:
                min_diff = diff
            if (w - i) % 2 == 0:
                vals[1] = h * ((w - i) // 2)
                vals[2] = vals[1]
            else:
                vals[1] = h * ((w - i) // 2 + 1)
                vals[2] = h * ((w - i) // 2)
            diff = max(vals) - min(vals)
            if diff < min_diff:
                min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))


### FIXED CODE ###

def solve(h, w):
    if h % 3 == 0 or w % 3 == 0:
        return 0
    if w < h:
        h, w = w, h
    if h == 1:
        return 1
    min_diff = h * w
    vals = [0, 0, 0]
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    h, w = w, h
    for i in range(1, w // 2 + 1):
        vals[0] = i * h
        if h % 2 == 0:
            vals[1] = (w - i) * (h // 2)
            vals[2] = vals[1]
        else:
            vals[1] = (w - i) * (h // 2 + 1)
            vals[2] = (w - i) * (h // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
        if (w - i) % 2 == 0:
            vals[1] = h * ((w - i) // 2)
            vals[2] = vals[1]
        else:
            vals[1] = h * ((w - i) // 2 + 1)
            vals[2] = h * ((w - i) // 2)
        diff = max(vals) - min(vals)
        if diff < min_diff:
            min_diff = diff
    return min_diff

_h, _w = map(int, input().split())
print(solve(_h, _w))