Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    pritn(sm)

Fixed submission:
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    print(sm)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
l = list(map(int, input().split())).sort()
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    pritn(sm)

Fixed submission:
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    print(sm)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
l = list(map(int, input().split()))
l = sorted(l)
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    pritn(sm)

Fixed submission:
n, k = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
if n<=k :
    print(0)
else :
    sm = 0
    for i in range(n-k):
        sm += l[i]
    print(sm)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input(),split())
print('Yes' if n==m else 'No')

Fixed submission:
n, m = map(int, input().split())
print('Yes' if n==m else 'No')
Buggy python submission (verdict: Runtime Error):
C = input()
alpha = iter("abcdefghijklmnopqrstuvwxyz")
if C in alpha :
    A = alpha.next()
print(A)

Fixed submission:
s = input()
t = chr(ord(s)+1)
print(t)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()
cnt=0
for i in range(n):
    if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
        cnt+=1
print(cnt)

Fixed submission:
n = int(input())
s = input()
print(s.count('ABC'))
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(500*500)
S = input()
T =input()
s = S.lower()
t = T.lower()
if len(s) or len(t) > 100 : 
    print("error")
print(T + S)

Fixed submission:
S, T = input().split()

print(T + S)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(500*500)
S = input()
T =input()
S.lower()
T.lower()
print(T + S)

Fixed submission:
S, T = input().split()

print(T + S)

Buggy python submission (verdict: Runtime Error):
S = input()
T =input()
S.lower()
T.lower()
print(T + S)

Fixed submission:
S, T = input().split()

print(T + S)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(500*500)

A = int(input())
B = int(input())
K = int(input())


for i in range(1, K+1) :
    if A >0:
        A -= 1
    elif B > 0 :
        B -= 1
        
print(A, B)
    

Fixed submission:
A, B, K = map(int, input().split())

a = max(A-K, 0)
K -= A - a
b = max(B-K, 0)

print(a, b)

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
K = int(input())


for i in range(1, K+1) :
    if A >0:
        A -= 1
    elif B > 0 :
        B -= 1
        
print(A, B)
    

Fixed submission:
A, B, K = map(int, input().split())

a = max(A-K, 0)
K -= A - a
b = max(B-K, 0)

print(a, b)
Buggy python submission (verdict: Runtime Error):
import math
a, b = map(int, input().split())
def lcm(aa, bb):
    return (aa*bb)//math.gcd(aa, bb)
print(lcm(a, b))    

Fixed submission:
import fractions
a, b = map(int, input().split())
def lcm(aa, bb):
    return (aa*bb)//fractions.gcd(aa, bb)
print(lcm(a, b))

Buggy python submission (verdict: Runtime Error):
import math
a, b = map(int, input().split())
def lcm(aa, bb):
    return (aa*bb)//math.gcd(aa, bb)
print(lcm(a, b))    

Fixed submission:
import fractions
a, b = map(int, input().split())
def lcm(aa, bb):
    return (aa*bb)//fractions.gcd(aa, bb)
print(lcm(a, b))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [[] for _ in range(n)]
for i in range(n):
  num = int(input())
  for j in range(num):
    person, state = map(int, input().split())
    a[i].append([person-1, state])
honest = 0
for i in range(1, 2**n):
  flag = True
  for j in range(n):
    if (i>>j)&1 == 1:
      for x, y in a[j]:
        if (i>>x)&1 != y:
          flag = False
          break
  if flag:
    honest = max(honest, bin(i)[2:].count('1'))
print(honest)


Fixed submission:
a, b, c = map(int, input().split())
print('win' if a+b+c<=21 else 'bust')
Buggy python submission (verdict: Runtime Error):
s = input()
l = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
for i in range(7):
    if s == l[i]:
        print(7%i)

Fixed submission:
s = input()
l = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT']
for i in range(7):
    if s == 'SUN':
        print(7)
        break
    else:
        if s == l[i]:
            print(7-i)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int, input().split()))
ls = sorted(l)
a = [str(l.index(ls[i])+1) for i in range(n)]
print(' '.join(a))

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
s = [0] * n
for i in range(n):
    s[a[i] - 1] = i + 1
print(*s)
Buggy python submission (verdict: Runtime Error):
import numpy as np
n, k, q = map(int, input().split())
l = [int(input()) for i in range(q)]
ls = np.array([k]*n)
for i in l:
    ls[:i-1] -= 1
    ls[i:] -= 1
for i in range(n):
    if ls[i]>0:
        print('Yes')
    else :
        print('No')

