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

#入力
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())
s=[]
for i in range(h):
  s_=list(input())
  s.append(s_)
#幅優先探索
from collections import deque
q=deque([(ch-1,cw-1)])
used=[[float("INF")]*w for i in range(h)]
used[ch-1][cw-1]=0
X=((-1,0),(1,0),(0,1),(0,-1))
Y=((-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),(-1,-2),(-1,-1),(-1,1),(-1,2),(0,-2),(0,1),(0,2),(1,-2),(1,-1),(1,1),(1,2),(2,-2),(2,-1),(2,0),(2,1),(2,2))
while len(q)>0:
  y,x=q.popleft()
  for dy,dx in X:
    nx=x+dx
    ny=y+dy
    if nx>=0 and nx<w and ny>=0 and ny<h and s[ny][nx]==".":
      if used[ny][nx]>used[y][x]:
        used[ny][nx]=used[y][x]
        q.appendleft((ny,nx))
  for dy,dx in Y:
    nx=x+dx
    ny=y+dy
    if nx>=0 and nx<w and ny>=0 and ny<h and s[ny][nx]==".":
      if used[ny][nx]>used[y][x]:
        used[ny][nx]=used[y][x]+1
        q.append((ny,nx))

if used[dh-1][dw-1]==float("INF"):
  print(-1)
else:
  print(used[dh-1][dw-1])

      
      
      
    
  
  


### FIXED CODE ###

#入力
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())
from sys import stdin
s=[stdin.readline() for i in range(h)]

#for i in range(h):
#  s_=list(input())
#  s.append(s_)
#幅優先探索
from collections import deque
q=deque([(ch-1,cw-1)])
used=[[float("INF")]*w for i in range(h)]
used[ch-1][cw-1]=0
X=((-1,0),(1,0),(0,1),(0,-1))
Y=((-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),(-1,-2),(-1,-1),(-1,1),(-1,2),(0,-2),(0,1),(0,2),(1,-2),(1,-1),(1,1),(1,2),(2,-2),(2,-1),(2,0),(2,1),(2,2))
while len(q)>0:
  y,x=q.popleft()
  for dy,dx in X:
    nx=x+dx
    ny=y+dy
    if nx>=0 and nx<w and ny>=0 and ny<h and s[ny][nx]==".":
      if used[ny][nx]>used[y][x]:
        used[ny][nx]=used[y][x]
        q.appendleft((ny,nx))
  for dy,dx in Y:
    nx=x+dx
    ny=y+dy
    if nx>=0 and nx<w and ny>=0 and ny<h and s[ny][nx]==".":
      if used[ny][nx]>used[y][x]:
        used[ny][nx]=used[y][x]+1
        q.append((ny,nx))

if used[dh-1][dw-1]==float("INF"):
  print(-1)
else:
  print(used[dh-1][dw-1])
### BUGGY CODE ###

import numpy as np
N = int(input())

x = np.empty(N, dtype=np.longlong)
y = np.empty(N, dtype=np.longlong)

for i in range(N):
    x[i], y[i] = (int(t) for t in input().split())

ind = np.argsort(x)
X = x[ind]
Y = y[ind]

max_dist = [0]
for i in range(1, N):
    max_dist.append(np.max(-X[:i] - Y[:i]) + X[i] + Y[i])
    max_dist.append(np.max(-X[:i] + Y[:i]) + X[i] - Y[i])

print(max(max_dist))

### FIXED CODE ###

import numpy as np
N = int(input())

x = []
y = []
xy1 = []
xy2 = []

for i in range(N):
    a, b = (int(t) for t in input().split())
    x.append(a)
    y.append(b)
    xy1.append(a + b)
    xy2.append(a - b)

max1 = max(xy1) - min(xy1)
max2 = max(xy2) - min(xy2)
print(max((max1, max2)))
### BUGGY CODE ###

# import bisect
# from collections import Counter, deque # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
        if S[i]=='R':
            R.append(i)
        if S[i]=='G':
            G.append(i)
        if S[i]=='B':
            B.append(i)

    L=list(product(R,G,B))
    cnt=0
    for i in L:
        i=sorted(list(i))
        if i[1]-i[0]==i[2]-i[1]:
            continue
        else:
            cnt+=1
    print(cnt)




resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    S=str(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
        if S[i]=='R':
            R.append(i)
        if S[i]=='G':
            G.append(i)
        if S[i]=='B':
            B.append(i)
    lr=len(R)
    lg=len(G)
    ans=lr*lg*(N-lr-lg)

    cnt=0
    for i in range(N):
        for j in range(i+1,N):
            k=2*j-i
            if k>=N:
                continue
            else:
                if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                    cnt+=1
    print(ans-cnt)




resolve()
### BUGGY CODE ###

import math
import functools
import sys

N = int(input())
A = list(map(int, input().split()))
Amax = max(A) + 1
if Amax == 2:
    print('pairwise coprime')
    sys.exit(0)

hurui = [1] * 1001
primes = []

for n in range(2, 1001):
    if hurui[n] != 1:
        continue

    primes.append(n)

    k = 2
    while n * k <= 1000:
        hurui[n * k] = n
        k += 1


#print(len(list(filter(lambda i: i == 0, hurui))))
#print(primes)

#print(primes)

wise = functools.reduce(math.gcd, A)
if wise != 1:
    print('not coprime')
    sys.exit(0)

count = [0] * (Amax+1)

pairwise = True
for i in range(N):
    a = A[i]
    if a == 1:
        continue

    for p in primes:
        if a % p == 0:
            count[p] += 1
            while a % p == 0:
                a = a // p
    if a != 1:
        count[a] += 1

if max(count)==1:
    print('pairwise coprime')
else:
    print('setwise coprime')



### FIXED CODE ###

import math
import functools
import sys

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

wise = functools.reduce(math.gcd, A)
if wise != 1:
    print('not coprime')
    sys.exit(0)

A = list(filter(lambda a: a != 1, A))

if len(A) == 0:
    print('pairwise coprime')
    sys.exit(0)

pcount = 78498
if len(A) > pcount:
    print('setwise coprime')
    sys.exit(0)

Amax = max(A) + 1

hurui = [1] * 1001
primes = []

for n in range(2, 1001):
    if hurui[n] != 1:
        continue

    primes.append(n)

    k = 2
    while n * k <= 1000:
        hurui[n * k] = n
        k += 1




count = [0] * (Amax+1)

pairwise = True
for i in range(len(A)):
    a = A[i]
    if a == 1:
        continue

    for p in primes:
        if a % p == 0:
            count[p] += 1
            while a % p == 0:
                a = a // p
    if a != 1:
        count[a] += 1

if max(count)==1:
    print('pairwise coprime')
else:
    print('setwise coprime')
### BUGGY CODE ###

import collections
import math
from functools import lru_cache
 
@lru_cache(maxsize = 64)
def C(n, r):
    if n-r < 0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
#d = {}
d = [-1]*1000000
sm = 0
for k,v in c.most_common():

    d[k] = v
    sm += C(v,2)

for a in A:
    print (sm - C(d[a],2) + C(d[a]-1,2) )


### FIXED CODE ###

import collections
import math

def C(n):
    if n-2 < 0:
        return 0
    return n * (n-1) // 2
N = int(input())
A = list(map(int,input().split()))
c = collections.Counter(A)
#d = {}
d = [-1]*200010
Cf = [C(i) for i in range(200010)]
sm = 0
for k,v in c.most_common():

    d[k] = v
    sm += C(v)

for a in A:
    print (sm - Cf[d[a]] + Cf[d[a]-1])
### BUGGY CODE ###

n,m=[int(s) for s in input().split()]
key=[]
for i in range(m):
  cost,b=[int(s) for s in input().split()]
  op=0
  for i in input().split():
    op+=1<<(int(i)-1)
  key.append([cost,op])
N=2**n
dp=[[10**7 for _ in range(N)] for _ in range(m)]
dp[0][key[0][1]]=key[0][0]
dp[0][0]=0
for i in range(1,m):
  for j in range(N):
    dp[i][j]=min([dp[i-1][j],dp[i][j]])
    x=j|key[i][1]
    dp[i][x]=min([dp[i][x],dp[i-1][j]+key[i][0]])
if dp[m-1][N-1]==10**7:
  print(-1)
else:
  print(dp[m-1][N-1])      

### FIXED CODE ###

n,m=[int(s) for s in input().split()]
key=[]
for i in range(m):
  cost,b=[int(s) for s in input().split()]
  op=0
  for i in input().split():
    op+=1<<(int(i)-1)
  key.append([cost,op])
N=2**n
dp=[[10**7 for _ in range(N)] for _ in range(m)]
dp[0][key[0][1]]=key[0][0]
dp[0][0]=0
for i in range(1,m):
  for j in range(N):
    dp[i][j]=min([dp[i-1][j],dp[i][j]])
    x=j|key[i][1]
    dp[i][x]=min([dp[i][x],dp[i-1][j]+key[i][0]])
if dp[m-1][N-1]==10**7:
  print(-1)
else:
  print(dp[m-1][N-1])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7

res = 0
for i in range(len(a)):
    for j in range(i+1, len(a)):
        res = (res+((a[i]*a[j])%mod))%mod

print(res)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7

res = 0
s = 0
for i in range(1, len(a)):
    s = (s+a[i-1])%mod
    res = (res+(s*a[i])%mod)%mod

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

import bisect


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    to[u - 1].append(v - 1)
    to[v - 1].append(u - 1)


dp = [None for _ in range(N)]


dp[0] = [a[0]]
Q = [0]

while Q:
    u = Q.pop()
    for v in to[u]:
        if dp[v] is not None:
            continue
        dp[v] = dp[u][::]
        if a[v] > dp[u][-1]:
            dp[v].append(a[v])
        else:
            dp[v][bisect.bisect_left(dp[v], a[v])] = a[v]
        Q.append(v)

for d in dp:
    print(len(d))


### FIXED CODE ###

import bisect
import sys


N = int(input())
a = list(map(int, input().split()))
to = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    to[u].append(v)
    to[v].append(u)
del u, v

ans = [-1] * N


def dfs(v, dp):
    # dp は参照渡し
    if len(dp) == 0 or a[v] > dp[-1]:
        dp.append(a[v])
        back = -1
    else:
        pos = bisect.bisect_left(dp, a[v])
        back = dp[pos]
        dp[pos] = a[v]
    ans[v] = len(dp)

    for u in to[v]:
        if ans[u] == -1:
            dfs(u, dp)
    if back == -1:
        dp.pop()
    else:
        dp[pos] = back


sys.setrecursionlimit(10 ** 6)
dfs(0, [])


for an in ans:
    print(an)
### BUGGY CODE ###

s = input()[::-1]
d = 1
num = 0
ans = 0
mod_list = [0] * 2019
mod_list[0] = 1

for char in s:
    num += int(char) * d
    mod_list[num % 2019] += 1
    d *= 10

for i in range(2019):
    ans += mod_list[i] * (mod_list[i] - 1) //2

print(ans)


### FIXED CODE ###

s = input()[::-1] # 入力文字列を逆順でsに格納

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans) # 答えの出力
### BUGGY CODE ###

