Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
b1 = 0
b2 = 0
b3 = 0
s = 0
s1 = 0
s2 = 0
t = 0
t1 = 0
u = 0
if b>=10000:
	s = b // 10000
	b1 = b - s*10000
    if 5000<=b1<10000:
        b2 = b1 - 5000
        s1 = b2 // 1000
        if (s + 1 + s1) <= a:
            print(s,1,s1)
        else:
            print(-1,-1,-1)
elif 5000<=b<10000:
    t = b // 5000
    b3 = b - t*5000
    t1 = b3 // 1000
    if (t + t1) <= a:
        print(0,t,t1)
    else:
        print(-1,-1,-1)
elif b<5000:
    u = b // 1000
    if u <= a:
        print(0,0,u)
    else:
        print(-1,-1,-1)
    


Fixed submission:
N,Y = map(int,input().split())
c = 0
for i in range(0,N+1):
    for j in range(0,N+1):
        c = ((Y-(10000*i+5000*j))/1000)
        if c.is_integer and c >= 0 and i+j+c == N:
            d = int(c)
            print(i,j,d)
            exit()
print(-1,-1,-1)

Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
b1 = 0
b2 = 0
s = 0
s1 = 0
s2 = 0
t = 0
t1 = 0
u = 0
if b>=10000:
	s = b // 10000
	b1 = b - s*10000
    if 5000<=b1<10000:
        b2 = b1 - 5000
        s1 = b2 // 1000
        if (s + 1 + s1) <= a:
            print(s,1,s1)
        else:
            print(-1,-1,-1)
elif 5000<=b<10000:
    t = b // 5000
    b1 = b - t*5000
    t1 = b1 // 1000
    if (t + t1) <= a:
        print(0,t,t1)
    else:
      print(-1,-1,-1)
elif b<5000:
    u = b // 1000
    if u <= a:
        print(0,0,u)
    else:
        print(-1,-1,-1)
    


Fixed submission:
N,Y = map(int,input().split())
c = 0
for i in range(0,N+1):
    for j in range(0,N+1):
        c = ((Y-(10000*i+5000*j))/1000)
        if c.is_integer and c >= 0 and i+j+c == N:
            d = int(c)
            print(i,j,d)
            exit()
print(-1,-1,-1)

Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
b1 = 0
b2 = 0
s = 0
s1 = 0
s2 = 0
t = 0
t1 = 0
u = 0
if b>=10000:
	s = b / 10000
	b1 = b - s*10000
    if 5000<=b1<10000:
        b2 = b1 - 5000
        s1 = b2 / 1000
        if (s + 1 + s1) <= a:
            print(s,1,s1)
        else:
            print(-1,-1,-1)
elif 5000<=b<10000:
    t = b / 5000
    b1 = b - t*5000
    t1 = b1 / 1000
    if (t + t1) <= a:
        print(0,t,t1)
    else:
      print(-1,-1,-1)
elif b<5000:
    u = b / 1000
    if u <= a:
        print(0,0,u)
    else:
        print(-1,-1,-1)
    

Fixed submission:
N,Y = map(int,input().split())
c = 0
for i in range(0,N+1):
    for j in range(0,N+1):
        c = ((Y-(10000*i+5000*j))/1000)
        if c.is_integer and c >= 0 and i+j+c == N:
            d = int(c)
            print(i,j,d)
            exit()