Fixed submission:
n,k,q = map(int,input().split())
s = [k]*n
for i in range(q):
    a = int(input())
    s[a-1] += 1
for key in s:
    if key-(q) > 0:
        print('Yes')
    else:
        print('No')

Buggy python submission (verdict: Runtime Error):
import numpy as np
n, k, q = map(int, input().split())
l = [int(input()) for i in range(q)]
ls = np.array([k]*n)
for i in l:
    ls[:i-1] -= 1
    ls[i:] -= 1
for i in range(n):
    if ls[i]>0:
        print('Yes')
    else :
        print('No')

Fixed submission:
n,k,q = map(int,input().split())
s = [k]*n
for i in range(q):
    a = int(input())
    s[a-1] += 1
for key in s:
    if key-(q) > 0:
        print('Yes')
    else:
        print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, k, q = map(int, input().split())
l = [int(input()) for i in range(q)]
ls = np.array([k]*n)
for i in l:
    ls[:i-1] -= 1
    ls[i:] -= 1
for i in range(n):
    if ls[i]>0:
        print('Yes')
    else :
        print('No')

Fixed submission:
n,k,q = map(int,input().split())
s = [k]*n
for i in range(q):
    a = int(input())
    s[a-1] += 1
for key in s:
    if key-(q) > 0:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
for i in range(n):
    if a[i] == max_list:
        ans = sorted(list(a))[-2]
        print(ans)
    else :
        print(max_list)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
ans = sorted(list(a))[-2]
for i in range(n):
    if a[i] == max_list:
        print(ans)
    else :
        print(max_list)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
for i in range(n):
    if a[i] == max_list:
        ans = sorted(list(a))[-2]
        print(ans)
    else :
        print(max_list)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
max_list = max(a)
ans = sorted(list(a))[-2]
for i in range(n):
    if a[i] == max_list:
        print(ans)
    else :
        print(max_list)
Buggy python submission (verdict: Runtime Error):
n = int(input())
l = []
for i in  range():
    s, p = input().split()
    l.append([s, int(p), i+1])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2])

Fixed submission:
n = int(input())
l = []
for i in range(n):
    s, p = input().split()
    l.append([s, int(p), i+1])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2])

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = []
for i in  range():
    s, p = input().split()
    l.append([s, int(p), i])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2]+1)

Fixed submission:
n = int(input())
l = []
for i in range(n):
    s, p = input().split()
    l.append([s, int(p), i+1])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2])

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = []
for i in  range():
    s, p = input().split()
    l.append([s, int(p), i])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in l_sorted:
    print(j[2]+1)

Fixed submission:
n = int(input())
l = []
for i in range(n):
    s, p = input().split()
    l.append([s, int(p), i+1])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2])

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = []
for i in  range():
    s, p = input().split()
    l.append([s, int(p), i])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for out in l_sorted:
    print(out[2]+1)

Fixed submission:
n = int(input())
l = []
for i in range(n):
    s, p = input().split()
    l.append([s, int(p), i+1])
l_sorted = sorted(l, key = lambda x: (x[0], -x[1]))
for j in range(n):
    print(l_sorted[j][2])
Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
l = [h[0]]
ans = 1
for i in h[1:]:
        if i>=sorted(l)[-1]:
            l.append(i)
print(len(i))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))
l = [str(h[0])]
for i in h[1:]:
        if i>=int(sorted(l)[-1]):
            i = str(i)
            l.append(i)
print(len(l))

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
l = [h[0]]
ans = 1
for i in h[1:]:
    for j in l:
        if i>=j:
            l.append(i)
print(len(i))


Fixed submission:
n = int(input())
h = list(map(int, input().split()))
l = [str(h[0])]
for i in h[1:]:
        if i>=int(sorted(l)[-1]):
            i = str(i)
            l.append(i)
print(len(l))
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    S = s[l-1:r]
    ans = S.count('AC')
    print(ans)


Fixed submission:
n, q = map(int, input().split())
s = input()
ls = [0] * n
cnt = 0
for i in range(1, n):
    if s[i] == 'C' and s[i-1] == 'A':
        cnt += 1
    ls[i] += cnt
