Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03260 (python)

Fixed submission:
a,b = map(int,input().split())
if a % 2 == 0 or b % 2 == 0: print('No')
else: print('Yes')

Buggy submission (verdict: Runtime Error):
n = int(input())
w = []
for i in range(n):
    w.append(input())
ok = True
for i in range(n-1):
    sz = len(w[i])
    if w[i][sz-1] != w[i+1][0]: ok = False
for i in range(n):
    for j in range(n):
        if i != j and w[i] == w[j]:
            ok = False
if ok: print('Yes')
else: print('No')
Problem p03337 (python)

Fixed submission:
a,b = map(int,input().split())
print(max(a+b,max(a-b,a*b)))

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
print(min({a+b,a-b,a*b})
Problem p03469 (python)

Fixed submission:
a,b,c = input().split('/')
n = int(a)
n += 1
print("{}/{}/{}".format(n,b,c))

Buggy submission (verdict: Runtime Error):
s = input()
s[3] = '8'
print(s)
Problem p04012 (python)

Fixed submission:
s = input()
alphabets = [0] * 26
for i in range(len(s)):
  a = ord(s[i])
  alphabets[a-97] += 1
flag = True
for x in alphabets:
  if x % 2 != 0:
    flag = False
if flag:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
s = input()
alphabets = [0] * 26
for i in range(len(s)):
    x = ord(s[i])
    alphabets[x-97] += 1
flag = True
for x in alphabets:
    if x % 2 != 0:
        ok = False
if ok:
    print('Yes')
else:
    print('No')
Problem p02641 (python)

Fixed submission:
x, n = map(int, input().split())
P = list(map(int, input().split()))
if not P:
    print(x)
    exit(0)
for i in range(x, 102):
    if i not in P:
        upper = i
        break
    else:
        upper = 102
for i in range(x, -1, -1):
    if i not in P:
        lower = i
        break
    else:
        lower = 0
print(lower if x-lower <= upper - x else upper)

Buggy submission (verdict: Runtime Error):
x, n = map(int, input().split())
P = list(map(int, input().split()))
if not P:
    print(x)
	exit(0)
for i in range(x, 102):
    if i not in P:
        upper = i
        break
    else:
        upper = 102
for i in range(x, -1, -1):
    if i not in P:
        lower = i
        break
    else:
        lower = 0
print(lower if x-lower <= upper - x else upper)
Problem p02664 (python)

Fixed submission:
def main():
    t = input()
    if t == '?':
        return 'D'
    n = len(t)
    ans = list(t)
    for i, c in enumerate(t):
        if c == '?':
            if i == 0:
                if t[i+1] == 'P':
                    s = 'D'
                else:
                    s = 'P'
            elif i == n-1:
                s = 'D'
            else:
                if ans[i-1] == 'D' and t[i+1] == 'P':
                    s = 'D'
                elif ans[i-1] == 'D' and t[i+1] == 'D':
                    s = 'P'
                elif ans[i-1] == 'P' and t[i+1] == 'P':
                    s = 'D'
                elif ans[i-1] == 'P' and t[i+1] == 'D':
                    s = 'D'
                elif ans[i-1] == 'P' and t[i+1] == '?':
                    s = 'D'
                elif ans[i-1] == 'D' and t[i+1] == '?':
                    s = 'P'
        else:
            s = c
        ans[i] = s
    return ''.join(ans)

print(main())

Buggy submission (verdict: Runtime Error):
t = input()
n = len(t)
ans = list(t)
for i, c in enumerate(t):
    if c == '?':
        if i == 0:
            if t[i+1] == 'P':
                s = 'D'
            else:
                s = 'P'
        elif i == n-1:
            s = 'D'
        else:
            if ans[i-1] == 'D' and t[i+1] == 'P':
                s = 'D'
            elif ans[i-1] == 'D' and t[i+1] == 'D':
                s = 'P'
            elif ans[i-1] == 'P' and t[i+1] == 'P':
                s = 'D'
            elif ans[i-1] == 'P' and t[i+1] == 'D':
                s = 'D'
            elif ans[i-1] == 'P' and t[i+1] == '?':
                s = 'D'
            elif ans[i-1] == 'D' and t[i+1] == '?':
                s = 'P'
    else:
        s = c
    ans[i] = s
print(''.join(ans))
Problem p02850 (python)

Fixed submission:
from collections import deque
def bfs(succs, preds, start):
    saisyoku = {}
    queue = deque([start])
    used = 0
    while queue:
        v = queue.popleft()
        for u in preds[v]:
            used = saisyoku[preds[v][u]]
        c = 1
        for w in succs[v]:
            if used and c == used:
                c += 1
            saisyoku[succs[v][w]] = c
            queue.append(w)
            c += 1
    return saisyoku
  
n = int(input())
vertices = range(1, n+1)
edges = [list(map(int, input().split())) for _ in range(n-1)]
jisu = {v: 0 for v in vertices}
succs = {v: {} for v in vertices}
preds = {v: {} for v in vertices}
for i, (u, v) in enumerate(edges):
  succs[u][v] = i
  preds[v][u] = i
  jisu[u] += 1
  jisu[v] += 1
colors = max(jisu.values())
print(colors)
saisyoku = bfs(succs, preds, 1)
for _, c in sorted(saisyoku.items()):
    print(c)

Buggy submission (verdict: Runtime Error):
from collections import deque
def bfs(succs, preds, start, colors):
    saisyoku = {}
    queue = deque([start])
    used = 0
    while queue:
        v = queue.popleft()
        for u in preds[v]:
            used = saisyoku[preds[v][u]]
        c = 1
        for w in succs[v]:
            if used and c == used:
                c += 1
            saisyoku[succs[v][w]] = c
            queue.append(w)
            c += 1
    return saisyoku
  
n = int(input())
vertices = range(1, n+1)
edges = [list(map(int, input().split())) for _ in range(n-1)]
jisu = {v: 0 for v in vertices}
succs = {v: {} for v in vertices}
preds = {v: {} for v in vertices}
for i, (u, v) in enumerate(edges):
  succs[u][v] = i
  preds[v][u] = i
  jisu[u] += 1
  jisu[v] += 1
colors = max(jisu.values())
print(colors)
saisyoku = bfs(succs, preds, 1)
for _, c in sorted(saisyoku.items()):
    print(c)
Problem p02583 (python)

Fixed submission:
import sys
import math


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
L = inintl()

ans = 0

for i in range(n):
    for j in range(n):
        for k in range(n):
            if i < j < k and (L[i]+L[j] > L[k] and L[j]+L[k] > L[i] and L[k]+L[i] > L[j]) and (L[i] != L[j] and L[i] != L[k] and L[j] != L[k]):
                ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
重複なimport sys
import math


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
L = inintl()

ans = 0

for i in range(n):
    for j in range(n):
        for k in range(n):
            if i < j < k and (L[i]+L[j] > L[k] and L[j]+L[k] > L[i] and L[k]+L[i] > L[j]) and (L[i] != L[j] and L[i] != L[k] and L[j] != L[k]):
                ans += 1

print(ans)
Problem p02732 (python)

Fixed submission:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

c = 0
cnt = [0]*n

for i in range(n):
    A[i] -= 1

for i in range(n):
    cnt[A[i]] += 1

def combinations(n):
    return n*(n-1)//2

for i in range(n):
    c += combinations(cnt[i])

for i in range(n):
    ans = c
    ans -= combinations(cnt[A[i]])
    ans += combinations(cnt[A[i]]-1)
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

c = 0
cnt = [0]*n

for i in range(n):
    cnt[A[i]] += 1

def combinations(n):
    return n*(n-1)//2

for i in range(n):
    c += combinations(cnt[i])

for i in range(n):
    ans = c
    ans -= combinations(cnt[A[i]])
    ans += combinations(cnt[A[i]]-1)
    print(ans)
Problem p02939 (python)

Fixed submission:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

s = input()

ans = 0
a = ""
b = ""

for i in range(len(s)):
    a += s[i]
    if a == b:
        continue
    ans += 1
    b = a
    a = ""

print(ans)

Buggy submission (verdict: Runtime Error):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

s = input()

ans = 0
a = 0
b = 0

for i in range(len(s)):
    a += s[i]
    if a == b:
        continue
    ans += 1
    b = a
    a = ""

print(ans)
Problem p02995 (python)

Fixed submission:
import sys
import math

inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

a, b, c, d = inintm()

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(b - (b//c + b//d) + b//lcm(c,d) - ((a-1) - ((a-1)//c + (a-1)//d) + (a-1)//lcm(c,d)))

Buggy submission (verdict: Runtime Error):
import sys

inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

a, b, c, d = inintm()

print(b-a+1 - (-(-(b-a)//c) + -(-(b-a)//d))) + 1
Problem p03103 (python)

Fixed submission:
n, m = map(int, input().split())
drink = sorted([list(map(int, input().split())) for _ in range(n)])

ans = 0

for a,b in drink:
    ans += a*min(b,m)
    m -= min(b,m)
    if m == 0:
        break

print(ans)

Buggy submission (verdict: Runtime Error):
import numba
from numba import njit
i8 = numba.int64


@njit((i8, ), cache=True)
def main(n, m):

    A = []
    B = []
    cnt = 0
    ans = 0

    for _ in range(n):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    while cnt < m:
        A_min = min(A)
        A_min_index = A.index(A_min)
        if cnt + B[A_min_index] < m:
            ans += A_min * B[A_min_index]
            cnt += B[A_min_index]
            A.pop(A_min_index)
            B.pop(A_min_index)
        else:
            for i in range(B[A_min_index]):
                if cnt == m:
                    break
                ans += A_min
                cnt += 1

    print(ans)


n, m = map(int, input().split())

main(n, m)

Buggy submission (verdict: Runtime Error):
from numba import njit


@njit('void(i8)')
def main(n, m):

    A = []
    B = []
    cnt = 0
    ans = 0

    for _ in range(n):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    while cnt < m:
        A_min = min(A)
        A_min_index = A.index(A_min)
        if cnt + B[A_min_index] < m:
            ans += A_min * B[A_min_index]
            cnt += B[A_min_index]
            A.pop(A_min_index)
            B.pop(A_min_index)
        else:
            for i in range(B[A_min_index]):
                if cnt == m:
                    break
                ans += A_min
                cnt += 1

    print(ans)


n, m = map(int, input().split())

main(n, m)
Problem p03497 (python)

Fixed submission:
from collections import Counter

n, k = map(int, input().split())
A = list(map(int, input().split()))

c = Counter(A)

print(sum(sorted(c.values())[:len(c) - k]))

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))

cnt_l = [0]*10**5
num = []
ans = 0

for i in A:
    cnt_l[i] += 1
    if i not in num:
        num.append(i)

cnt_l.sort()

cnt_l = cnt_l[10**5-len(num):]

if len(cnt_l) <= k:
    print(0)
    exit()

cnt_l = cnt_l[:k-1]

print(sum(cnt_l))
Problem p03612 (python)

Fixed submission:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
P = inintl()

ans = 0

for i in range(n-1):
    a = P[i]
    b = P[i+1]
    if a == i+1:
        P[i] = b
        P[i+1] = a
        ans += 1

if P[-1] == n:
    ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
P = inintl()

ans = 0

if n == 2:
    if P[0] == 1:
        print(1)
        exit()
    else:
        print(0)
        exit()

for i in range(n):
    if i+1 == P[i]:
        if i+2 == P[i+1]:
            continue
        else:
            ans += 1

print(ans)
Problem p02995 (python)

Fixed submission:
from fractions import gcd 

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //gcd(C,D)

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
from fractions import gcd 

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
import fractions

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
import math

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
# def gcd(n,m):
#     if m == 0:
#         return n
#     else:
#         x = max(n,m)%min(n,m)
#         while x != 0:
#             return gcd(m,x)
#         else:
#             return m

import math
import copy

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
import math
import copy

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)
# print(gcd(C,D))
# print(math.gcd(C,D))

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)

Buggy submission (verdict: Runtime Error):
def gcd(n,m):
    if m == 0:
        return n
    else:
        x = max(n,m)%min(n,m)
        while x != 0:
            return gcd(m,x)
        else:
            return m

import math
import copy

l = list(map(int,input().split()))
A = l[0]
B = l[1]
C = l[2]
D = l[3]

E = C * D //math.gcd(C,D)
# print(gcd(C,D))
# print(math.gcd(C,D))

f = B//C - (A-1)//C
f = f+(B//D - (A-1)//D)
ans = B-A+1-f + B//E-(A-1)//E
print(ans)
Problem p03029 (python)

Fixed submission:
A,P = map(int,input().split(" "))
print((A*3+P)//2)

Buggy submission (verdict: Runtime Error):
from collections import deque
import copy

N,K = map(int,input().split(" "))

V = deque(map(int,input().split(" ")))

def search(N,K,V,score,a,b):
    if a+b < min(N,K):
        Vb = copy.copy(V)
        scoreA = copy.copy(score)
        scoreB=copy.copy(score)
        scoreA.append(V.popleft())
        A = search(N,K,V,scoreA,a+1,b)
        scoreB.append(Vb.pop())
        B = search(N,K,Vb,scoreB,a,b+1)
    score = sorted(score)
    counter = 0
    while(counter < K-a-b):
        if len(score)== 0:
            break;
        if score[0]<0:
            score.pop(0)
            counter +=1
        else:
            break;
    ans = sum(score)
    if a+b < min(N,K):
        return max(0,ans,A,B)
    else:
        return max(0,ans)

score = []
a = 0
b= 0
ans = search(N,K,V,score,a,b)
print(ans)
Problem p03061 (python)

Fixed submission:
import fractions
n = int(input())
a = list(map(int,input().split(" ")))
l = [0 for i in range(n+1)]
r = [0 for i in range(n+1)]
for i in range(n):
    l[i+1] = fractions.gcd(l[i],a[i])

for i in range(n-1,-1,-1):
    r[i] = fractions.gcd(r[i+1],a[i])



m = 1
for i in range(n):
    m = max(m,fractions.gcd(l[i],r[i+1]))
print(m)

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
a = list(map(int,input().split(" ")))
b = a[0]
for i in range(n-1):
    b = math.gcd(b,a[i+1])
for i in range(n-1):
    if math.gcd(a[i],a[i+1]) == b:
        a[i] = min(a[i],a[i+1])
        a[i+1] = min(a[i],a[i+1])
        break;
b = a[0]
for i in range(n-1):
    b = math.gcd(b,a[i+1])
print(b)
Problem p03074 (python)

Fixed submission:
n,k = map(int,input().split(" "))
s = list(input())
a = [1]
for i in range(1,n):
    if s[i] != s[i-1]:
        a.append(i+1)
m = len(a)
ans = 1
i = 1
for i in range(m):
    if s[a[i]-1] == "1":
        if 2*k + i +1 < m:        
            ans = max(ans,a[2*k+i+1]-a[i])
        else:
            ans = max(ans,n+1- a[i])
    else:
        if 2*k+i< m:        
            ans = max(ans,a[2*k+i]-a[i])
        else:
            ans = max(ans,n+1-a[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split(" "))
s = list(input())

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
i = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    while (i <= m-2*k):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
        i += 1
else:
    while (i < m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
        i += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split(" "))
s = list(input())

a = [0]
tmp = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        a.append(tmp)
        tmp += 1
a.append(tmp)
m = len(a)
ans = 1
if len(a) == 2 or m-2*k < 1:
    ans = a[m-1]
elif s[0] == "1":
    for i in range(1,m-2*k+1):
        if i % 2 != 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])
else:
    for i in range(1,m-2*k):
        if i % 2 == 0:        
            ans = max(ans,a[2*k+i]-a[i-1])
        else:
            ans = max(ans,a[2*k+i-1]-a[i-1])

print(ans)
Problem p03852 (python)

Fixed submission:
c = input()
if c == 'a' or c == 'i' or c == 'u' or c == 'e' or c == 'o':
  print('vowel')
else:
  print("consonant")

Buggy submission (verdict: Runtime Error):
c = inuput()
if c == 'a' or c == 'i' or c == 'u' or c == 'e' or c == 'o':
  print('vowel')
else:
  print("consonant")

Buggy submission (verdict: Runtime Error):
c = inuput()
if c == 'a' or c == 'i' or c == 'u' or c == 'e' or c == 'o':
  print('vowel')
else:
  print("consonant"
Problem p04043 (python)

Fixed submission:
def X(A,B,C):
  if A == 5 or A == 7:
    if B == 5 or B == 7:
      if C == 5 or C == 7:
          if A + B + C == 17:
              return "YES"
  return "NO"
A,B,C = map(int, input().split())
print(X(A,B,C))

Buggy submission (verdict: Runtime Error):
def X(A,B,C):
  if A == 5 or A == 7:
    if B == 5 or B == 7:
      if C == 5 or C == 7:
          if A + B + C == 19:
              return "YES"
  return "NO"
A,B,C = int(input())
print(X(A,B,C))
Problem p03671 (python)

Fixed submission:
a,b,c=map(int,input().split())
print(a+b+c-max(a,b,c))

Buggy submission (verdict: Runtime Error):
a,b,c＝map(int,input().split())
print(a+b+c-max(a,b,c))
Problem p03262 (python)

Fixed submission:
n, X = map(int, input().split())
import numpy as np

x = np.array(list(map(int, input().split())))
x -= X

x = np.abs(x)
D = 0
from fractions import gcd

for d in x:
  D = gcd(D,d)

print(D)

Buggy submission (verdict: Runtime Error):
n, X = map(int, input().split())
import numpy as np

x = np.array(list(map(int, input().split())))
x -= X

x = np.abs(x)
D = 0
from math import gcd

for d in x:
  D = gcd(D,d)

print(D)
Problem p02579 (python)

Fixed submission:
import sys
input = sys.stdin.readline

import heapq

INF = 10 ** 10

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for end, cost in G[v]:
            if dist[end] > dist[v] + cost:
                dist[end] = dist[v] + cost
                heapq.heappush(h, (dist[end], end))
    return dist


H, W = map(int, input().split())
N = H * W
ch, cw = map(lambda x:int(x)-1, input().split())
gh, gw = map(lambda x:int(x)-1, input().split())

s = []
es = tuple([] for i in range(N))

for h in range(H):
   s.append(input().rstrip('\n'))

for h in range(H):
    for w in range(W):
        if s[h][w] != '.':
            continue
        a = h*W+w
        esappend = es[a].append
        for dh in (-2, -1, 0, 1, 2):
            for dw in (-2, -1, 0, 1, 2):
                if dh == 0 and dw == 0:
                    continue
                nh = h + dh
                nw = w + dw
                if 0 <= nh and nh < H and 0 <= nw and nw < W and s[nh][nw] == '.':
                    b = nh*W+nw
                    if (dh, dw) in [(-1,0),(1,0),(0,-1),(0,1)]:
                        esappend((b, 0))
                    else:
                        esappend((b, 1))

dist = dijkstra(N, es, ch*W+cw)
if dist[gh*W+gw] == INF:
    print(-1)
else:
    print(dist[gh*W+gw])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
gh, gw = map(int, input().split())

s = []
for i in range(h):
   s.append(input().rstrip('\n'))

INF = 10 ** 10
d = [INF] * (h*w)

stack = deque([[ch-1, cw-1, 0]])
stack_append = stack.append
stack_popleft = stack.popleft

nex = []
for i in range(-2, 3):
    for j in range(-2, 3):
        if (i, j) in [(-1,0),(1,0),(0,-1),(0,1),(0,0)]:
            continue
        else:
            nex.append((i,j))

while stack:
    curh, curw, cnt = stack_popleft()
    f = 0
    if d[curh * w + curw] <= cnt:
        continue
    else:
        d[curh * w + curw] = cnt
    for dh, dw in ((-1,0), (1,0), (0,1), (0,-1)):
        nh = curh + dh
        nw = curw + dw
        if nh >= 0 and nh < h and nw >= 0 and nw < w and s[nh][nw] == '.' and d[nh * w + nw] > cnt:
            stack_append([nh, nw, cnt])
        if s[nh][nw] == '#':
            f = 1
    if f:
        for dh, dw in nex:
            nh = curh + dh
            nw = curw + dw
            if nh >= 0 and nh < h and nw >= 0 and nw < w and s[nh][nw] == '.' and  d[nh * w + nw] > cnt:
                stack_append([nh, nw, cnt+1])

# for item in d:
#     print(item)
gh -= 1
gw -= 1

if d[gh*w + gw] == INF:
    print(-1)
else:
    print(d[gh*w + gw])