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


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

x = functools.partial(functools.reduce, operator.mul)
ans = x(a)
if ans > 10 ** 18:
    ans = -1

print(ans)


### FIXED CODE ###

#import numpy as np
import math
n = int(input())
a= list(map(int,input().split()))
a.sort(reverse=1)

ans = a[0]

for i in range(1,n):
    if a[-1] == 0:
        print("0")
        exit()
    elif a[i] > 10**18:
        print("-1")
        exit()
    elif ans > 10**18:
        print("-1")
        exit()
    else:
        ans *= a[i]

if ans > 10**18:
    ans = -1
print(ans)
### BUGGY CODE ###

import sys
from collections import deque
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    A = string_to_int(inputs[0])
    odd_num = 0
    count = -1
    while odd_num < len(A):
        odd_num = 0
        devided = False
        for i, _ in enumerate(A):
            if A[i] % 2 == 0:
                if not devided:
                    A[i] /= 2
                    devided = True
            else:
                odd_num += 1
        count += 1
    return count


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)


### FIXED CODE ###

import sys
from collections import deque
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    A = string_to_int(inputs[0])
    odds = []

    for a in A:
        if a % 2 == 0:
            odds.append(a)
    count = 0
    for o in odds:
        while o % 2 != 1:
            o /= 2
            count += 1

    return count


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)
### BUGGY CODE ###

def merge(A, l, m, r):
    n1, n2 = m - l, r - m
    L = [A[l + i] for i in range(n1)] + [10 ** 10]
    R = [A[m + i] for i in range(n2)] + [10 ** 10]
    i, j = 0, 0
    for k in range(l, r):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
    global cnt
    cnt += r - l

def mergeSort(A, l, r):
    if r - l > 1:
        m = (l + r) // 2
        mergeSort(A, l, m)
        mergeSort(A, m, r)
        merge(A, l, m, r)

n = int(input())
A = list(map(int, input().split()))
cnt = 0
mergeSort(A, 0, n)
print(" ".join(list(map(str, A))))
print(cnt)

### FIXED CODE ###

def merge(A, l, m, r):
    L = A[l:m] + [10 ** 9 + 1]
    R = A[m:r] + [10 ** 9 + 1]
    i, j = 0, 0
    for k in range(l, r):
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
    global cnt
    cnt += r - l

def mergeSort(A, l, r):
    if r - l > 1:
        m = (l + r) // 2
        mergeSort(A, l, m)
        mergeSort(A, m, r)
        merge(A, l, m, r)

n = int(input())
A = list(map(int, input().split()))
cnt = 0
mergeSort(A, 0, n)
print(" ".join(list(map(str, A))))
print(cnt)
### BUGGY CODE ###

def main():
    N = int(input())
    A = list(map(int,input().split()))
    
    if 0 in A:
        return print(0)
    prod = 1
    for n in A:
        prod *= n
    if prod > 1000000000000000000:
        prod = -1
    return print(prod)

main()

### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int,input().split()))
    
    if 0 in A:
        return print(0)
    prod = 1
    for n in A:
        prod *= n
        if prod > 1000000000000000000:
            return print(-1)
    return print(prod)

main()
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
l = []
r = []
cnt = []
for i in range(q):
    _l,_r = [int(i) for i in input().split()]
    l.append(_l)
    r.append(_r)
for i in range(q):
    cnt.append(s[l[i]-1:r[i]].count('AC'))
for i in range(len(cnt)):
    print(cnt[i])


### FIXED CODE ###

import sys
input = sys.stdin.readline
n,q = map(int,input().split())
s = input()
t = [0] * (n+1)
for i in range(n):
    t[i+1] = t[i] + (1 if s[i:i+2]=='AC' else 0)
for i in range(q):
    l,r = map(int,input().split())
    print(t[r-1] - t[l-1])
### BUGGY CODE ###

#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
import bisect
from numba import jit


@jit
def solve():
    n = I()
    yaku = [0] * (n+1)
    for i in range(1,n+1):
        for j in range(i,n+1,i):
            yaku[j] += 1
    ans = 0
    for i in range(1,n+1):
        ans += i * yaku[i]
    print(ans)