print(-1,-1,-1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
s = 0
while N//10 > 0
    a = N % 10
    s += a
    N = N // 10
if N % s == 0:
    print("Yes")
else:
    print("No")

Fixed submission:
N = int(input())
n = N
s = 0
for i in range(10):
    a = n % 10
    s += a
    if n // 10 == 0:
        break
    else:
        n = n // 10
if N % s == 0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
data = list(map(int, input()))

_sum = sum(data)

print( 15-_sum )

Fixed submission:
data = list(map(int, input().split()))

_sum = sum(data)

print( 15-_sum )
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
p.sort()
for i in range(k) :
    s += p[i]
print(s)

Fixed submission:
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
p.sort()
s = 0
for i in range(k) :
    s += p[i]
print(s)

Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
p.sort()
s = p[0]+p[1]+p[2]
print(s)


Fixed submission:
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
p.sort()
s = 0
for i in range(k) :
    s += p[i]
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
ans = []

combi = list(itertools.combinations(p,k))
for i in range(len(combi)) :
    ans.append(sum(combi[i]))
print(min(ans))

Fixed submission:
n,k = map(int,input().split())
p = [int(i) for i in input().split()]
p.sort()
s = 0
for i in range(k) :
    s += p[i]
print(s)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
j = 0
for i in range(1,len(A)+1) :
    ans = A.count(i)
    print(ans)
    A = [n for n in A if n != i]
    if len(A) == 0 :
        j = i
        break
for i in range(N-j) :
    print('0')

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
C = [0] * N
for i in range(len(A)) :
    x = A[i]
    C[x-1] += 1
for i in range(N) :
    print(C[i])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
a = 0
for i in range(1,N+1) :
    ans = A.count(i)
    print(ans)
    a += ans
    if a >= len(A) :
        for j in range(N-i) :
            print('0')
        exit()

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
C = [0] * N
for i in range(len(A)) :
    x = A[i]
    C[x-1] += 1
for i in range(N) :
    print(C[i])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
for i in range(1,N+1) :
    ans = A.count(i)
    print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
C = [0] * N
for i in range(len(A)) :
    x = A[i]
    C[x-1] += 1
for i in range(N) :
    print(C[i])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
for i in range(1,N+1) :
    ans = A.count(i)
    print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
C = [0] * N
for i in range(len(A)) :
    x = A[i]
    C[x-1] += 1
for i in range(N) :
    print(C[i])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
for i in range(1,N+1) :
    ans = A.count(i)
    print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
C = [0] * N
for i in range(len(A)) :
    x = A[i]
    C[x-1] += 1
for i in range(N) :
    print(C[i])
Buggy python submission (verdict: Time Limit Exceeded):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())
sum = K ** 2
for a in range(2,K+1) :
    for b in range(1,K+1) :
        for c in range(1,K+1) :
            sum += gcd(a, b, c)

print(sum)

Fixed submission:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())
sum = K ** 2
for a in range(2,K+1) :
    for b in range(1,K+1) :
        if gcd(a,b) == 1 :
            sum += K
        else :
            for c in range(1,K+1) :            
                sum += gcd(a, b, c)
            
print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())
sum = 0
for a in range(1,K+1) :
    for b in range(1,K+1) :
        for c in range(1,K+1) :
            sum += gcd(a, b, c)

print(sum)

Fixed submission:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

K = int(input())
sum = K ** 2
for a in range(2,K+1) :
    for b in range(1,K+1) :
        if gcd(a,b) == 1 :
            sum += K
        else :
            for c in range(1,K+1) :            
                sum += gcd(a, b, c)
            
print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
l = []
ans = 0
for x in range(1000) :
    X = list(str(x).zfill(3))
    c = 0
    I = 0
    J = 0
    for i in range(N-2) :
        if X[0] == S[i] :
            I = i
            c += 1
            break
    for j in range(I+1,N-1) :
        if X[1] == S[j] :
            c += 1
            J = j
            break
    for k in range(J+1,N) :
        if X[2] == S[k] :
            c += 1
            break
    if c == 3 :
        ans += 1
                        
print(ans)

Fixed submission:
N = int(input())
S = list(input())
ans = 0
for x in range(1000) :
    X = list(str(x).zfill(3))
    i = 0
    j = 0
    if X[0] in S[:N-2] :
        i = S.index(X[0])
    else :
        continue
    if X[1] in S[i+1:N-1] :
        j = S[i+1:N-1].index(X[1])+i+1
    else :
        continue
    if X[2] in S[j+1:N] :
        ans += 1
                        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
l = []
for i in range(N-2) :
    for j in range(i+1,N-1) :
        for k in range(j+1,N) :
            x = int(str(S[i]) + str(S[j]) + str(S[k]))
            l.append(x)

print(len(set(l)))

Fixed submission:
N = int(input())
S = list(input())
ans = 0
for x in range(1000) :
    X = list(str(x).zfill(3))
    i = 0
    j = 0
    if X[0] in S[:N-2] :
        i = S.index(X[0])
    else :
        continue
    if X[1] in S[i+1:N-1] :
        j = S[i+1:N-1].index(X[1])+i+1
    else :
        continue
    if X[2] in S[j+1:N] :
        ans += 1
                        
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0
for i in range(n) :
    ans += i
print(ans)

Fixed submission:
n = int(input())

print(n*(n-1)//2)
Buggy python submission (verdict: Runtime Error):
N,X = map(int,input().split())
L = [int(i) for i in input().split()]
D = 0
for i in range(N+2) :
    D = L[i]+D
#    print(i,D)
    if D>X :
        print(i+1)
        exit()

print(N)

Fixed submission:
N,X = map(int,input().split())
L = [int(i) for i in input().split()]
D = 0
for i in range(N) :
    D = L[i]+D
 #   print(i,D)
    if D>X :
        print(i+1)
        exit()

print(N+1)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]

