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

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

cnt = 0
for i in range(n):
  cnt_j = 0
  for j in range(i+1):
    if p[i] <= p[j]:
      cnt_j += 1
    else:
      continue
  if cnt_j == i+1:
    cnt += 1
print(cnt)



### FIXED CODE ###

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

cnt = 0
p_min = p[0]
for i in range(n):
  pi = p[i]
  if pi <= p_min:
    cnt += 1
    p_min = pi
print(cnt)
### BUGGY CODE ###

N, W = map(int, input().split())
w, v = [], []
for _ in range(N):
    x, y = map(int, input().split())
    w.append(x)
    v.append(y)
from collections import defaultdict
dp = defaultdict(lambda: 0)
dp[0,0] = 0

for i in range(N):
    for weight in range(W+1):
        if weight >= w[i]:
            dp[i+1,weight] = max(dp[i,weight], dp[i,weight-w[i]]+v[i])
        else:
            dp[i+1,weight] = dp[i,weight]
print(dp[N,W])


### FIXED CODE ###

N, W = map(int, input().split())
Items = [tuple(map(int, input().split())) for _ in range(N)]
from collections import defaultdict as dd
Bag = dd(lambda:0)
Bag[0] = 0
for w,v in Items:
    temp = [(key+w,Bag[key]+v) for key in Bag if key + w <= W]
    for key,value in temp:
        Bag[key] = max(Bag[key],value)
ans = max(Bag.values())
print(ans)
### BUGGY CODE ###

import math
mod = 10**9+7
def comb(p,q):
    if p >= q:
        a = math.factorial(p)%mod
        b = math.factorial(q)%mod
        c = math.factorial(p-q)%mod
        return ((a*pow(b*c, mod-2, mod))%mod)
    else:
        return 0
n = int(input())
L = list(map(int,input().split()))
M = []
for i in range(n+1):
    M.append(L[i])
M.sort()
dou = 1 #重複する数
for i in range(n+1):
    if M[i] == dou:
        dou += 1
    else:
        dou = M[i]
        break
l = 0
r = 0
cnt = 0
for i in range(n+1):
    if L[i] == dou and cnt == 0:
        l = i+1
        cnt += 1
    elif L[i] == dou and cnt == 1:
        r = i+1
        break
for i in range(1, n+1):
    print(comb(n+1,i)-comb(l+n-r, i-1))
print(1)


### FIXED CODE ###

mod = 10**9+7
MAX = 10**5+1
fact = [1]*(MAX+1)
for i in range(1, MAX+1):
    fact[i] = (fact[i-1]*i)%mod