def main():
    solve()
main()


### FIXED CODE ###

#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
import math
## 素数判定 O (√n)

n = I()
ans = 0
yaku = [1] *(10**7+1)
yaku[0] = 0
yaku[1] = 1
for i in range(1,n+1):
    for j in range(i*2,n+1,i):
        yaku[j] += 1
for i in range(1,n+1):
    y = yaku[i]
    ans += i * y
print(ans)
### BUGGY CODE ###

N = int(input())
 
sum = 0
for n in range(1,N+1):
    num = int(N/n)
    s = int(((1+num)*num)/2)
    sum += n*s
print(sum)

### FIXED CODE ###

N = int(input())
 
sum = 0
ro = 1
for n in range(1,N+1):
    num = int(N/n)
    if num == 1:
        record = n
        break
    s = int(((1+num)*num)/2)
    sum += ro*s
    ro += 1


sum += int(((record+N)*(N-record+1))/2)
print(sum)
### BUGGY CODE ###

s = input()

res = [0 for i in range(2019)]
res[0] += 1
for i in range(len(s)):
    res[int(s[i:])%2019] += 1

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

### FIXED CODE ###

s = input()

res = [0 for i in range(2019)]
res[0] += 1

resten = 1
ressum = int(s[-1])
res[ressum] += 1

for i in range(2, len(s)+1):
    resten = (resten*10)%2019
    ressum = (int(s[-i])*resten + ressum)%2019
    res[ressum] += 1

ans = 0
for i in range(2019):
    ans += res[i]*(res[i]-1)//2
print(ans)
### BUGGY CODE ###