heapq.heapify(a)
for i in range(m) :
    B = b[i][0]
    C = b[i][1]
    for j in range(B) :
        if a[0] < C :
            heapq.heappop(a)
            heapq.heappush(a,C)
        else :
            break
 
print(sum(a))

Fixed submission:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]
a.sort()
b = sorted(b,key = lambda  x: x[1],reverse=True)
d = []
j = 0
while (n<=len(a) or j<m) :
    C = b[j][1]
    B = b[j][0]
    for i in range(B) :
        d.append(C)
    j += 1
    if j>=m or len(d) > len(a) :
        break

for i in range(min(len(d),len(a))) :
    if a[i] < d[i] :
        a[i] = d[i]

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]

heapq.heapify(a)
c = 0
for i in range(m) :
    B = b[i][0]
    C = b[i][1]
    for i in range(B) :
        if a[0] < C :
            heapq.heappop(a)
            c += 1
        else :
            break
    for i in range(c) :
        heapq.heappush(a,C)
    c = 0

print(sum(a))

Fixed submission:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]
a.sort()
b = sorted(b,key = lambda  x: x[1],reverse=True)
d = []
j = 0
while (n<=len(a) or j<m) :
    C = b[j][1]
    B = b[j][0]
    for i in range(B) :
        d.append(C)
    j += 1
    if j>=m or len(d) > len(a) :
        break

for i in range(min(len(d),len(a))) :
    if a[i] < d[i] :
        a[i] = d[i]

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]

heapq.heapify(a)
c = 0
for i in range(m) :
    B = b[i][0]
    C = b[i][1]
    for i in range(B) :
        if a[0] < C :
            heapq.heappop(a)
            c += 1
    for i in range(c) :
        heapq.heappush(a,C)
    c = 0

print(sum(a))

Fixed submission:
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [[int(i) for i in input().split()] for _ in range(m)]
a.sort()
b = sorted(b,key = lambda  x: x[1],reverse=True)
d = []
j = 0
while (n<=len(a) or j<m) :
    C = b[j][1]
    B = b[j][0]
    for i in range(B) :
        d.append(C)
    j += 1
    if j>=m or len(d) > len(a) :
        break

for i in range(min(len(d),len(a))) :
    if a[i] < d[i] :
        a[i] = d[i]