N, Q = [int(s) for s in input().split()]
S = input()
AC = []
for i in range(N-1):
    if S[i:i+2] == 'AC':
        AC.append(i)
for i in range(Q):
    l, r = [int(s)-1 for s in input().split()]
    n_AC = 0
    for j in AC:
        if j >= l and j+1 <=r:
            n_AC += 1
    print(n_AC)

### FIXED CODE ###

N, Q = [int(s) for s in input().split()]
S = input()
AC = [0]
for i in range(N-1):
    if S[i:i+2] == 'AC':
        AC.append(AC[-1] + 1)
    else:
        AC.append(AC[-1])
for i in range(Q):
    l, r = [int(s)-1 for s in input().split()]
    print(AC[r] - AC[l])
### BUGGY CODE ###

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

gcd = A[0]
for i in range(1, N):
    gcd = math.gcd(gcd, A[i])
if gcd != 1:
    print("not coprime")
    sys.exit()



NUM = 10**6
T = [True]*(NUM+1)
prime = []
for i in range(2, NUM+1):
    if T[i]:
        prime.append(i)
        tmp = i
        while tmp <= NUM:
            T[tmp] = False
            tmp += i
L = len(prime)
T = [False]*L

for num in A:
    for i in range(L):
        if num%prime[i] == 0:
            if T[i]:
                print("setwise coprime")
                sys.exit()
            T[i] = True
            while num%prime[i] == 0:
                num //= prime[i]
print("pairwise coprime")



### FIXED CODE ###

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

NUM = 10**6

gcd = A[0]
for i in range(1, N):
    gcd = math.gcd(gcd, A[i])
if gcd != 1:
    print("not coprime")
    sys.exit()


def osa_k(num):
    S = list(i for i in range(num+1))
    for i in range(2, int(num**0.5)+1):
        if S[i] < i:
            continue
        j = i**2
        while j <= num:
            if S[j] == j:
                S[j] = i
            j += i
    return S


prime = osa_k(NUM)
S = [False]*(NUM+1)
for num in A:
    tmp = prime[num]
    while num > 1:
        num //= tmp
        if tmp == prime[num]:
            # S[tmp] = True
            continue
        if S[tmp]:
            print("setwise coprime")
            sys.exit()
        S[tmp] = True
        tmp = prime[num]
