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

n=int(input())
alist=list(map(int, input().split()))
ans=1
for a in alist:
    ans=ans*a
if ans>10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

n=int(input())
alist=list(map(int, input().split()))
ans=1
for a in alist:
    ans=ans*a
    if ans>10**18:
        ans=-1
        break
if 0 in alist:
    ans=0
print(ans)
### BUGGY CODE ###

#from heapq import heappop, heappush
from numpy import argmin
n, m = map(int, input().split())
lst = [list(map(int, input().split())) for i in range(n)]
lst_f = [[] for i in range(m)]
#lst = sorted(lst, key=lambda x:x[0])
#lst = sorted(lst, key=lambda x:x[1], reverse=True)
cnt = 0
ans = 0
cntup = m
for a, b in lst:
  if a <= m:
    lst_f[a-1].append(b)
  #print(filter(lambda y:y<=m-i, map(lambda x:x[0], lst)))
  #lst2 = list(filter(lambda y:y[1]<=m-i, lst))
avb = [] 
for i in lst_f:
  for j in i:
    avb.append(-j)
  if avb != []:
    ans -= avb.pop(argmin(avb))
print(ans)


### FIXED CODE ###

from heapq import heappop, heappush
n, m = map(int, input().split())
lst = [list(map(int, input().split())) for i in range(n)]
lst_f = [[] for i in range(m)]
#lst = sorted(lst, key=lambda x:x[0])
#lst = sorted(lst, key=lambda x:x[1], reverse=True)
cnt = 0
ans = 0
cntup = m
for a, b in lst:
  if a <= m:
    lst_f[a-1].append(b)
  #print(filter(lambda y:y<=m-i, map(lambda x:x[0], lst)))
  #lst2 = list(filter(lambda y:y[1]<=m-i, lst))
avb = [] 
for i in lst_f:
  for j in i:
    heappush(avb, -j)
  if avb != []:
    ans -= heappop(avb)
print(ans)
### BUGGY CODE ###

N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ALLB = sorted(A+B)

sumt = 0
cnt = 0
summax = sum(ALLB)
if summax <= K:
    print(N+M)
    exit(0)
    
for i in range(N+M):
    sumt += ALLB.pop(0)
    if sumt > K:
        print(cnt)
        exit(0)
        
    cnt += 1



### FIXED CODE ###

import bisect
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

SumA = [0]
SumB = [0]
cnt = 0
for i in range(N):
    SumA.append(SumA[i] + A[i])
for i in range(M):
    SumB.append(SumB[i] + B[i])

for i in range(N+1):
    if SumA[i] > K:
        break
    tmpcnt = bisect.bisect_right(SumB, K - SumA[i]) - 1
    #print([SumA,SumB])
    #print(SumA[i])
    #print(K - SumA[i])
    #print(tmpcnt)
    #print([i,tmpcnt])
    cnt = max(cnt, i + tmpcnt)
print(cnt)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))  
B = []

for i in range(N):
  if all([A[v] >= A[i] for v in range(i)]): 
    B.append(True)
  else:
    B.append(False)

print(B.count(True))

### FIXED CODE ###

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

min = 0
cnt = 0
for i in range(N):
  if i == 0:
    cnt += 1
    min = A[i]
    continue
  if A[i] <= min:
    cnt += 1
    min = A[i]

print(cnt)
### BUGGY CODE ###

n = int(input())
butnum = {}
for i in range(1,n+1):
    butnum[i] = int(input())
pushbut = 1
pushedbut =[]
jud = 0
for j in range(1,n+1):
    pushedbut.append(pushbut)
    pushbut = butnum[pushbut]
    if pushbut == 2:
        print(j)
        jud = 1
        break
    elif pushbut in pushedbut:
        print(-1)
        jud = 1
        break
if jud == 0:
    print(-1)

### FIXED CODE ###

n = int(input())
butnum = {}
for i in range(1,n+1):
    butnum[i] = int(input())
pushbut = 1
pushedbut =[]
jud = 0
for j in range(1,n+1):
    pushbut = butnum[pushbut]
    if pushbut == 2:
        print(j)
        jud = 1
        break
if jud == 0:
    print(-1)
### BUGGY CODE ###

INF = 10**18
MOD = 998244353

N = int(input())
robots = [(-INF, INF)]

for _ in range(N):
    x, d = map(int, input().split())
    robots.append((x, x + d))

robots.sort()
stack = list()

tree = [[] for _ in range(N + 1)]

