Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import fractions

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

lcm = a[0]
for i in a:
    lcm = lcm * i / fractions.gcd(lcm, i)

ans = 0
for i in a:
    ans += (lcm-1) % i
print(int(ans))


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

ans = 0
for i in a:
    ans += i - 1
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np

n, h = map(int, input().split())
a = 0
b = []
for i in range(n):
    A, B = map(int, input().split())
    a = max(a, A)
    b.append(B)
b = np.array(b, dtype=np.int64)
b = np.sort(b[b > a])[::-1]

if b.sum() >= h:
    cnt = 0
    for i in b:
        h -= b
        cnt += 1
        if h < 0:
            print(cnt)
            exit()

print((h - b.sum() + a - 1) // a + len(b))

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

a = 0
b = []
for i in range(n):
    A, B = map(int, input().split())
    a = max(a, A)
    b.append(B)
b = sorted([i for i in b if a < i], reverse=True)

cnt = 0
for i in b:
    cnt += 1
    h -= i
    if h <= 0:
        print(cnt)
        exit()
print(cnt + (h + a - 1) // a)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
s = input()
l = list(range(10))
for i in range(len(s)):
    if i == 3:
        if s[i] != "-":
            print("No")
            exit()
    else:
        if int(s[i]) not in l:
            print("No")
            exit()
print("Yes")


Fixed submission:
a, b = map(int, input().split())
s = input()

for i in range(len(s)):
    if i == a:
        if s[a] != "-":
            print("No")
            exit()
    else:
        if not "0" <= s[i] <= "9":
            print("No")
            exit()
print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
def lu(n):
    if n == 0:
        return 2
    if n == 1:
        return 1
    n = lu(n - 1) + lu(n - 2)
    return n


print(lu(int(input())))

Fixed submission:
def lu(n):
    a, b = 2, 1
    for i in range(n):
        a, b = b, a + b
    return a
print(lu(int(input())))
Buggy python submission (verdict: Runtime Error):
a, b, c, d, e, f = map(int, input().split())
limit = 100 * e / (100 + e)
mx = -1

waters = set()
for i in range(0, f // a + 1, 100 * a):
    for j in range(0, (f - i * a) // b + 1, 100 * b):
        waters.add(i + j)

sugars = set()
for i in range(0, f // c + 1, c):
    for j in range(0, (f - c) // d + 1, d):
        sugars.add(i + j)

for i in waters:
    for j in sugars:
        if i + j > f or i + j == 0:
            break
        noudo = (100 * j / (i + j))
        if noudo <= limit:
            if noudo > mx:
                mx = noudo
                x, y = i + j, j
print(x, y)

Fixed submission:
a, b, c, d, e, f = map(int, input().split())
a, b = 100 * a, 100 * b
limit = 100 * e / (100 + e)
mx = -1

for i in range(f // a + 1):
    for j in range((f - i * a) // b + 1):
        for k in range((f - i * a - j * b) // c + 1):
            for l in range((f - i * a - j * b - k * c) // d + 1):
                water = i * a + j * b + k * c + l * d
                sugar = k * c + l * d
                if water > f or water == 0:
                    break
                if (100 * sugar / water) <= limit:
                    if (100 * sugar / water) > mx:
                        mx = (100 * sugar / water)
                        x, y = water, sugar
print(x, y)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

odd = []
even = []
for i in range(n):
    if i % 2 == 0:
        even.append(a[i])
    else:
        odd.append(a[i])

if n % 2 == 1:
    print(*even[::-1], *odd)
else:
    print(*odd[::-1], *even)

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

odd = []
even = []
for i in range(n):
    if i % 2 == 0:
        even.append(a[i])
    else:
        odd.append(a[i])

if n % 2 == 1:
    ans = even[::-1]+odd
else:
    ans = odd[::-1]+even
print(*ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()

x = n - s[::-1].find(")")
l = 0
r = 0
for i in range(x):
    if s[i] == "(":
        l += 1
    else:
        r += 1

if l < r:
    ans = "(" * (r - l) + s
print(ans + ")" * max(0, (ans.count("(") - ans.count(")"))))


Fixed submission:
n = int(input())
s = input()

tmp = 0
a = 0
for i in s:
    if i == "(":
        tmp -= 1
    else:
        tmp += 1
    a = max(a, tmp)
ans = a * "(" + s
print(ans + ")" * (ans.count("(") - ans.count(")")))
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = []
length = 100
for i in range(n):
    S = sorted(input())
    s.append(S)
    length = min(length, len(S))

ans = ""
for i in range(length):
    tmp = 0
    for j in range(n-1):
        if s[j][i] == s[j+1][i]:
            tmp += 1
    if tmp == n - 1:
        ans += s[j][i]
print(ans)

Fixed submission:
from collections import Counter
n = int(input())
s = [input() for _ in range(n)]

cnt = Counter(s[0])
for i in s[1:]:
    cnt &= Counter(i)
ans = ""
for i, v in sorted(cnt.items()):
    ans += i * v
print(ans)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())
if a == 1:
    a = 14
if: b == 1:
    b = 14
if a < b:
    print("Bob")
elif a > b:
    print("Alice")
else:
    print("Draw")

Fixed submission:
a, b = map(int, input().split())
if a == 1:
    a = 14
if b == 1:
    b = 14
if a < b:
    print("Bob")
elif a > b:
    print("Alice")
else:
    print("Draw")
Buggy python submission (verdict: Runtime Error):
x, y = map(int, input().split())
X, Y = x // abs(x), y // abs(y)

if abs(x) < abs(y):
    if X == 1 and Y == 1:
        print(abs(x - y))
    elif X == -1 and Y == -1:
        print(abs(x - y) + 2)
    else:
        print(abs(x - y) + 1)
elif abs(x) == abs(y):
    if X == Y:
        print(0)
    else:
        print(1)
else:
    if X == 1 and Y == 1:
        print(abs(x - y) + 2)
    elif X == -1 and Y == -1:
        print(abs(x - y))
    else:
        print(abs(x - y) + 1)

Fixed submission:
x, y = map(int, input().split())

if x < y:
    if x * y >= 0:
        print(y - x)
    else:
        print(abs(x + y) + 1)
else:
    if x * y <= 0:
        print(abs(x + y) + 1)
    else:
        print(x - y + 2)
Buggy python submission (verdict: Time Limit Exceeded):
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    while a[i] + a[i + 1] > x:
        if a[i + 1] != 0:
            ans += 1
            a[i + 1] -= 1
        else:
            ans += 1
            a[i] -= 1
print(ans)


Fixed submission:
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    if a[i] + a[i + 1] > x:
        ans += a[i + 1] + a[i] - x
        a[i + 1] = x - a[i]
        if a[i + 1] < 0:
            a[i + 1] = 0
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    while a[i] + a[i + 1] > x:
        if a[i + 1] == 0:
            ans += 1
            a[i] -= 1
        else:
            ans += 1
            a[i + 1] -= 1
print(ans)

Fixed submission:
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    if a[i] + a[i + 1] > x:
        ans += a[i + 1] + a[i] - x
        a[i + 1] = x - a[i]
        if a[i + 1] < 0:
            a[i + 1] = 0
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
f = 0
ans = 0
if s[0] == "B":
    ans += 1
    while s[0] == "B":
        s = s[1:]

s = s[::-1]

if s[0] == "B":
    ans += 1
    while s[0] == "B":
        s = s[1:]

print(ans+s.count("B")*2)

Fixed submission:
s = input()
ans = 0
bw = s[0]
for i in s:
    if bw == "B" and i == "W":
        ans += 1
        bw = "W"
    elif bw == "W" and i == "B":
        ans += 1
        bw = "B"
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.buffer.readline

n, m = map(int, input().split())
b = [False] * (n + 1)
c = [1] * (n + 1)
for i in range(m):
    x, y = map(int, input().split())
    c[x] -= 1
    c[y] += 1
    if b[x]:
        b[y] = True
    if c[x] == 0:
        b[x] = False
print(sum(b))

Fixed submission:
a, b = map(int, input().split())

if a > 0:
    print("Positive")
elif b < 0:
    print("Negative" if (abs(a - b)) % 2 == 0 else "Positive")
else:
    print("Zero")
Buggy python submission (verdict: Runtime Error):
import collections
sys.setrecursionlimit(10**6)
def dfs(visited,v,adj):
    visited[v]=True 
    for i in adj[v]:
        if not visited[i]:
            dfs(visited,i,adj)  
adj=collections.defaultdict(list)
n,m=map(int,input().split()) 
visited=[False]*n  
for i in range(m):
    u,v=map(int,input().split()) 
    adj[u-1].append(v-1)  
    adj[v-1].append(u-1) 
c=0 
for i in range(n):
    if not visited[i]:
        dfs(visited,i,adj) 
        c+=1 
print(c-1)

Fixed submission:
import collections
import sys
sys.setrecursionlimit(10**6)
def dfs(visited,v,adj):
    visited[v]=True 
    for i in adj[v]:
        if not visited[i]:
            dfs(visited,i,adj)  
adj=collections.defaultdict(list)
n,m=map(int,input().split()) 
visited=[False]*n  
for i in range(m):
    u,v=map(int,input().split()) 
    adj[u-1].append(v-1)  
    adj[v-1].append(u-1) 
c=0 
for i in range(n):
    if not visited[i]:
        dfs(visited,i,adj) 
        c+=1 
print(c-1)

Buggy python submission (verdict: Runtime Error):
import collections

def dfs(visited,v,adj):
    visited[v]=True 
    for i in adj[v]:
        if not visited[i]:
            dfs(visited,i,adj)  
adj=collections.defaultdict(list)
n,m=map(int,input().split()) 
visited=[False]*n  
for i in range(m):
    u,v=map(int,input().split()) 
    adj[u-1].append(v-1)  
    adj[v-1].append(u-1) 
c=0 
for i in range(n):
    if not visited[i]:
        dfs(visited,i,adj) 
        c+=1 
print(c-1)

Fixed submission:
import collections
import sys
sys.setrecursionlimit(10**6)
def dfs(visited,v,adj):
    visited[v]=True 
    for i in adj[v]:
        if not visited[i]:
            dfs(visited,i,adj)  
adj=collections.defaultdict(list)
n,m=map(int,input().split()) 
visited=[False]*n  
for i in range(m):
    u,v=map(int,input().split()) 
    adj[u-1].append(v-1)  
    adj[v-1].append(u-1) 
c=0 
for i in range(n):
    if not visited[i]:
        dfs(visited,i,adj) 
        c+=1 
print(c-1)
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
def solve(rr,gg,bb):
    d1,d2=defaultdict(list),defaultdict(list)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(rr)):
            jj=rr[j]
            if ii>jj:
                d1[ii].append(ii-jj)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(bb)):
            jj=bb[j]
            if ii<jj:
                d2[ii].append(jj-ii)
    s=0
    #print(d1,d2)
    for x in d1:
        for y in d1[x]:
            for z in d2[x]:
                if y!=z:
                    s+=1
    return s
        
n=int(input())
s=input()
l=list(s)
rr,gg,bb=[],[],[]
for i in range(len(s)):
    ii=s[i]
    if ii=='R':rr.append(i)
    if ii=="G":gg.append(i)
    if ii=="B":bb.append(i)
#print(rr,gg,bb)
print(solve(rr,gg,bb)+solve(rr,bb,gg)+solve(bb,rr,gg)+solve(bb,gg,rr)+solve(gg,rr,bb)+solve(gg,bb,rr))


Fixed submission:
n=int(input())
s=0
s1,s2=0,0
for i in range(1,n+1):
    if i%3==0:
        s1+=i 
    elif i%5==0:
        s2+=i 
print((n*(n+1))//2-s1-s2)
Buggy python submission (verdict: Runtime Error):
from math import gcd
a,b=map(int,input().split())
if a%b==0:
    print(0)
else:
    print(gcd(a,b))

Fixed submission:
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
a,b=map(int,input().split())
if a==0:
    print(0)
else:
    if a%b==0:
        print(0)
    else:
        x=a%b 
        while True:
            if x>abs(x-b):
                x=abs(x-b)
            else:
                break 
        print(x)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
k = int(input())

str = ""
for i in k:
  str += "ALC"
 
print(str)


Fixed submission:
# -*- coding: utf-8 -*-
k = input()

str = ""
for i in range(int(k)):
  str += "ACL"
 
print(str)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
k = int(input())

for i in k:
  str += "ALC"
 
print(str)

Fixed submission:
# -*- coding: utf-8 -*-
k = input()

str = ""
for i in range(int(k)):
  str += "ACL"
 
print(str)
Buggy python submission (verdict: Runtime Error):
a,b=map(float,input().split())
print(trunc(a*b))

Fixed submission:
import decimal
a,b=input().split()
a=int(a)
b=decimal.Decimal(b)
print(int(a*b))

Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
print(int(a*b))

Fixed submission:
import decimal
a,b=input().split()
a=int(a)
b=decimal.Decimal(b)
print(int(a*b))
Buggy python submission (verdict: Runtime Error):
wt = str(input())
if wt == 'SSS':
    print(0)
if wt == "RRR":
    print(3)
else:
    id = wt.index('R')
    if wt.count('R') == 2 and wt[id] == wt[id+1] == 'R':
        print(2)
    else:
        print(1)

Fixed submission:
wt = str(input())
if wt == 'SSS':
    print(0)
elif wt == "RRR":
    print(3)
else:
    id = wt.index('R')
    if wt.count('R') == 2 and wt[id] == wt[id+1] == 'R':
        print(2)
    else:
        print(1)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
thres = 10**18
res = A[0]
for i in range(N-1):
    res *= A[i+1]
    if res > thres and 0 not in A:
        res = -1
        break
print(res)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
thres = 10**18
res = A[0]
if 0 in A:
    print(0)
    exit()
for i in range(N-1):
    res *= A[i+1]
    if res > thres:
        res = -1
        break
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
thres = 10**18
res = A[0]
for i in range(N-1):
   res *= A[i+1]
if res > thres:
    res = -1
print(res)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
thres = 10**18
res = A[0]
if 0 in A:
    print(0)
    exit()
for i in range(N-1):
    res *= A[i+1]
    if res > thres:
        res = -1
        break
print(res)
Buggy python submission (verdict: Runtime Error):
import math
A, B = map(eval,input().split())
ans = A*B
print(math.tr

Fixed submission:
from decimal import Decimal

a, b  = map(float, input().split())
a = Decimal(str(a))
b = Decimal(str(b))

print(int(a*b))
Buggy python submission (verdict: Runtime Error):

A, B, C = map(int,input().split())
if A == B == C:
    print('No')
elif B == C:
    print('Yes')
elif A == C:
    print('Yes')
elif A == B:
    print('Yes')
else:
    print('No'

Fixed submission:
A, B, C = map(int,input().split())
if A ==B and B == C:
    print('No')
elif B == C:
    print('Yes')
elif A == C:
    print('Yes')
elif A == B:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
 N = int(input())
    A = list(map(int, input().split(' ')))

    flag = True
    for i in range(N):
        if A[i] % 2 == 0:
            if A[i] % 3 == 0 or A[i] % 5 == 0:
                pass
            else:
                flag = False

    if flag:
        print("APPROVED")
    else:
        print("DENIED")

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
even_num = []
for i in A:
    if i % 2 == 0:
        even_num.append(i)
for j in range(len(even_num)):
    if even_num[j] % 3 != 0 and even_num[j] % 5 != 0:
        print('DENIED')
        exit()
print('APPROVED')

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
for i in A:
    if i % 2 == 0:
        even_num.append(i)
for j in range(len(even_num)):
    if even_num[j] % 3 != 0 and even_num[j] % 5 != 0:
        print('DENIED')
        exit()
print('APPROVED')

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
even_num = []
for i in A:
    if i % 2 == 0:
        even_num.append(i)
for j in range(len(even_num)):
    if even_num[j] % 3 != 0 and even_num[j] % 5 != 0:
        print('DENIED')
        exit()
print('APPROVED')
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
kkn = [list(map(int, input().split())) for _ in range(k)]
MOD = 998244353

dp = [0]*(n+1)
dp[1] = 1
dpac = [0]*(n+1)
dpac[1] = 1
for i in range(2, n+1):
    for kl, kr in kkn:
        l = i-kr
        r = i-kl
        if r < 0: continue
        l = max(l, 0)
        dp[i] += dpac[r] - dpac[l-1]
    dpac[i] = dpac[i-1] + dp[i]
print(dp[n]%MOD)

Fixed submission:
n, k = map(int, input().split())
kkn = [list(map(int, input().split())) for _ in range(k)]
MOD = 998244353

dp = [0]*(n+1)
dp[1] = 1
dpac = [0]*(n+1)
dpac[1] = 1
for i in range(2, n+1):
    for kl, kr in kkn:
        l = i-kr
        r = i-kl
        if r < 0: continue
        l = max(l, 0)
        dp[i] += dpac[r] - dpac[l-1]
        dp[i] %= MOD
    dpac[i] = dpac[i-1] + dp[i]
    dpac[i] %= MOD
print(dp[n])
Buggy python submission (verdict: Time Limit Exceeded):
n, x, m = map(int, input().split())
amr = []
for i in range(n):
    if i == 0:
        x %= m
        amr.append(x)
        continue
    x = pow(x, 2, m)

    if x in amr:
        break
    if x == 0:
        break
    amr.append(x)

if x == 0:
    print(sum(amr[:n]))
else:
    idx = amr.index(x)

    loop = sum(amr[idx:])
    l = len(amr[idx:])

    if n >= idx:
        cnt = (n-idx)//l
        print(cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:]))
    else:
        print(sum(amr[:n]))



Fixed submission:
n, x, m = map(int, input().split())
amr = []
for i in range(n):
    if i == 0:
        x %= m
        amr.append(x)
        continue
    x = pow(x, 2, m)

    if x in amr:
        break
    if x == 0:
        break
    amr.append(x)

if x == 0:
    print(sum(amr[:n]))
else:
    idx = amr.index(x)

    loop = sum(amr[idx:])
    l = len(amr[idx:])

    if n >= idx:
        cnt = (n-idx)//l
        print(cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:]))
    else:
        print(sum(amr[:n]))
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from math import gcd 

n = int(input())
A = list(map(int, input().split()))
pr = [True]*(10**6+5)

def prime_fact(x):
    memo = [0]*(x+1)
    primes = []
    if x <= 1: return False
    for i in range(2, x+1):
        if memo[i]:
            continue
        primes.append(i)
        memo[i] = i
        for j in range(i*i, x+1, i):
            if memo[j]:
                continue
            memo[j] = i
    a = x
    b = []
    while a > 1:
        b.append(memo[a])
        a //= memo[a]
    return b


for a in A:
    if a == 1: continue
    for p in set(prime_fact(a)):
        if not pr[p]:
            break
        pr[p] = False
    else:
        continue
    break
else:
    print('pairwise coprime')
    exit()

if reduce(gcd, A) == 1:
    print('setwise coprime')
else:
    print('not coprime')

Fixed submission:
from functools import reduce
from math import gcd 

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

def furui(x):
    memo = [0]*(x+1)
    primes = []
    for i in range(2, x+1):
        if memo[i]: continue
        primes.append(i)
        memo[i] = i
        for j in range(i*i, x+1, i):
            if memo[j]: continue
            memo[j] = i
    return memo, primes

memo, primes = furui(10**6+5)

pr = [True]*(10**6+5)
for a in A:
    if a == 1: continue
    while a != 1:
        w = memo[a]
        if not pr[w]:
            break
        pr[w] = False
        while a%w == 0:
            a = a // w
    else:
        continue
    break
else:
    print('pairwise coprime')
    exit()

if reduce(gcd, A) == 1:
    print('setwise coprime')
else:
    print('not coprime')

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

n = int(input())
A = list(map(int, input().split()))
pr = [True]*(10**6+5)
for a in A:
    vals = [i for i in range(2, a) if a % i == 0]
    for v in vals:
        for i in range(v, 10**6+2, v):
            if not pr[i]:
                break
            pr[i] = False
        else:
            continue
        break
    else:
        continue
    break
else:
    print('pairwise coprime')
    exit()

if reduce(gcd, A) == 1:
    print('setwise coprime')
else:
    print('not coprime')


Fixed submission:
from functools import reduce
from math import gcd 

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

def furui(x):
    memo = [0]*(x+1)
    primes = []
    for i in range(2, x+1):
        if memo[i]: continue
        primes.append(i)
        memo[i] = i
        for j in range(i*i, x+1, i):
            if memo[j]: continue
            memo[j] = i
    return memo, primes

memo, primes = furui(10**6+5)

pr = [True]*(10**6+5)
for a in A:
    if a == 1: continue
    while a != 1:
        w = memo[a]
        if not pr[w]:
            break
        pr[w] = False
        while a%w == 0:
            a = a // w
    else:
        continue
    break
else:
    print('pairwise coprime')
    exit()

if reduce(gcd, A) == 1:
    print('setwise coprime')
else:
    print('not coprime')
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
ch, cw, dh, dw = ch-1, cw-1, dh-1, dw-1
s = [list(input()) for _ in range(h)]

def bfs(sy, sx):
    que = deque([])
    que.append([sy, sx])

    while que:
        y, x = que.popleft()
        dx, dy = [0,1,0,-1], [1,0,-1,0]
        for i in range(4):
            ny, nx = y+dy[i], x+dx[i]
            if ny < 0 or ny >= h or nx < 0 or nx >= w: continue
            if s[ny][nx] == '#': continue
            if seen[ny][nx] != -1: continue
            
            seen[ny][nx] = seen[y][x]
            que.append([ny, nx])
            wp.append([ny, nx])

            
dq = deque([])
dq.append([ch, cw])
seen = [[-1]*w for _ in range(h)]
seen[ch][cw] = 0
wp = deque([])
while dq:
    while dq:
        y, x = dq.popleft()
        wp.append([y, x])
        bfs(y, x)

    ok = False
    while wp:
        y, x = wp.popleft()
        for dy in range(-2, 3):
            for dx in range(-2, 3):
                if dy == 0 and dx == 0: continue
                ny, nx = y+dy, x+dx
                if ny < 0 or ny >= h or nx < 0 or nx >= w: continue
                if s[ny][nx] == '#': continue
                if seen[ny][nx] >= 0: continue

                ok = True
                seen[ny][nx] = seen[y][x] + 1
                dq.append([ny, nx])

    if not ok: break

if seen[dh][dw] >= 0:
    print(seen[dh][dw])
else:
    print(-1)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
ch, cw = ch-1, cw-1
dh, dw = map(int, input().split())
dh, dw = dh-1, dw-1
s = [list(input()) for _ in range(h)]

def bfs(sy, sx):
    que = deque([])
    que.append([sy, sx])

    while que:
        y, x = que.popleft()
        dx = [0,1,0,-1]
        dy = [1,0,-1,0]
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if ny < 0 or ny >= h or nx < 0 or nx >= w: continue
            if s[ny][nx] == '#': continue
            if seen[ny][nx] != -1: continue
            
            seen[ny][nx] = cnt[0]
            que.append([ny, nx])
            wp.append([ny, nx])

dq = deque([])
dq.append([ch, cw])
seen = [[-1]*w for _ in range(h)]
seen[ch][cw] = 0
cnt = [0]
wp = deque([])
ok = False

while dq:
    while dq:
        y, x = dq.popleft()
        bfs(y, x)
        wp.append([y, x])    

    ok = False
    while wp:
        y, x = wp.popleft()
        for dy in range(-2, 3):
            for dx in range(-2, 3):
                if dy == 0 and dx == 0: continue
                ny = y+dy
                nx = x+dx
                if ny < 0 or ny >= h or nx < 0 or nx >= w: continue
                if s[ny][nx] == '#': continue
                if seen[ny][nx] == -1:
                    ok = True
                    seen[ny][nx] = (cnt[0]+1)
                    dq.append([ny, nx])
    if ok:
        cnt[0] += 1

if seen[dh][dw] >= 0:
    print(seen[dh][dw])
else:
    print(-1)