print("pairwise coprime")
### BUGGY CODE ###

MOD = 1000000007#い　つ　も　の
n,k = map(int,input().split())

nCk = 1#nC0
ans = 1

if k > n-1:
    k = n-1

for i in range(1,k+1,1):
    nCk = (((nCk*(n-i+1))%MOD)*pow(i,1000000005,1000000007))%MOD
    ans = (ans%MOD + ((((nCk*nCk)%MOD)*(n-i)%MOD)*pow(n,1000000005,1000000007)%MOD)%MOD)%MOD

print(ans)


### FIXED CODE ###

MOD = 1000000007#い　つ　も　の
n,k = map(int,input().split())

nCk = 1#nC0
ans = 1

if k > n-1:
    k = n-1

for i in range(1,k+1,1):
    nCk = (((nCk*(n-i+1))%MOD)*pow(i,1000000005,1000000007))%MOD
    ans = (ans%MOD + ((((nCk*nCk)%MOD)*(n-i)%MOD)*pow(n,1000000005,1000000007)%MOD)%MOD)%MOD

print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(b)
    except:
        pass
w.reverse()
a= [[] for i in range(M)]
ans = 0
for i in range(M):
    a[i] = w[M-i-1]
    if i>=1:
        a[i-1].sort()
        a[i] += a[i-1][:-1]
    try:
        ans += max(a[i])
    except:
        pass
print(ans)

### FIXED CODE ###

import heapq
N, M = map(int, input().split())
w = [[] for i in range(M)]
for i in range(N):
    a, b = map(int, input().split())
    try:
        w[a-1].append(-b)
    except:
        pass
ans = 0
a = []
heapq.heapify(a)
for i in range(M):
    for x in w[i]:
        heapq.heappush(a, x)
    try:
        ans += -heapq.heappop(a)
    except:
        pass
print(ans)
### BUGGY CODE ###

X,Y,Z,K = map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
xy=[]
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
for a in A:
    for b in B:
        xy.append(a+b)
xy.sort(reverse=True)
xy=xy[:K]
xyz=[]
for x in xy:
    for c in C:
        xyz.append(x+c)
xyz.sort(reverse=True)
for i in range(K):
    print(xyz[i])


### FIXED CODE ###

X,Y,Z,K = map(int,input().split())
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
xy=[]
for a in A:
    for b in B:
        xy.append(a+b)
xy=sorted(xy,reverse=True)
xy=xy[:K]
xyz=[]
for c in C:
    for x in xy:
        xyz.append(x+c)
xyz=sorted(xyz,reverse=True)
for i in range(K):
    print(xyz[i])
### BUGGY CODE ###

import heapq
def SME(start,end):
    A = []
    if end > N : end = N
    count = 0
    count2 = 0
    for i,x in enumerate(Whole[start:end]):
        heapq.heappush(A,[x,i])
    for j in range(start,end):
        a = heapq.heappop(A)
        tmp = count
        for i in range(L):
            idx = a[1] + i
            try :
                if ANS[idx] : continue
                else :
                    ANS[idx] = a[0]
                    count += 1
            except IndexError : continue
        if count == tmp : count2 += 1
        if count2 > 350 : break
    for i in range(L):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[:i+1])
    for i in range(L,N):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[i+1-L:i+1])
    return ANS