for i in range(N + 1)[::-1]:
    x, r = robots[i]
    while stack and stack[-1][0] < r:
        y, j = stack.pop()
        tree[i].append(j)
    stack.append((x, i))

stack = list()
stack.append(0)

order = [None for _ in range(N + 1)]
order[0] = (0, 0)
od = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if order[adj] is None:
            od += 1
            order[adj] = (od, adj)
            stack.append(adj)

order.sort()
dp = [1 for _ in range(N + 1)]

for od, node in order[::-1]:
    for adj in tree[node]:
        dp[node] *= dp[adj]
    dp[node] += 1
    dp[node] %= MOD

print(dp[0] - 1)

### FIXED CODE ###

INF = 10**18
MOD = 998244353

N = int(input())
robots = [(-INF, INF)]

for _ in range(N):
    x, d = map(int, input().split())
    robots.append((x, x + d))

robots.sort()
stack = list()

tree = [[] for _ in range(N + 1)]

for i in range(N + 1)[::-1]:
    x, r = robots[i]
    while stack and stack[-1][0] < r:
        y, j = stack.pop()
        tree[i].append(j)
    stack.append((x, i))

stack = list()
stack.append(0)

visited = [0 for _ in range(N + 1)]
visited[0] = 1
order = [None for _ in range(N + 1)]
order[0] = 0
od = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if not visited[adj]:
            visited[adj] = 1
            od += 1
            order[od] = adj
            stack.append(adj)

dp = [1 for _ in range(N + 1)]

for node in order[::-1]:
    for adj in tree[node]:
        dp[node] *= dp[adj]
    dp[node] += 1
    dp[node] %= MOD

print(dp[0] - 1)
### BUGGY CODE ###

import copy
n=int(input())
a=list(map(int,input().split()))
b=copy.copy(a)
for i in range(n):
    del a[i]
    ans=abs(a[0])
    for j in range(n-2):
        ans+=abs(a[j]-a[j+1])
    ans+=abs(a[n-2])
    print(ans)
    a=copy.copy(b)

### FIXED CODE ###

import copy
n=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
a.append(0)
s=0
for i in range(n+1):
    s+=abs(a[i]-a[i+1])

for i in range(1,n+1):
    print(s+abs(a[i-1]-a[i+1])-abs(a[i-1]-a[i])-abs(a[i]-a[i+1]))
### BUGGY CODE ###

import collections
b = False
s = raw_input()
for d in range(len(s)):
    for f in range(d+2, len(s)+1):
        l = len(s[d:f])
        c = collections.Counter(s[d:f])
        if not b and l>=2 and c.most_common()[0][1] > l/2:
            print d+1, f
            b = True
if not b:
    print "-1 -1"


### FIXED CODE ###

import collections
b = False
s = raw_input()
for i in range(len(s)-1):
    if not b and s[i] == s[i+1]:
        print i+1, i+2
        b = True
if not b:
    for i in range(len(s)-2):
        if not b and s[i] == s[i+2]:
            print i+1, i+3
            b = True
if not b:
    print "-1 -1"
### BUGGY CODE ###

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

for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1

for i in range(n):
    d2 = d.copy()
    d2[a[i]] -= 1

    cnt = 0
    for value in d2.values():
        cnt += value*(value-1)//2

    print(cnt)

### FIXED CODE ###

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

for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1