for j in range(q):
    l, r = map(int, input().split())
    print(ls[r-1]-ls[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    S = s[l-1:r]
    ans = S.count('AC')
    print(ans)


Fixed submission:
n, q = map(int, input().split())
s = input()
ls = [0] * n
cnt = 0
for i in range(1, n):
    if s[i] == 'C' and s[i-1] == 'A':
        cnt += 1
    ls[i] += cnt
for j in range(q):
    l, r = map(int, input().split())
    print(ls[r-1]-ls[l-1])
Buggy python submission (verdict: Runtime Error):
import math
a, b, k = map(int, input().split())
l = []
gcd = math.gcd(a, b)
for i in range(1, int(gcd**0.5)+1):
    if gcd%i==0:
        l.append(i)
        if i != gcd//i:
            l.append(gcd//i)
l.sort(reverse = True)
print(l[k-1])


Fixed submission:
import fractions
a, b, k = map(int, input().split())
l = []
gcd = fractions.gcd(a, b)
for i in range(1, int(gcd**0.5)+1):
    if gcd%i==0:
        l.append(i)
        if i != gcd//i:
            l.append(gcd//i)
l.sort(reverse = True)
print(l[k-1])
Buggy python submission (verdict: Runtime Error):
from collections import deque
n, m = map(int, input().split())
a = [0]*m
for _ in range(n):
    A = deque(map(int, input().split()))
    k = A.popleft()
    if i == 0:
        S = set(A)
    else:
        S = S & set(A)
print(len(S))


Fixed submission:
from collections import deque
n, m = map(int, input().split())
a = [0]*m
for _ in range(n):
    A = deque(map(int, input().split()))
    k = A.popleft()
    if _ == 0:
        S = set(A)
    else:
        S = S & set(A)
print(len(S))

Buggy python submission (verdict: Runtime Error):
from collections import deque
n, m = map(int, input().split())
a = [0]*m
for _ in range(n):
    A = deque(map(int, input().split()))
    k = A.popleft()
    if i == 0:
        S = set(A)
    else:
        S = S & set(A)
 
print(len(S))


Fixed submission:
from collections import deque
n, m = map(int, input().split())
a = [0]*m
for _ in range(n):
    A = deque(map(int, input().split()))
    k = A.popleft()
    if _ == 0:
        S = set(A)
    else:
        S = S & set(A)
print(len(S))
Buggy python submission (verdict: Time Limit Exceeded):
n, w = map(int, input().split())
w_v = [list(map(int, input().split())) for _ in range(n)]

dp = [[0]*(w+1) for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        dp[i+1][j] = max(dp[i][j], dp[i+1][j])
        if j-w_v[i][0] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-w_v[i][0]] + w_v[i][1])
print(dp[n][w])



Fixed submission:
n, w = map(int, input().split())
w_v = [list(map(int, input().split())) for _ in range(n)]
#import numpy as np
dp = [[0]*(w+1) for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        dp[i+1][j] = max(dp[i][j], dp[i+1][j])
        if j-w_v[i][0] >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-w_v[i][0]] + w_v[i][1])

    
print(dp[n][w])
Buggy python submission (verdict: Runtime Error):
n = input()
l = [111, 222, 333, 444, 555, 666, 777, 888, 999]
for i in l:
    if n<=i:
        print(i)
        break

Fixed submission:
n = int(input())
l = [111, 222, 333, 444, 555, 666, 777, 888, 999]
for i in l:
    if n<=i:
        print(i)
        break
Buggy python submission (verdict: Runtime Error):
n, m, x, y = map(int, input().split())
if x>y :
    print('War')
a = 0
else:
    X = sorted(list(map(int, input().split())))
    Y = sorted(list(map(int, input().split())))
    for i in range(x+1, y+1):
        if X[-1]<i and i <= Y[0] and x < i and i <= y:
            print('No War')
            break
        else :
            a += 1
    if a == y-x:
        print('War')


Fixed submission:
n, m, x, y = map(int, input().split())
if x>y :
    print('War')
else  :
    X = sorted(list(map(int, input().split())))
    Y = sorted(list(map(int, input().split())))
    a = 0
    for i in range(x+1, y+1):
        if X[-1]<i and i <= Y[0] and x < i and i <= y:
            print('No War')
            break
        else :
            a += 1
    if a == y-x:
        print('War')

Buggy python submission (verdict: Runtime Error):
n, m, x, y = map(int, input().split())
if x>y :
    print('War')
else:
    X = sorted(list(map(int, input().split())))
    Y = sorted(list(map(int, input().split())))
    for i in range(x+1, y+1):
        if X[-1]<i and i <= Y[0] and x < i and i <= y:
            print('No War')
            break
        else :
            a += 1
    if a == y-x:
        print('War')


Fixed submission:
n, m, x, y = map(int, input().split())
if x>y :
    print('War')
else  :
    X = sorted(list(map(int, input().split())))
    Y = sorted(list(map(int, input().split())))
    a = 0
    for i in range(x+1, y+1):
        if X[-1]<i and i <= Y[0] and x < i and i <= y:
            print('No War')
            break
        else :
            a += 1
    if a == y-x:
        print('War')
Buggy python submission (verdict: Runtime Error):
#abc104c All Green
#全てとかない問いは一種類以下になるので、各問いに対し解く/解かないを決める

def dfs(i, sum, count, rest):
    global ans
    if i == d:
        if sum < g:
            rest_max = max(rest)
            n = min(l[rest_max-1][0], -(-(g-sum)//(rest_max*100)))
            count += n
            sum += n * rest_max * 100
        if sum >= g:
            ans = min(ans, count)
    else :
        dfs(i+1, sum, count, rest) #二分岐のうち解かない選択
        dfs(i+1, sum + l[i][0]*(i+1)*100, count + l[i][0], rest - {i+1}) #解く選択

d, g = map(int, input().split())
l = [list((map(int, input().split()))) for _ in range(d)]
ans = float("inf")
dfs(0, 0, 0, set(range(1, d+1)))
print(ans)



Fixed submission:
#abc104c all green 
d, g = map(int, input().split())
pc = [list(map(int, input().split())) for _ in range(d)]
all_sum = []
for bit in range(1<<d):
    s = 0
    a = 0
    rest = []
    for i in range(d):
        if bit & (1<<i):
            s += pc[i][0]*100*(i+1) + pc[i][1]
            a += pc[i][0]
        else :
            rest.append(i)
    
    if s >= g :
        all_sum.append(a)
    else :
        k = max(rest)
        diff = g - s
        for j in range(pc[k][0]):
            if j*(k+1)*100 >= diff:
                all_sum.append(a+j)
print(min(all_sum))
Buggy python submission (verdict: Runtime Error):
a, b, k = [int(x) for x in input().split()]
if a + k - 1 < b - k + 1:
    l = list(range(a, a + k)) + list(range(b - k + 1, b + 1))
else:
    lcnt = list(range(a, b + 1))
for ans in l:
    print(ans)


Fixed submission:
a, b, k = [int(x) for x in input().split()]
if a + k - 1 < b - k + 1:
    l = list(range(a, a + k)) + list(range(b - k + 1, b + 1))
else:
    l = list(range(a, b + 1))
for ans in l:
    print(ans)
Buggy python submission (verdict: Runtime Error):
def dfs(i, f, sum):
    if i == 3 and sum == 7:
        print(f+"=7")
        exit()
    else :
        dfs(i+1, f + '-' + s[i+1], sum - int(s[i+1]))
        dfs(i+1, f + '+' + s[i+1], sum + int(s[i+1]))
        
s = input()
dfs(0, s[0], int(s[0]))





Fixed submission:
#abc079 train ticket
s = input()
for bit in range(1<<3):
    a = int(s[0])
    func = s[0]
    for i in range(3):
        if bit & (1<<i):
            func += '+'
            a += int(s[i+1])
        else : #もし1というフラグが立っていなければ、引くというプログラム
            func += '-'
            a -= int(s[i+1])
        func += s[i+1]
    
    if a == 7:
        print(func + '=7')
        exit()
print('No')
Buggy python submission (verdict: Runtime Error):
n = int(input())
p = list(map(int, input().split()))
q = [i for i in range(1, n+1)]
cnt = 0
for j in range(n):
    if p[j] == q[j]:
        p[j], p[j+1] = p[j+1], p[j]
        cnt += 1
print(cnt)


Fixed submission:
n = int(input())
p = list(map(int, input().split()))
q = [i for i in range(1, n+1)]
cnt = 0
for j in range(n):
    if p[j] == q[j]:
        if j != n-1:
            p[j], p[j+1] = p[j+1], p[j]
            cnt += 1
        else :
            p[j], p[j-1] = p[j-1], p[j]
            cnt += 1
print(cnt)
Buggy python submission (verdict: Runtime Error):
c = input()
print("vowel" if c = "a" or c = "i" or c = "u" or c = "e" or c = "o" else "consonant")

Fixed submission:
c = input()
print("vowel" if c == "a" or c == "i" or c == "u" or c == "e" or c == "o" else "consonant")
Buggy python submission (verdict: Runtime Error):
a= set(map(int, input().split()))
print(len(set(X)))

Fixed submission:
a= set(map(int, input().split()))
print(len(a))
Buggy python submission (verdict: Runtime Error):
A = list(input())
B = list(input())
C = list(input())
s = A.pop(0)
while True:
    if A == [] and s == 'a':
        print('A')
        exit()
    if B == [] and s == 'b':
        print('B')
        exit()
    if C == [] and s == 'c':
        print('C')
        exit()
    
    if s == 'a':
        s = A.pop(0)
    if s == 'b':
        s = B.pop(0)
    if s == 'c':
        s = C.pop(0)

Fixed submission:
A = list(input())
B = list(input())
C = list(input())
s = A.pop(0)
while True:
    if A == [] and s == 'a':
        print('A')
        exit()
    elif B == [] and s == 'b':
        print('B')
        exit()
    elif C == [] and s == 'c':
        print('C')
        exit()
    if s == 'a':
        s = A.pop(0)
    elif s == 'b':
        s = B.pop(0)
    elif s == 'c':
        s = C.pop(0)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int, input().split())
    X.append(x + y)
    Y.append(x - y)
# print(X, Y)
ans = 0
for i in range(N):
    for j in range(i):
        if i == j:
            continue
        tmp = max(abs(X[i] - X[j]), abs(Y[i] - Y[j]))
        ans = max(ans, tmp)
print(ans)



Fixed submission:
N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int, input().split())
    X.append(x + y)
    Y.append(x - y)
# print(X, Y)

maxX = max(X)
maxY = max(Y)
minX = min(X)
minY = min(Y)

ans = max(maxX - minX, maxY - minY)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int, input().split())
    X.append(x + y)
    Y.append(x - y)
# print(X, Y)
ans = 0
for i in range(N):
    for j in range(i):
        if i == j:
            continue
        tmp = max(abs(X[i] - X[j]), abs(Y[i] - Y[j]))
        ans = max(ans, tmp)
print(ans)



Fixed submission:
N = int(input())
X = []
Y = []
for i in range(N):
    x, y = map(int, input().split())
    X.append(x + y)
    Y.append(x - y)
# print(X, Y)

maxX = max(X)
maxY = max(Y)
minX = min(X)
minY = min(Y)

ans = max(maxX - minX, maxY - minY)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

# import sys

# input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 7)


def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Ch -= 1
    Cw -= 1
    Dh, Dw = map(int, input().split())
    Dh -= 1
    Dw -= 1
    S = []
    S.append(["#"] * (4 + W))
    S.append(["#"] * (4 + W))
    for i in range(H):
        S.append(["#", "#"] + list(input()) + ["#", "#"])
    S.append(["#"] * (4 + W))
    S.append(["#"] * (4 + W))
    dist = [[10 ** 18] * (4 + W) for _ in range(H + 4)]
    # print(S, warp)

    def bfs(i, j):
        q = deque()
        q.append((i, j))
        dist[i][j] = 0
        while q:
            a, b = q.popleft()
            for x in range(-2, 3):
                for y in range(-2, 3):
                    if S[a + x][b + y] == ".":
                        warp = abs(x) + abs(y) != 1
                        d = dist[a][b] + int(warp)
                        if d < dist[a + x][b + y]:
                            dist[a + x][b + y] = d
                            if warp:
                                q.append((a + x, b + y))
                            else:
                                q.appendleft((a + x, b + y))
                            # print(q)

    bfs(Ch + 2, Cw + 2)
    if dist[Dh + 2][Dw + 2] == 10 ** 18:
        print(-1)
    else:
        print(dist[Dh + 2][Dw + 2])


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

# import sys

# input = sys.stdin.readline
# sys.setrecursionlimit(10 ** 7)


def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Ch -= 1
    Cw -= 1
    Dh, Dw = map(int, input().split())
    Dh -= 1
    Dw -= 1
    S = []
    S.append(["#"] * (4 + W))
    S.append(["#"] * (4 + W))
    for i in range(H):
        S.append(["#", "#"] + list(input()) + ["#", "#"])
    S.append(["#"] * (4 + W))
    S.append(["#"] * (4 + W))
    dist = [[10 ** 18] * (4 + W) for _ in range(H + 4)]
    # print(S, warp)

    def bfs(i, j):
        q = deque()
        q.append((i, j))
        dist[i][j] = 0
        while q:
            a, b = q.popleft()
            for x in range(-2, 3):
                for y in range(-2, 3):
                    if S[a + x][b + y] == ".":
                        warp = abs(x) + abs(y) != 1
                        d = dist[a][b] + int(warp)
                        if d < dist[a + x][b + y]:
                            dist[a + x][b + y] = d
                            if warp:
                                q.append((a + x, b + y))
                            else:
                                q.appendleft((a + x, b + y))
                            # print(q)

    bfs(Ch + 2, Cw + 2)
    if dist[Dh + 2][Dw + 2] == 10 ** 18:
        print(-1)
    else:
        print(dist[Dh + 2][Dw + 2])


if __name__ == "__main__":
    main()