print(sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
c = 0
while (1) :
    for i in range(len(S)-1) :
        if S[i] == 'B' and S[i+1] == 'W' :
            S[i] = 'W'
            S[i+1] = 'B'
            c += 1
            break
        elif i == len(S)-2 :
            print(c)
            exit()

Fixed submission:
S = list(input())
c = 0
w = 0
for i in range(len(S)) :
    if S[i] == 'W' :
        c += i - w
        w += 1
print(c)
Buggy python submission (verdict: Runtime Error):
N = int(input())
l = [2,1]
if N <= 2 :
    print(l[N])
    exit()
for i in range(2,N+1) :
    l.append(l[i-2] + l[i-1])
print(l[-1])

Fixed submission:
N = int(input())
l = [2,1]
if N  == 1 :
    print(l[1])
    exit()
for i in range(2,N+1) :
    l.append(l[i-2] + l[i-1])
print(l[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import itertools as it
n = int(input())
s = [int(input()) for _ in range(n)]

ans = [0]
for i in range(1,n+1) :
    sa = list(it.combinations(s,i))
    for j in range(len(sa)) :
        if sum(sa[j])%10 != 0 :
            ans.append(sum(sa[j]))

print(max(ans))

Fixed submission:
n = int(input())
sum = 0
s = []
for i in range(n) :
    x = int(input())
    sum += x
    if x%10 != 0 :
        s.append(x)


if sum %10 != 0 :
    print(sum)
    exit()

while len(s)>0 :
    m = min(s)
    sum -= m
    if sum%10 != 0 :
        print(sum)
        exit()

print(0)
Buggy python submission (verdict: Runtime Error):
a,b,x = map(int,input().split())
fb = b//x+1
if a!= 0 :
    fa = (a-1)//x+1
else :
    0
print(fb-fa)

Fixed submission:
a,b,x = map(int,input().split())
fb = b//x+1
if a!= 0 :
    fa = (a-1)//x+1
else :
    fa = 0
print(fb-fa)
Buggy python submission (verdict: Runtime Error):
or i in range(N) :
    if (S[i] == 'a') and (c < A + B) :
        print('Yes')
        c += 1
    elif S[i] == 'b' and (c < A + B) and b < B:
        print('Yes')
        b += 1
        c += 1
    else :
        print('No')

Fixed submission:
N, A, B = map(int,input().split())
S = list(input())
b = 0
c = 0

for i in range(N) :
    if (S[i] == 'a') and (c < A + B) :
        print('Yes')
        c += 1
    elif S[i] == 'b' and (c < A + B) and b < B:
        print('Yes')
        b += 1
        c += 1
    else :
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list = list(map(int, input().split()))

ans=0

for i in range(n-1):
    for j in range(i+1,n):
        ans+=list[i]*list[j]

print(ans%1000000007)

Fixed submission:
n = int(input())
list = list(map(int, input().split()))

ans=0
wa=0

for i in range(n-2,-1,-1):
    wa+=list[i+1]
    ans+=list[i]*wa

print(ans%1000000007)
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [list(map(int, input().split())) for i in range(q)]

list=[0]*100000
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum+=(bc[1]-bc[0])*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [list(map(int, input().split())) for i in range(q)]

list=[0]*100001
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum+=(bc[1]-bc[0])*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [list(map(int, input().split())) for i in range(q)]

list=[0]*100000
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum-=bc[0]*x
    sum+=bc[1]*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

Fixed submission:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [list(map(int, input().split())) for i in range(q)]

list=[0]*100001
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum+=(bc[1]-bc[0])*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list = list(map(int, input().split()))

if 0 in list:
    ans=0
else:
    ans=1
    for i in range(n):
        ans=ans*list[i]

    if ans>10**18:
        ans=-1

print(ans)

Fixed submission:
n = int(input())
list = list(map(int, input().split()))

if 0 in list:
    ans=0
else:
    ans=1
    for i in range(n):
        ans=ans*list[i]
        if ans>10**18:
            ans=-1
            break

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list = list(map(int, input().split()))

ans=1
for i in range(n):
    ans=ans*list[i]

if ans>10**18:
    ans=-1

print(ans)

Fixed submission:
n = int(input())
list = list(map(int, input().split()))

if 0 in list:
    ans=0
else:
    ans=1
    for i in range(n):
        ans=ans*list[i]
        if ans>10**18:
            ans=-1
            break

print(ans)
Buggy python submission (verdict: Runtime Error):
import math

a, b = input().split()
A = int(a)
B = float(b)

print(int(ath.floor(A*B)))

Fixed submission:
a, b = input().split()
A = int(a)
B = float(b)
B100 = round(100*B)

C=A*B100
print(str(C//100))
Buggy python submission (verdict: Runtime Error):
import numpy as np

n, m = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(m)]

root = np.zeros((n,n), dtype = int)

for ab in AB:
    root[ab[0]-1][ab[1]-1]=1
    root[ab[1]-1][ab[0]-1]=1

sign = [0]*n
sign[0]=-1
list = [0]

while True:
    x = list.pop(0)

    for y, bool in enumerate(root[x]):
        if sign[y]==0 and bool==1:
            sign[y]=x+1
            list.append(y)

    if len(list)==0:
        break

print('Yes')
for p in sign[1:]:
    print(p)

Fixed submission:
import numpy as np

n, m = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(m)]

root = []
for i in range(n):
    root.append(set())

for ab in AB:
    root[ab[0]-1].add(ab[1]-1)
    root[ab[1]-1].add(ab[0]-1)

sign = [0]*n
sign[0]=-1
list = [0]

while True:
    x = list.pop(0)

    for next in root[x]:
        if sign[next]==0:
            sign[next]=x+1
            list.append(next)

    if len(list)==0:
        break

print('Yes')
for p in sign[1:]:
    print(p)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

ans=0
for i in range(M,N+2): #M~N+1までやる
    min=sum(list(range(0,i)))
    max=sum(list(range(N+1-i,N+1)))

    ans+=max-min+1

print(ans)

Fixed submission:
N, M = map(int, input().split())

list=[]
a=1
for i in range(N+2):
    if i==0 or i==N+1:
        list.append(1)
    else:
        list.append(N*i+a)
        a-=2*i

ans=0
for i in range(M,N+2):
    ans+=list[i]

print(ans%1000000007)
Buggy python submission (verdict: Time Limit Exceeded):
def gcd(x1,x2):
    r=0
    while True:
        if x1==x2:
            r=x1
            break
        elif x1<x2:
            x2=x2-x1
        else:
            x1=x1-x2
    return r

x = int(input())

ans=0

for a in range(1,x+1):
    for b in range(1,a+1):
        for c in range(1,b+1):
            gcd1=gcd(a,b)
            gcd2=gcd(gcd1,c)
            if a==b and b==c:
                ans += gcd2
            elif a==b or b==c or c==a:
                ans += 3*gcd2
            else:
                ans += 6*gcd2

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)

def gcd(p,q): #p<q
    if q%p==0:
        return p
    return gcd(q%p,p)

x = int(input())

ans=0

for a in range(1,x+1):
    for b in range(1,a+1):
        for c in range(1,b+1):
            gcd1=gcd(a,b)
            gcd2=gcd(gcd1,c)
            if a==b and b==c:
                ans += gcd2
            elif a==b or b==c or c==a:
                ans += 3*gcd2
            else:
                ans += 6*gcd2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

x = int(input())

ans=0

for a in range(1,x+1):
    for b in range(a,x+1):
        for c in range(b,x+1):
            if a==b and b==c:
                ans += np.gcd.reduce([a,b,c])
            elif a==b or b==c or c==a:
                ans += 3*np.gcd.reduce([a,b,c])
            else:
                ans += 6*np.gcd.reduce([a,b,c])

print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)

def gcd(p,q): #p<q
    if q%p==0:
        return p
    return gcd(q%p,p)

x = int(input())

ans=0

for a in range(1,x+1):
    for b in range(1,a+1):
        for c in range(1,b+1):
            gcd1=gcd(a,b)
            gcd2=gcd(gcd1,c)
            if a==b and b==c:
                ans += gcd2
            elif a==b or b==c or c==a:
                ans += 3*gcd2
            else:
                ans += 6*gcd2

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H,W=map(int,input().split())

array_st=list(map(int,input().split()))
array_gl=list(map(int,input().split()))
start_point=[]
goal_point=[]
for i in range(2):
    start_point.append(array_st[i]-1)
    goal_point.append(array_gl[i]-1)

matrix=[]
for i in range(H):
    matrix.append(list(input()))

d=deque()
d.append([start_point[0],start_point[1],0])

road=[-1,1]

ans=-1
while d:
    v=d.popleft()
    matrix[v[0]][v[1]]="x"
    if [v[0],v[1]]==goal_point:
        ans=v[2]
        break
    for i in road:
        if v[0]+i>=0 and v[0]+i<H:
            if matrix[v[0]+i][v[1]]==".":
                d.appendleft([v[0]+i,v[1],v[2]])
        if v[1]+i>=0 and v[1]+i<W:
            if matrix[v[0]][v[1]+i]==".":
                d.appendleft([v[0],v[1]+i,v[2]])

    for i in range(-2,3):
        for j in range(-2,3):
            if v[0]+i>=0 and v[0]+i<H:
                if v[1]+j>=0 and v[1]+j<W:
                    if matrix[v[0]+i][v[1]+j]==".":
                        d.append([v[0]+i,v[1]+j,v[2]+1])

print(ans)

Fixed submission:
from collections import deque

H,W=map(int,input().split())

array_st=list(map(int,input().split()))
array_gl=list(map(int,input().split()))
start_point=[]
goal_point=[]
for i in range(2):
    start_point.append(array_st[i]-1)
    goal_point.append(array_gl[i]-1)

matrix=[]
for i in range(H):
    matrix.append(list(input()))

d=deque()
d.append([start_point[0],start_point[1],0])

road=[-1,1]

ans=-1
while d:
    v=d.popleft()
    if matrix[v[0]][v[1]]=="x":
        continue
    matrix[v[0]][v[1]]="x"
    if [v[0],v[1]]==goal_point:
        ans=v[2]
        break
    for i in road:
        if v[0]+i>=0 and v[0]+i<H:
            if matrix[v[0]+i][v[1]]==".":
                d.appendleft([v[0]+i,v[1],v[2]])
        if v[1]+i>=0 and v[1]+i<W:
            if matrix[v[0]][v[1]+i]==".":
                d.appendleft([v[0],v[1]+i,v[2]])

    for i in range(-2,3):
        for j in range(-2,3):
            if v[0]+i>=0 and v[0]+i<H:
                if v[1]+j>=0 and v[1]+j<W:
                    if matrix[v[0]+i][v[1]+j]==".":
                        d.append([v[0]+i,v[1]+j,v[2]+1])

print(ans)