[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
parameter = 0.97
ANS = [None]*N
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
### BUGGY CODE ###

N=int(input())

a=1
b=1
c=1

for i in range(N):
    a*=9
    b*=8
    c*=10
A=c-2*a+b

print(A%(1000000007))


### FIXED CODE ###

N=int(input())

a=9**N
b=8**N
c=10**N
A=c-2*a+b

print(A%(1000000007))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math

# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    A = inputList()
    
    ans = [0 for i in range(N)]
    ans_cnt = 0
    ans_str = ""
    for i in range(N, 0, -1):
        cons = 0
        for j in range(i, N+1, i):
            cons ^= ans[j-1]
        if A[i-1] == 1 and cons % 2 == 0:
            ans[i-1] = 1
            ans_cnt += 1
            ans_str = ans_str + str(i) + " "
            continue
        if A[i-1] == 0 and cons % 2 == 1:
            ans[i-1] = 1
            ans_cnt += 1
            ans_str = ans_str + str(i) + " "
            continue
            
    print(ans_cnt)
    print(ans_str)
            	
if __name__ == "__main__":
	main()


### FIXED CODE ###

import sys
 
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
 
N=INT()
A=LIST()
 
# その位置にボールを入れるかを保持するA2
A2=[0]*N
B=[]
# 後ろから決めていく
for i in range(N-1, -1, -1):
    cur=i+1
    cnt=0
    # curの倍数を全部見に行く
    for j in range(cur*2, N+1, cur):
        cnt^=A2[j-1]
    # ボールを入れるべきかどうか
    if A[i]!=cnt:
        B.append(cur)
        A2[i]=1
 
print(len(B))
print(*B)
### BUGGY CODE ###

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

for n in range(N):
    print(A.count(n+1))

### FIXED CODE ###

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

#for n in range(N):
#    print(A.count(n+1))

M = {}
for a in A:
    M[a] = M.get(a,0) + 1

for n in range(N):
    print(M.get(n+1,0))
### BUGGY CODE ###

S = input()
length = len(S)
count = 0
for j in range(1, length+1, 1):
    for i in range(0,j, 1 ):
        subset = S[i:j]
        if(int(subset) % 2019 == 0):
            count = count + 1

print(count)

### FIXED CODE ###

reverse_S = input()[::-1]
n = len(reverse_S)
count = 0
mod = 0
mods = [0] * 2019
mods[0] = 1
x = 1
for s in reverse_S:
    mod = (mod + (int(s) * x)) % 2019
    mods[mod] = mods[mod] + 1
    x = x * 10
    x = x % 2019

for v in mods:
    count = count + v*(v-1)//2

print(count)
### BUGGY CODE ###

from collections import deque

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False
check=deque()
hit=set()

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1



### FIXED CODE ###

from collections import deque

H,W=map(int,input().split())
Sx,Sy=map(int,input().split())
Gx,Gy=map(int,input().split())
Sx-=1
Sy-=1
Gx-=1
Gy-=1
tmp=False
check=deque()

M=[[0 if j=='.' else 1 for j in input()]for i in range(H)]

# 0通路 1壁 2確認済み

ans=0
check.append([Sx,Sy])
while True:
    hit=set()
    while check:
        x,y = check.pop()
        #goul
        if x==Gx and y==Gy:
            tmp=True
            break
        M[x][y]=2
        #壁があるか
        for dx,dy in [ [x+1,y],[x-1,y],[x,y-1],[x,y+1] ]:
            if not(0<=dx and dx<H and 0<=dy and dy<W):
                continue
            if M[dx][dy]==1:
                hit.add((x,y))
            if M[dx][dy]==0:
                check.append([dx,dy])
    if tmp:
        print(ans)
        break
    if len(hit)==0:
        print(-1)
        break
    for e in hit:
        x,y=e
        for i in range(-2,3):
            for j in range(-2,3):
                if 0<=x+i and x+i<H and 0<=y+j and y+j<W and M[x+i][y+j]==0:
                    check.append([x+i,y+j])
    if len(check)==0:
        print(-1)
        break

    ans+=1
### BUGGY CODE ###

# C - Cat Snuke and a Voyage
N,M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(M)]
goal2 = []
goal1 = []
for i in range(M):
    if A[i][1] == N:
        goal2.append(A[i][0])
    else:
        pass

for j in range(M):
    if A[j][0] == 1:
        goal1.append(A[j][1])
    else:
        pass


for k in range(len(goal1)):
    if goal1[k] in goal2:
        print("POSSIBLE")
        exit()
print("IMPOSSIBLE")


### FIXED CODE ###

# C - Cat Snuke and a Voyage
N,M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(M)]
goal2 = []
goal1 = []
for i in range(M):
    if A[i][1] == N:
        goal2.append(A[i][0])
    elif A[i][0] == 1:
        goal1.append(A[i][1])
    else:
        pass

if len(set(goal1)&set(goal2)) != 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
### BUGGY CODE ###

import fractions
  
N = int(input())
A = tuple(map(int, input().split()))

ans = 0
MOD = 10**9 + 7

LCM = A[0]
for i in range(1, N):
    LCM *= A[i]//fractions.gcd(LCM, A[i])
    
for a in A:
  ans += LCM//a
  ans = ans%MOD
  
print(ans)

### FIXED CODE ###

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

ans = 0
MOD = 10**9 + 7

LCM = A[0]
for i in range(1, N):
    LCM *= A[i]//fractions.gcd(LCM, A[i])
    
for a in A:
  ans += LCM//a
  
print(ans%MOD)