cnt = sum([value*(value-1)//2 for value in d.values()])

for i in range(n):
    val = d[a[i]]
    res = cnt - val*(val-1)//2

    if val >= 3:
        res += (val-1)*(val-2)//2

    print(res)
### BUGGY CODE ###

def divisor(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors
a = 0
n = int(input())
for i in range(1,n+1):
    a += i*(len(divisor(i)))
print(a)

### FIXED CODE ###

n = int(input())
ans = 0
for i in range(1,n+1):
    a = n//i
    ans += i*(a*(a+1)//2)
print(ans)
### BUGGY CODE ###

N = int(input())
S = []
for i in range(N):
    S.append(int(input()))

ans_cand = []
def dfs(cp, length):
    global ans_cand
    
    if length == N:
        ans_cand.append(cp)
    else:
        dfs(cp+S[length], length+1)
        dfs(cp, length+1)
        
def ten2zero(num):
    if num%10==0:
        return 0
    else:
        return num        

dfs(0,0)
ans_cand = [ten2zero(ans) for ans in ans_cand]

ans_cand.sort(reverse=True)

print(ans_cand[0])

### FIXED CODE ###

N = int(input())
S = []
for i in range(N):
    S.append(int(input()))

total = sum(S)

if total%10 == 0:
    
    new_S = []
    for s in S:
        if s%10==0:
            new_S.append(101)
        else:
            new_S.append(s)
    
        if sum(new_S) == 101*len(S):
            min_s = total
        else:
            min_s = min(new_S)
    
    print(total-min_s)
else:
    print(total)
### BUGGY CODE ###

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

islow = True
counter = 0

for i in range(len(plist)):
    islow = True
    if i != 0 and plist[i] < plist[i-1]:
        for j in range(plist.index(plist[i])):
            if plist[i] <= plist[j]:
                pass
            else:
                islow = False
                break
        if islow:
            counter += 1
    elif i == 0:
        counter += 1


print(counter)   

### FIXED CODE ###

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

islow = True
counter = 0
min = 0

for i in range(len(plist)):
    islow = True
    if i == 0:
        counter += 1
        min = plist[i]
        current_min = i
    elif min > plist[i]:
        counter += 1
        min = plist[i]
        current_min = i
        
print(counter)
### BUGGY CODE ###

n = int(input())
s = input()
count = 1
l = len(set(list(s)))
while( l!=1 ):
    if s[1]!=s[0]:
        count += 1
        s = s[1:]
        l = len(set(list(s)))
    else:
        s = s[1:]
print(count)

### FIXED CODE ###

n = int(input())
s = input()

for i in range(n):
    if i==0:
        count = 1
    else:
        if s[i]!=s[i-1]:
            count += 1
        else:
            pass
print(count)
### BUGGY CODE ###

import sys
tmp=sys.stdin.readline().rstrip()
def main():
   ans=0
   cnt=[0]*2019
   cnt[0]+=1
   d=0
   for i,j in enumerate(reversed(tmp)):
      d+=int(j)%2019*10**(i)%2019
      cnt[d%2019]+=1
   for i in cnt:
      ans+=i*(i-1)//2
   print(ans)
if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
tmp=sys.stdin.readline().rstrip()
def main():
   ans=0
   cnt=[0]*2019
   cnt[0]+=1
   t=0
   d=1
   for i in reversed(tmp):
      t=(t+int(i)*d)%2019
      cnt[t]+=1
      d=d*10%2019
   for i in cnt:
      ans+=i*(i-1)//2
   print(ans)
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import bisect
n = int(input())
length_list = sorted(list(map(int, input().split())))
result = 0

for i in range(n):
  for j in range(i+1, n):
    upper = length_list[i] + length_list[j] - 1
    search_target = length_list[j+1:]
    less_than_upper = bisect.bisect_right(search_target, upper)
    result += less_than_upper
    
print(result)

### FIXED CODE ###

import bisect
n = int(input())
length_list = sorted(list(map(int, input().split())))
result = 0
 
for i in range(n-2):
  for j in range(i+1, n-1):
    upper = length_list[i] + length_list[j] - 1
    search_target = length_list[j+1:]
    less_than_upper = bisect.bisect_right(search_target, upper)
    result += less_than_upper
    
print(result)
### BUGGY CODE ###

N=int(input())

print(1)
nex=input()
if nex=="Vacant":
    exit()
nleft=1
left=nex
print(N)
nex=input()
if nex=="Vacant":
    exit()
nright=N
right=nex

for i in range(20)
    nmid=(nright+nleft)//2
    print(nmid)
    nex=input()
    if nex=="Vacant":
        exit()
    mid=nex
    flag=True
    if nleft%2!=nmid%2:
        if left==mid:
            right=mid
            nright=nmid
            flag=False
    else:
        if left!=mid:
            right=mid
            nright=nmid
            flag=False
    if flag:
        left=mid
        nleft=nmid

### FIXED CODE ###

N=int(input())

print(0)
nex=input()
if nex=="Vacant":
    exit()
nleft=0
left=nex
print(N-1)
nex=input()
if nex=="Vacant":
    exit()
nright=N-1
right=nex

for i in range(20):
    nmid=(nright+nleft)//2
    print(nmid)
    nex=input()
    if nex=="Vacant":
        exit()
    mid=nex
    flag=True
    if nleft%2!=nmid%2:
        if left==mid:
            right=mid
            nright=nmid
            flag=False
    else:
        if left!=mid:
            right=mid
            nright=nmid
            flag=False
    if flag:
        left=mid
        nleft=nmid
### BUGGY CODE ###

from collections import defaultdict

n, m, k = map(int, input().split())
def connection(n):
    connect = defaultdict(set)
    for i in range(n):
        a, b = map(int, input().split())
        connect[a].add(b)
        connect[b].add(a)
    return connect
    
friends = connection(m)
blocks = connection(k)
groups = []
def dfs(start, check_set):
    seen = [False for i in range(n+1)]
    todo = []
    seen[start] = True
    todo.append(start)
    while todo:
        v = todo.pop()
        for w in check_set[v]:
            if not seen[w]:
                seen[w] = True
                todo.append(w)
    return seen

not_checked_friends = {i+1 for i in range(n)}
groups = {}
count = 0
belong_group = {}
while not_checked_friends:
    count += 1
    person = list(not_checked_friends).pop()
    seen_friends = dfs(person, friends)
    seen_set = set(i for i, flag in enumerate(seen_friends) if flag)
    for i in seen_set:
        belong_group[i] = count
    not_checked_friends -= seen_set
    groups[count] = seen_set
print(' '.join(str(len(groups[belong_group[i]])-len(groups[belong_group[i]]&(friends[i]|blocks[i]))-1) for i in range(1, n+1)))
    
    

### FIXED CODE ###

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

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return ''.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

uf = UnionFind(n)

from collections import defaultdict
direct_f = defaultdict(int)
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    direct_f[a] += 1
    direct_f[b] += 1
    uf.union(a, b)
ans = [uf.size(i) - direct_f[i] - 1 for i in range(n)]
for i in range(k):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if uf.same(c,d):
        ans[c] -= 1
        ans[d] -= 1
print(*ans)
### BUGGY CODE ###

count = 0
N = int(input())
S = list(input())
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if(S[i] != S[j] and S[i] != S[k] and S[j] != S[k] and (j - i) != (k - j)):
                count+=1
print(count)

### FIXED CODE ###

# 先に組み合わせの数を決める
# RとGとBの数を先に数え上げると、Rの数 * Gの数 * Bの数が組み合わせの数になる
# 累積和でもできる

#組み合わせの数は
kumiawase = 0
countR=0
countG=0
countB=0
N = int(input())
S = list(input())
for i in range(N):
    if(S[i] == "R"):
        countR += 1
    elif(S[i] == "G"):
        countG += 1
    elif(S[i] == "B"):
        countB += 1
kumiawase = countR * countG * countB

count = 0
#その組み合わせの数から、j-i = k-jをみたす数を引けばいい
#条件を満たし、かつ1つ目の条件を満たすものは
for i in range(N):
    for j in range(i+1, N):
        k = 2 * j - i
        if(k < N):
            if(S[i] != S[j] and S[i] != S[k] and S[j] != S[k]):
                count+=1

print(kumiawase - count)
### BUGGY CODE ###

import sys


def query(S):
    print(S)
    sys.stdout.flush()
    return input()


def main():
    N = int(input())
    base = query(0)
    lo = 1
    hi = N-1
    while lo < hi:
        mid = (lo+hi) // 2
        ans = query(mid)
        if ans == "Vacant":
            return
        if (mid % 2 == 0 and ans == base) or \
           (mid % 2 == 1 and ans != base):
            lo = mid + 1
        else:
            hi = mid


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys


def query(S):
    print(S)
    sys.stdout.flush()
    return input()


def main():
    N = int(input())
    base = query(0)
    if base == "Vacant":
        return
    lo = 1
    hi = N-1
    while lo < hi:
        mid = (lo+hi) // 2
        ans = query(mid)
        if ans == "Vacant":
            return
        if (mid % 2 == 0 and ans == base) or \
           (mid % 2 == 1 and ans != base):
            lo = mid + 1
        else:
            hi = mid
    query(lo)


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

def inv(s):
    t = ''
    for i in range(len(s)):
        t += s[-1 * (i + 1)]
    return t

s = input()
q = int(input())
ans = s
parity = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        parity += 1
    else:
        if qi[2] == '1' and parity % 2 == 0:
            ans = qi[4] + ans
        elif qi[2] == '2' and parity % 2 == 0:
            ans = ans + qi[4]
        if qi[2] == '1' and parity % 2 == 1:
            ans = ans + qi[4]
        elif qi[2] == '2' and parity % 2 == 1:
            ans = qi[4] + ans
if parity % 2:
    ans = inv(ans)
print(ans)

### FIXED CODE ###

from collections import deque
s = input()
q = int(input())
ans = deque(s)
rev = 0
for i in range(q):
    qi = input()
    if qi[0] == '1':
        rev += 1
    else:
        if qi[2] == '1' and rev % 2 == 0:
            ans.appendleft(qi[4])
        elif qi[2] == '2' and rev % 2 == 0:
            ans.append(qi[4])
        if qi[2] == '1' and rev % 2 == 1:
            ans.append(qi[4])
        elif qi[2] == '2' and rev % 2 == 1:
            ans.appendleft(qi[4])

parity = rev % 2
if parity:
    ans.reverse()
    for i in ans:
        print(i, end='')
else:
    for i in ans:
        print(i, end='')
### BUGGY CODE ###

#bin()やformat(数字 ,"b")を使うとstr型になる
#bin()とformat(数字 ,"#b")は同意

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

for i in range(N): A[i] = format(A[i], "060b") #ここで全て60ビットの二進数に変換

mod = 10**9+7

sum = 0
for i in range(1,61):
    count0 = 0
    count1 = 0
    for j in range(N):
        print
        if A[j][-i] == "0": #str型であることに注意！
            count0 += 1
        else:
            count1 += 1 
    sum += (count0 * count1) * 2**(i-1) #左シフトだが，2**(i-1)でもOK
print(sum%mod)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
xorsum = 0
for i in range(60):
  c1 = sum([1 for a in A if (a >> i) & 1]) #
  xorsum += (c1*(N-c1)*(1<<i))
  xorsum = xorsum%mod
print(xorsum%mod)
### BUGGY CODE ###

def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, w = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    A = [list(map(int, input().split())) for i in range(n)]

    Knap = [[0] * (w+1) for _ in range(n+1)]

    for i in range(n):
        for j in reversed(range(i+1)):
            for k in range(w+1):
                if (Knap[j][k] > 0 or (j == 0 and k ==0)) and k + A[i][0] <= w:
                    Knap[j+1][k + A[i][0]] = max((Knap[j+1][k + A[i][0]]), (Knap[j][k] + A[i][1]))

    knapmax = 0
    for i in range(n):
        knapmax = max(knapmax, max(Knap[i+1]))
    print(knapmax)

resolve()

### FIXED CODE ###

def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, w = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    A = [list(map(int, input().split())) for i in range(n)]

    Knap = [[0] * (w+1) for _ in range(n+1)]
#    Knap = [[0] * (110) for _ in range(10**5+10)]

    for i in range(n):
        for k in range(w+1):
            if k + A[i][0] <= w:
#                point = k + A[i][0]
                Knap[i+1][k+A[i][0]] = max(Knap[i+1][k+A[i][0]], (Knap[i][k] + A[i][1]))

            Knap[i+1][k] = max(Knap[i][k], Knap[i+1][k])

#    knapmax = 0
    # for i in range(n):
    #     knapmax = max(knapmax, max(Knap[i+1]))
    print(Knap[n][w])
#    print(*Knap, sep="\n")
resolve()
### BUGGY CODE ###

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.extend(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


while stack:
    curh, curw, cnt = stack_popleft()
    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 * w + nw] == '.':
            stack_append([nh, nw, cnt])
    for nh in range(curh-2, curh+3):
        if nh >= 0 and nh < h:
            for nw in range(curw-2,curw+3):
                if nw >= 0 and nw < w and s[nh * w + nw] == '.':
                     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])

### FIXED CODE ###

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 CODE ###

N = int(input())
A = list(map(int, input().split()))
A_div = [0 for _ in range(N)]
answer = 0
for i in range(N):
    for j in range(i + 1, N):
        if A[i] % A[j] == 0:
            A_div[i] += 1
        if A[j] % A[i] == 0:
            A_div[j] += 1

print(A_div.count(0))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
A_div = [0] * (10 ** 6 + 1)

for a in A:
    A_div[a] += 1
    if A_div[a] == 1:
        for j in range(2 * a, 10 ** 6 + 1, a):
            A_div[j] += 100

print(A_div.count(1))
### BUGGY CODE ###

a, b, c, k = map(int, input().split())
for i in range(k):
 A=b+c
 B=a+c
 C=a+b
 a=A
 b=B
 c=C
if abs(a-b) <= 10**18:
 print (a-b)
else:
 print('Unfair')

### FIXED CODE ###

a, b, c, k = map(int, input().split())
d=(a-b)*(-1)**k

if abs(d) < 10**18:
 print (d)
else:
 print('Unfair')