def main():
    MOD = 998244353

    N, S = map(int, input().split())
    *A, = map(int, input().split())

    dp = [[[0] * 3 for _ in range(S + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 1

    for i, x in enumerate(A):
        for s in range(S + 1):
            dp[i + 1][s][0] = (dp[i + 1][s][0] + dp[i][s][0]) % MOD
            dp[i + 1][s][1] = (dp[i + 1][s][1]
                               + dp[i][s][1] + (dp[i][s - x][1] if s - x >= 0 else 0)
                               + dp[i][s][0] + (dp[i][s - x][0] if s - x >= 0 else 0)) % MOD
            dp[i + 1][s][2] = (dp[i + 1][s][2]
                               + dp[i][s][2] +
                               dp[i][s][1] + (dp[i][s - x][1] if s - x >= 0 else 0)
                               + dp[i][s][0] + (dp[i][s - x][0] if s - x >= 0 else 0)) % MOD

    print(dp[N][S][2])


if __name__ == '__main__':
    main()


### FIXED CODE ###

# https://atcoder.jp/contests/abc159/submissions/11139734

def main():
    MOD = 998244353

    N, S = map(int, input().split())
    *a, = map(int, input().split())

    dp = [0] * (S + 1)

    ret = 0
    for i, x in enumerate(a):
        dp[0] += 1
        if x > S: continue
        ret = (ret + dp[S - x] * (N - i)) % MOD
        for k in range(S - 1, x - 1, -1):
            dp[k] = (dp[k] + dp[k - x]) % MOD

    print(ret)


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


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    sval = sum(a)
    for k in range(1, n+1):
        a_s = 0
        for l in range(n//2):
            a_s += a[((k+1)+(2*l))%n-1]
        ans = sval - 2 * a_s
        print(ans, end=' ')
main()


### FIXED CODE ###

#python3

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    c = 0
    for i in range(n):
        if i%2 == 0:
            c += a[i]
        else:
            c -= a[i]
    x = c//2
    xs = []
    xs.append(x)
    for i in range(2, n+1):
        z = a[i-2] - xs[i-2]
        xs.append(z)
    for i in range(n):
        xs[i] = 2*xs[i]
    for i in range(n):
        if i == n-1:
            print(xs[i])
        else:
            print(xs[i], end=' ')
main()
### BUGGY CODE ###

from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline
import time
t=time.time()

def main():
    n=int(input())
    d=[[]]+[deque(map(int,input().split()))for i in n*[0]]

    c=0
    while any(d):
        c+=1
        f=0
        skip=set()
        for i in range(1,n+1):
            if i not in skip:
                if d[i]:
                    if d[i][0] in skip:
                        continue
                    elif i==d[d[i][0]][0]:
                        oppo=d[i].popleft()
                        d[oppo].popleft()
                        skip.add(oppo)
                        skip.add(i)
                        f=1

        if not f:
            print(-1)
            exit()
        if time.time()-t>1:
            print(n*(n-1)//2)

    print(c)

if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import* #defaultdict Counter deque appendleft
import sys
input=sys.stdin.readline
import time
t=time.time()

def main():
    n=int(input())
    d=[[]]+[deque(map(int,input().split()))for i in n*[0]]

    c=0
    while any(d):
        c+=1
        f=0
        skip=set()
        for i in range(1,n+1):
            if i not in skip:
                if d[i]:
                    if d[i][0] in skip:
                        continue
                    elif i==d[d[i][0]][0]:
                        oppo=d[i].popleft()
                        d[oppo].popleft()
                        skip.add(oppo)
                        skip.add(i)
                        f=1

        if not f:
            print(-1)
            exit()
        if abs(time.time()-t)>1:
            print(n*(n-1)//2)
            exit()

    print(c)

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

# 外周Cメートル
# カウンター上には N 貫の寿司
# i 貫目は中橋くんがいる位置から x_i メートル時計回りに進んだ位置に置かれており、
# v_i キロカロリーの栄養価を持ちます
# 歩く際に 1 メートルあたり 1 キロカロリーを消費

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

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

#時計回りの場合の最大
CalR=[0 for i in range(N)]
for i in range(N):
    CalR[i]=sum(v[:i+1])-x[i]

MaxR=[0 for i in range(N)]

for i in range(N):
    MaxR[i]=max(CalR[:i+1])
    
MaxR=[0]+MaxR

#反時計回りの最大
CalL=[0 for i in range(N)]
for i in range(N):
    CalL[i]=sum(v[N-i-1:])-(C-x[N-i-1])

MaxL=[0 for i in range(N)]

for i in range(N):
    MaxL[i]=max(CalL[:i+1])

MaxL=[0]+MaxL

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

### FIXED CODE ###

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

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

#時計回りの場合の最大
CalR=[0 for i in range(N)]
CalR[0]=v[0]-x[0]
for i in range(1,N):
    CalR[i]=CalR[i-1]+v[i]-x[i]+x[i-1]

MaxR=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalR[i-1]>=MaxR[i-1]:
        MaxR[i]=CalR[i-1]
    else:
        MaxR[i]=MaxR[i-1]

#反時計回りの最大
CalL=[0 for i in range(N)]
CalL[0]=v[N-1]-(C-x[N-1])
for i in range(1,N):
    CalL[i]=CalL[i-1]+v[N-i-1]+x[N-i-1]-x[N-i]

MaxL=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalL[i-1]>=MaxL[i-1]:
        MaxL[i]=CalL[i-1]
    else:
        MaxL[i]=MaxL[i-1]

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))
### BUGGY CODE ###

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

hh = list(map(int, input().strip().split()))

rrr = [[0 for j in range(n)] for i in range(n)]

for i in range(m):
  a, b = map(int, input().strip().split())
  rrr[a-1][b-1] = hh[b-1]
  rrr[b-1][a-1] = hh[a-1]

c = 0
for a in range(n):
  if all(hh[a] > h for h in rrr[a]):
    c += 1
    
print(c)

### FIXED CODE ###

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

hh = list(map(int, input().strip().split()))

rr = [1] * n

for i in range(m):
  a, b = map(int, input().strip().split())
  if hh[a-1] <= hh[b-1]: 
    rr[a-1] = 0
  if hh[b-1] <= hh[a-1]:
    rr[b-1] = 0
    
print(sum(rr))
### BUGGY CODE ###

import sys
from  heapq import heappush, heappop, heapify

N=input()

par=[ i for i in range(N) ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y



X=[]
Y=[]
for i in range(N):
	x,y=map(int, sys.stdin.readline().split()) 
	X.append( (x,i) )
	Y.append( (y,i) )

X.sort(key=lambda x: x[0])
Y.sort(key=lambda x: x[0])

hq=[]

prev_x=None
prev_i=None
for x,i in X:
	if prev_x is not None:	
		heappush( hq, ( x-prev_x, i, prev_i) )
	prev_x=x
	prev_i=i

prev_x=None
prev_i=None
for x,i in Y:
	if prev_x is not None:	
		heappush( hq, ( x-prev_x, i, prev_i) )
	prev_x=x
	prev_i=i


ans=0
while hq:
	d,a,b=heappop(hq)
	if not same(a,b):
		ans+=d
		unite(a,b)
		
print ans


### FIXED CODE ###

# -*- coding: utf-8 -*-
import heapq
import sys
sys.setrecursionlimit(10**6)
N = input()
a = []
for i in range(N):
	x,y=map(int,raw_input().split())
	a.append((i,x,y))

#a => point,x,y
a1=sorted(a, key=lambda x: x[1])
a2=sorted(a, key=lambda x: x[2])


#Union Find
#初めは各頂点が根
par=[ i for i in range(N) ]
#木の根を求める
def root(x):
	if par[x]==x:
		return x
	else:
		r=root(par[x])
		#経路圧縮
		par[x]=r
		return r
#xとyが同じ集合に属するか
def same(x,y):
	return root(x)==root(y)

#xとyが同じ根に所属していた場合、連結
def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y:
		return
	else:
		par[x]=y


b=[]
for i in range(N-1):
	# len_x,x,y
	heapq.heappush( b, ( abs(a1[i+1][1]-a1[i][1]),a1[i][0],a1[i+1][0],  ))

for i in range(N-1):
	# len_y,x,y
	heapq.heappush( b, ( abs(a2[i+1][2]-a2[i][2]),a2[i][0],a2[i+1][0],  ))

#print "# b"
#print b


ans=0
cnt=0
for i in range(len(b)):
	if cnt==N-1:
		break
	val=heapq.heappop(b)
	#２点が同じ根でない場合は、木を結合する
	if not same(val[1],val[2]):
		unite(val[1],val[2])
		ans+=val[0]
		cnt+=1

print ans
### BUGGY CODE ###

import numpy as np
import sys
input = sys.stdin.buffer.readline

a, b, q = map(int,input().split())
ab = []
aa = []
bb = []
for i in range(a):
    x = int(input())
    ab.append([0, x])
    aa.append(x)
    
for i in range(b):
    x = int(input())
    ab.append([1, x])
    bb.append(x)
ab = sorted(ab, key=lambda x:(x[1], x[0]))

ab_arr = np.array(ab)
ab_arr = ab_arr.T[1]

a_arr = np.array(aa)
b_arr = np.array(bb)

for cq in range(q):
    ans = 10**18
    s = int(input())
    ci = np.searchsorted(ab_arr, s)
    for cj in [ci, ci-1]:
        if cj < 0 or cj >= a+b:
            continue
        d = abs(ab_arr[cj] - s)
        flg = ab[cj][0]
        if flg:
            nj = np.abs(a_arr - ab_arr[cj]).argmin()
            d += abs(a_arr[nj] - ab_arr[cj])
        else:
            nj = np.abs(b_arr - ab_arr[cj]).argmin()
            d += abs(b_arr[nj] - ab_arr[cj])
        ans = min(ans, d)
    print(ans)

### FIXED CODE ###

import numpy as np
import sys
input = sys.stdin.buffer.readline

a, b, q = map(int,input().split())
ab = [[-1, -10**11], [-1, 10**11]]
aa = [-10**11, 10**11]
bb = [-10**11, 10**11]
for i in range(a):
    x = int(input())
    ab.append([0, x])
    aa.append(x)
    
for i in range(b):
    x = int(input())
    ab.append([1, x])
    bb.append(x)
ab = sorted(ab, key=lambda x:(x[1], x[0]))
aa.sort()
bb.sort()

ab_arr = np.array(ab)
ab_arr = ab_arr.T[1]

a_arr = np.array(aa)
b_arr = np.array(bb)

for cq in range(q):
    ans = 10**12
    s = int(input())
    ci = np.searchsorted(ab_arr, s)
    for cj in [ci, ci-1]:
        d = abs(ab_arr[cj] - s)
        flg = ab[cj][0]
        if flg:
            na = np.searchsorted(a_arr, ab_arr[cj])
            d2 = min(abs(a_arr[na] - ab_arr[cj]), abs(a_arr[na-1] - ab_arr[cj]))
            d += d2
        else:
            nb = np.searchsorted(b_arr, ab_arr[cj])
            d2 = min(abs(b_arr[nb] - ab_arr[cj]), abs(b_arr[nb-1] - ab_arr[cj]))
            d += d2
        ans = min(ans, d)
    print(ans)
### BUGGY CODE ###

import numpy as np

A,B,Q = map(int, input().split())
S=np.zeros([A])
T=np.zeros([B])
X=np.zeros([Q])
for i in range(A):
  S[i] = int(input())
for i in range(B):
  T[i] = int(input())
for i in range(Q):
  x = int(input())
  dist_x_S = np.zeros([A,2])
  for j in range(A):
    dist_x_S[j][1] = S[j] - x
    dist_x_S[j][0] = np.abs(dist_x_S[j][1])
  sorted_dist_x_S = np.sort(dist_x_S,axis=0)
  if A > 1:
    nearest_x_S = np.array([sorted_dist_x_S[0][1],sorted_dist_x_S[1][1]])
  else:
    nearest_x_S = np.array([sorted_dist_x_S[0][1],float('inf')])
  
  dist_x_T = np.zeros([B,2])
  for j in range(B):
    dist_x_T[j][1] = T[j] - x
    dist_x_T[j][0] = np.abs(dist_x_T[j][1])
  sorted_dist_x_T = np.sort(dist_x_T,axis=0)
  if B > 1:
    nearest_x_T = np.array([sorted_dist_x_T[0][1],sorted_dist_x_T[1][1]])
  else:
    nearest_x_T = np.array([sorted_dist_x_T[0][1],float('inf')])
  
  ans_cand = []
  for x_S in nearest_x_S:
    for x_T in nearest_x_T:
      if x_S*x_T > 0:
        ans_cand.append(max(abs(x_S),abs(x_T)))
      else:
        ans_cand.append(abs(x_S)+abs(x_T)+min(abs(x_S),abs(x_T)))
  print(int(min(ans_cand)))

### FIXED CODE ###

mod = 1000000007
eps = 10**-9
inf = 10**15


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.buffer.readline

    A, B, Q = map(int, input().split())
    S = [-inf]
    for _ in range(A):
        S.append(int(input()))
    S.append(inf)
    T = [-inf]
    for _ in range(B):
        T.append(int(input()))
    T.append(inf)

    for _ in range(Q):
        x = int(input())
        i = bisect_left(S, x)
        j = bisect_left(T, x)
        ans = []
        ans.append(x - S[i-1] + abs(S[i-1] - T[j-1]))
        ans.append(x - S[i-1] + abs(S[i-1] - T[j]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i-1]))
        ans.append(x - T[j-1] + abs(T[j-1] - S[i]))
        ans.append(S[i] - x + abs(S[i] - T[j-1]))
        ans.append(S[i] - x + abs(S[i] - T[j]))
        ans.append(T[j] - x + abs(T[j] - S[i-1]))
        ans.append(T[j] - x + abs(T[j] - S[i]))
        print(min(ans))


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

from collections import deque
from itertools import product


def bfs(S):
    x, y = S
    dist = [[10 ** 9] * w for _ in range(h)]
    dist[y][x] = 0
    que = deque([S])

    while len(que) > 0:
        x, y = que.popleft()
        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
        num = product([-2, -1, 0, 1, 2], repeat=2)
        near_b = [(x + dx, y + dy) for dx, dy in num]

        for i, j in near_a:
            if (0 <= i < w and 0 <= j < h and
                    dist[j][i] > dist[y][x] and s[j][i] == '.'):
                dist[j][i] = dist[y][x]
                que.append((i, j))
        for i, j in near_b:
            if (0 <= i < w and 0 <= j < h and
                    dist[j][i] > dist[y][x] + 1 and s[j][i] == '.'):
                dist[j][i] = dist[y][x] + 1
                que.append((i, j))
    return dist


h, w = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
s = [input() for _ in range(h)]

ans = bfs((Cw - 1, Ch - 1))[Dh - 1][Dw - 1]
print(-1 if ans == 10 ** 9 else ans)


### FIXED CODE ###

from collections import deque
from itertools import product


def bfs(S):
    x, y = S
    dist = [[10 ** 9] * w for _ in range(h)]
    dist[y][x] = 0
    que = deque([S])

    while len(que) > 0:
        x, y = que.popleft()
        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
        num = product([-2, -1, 0, 1, 2], repeat=2)
        near_b = [(x + dx, y + dy) for dx, dy in num]

        for i, j in near_a:
            if (0 <= i < w and 0 <= j < h and
                    dist[j][i] > dist[y][x] and s[j][i] == '.'):
                dist[j][i] = dist[y][x]
                que.appendleft((i, j))
        for i, j in near_b:
            if (0 <= i < w and 0 <= j < h and
                    dist[j][i] > dist[y][x] + 1 and s[j][i] == '.'):
                dist[j][i] = dist[y][x] + 1
                que.append((i, j))
    return dist


h, w = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
s = [input() for _ in range(h)]

ans = bfs((Cw - 1, Ch - 1))[Dh - 1][Dw - 1]
print(-1 if ans == 10 ** 9 else ans)
### BUGGY CODE ###

N,K = map(int,input().split())
h = [int(i) for i in input().split()]
dp = [10**10 for _ in range(N)]
dp[0] = 0
for i in range(1,N):
    dp[i] = min([dp[i-k]+abs(h[i-k]-h[i]) for k in range(min(K,i)+1)])
        
print(dp[-1])

### FIXED CODE ###

import numpy as np
N,K = map(int,input().split())
h = np.array([int(i) for i in input().split()])
dp = np.full(N,10**10,dtype=int)
dp[0] = 0
for i in range(1,N):
    dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h[i:i+K] - h[i-1]))
        
print(dp[-1])
### BUGGY CODE ###

import math
from itertools import permutations

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    n=len(d[0])
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d


def dijkstra(s, n, w, cost):
    # 始点sから各頂点への最短距離
    # n:頂点数,　w:辺の数, cost[u][v] : 辺uvのコスト(存在しないときはinf)
    d = [float("inf")] * n
    used = [False] * n
    d[s] = 0

    while True:
        v = -1
        # まだ使われてない頂点の中から最小の距離のものを探す
        for i in range(n):
            if (not used[i]) and (v == -1):
                v = i
            elif (not used[i]) and d[i] < d[v]:
                v = i
        if v == -1:
            break
        used[v] = True

        for j in range(n):
            d[j] = min(d[j], d[v] + cost[v][j])
    return d


N,M,R=[int(x) for x in input().split()]
r=[int(x)-1 for x in input().split()]

route_list=[[0]*N for i in range(N)]

for i in range(M):
    a,b,c=[int(x) for x in input().split()]
    route_list[a-1][b-1]=c
    route_list[b-1][a-1]=c

#print(route_list)

wa_list=warshall_floyd(route_list)
r_list=[[0]*N for i in range(N)]
ans=10**9

for perm in permutations(r):
    cur = sum(wa_list[v][u] for v, u in zip(perm, perm[1:]))
    ans = min(ans, cur)


print(ans)


### FIXED CODE ###

import math
from itertools import permutations

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    n=len(d[0])
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

N,M,R=[int(x) for x in input().split()]
r=[int(x)-1 for x in input().split()]

route_list=[[10**9]*N for i in range(N)]

for i in range(M):
    a,b,c=[int(x) for x in input().split()]
    route_list[a-1][b-1]=c
    route_list[b-1][a-1]=c

wa_list=warshall_floyd(route_list)
ans=30**9

for perm in permutations(r):
    #print(perm)
    cur = sum(wa_list[v][u] for v, u in zip(perm, perm[1:]))
    ans = min(ans, cur)

print(ans)