inv = [1]*(MAX+1)
for i in range(2, MAX+1):
    inv[i] = inv[mod%i]*(mod-mod//i)%mod
fact_inv = [1] * (MAX + 1)
for i in range(1, MAX + 1):
    fact_inv[i] = fact_inv[i-1]*inv[i]%mod
def comb(n, r):
    if n < r:
        return 0
    return fact[n]*fact_inv[n-r]*fact_inv[r] % mod

n = int(input())
L = list(map(int,input().split()))
M = []
for i in range(n+1):
    M.append(L[i])
M.sort()
dou = 1 #重複する数
for i in range(n+1):
    if M[i] == dou:
        dou += 1
    else:
        dou = M[i]
        break
l = 0
r = 0
cnt = 0
for i in range(n+1):
    if L[i] == dou and cnt == 0:
        l = i+1
        cnt += 1
    elif L[i] == dou and cnt == 1:
        r = i+1
        break
for i in range(1,n+1):
    print((comb(n+1,i)-comb(l+n-r,i-1))%mod)
print(1)
### BUGGY CODE ###

import math
S = int(input())
x = int(math.ceil(math.sqrt(S)))
Z = x * x - S
z = int(math.ceil(math.sqrt(Z)))

if z == 0:
    oz = 0
else:
    while True:
        oz = Z // z
        if Z % z == 0:
            break
        z += 1

print("0 0 {} {} {} {}".format(x, z, oz, x))


### FIXED CODE ###

import math
def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct

S = int(input())
x = int(math.ceil(math.sqrt(S)))
Z = x * x - S

if Z == 0:
    z = 0
    oz = 0
else:
    fs = factorize(Z)
    z = 1
    oz = 1
    for k, c in fs:
        while c >= 2:
            z *= k
            oz *= k
            c -= 2
        if c > 0:
            if z < oz:
                z *= k
            else:
                oz *= k




print("0 0 {} {} {} {}".format(x, z, oz, x))
### BUGGY CODE ###

import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,K,Q= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
l=np.zeros(N)
l+=K
#print(l)
for m in range(Q):
    A = int(input())
    l-=1
    l[A-1]+=1
#    print(l)

for i in np.where(l<1,'No','Yes'):
    print(i)


### FIXED CODE ###

import numpy as np
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with

#A = int(input())
N,K,Q= map(int,input().split())
#A = list(map(int,input().split()))
#B = list(map(int,input().split()))
#S= str(input())
l=np.zeros(N)
l+=(K-Q)
#print(l)
for m in range(Q):
    A = int(input())
    l[A-1]+=1
#    print(l)
l=np.where(l<1,'No','Yes')
for i in l:
    print(i)
### BUGGY CODE ###

import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=[[] for i in range(n+1)]
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
itta=[0]*(n+1)
ta=deque([u])
ao=deque([v])
itta[v]=1
cnt=0
while 1:
    ittata=[0]*(n+1)
    for i in range(len(ta)):
        tmp=ta.popleft()
        if itta[tmp]==1:
            continue
        for i in dic[tmp]:
            if itta[i]==0 and ittata[i]==0:
                ittata[i]=1
                ta.append(i)
    if len(ta)==0:
        print(cnt)
        break
    cnt+=1
    for i in range(len(ao)):
        tmp=ao.popleft()
        for i in dic[tmp]:
            if itta[i]==0:
                itta[i]=1
                ao.append(i)
    # print(ta)
    # print(ao)

### FIXED CODE ###

import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n,u,v=map(int,input().split())
ab=[list(map(int,input().split())) for i in range(n-1)]

dic=defaultdict(list)
for i in range(n-1):
    a,b=ab[i]
    dic[a].append(b)
    dic[b].append(a)
# print(dic)

taka=[-1]*(n+1)
aoki=[-1]*(n+1)

d=deque([[u,0]])
if len(dic[u])==1:
    taka[u]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if taka[dic[now][i]]==-1:
            taka[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        taka[now]=cnt
# print(taka)

d=deque([[v,0]])
if len(dic[v])==1:
    aoki[v]=0
while d:
    now,cnt=d.popleft()
    flag=0
    for i in range(len(dic[now])):
        if aoki[dic[now][i]]==-1:
            aoki[dic[now][i]]=-2
            flag=1
            d.append([dic[now][i],cnt+1])
    if flag==0:
        aoki[now]=cnt
# print(aoki)

ans=0
for i in range(n+1):
    if taka[i]<aoki[i]:
        ans=max(ans, aoki[i]-1)
print(ans)
### BUGGY CODE ###

N=int(input())

for n in range(1,N+1):
    ans = 0
    for i in range(1,int(n**0.5)):
        for j in range(i,int(n**0.5)):
            for k in range(j,int(n**0.5)):
                if i**2+j**2+k**2+i*j+j*k+k*i==n:
                    if i == j and j == k:
                        ans += 1
                    else:
                        ans += 3
    print(ans)


### FIXED CODE ###

N=int(input())
ans = [0]*10000000
for i in range(1,100):
    for j in range(1,100):
        for k in range(1,100):
            ans[i**2+j**2+k**2+i*j+j*k+i*k] += 1
for i in range(1,N+1):
    print(ans[i])
### BUGGY CODE ###

n=int(input())
a= list(map(int, input().split()))
li=[]
for i in range(n-1,-1,-1):
    check=0
    for j in range(n//(i+1)):
        check+=(i+1)*(j+1) in li
    if check%2!=a[i]:
        li.append(i+1)
print(len(li))
print(" ".join(map(str,li)))

### FIXED CODE ###

n=int(input())
a= list(map(int, input().split()))
li=[0 for _ in range(n)]
for i in range(n-1,-1,-1):
    check=0
    for j in range(n//(i+1)):
        check^=li[(i+1)*(j+1)-1]
    if check!=a[i]:
        li[i]+=1
ans=[i+1 for i, x in enumerate(li) if x == 1]
print(len(ans))
print(" ".join(map(str,ans)))
### BUGGY CODE ###

import functools
N, W = map(int, input().split())
v, w = [0] * N, [0] * N
for i in range(N):
    v[i], w[i] = map(int, input().split())


@functools.lru_cache(maxsize=None)
def rec(i, weight):
    if i == N:
        return 0
    elif weight + w[i] <= W:
        return max(rec(i+1, weight), rec(i+1, weight + w[i]) + v[i])
    else:
        return rec(i+1, weight)
print(rec(0, 0))

### FIXED CODE ###

N, maxW = map(int, input().split())
V, W = [], []
for i in range(N):
    v, w = map(int, input().split())
    V.append(v); W.append(w)
dp = [[0]*(maxW+1) for _ in range(2)]
for i in range(N):
    for j in range(maxW+1):
        if j < W[i]:
            dp[(i+1)&1][j] = dp[i&1][j]
        else:
            dp[(i+1)&1][j] = max(dp[i&1][j], dp[i&1][j-W[i]] + V[i])
print(dp[N&1][maxW])
### BUGGY CODE ###

from bisect import*
def main():
    root=[[],[]]
    def search_higher(key):
        ptr=root
        ret=None
        while ptr[1]:
            i=bisect(ptr[0],key)
            if i!=len(ptr[0]):ret=ptr[0][i]
            ptr=ptr[1][i]
        i=bisect(ptr[0],key)
        if i!=len(ptr[0]):ret=ptr[0][i]
        return ret
    def search_lower(key):
        ptr=root
        ret=None
        while ptr[1]:
            i=bisect_left(ptr[0],key)
            if i!=0:ret=ptr[0][i-1]
            ptr=ptr[1][i]
        i=bisect_left(ptr[0],key)
        if i!=0:ret=ptr[0][i-1]
        return ret
    def insert_rec(ptr,key):
        b_size=768
        if not ptr[1]:
            insort(ptr[0],key)
            if len(ptr[0])==b_size*2-1:
                ret=[ptr[0][:b_size],[]]
                ptr[0]=ptr[0][b_size:]
                return ret
        else:
            i=bisect(ptr[0],key)
            tmp=insert_rec(ptr[1][i],key)
            if tmp:
                ptr[0].insert(i,tmp[0].pop())
                ptr[1].insert(i,tmp)
                if len(ptr[1])==b_size*2:
                    ret=[ptr[0][:b_size],ptr[1][:b_size]]
                    ptr[0],ptr[1]=ptr[0][b_size:],ptr[1][b_size:]
                    return ret
    def insert(key):
        tmp=insert_rec(root,key)
        if tmp:root[0],root[1]=[tmp[0].pop()],[tmp,root]
    n,*a=map(int,open(0).read().split())
    l=[0]*n
    for i,v in enumerate(a,1):l[v-1]=i
    insert(0)
    insert(n+1)
    c=0
    for i,v in enumerate(l,1):
        c+=(search_higher(v)-v)*(v-search_lower(v))*i
        insert(v)
    print(c)
main()

### FIXED CODE ###

root=lambda x:x if par[x]==x else root(par[x])
same=lambda x,y:root(x)==root(y)
def unite(*z):
  x,y=map(root,z)
  if x!=y:
    if rank[x]<rank[y]:
      par[x]=y
      size[y]+=size[x]
    else:
      par[y]=x
      size[x]+=size[y]
      if rank[x]==rank[y]:rank[x]+=1
n,*a=map(int,open(0).read().split())
*par,=range(n)
rank=[1]*n
size=[1]*n
b=[0]*n
for i,t in enumerate(a):b[t-1]=i
c=0
for k in b[::-1]:
  l=r=1
  if k>0 and a[k-1]>a[k]:
    l+=size[root(k-1)]
    unite(k-1,k)
  if k+1<n and a[k+1]>a[k]:
    r+=size[root(k+1)]
    unite(k+1,k)
  c+=l*r*a[k]
print(c)
### BUGGY CODE ###

from heapq import *

def calc():
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(a) for a in input().split()]

    h = []
    for i in range(N):
        heappush(h, (-B[i], i))

    ans = 0
    while h:
        b, i = heappop(h)
        b = -b
        if b == A[i]:
            continue
        k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
        if k <= 0:
            return -1
        B[i] -= (B[i-1] + B[(i+1)%N]) * k
        heappush(h, (-B[i], i))
        ans += k
    return ans
print(calc())

### FIXED CODE ###

N = int(input())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
if N > 10000:
    A = A[5050:] + A[:5050]
    B = B[5050:] + B[:5050]
ans = -1

D = [0] * N
L = [i-1 for i in range(N)]
R = [i+1 for i in range(N)]

def calc():
    i = 0
    c = 0
    ans = 0
    while c < N:
        while i < N:
            if B[i] == A[i]:
                if D[i] == 0:
                    c += 1
                    D[i] = 1
                if L[i] >= 0: R[L[i]] = R[i]
                if R[i] < N: L[R[i]] = L[i]
            if B[i] > A[i] and B[i] >= max(B[i-1], B[(i+1)%N]):
                k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
                B[i] -= (B[i-1] + B[(i+1)%N]) * k
                ans += k
                if k <= 0:
                    return -1
            i = R[i]
        i = N-1
        while i >= 0:
            if B[i] == A[i]:
                if D[i] == 0:
                    c += 1
                    D[i] = 1
                if L[i] >= 0: R[L[i]] = R[i]
                if R[i] < N: L[R[i]] = L[i]
            if B[i] > A[i] and B[i] >= max(B[i-1], B[(i+1)%N]):
                k = - (-(B[i] - max(B[i-1], B[(i+1)%N], A[i])) // (B[i-1] + B[(i+1)%N]))
                B[i] -= (B[i-1] + B[(i+1)%N]) * k
                ans += k
                if k <= 0:
                    return -1
            i = L[i]
    return ans

print(calc())
### BUGGY CODE ###

import numpy as np

n,m,qq  = map(int,input().split())
xlist = np.array([[0 for j in range(n)] for i in range(n)])
for i in range(m):
    l,r = map(int,input().split())
    xlist[l-1,r-1]+=1


sumsumlist = np.cumsum(np.cumsum(xlist,axis=1),axis=0)


for i in range(qq):
    p,q = map(int,input().split())
    if p>1:
        ans = sumsumlist[q-1][q-1] - sumsumlist[q-1][p-2] - sumsumlist[p-2][q-1] + sumsumlist[p-2][p-2]
    elif p==1:
        ans = sumsumlist[q-1][q-1]

    print(ans)


### FIXED CODE ###

from collections import Counter
from itertools import product

n,m,q  = map(int,input().split())
rails=[tuple(map(int,input().split())) for i in range(m)]
qs=[tuple(map(int,input().split())) for i in range(q)]

xlist = [[0 for j in range(n)] for i in range(n)]
sumlist = [[0 for j in range(n+1)] for i in range(n)]
for (l,r),i in Counter(rails).items():
    xlist[l-1][r-1]=i


for (i,j) in product(range(n),range(n)):
    sumlist[i][j+1]=sum(xlist[i][0:j+1])


for (p,q) in qs:
    ans = 0
    for i in range(p-1,q):
        ans = ans+sumlist[i][q]-sumlist[i][p-1]
    print(ans)
### BUGGY CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
sc = [1]*(N-K+1)
for j in range(N-K+1):
  for i in range(K):
    sc[j] = sc[j]*A[j+i]
    
for j in range(N-K):
  if sc[j+1] > sc[j]:
    print('Yes')
  else:
    print('No')

### FIXED CODE ###

N,K = map(int,input().split())
A = list(map(int,input().split()))
    
for j in range(N-K):
  if A[j+K] > A[j]:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

raw_input('').split()
A = [int(x) for x in raw_input('').split()]
max = 0
msp = A[0]
for i in range(0,len(A)-1):
  if msp <= A[i]:
    continue
  else:
    msp = A[i]
    for j in range(i+1,len(A)):
        pf_t = A[j] - A[i]
        if pf_t > max:
          max = pf_t
          spot = [(i,j)]
        elif pf_t == max:
          spot.append((i,j))
print len(spot)

### FIXED CODE ###

raw_input('').split()
A = [int(x) for x in raw_input('').split()]
max = 0
count = 0
pl = 0
msp = A[0]
for i in range(1,len(A)):
  pl_t = pl + A[i] - A[i-1]
  if pl_t <= 0:
    pl = 0
  else:
    pl = pl_t
    if max < pl_t:
      max = pl_t
      count = 1
    elif max == pl_t:
      count += 1
print count
### BUGGY CODE ###

n = int(input())
dv = []
dved = []
for i in range(1,n//2):
    if n%i==0 and not(i in dved):
        dv.append(i)
        dved.append(n//i) 
    if i in dved:
        break
print(len(str(dved[-1]))if len(dv)>0 else 1)


### FIXED CODE ###

import math
n = int(input())
dv = []
dved = []
for i in range(1,int(math.sqrt(n)+1)):
    if n%i==0 and not(i in dved):
        dv.append(i)
        dved.append(n//i) 
    if i in dved:
        break
print(len(str(dved[-1]))if len(dv)>0 else 1)
### BUGGY CODE ###

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**9
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

K = int(input())
S = input()
n = len(S)
ans = 0
for i in range(K+1):
    ans += cmb(i+n-1, n-1, mod) * pow(25, i) * pow(26, (K-i))
print(ans%mod)

### FIXED CODE ###

# F - Strivore
import sys
MOD = 10**9+7
K = int(input())
S = input()
l = len(S)
ans = 0
 
tmp = pow(26,K,MOD)
ans = tmp
waru = pow(26,-1,MOD)
 
for i in range(1,K+1):
    tmp = (tmp*25*waru)%MOD
    tmp = (tmp*(l-1+i)*pow(i,-1,MOD))%MOD
    ans = (ans+tmp)%MOD
 
print(ans)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
 
maxi = A[0]
for  i in range(N):
    if A[i] >= maxi:
        maxi = A[i]
    else:
        maxi = maxi
        
count = 0
position = 0
while position < N:
  for j in range(1,maxi+1):
    for i in range(position,N):
      if A[i] == j:
        count = count + 1
        break
      else:
        position = position + 1
 
if count != 0:
    print(N-count)
else:
    print(-1)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
count = 0
position = 0
while position < N:
  for j in range(1,N+1):
    for i in range(position,N):
      if A[i] == j:
        count = count + 1
        position += 1
        break
      else:
        position = position + 1

if count != 0:
    print(N-count)
else:
    print(-1)
### BUGGY CODE ###

from functools import reduce
import math
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
def modinv(a, m):
    g, x, y = xgcd(a, m)
    return x % m
def modmul(a,b):
    return a * b % 998244353
def comb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(modmul, range(n, n - r, -1))
    under = reduce(modmul, range(1,r + 1))
    return over * modinv(under, 998244353)
def mempow(a,b):
    temp = 1
    yield temp
    for i in range(b):
        temp = temp * a % 998244353
        yield temp

N,M,K = (int(x) for x in input().split())
mem = []
for x in mempow(M-1,N-1):
    mem.append(x)
ans = 0
if M == 1:
    if K + 1 < N:
        print('0')
    else:
        print('1')
else:
    for i in range(K+1):
        ans = (ans + (comb(N-1,i)*M*mem[N-i-1])) % 998244353
    print(ans)

### FIXED CODE ###

from functools import reduce
import math
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
def modinv(a, m):
    g, x, y = xgcd(a, m)
    return x % m
def modmul(a,b):
    return a * b % 998244353
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)
def mempow(a,b):
    temp = 1
    yield temp
    for i in range(b):
        temp = temp * a % 998244353
        yield temp
def memfact(a):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % 998244353
        yield temp

N,M,K = (int(x) for x in input().split())
memp = []
memf = []
mpappend = memp.append
mfappend = memf.append
for x in mempow(M-1,N-1):
    mpappend(x)
for x in memfact(N-1):
    mfappend(x)
ans = 0
if M == 1:
    if K + 1 < N:
        print('0')
    else:
        print('1')
else:
    i = 0
    for i in range(K+1):
        ans = (ans + (comb(N-1,i,998244353)*M*memp[N-i-1])) % 998244353
    print(ans)
### BUGGY CODE ###

from heapq import heappush, heappop

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        ret += 1
        _, i = heappop(h)
        B[i] -= B[(i-1)%N] + B[(i+1)%N]
        if B[i] == A[i] :
            continue
        elif B[i] < A[i] :
            return -1
        else :
            heappush(h, (-B[i], i))
        
    return ret
    
print(solve())

### FIXED CODE ###

from heapq import heappush, heappop

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
       
        m, M = B[(i-1)%N], B[(i+1)%N]
        if m > M :
            m, M = M, m
        
        if M >= A[i] :
            x = (B[i] + m - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        else :
            if (B[i] - A[i]) % (m + M) != 0 :
                return -1
            ret += (B[i] - A[i]) // (m + M)
            B[i] = A[i]
        
        if A[i] > B[i] :
            return -1
            
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    return ret
    
print(solve())
### BUGGY CODE ###

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 11:57:24 2020

@author: liang
"""

H, W = map(int, input().split())
sy, sx = map(int, input().split())
sy -= 1
sx -= 1
gy, gx = map(int, input().split())
gy -= 1
gx -= 1
field = [input() for _ in range(H)]

from collections import deque
q = deque()
adj1 = [(0,1), (0,-1), (1,0), (-1,0)]
visited = [[False]*W for _ in range(H)]

q.append((sy, sx, 0))

while q:
    cy, cx, cost = q.popleft()
    #print(cy,cx,cost)
    if (cy,cx) == (gy,gx):
        print(cost)
        break
    if not visited[cy][cx]:
        #print("visit!")
        visited[cy][cx] = True
        #cost 0
        for a in adj1:
            ny, nx = cy + a[0], cx + a[1]
            if ny >= H or ny < 0:
                continue
            if nx >= W or nx < 0:
                continue
            if field[ny][nx] == "#":
                continue
            if not visited[ny][nx]:
                q.appendleft((ny, nx, cost))
        #cost 1
        for i in range(-2,3):
            for j in range(-2,3):
                ny, nx = cy + i, cx + j
                if ny >= H or ny < 0:
                    continue
                if nx >= W or nx < 0:
                    continue
                if field[ny][nx] == "#":
                    continue
                if not visited[ny][nx]:
                    q.append((ny, nx, cost+1))
else:
    print(-1)

### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 11:57:24 2020

@author: liang
"""

H, W = map(int, input().split())
sy, sx = map(int, input().split())
sy -= 1
sx -= 1
gy, gx = map(int, input().split())
gy -= 1
gx -= 1
field = [input() for _ in range(H)]

from collections import deque
q = deque()
adj1 = [(0,1), (0,-1), (1,0), (-1,0)]
visited = [[False]*W for _ in range(H)]

q.append((sy, sx, 0))

while q:
    cy, cx, cost = q.popleft()
    #print(cy,cx,cost)
    if (cy,cx) == (gy,gx):
        print(cost)
        break
    if not visited[cy][cx]:
        #print("visit!")
        visited[cy][cx] = True
        #cost 0
        for a in adj1:
            ny, nx = cy + a[0], cx + a[1]
            if ny >= H or ny < 0:
                continue
            if nx >= W or nx < 0:
                continue
            if field[ny][nx] == "#":
                continue
            if not visited[ny][nx]:
                q.appendleft((ny, nx, cost))
        #cost 1
        for i in range(-2,3):
            for j in range(-2,3):
                ny, nx = cy + i, cx + j
                if ny >= H or ny < 0:
                    continue
                if nx >= W or nx < 0:
                    continue
                if field[ny][nx] == "#":
                    continue
                if not visited[ny][nx]:
                    q.append((ny, nx, cost+1))
else:
    print(-1)