Harryxun/stf_run huggingface dataset:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""
n=  int(sys.stdin.readline())
p=list(range(2,n))
if n ==1:
    print(0)
else:
    print(n*(n-1)//2)

### FIXED CODE ###

import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""
n=  int(sys.stdin.readline())
if n ==1:
    print(0)
elif n == 2:#なぜ？
    print(1)
else:
    print(n*(n-1)//2)
import math
N, M = list(map(int, input().split(" ")))
abc = [[0, 0, 0] for i in range(M)]
for i in range(M):
  abc[i][0], abc[i][1] = list(map(int, input().split(" ")))
  c = list(map(int, input().split(" ")))
  for j in range(N):
    cbits = (1 << j) * (j+1 in c)
    abc[i][2] |= cbits

dp = [0 if i == 0 else math.inf for i in range(2**N)]
for i in range(M):
  for j in range(2**N):
    dp[j|AB[i][2]] = min(dp[j]+AB[i][0], dp[j|AB[i][2]])
print(dp[-1] if dp[-1] != math.inf else -1)

### FIXED CODE ###

N, M = list(map(int, input().split(" ")))
abc = [[0, 0, 0] for i in range(M)]
for i in range(M):
  abc[i][0], abc[i][1] = list(map(int, input().split(" ")))
  c = list(map(int, input().split(" ")))
  for j in c:
    cbits = (1 << j-1) 
    abc[i][2] |= cbits

dp = [0 if i == 0 else float("inf") for i in range(2**N)]
for i in range(M):
  for j in range(2**N):
    if dp[j|abc[i][2]] > dp[j]+abc[i][0]:
      dp[j|abc[i][2]] = dp[j]+abc[i][0]
print(dp[-1] if dp[-1] != float("inf") else -1)
from collections import Counter

N,*A = map(int,open(0).read().split())
#j - i = Aj + Ai
#j - Aj = Ai + i
#L = i + Ai, R = j - Aj とおく
L = []
R = []
for i in range(N):
    L.append(i+1 + A[i])
    R.append(i+1 - A[i])
l = Counter(L)
r = Counter(R)
 
cnt = 0
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i] * r[i]
        a.append(i)
print(cnt)

### FIXED CODE ###

from collections import Counter

N,*A = map(int,open(0).read().split())
#j - i = Aj + Ai
#j - Aj = Ai + i
#L = i + Ai, R = j - Aj とおく
L = []
R = []
for i in range(N):
    L.append(i+1 + A[i])
    R.append(i+1 - A[i])
l = Counter(L)
r = Counter(R)
 
#print(l)
#print(r)

cnt = 0
a=[]
for i in range(1,N+1):
    if i in l and i in r:
        cnt += l[i]*r[i]
        a.append(i)
print(cnt)
ref=[63,6,91,79,102,109,125,39,127,111]
while 1:
	n=int(raw_input())
	if n==-1:break
	e=0
	for i in range(n):
		a=ref[int(raw_input())]
		print "{:07b}".format(a^e)
		e=a

### FIXED CODE ###

ref=[63,6,91,79,102,109,125,39,127,111]
while 1:
	try:
		n=input()
		if n==-1:break
		e=0
		for i in range(n):
			a=ref[input()]
			print "{:07b}".format(a^e)
			e=a
	except:
		pass
a = int(input())
s = string(input())

if a >= 3200:
  print(s)
elif:
  print('red')  

### FIXED CODE ###

a = int(input())
s = str(input())

if a >= 3200:
  print(s)
else:
  print('red')
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

N, M = map(int, input().split())
graph = [ [0 for _ in range(M)] for _ in range(M) ]
visited = [False] * M

for i in range(N):
    a, b = map(int, input().split())
    graph[a-1][b-1] = 1
    graph[b-1][a-1] = 1

#print(graph)

# u は 0-index
def dfs(u):
    visited[u] = True
    if False in visited:
        pass
    else:
        return 1

    count = 0
    for i in range(M):
        if graph[u][i] == False: continue
        if visited[i]: continue

        visited[i] = True
        count += dfs(i)
        visited[i] = False

    return count

count = 0
count = dfs(0)
print(count)

### FIXED CODE ###


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

# N = 頂点の数(= 数字の数)
# M = 辺の数(= aの行数)

N, M = map(int, input().split())
graph = [ [0 for _ in range(N)] for _ in range(N) ]
visited = [False] * N

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

#print(graph)

# u は 0-index
def dfs(u):
    visited[u] = True
    if False in visited:
        pass
    else:
        return 1

    count = 0
    for i in range(N):
        if graph[u][i] == False: continue
        if visited[i]: continue

        visited[i] = True
        count += dfs(i)
        visited[i] = False

    return count

count = dfs(0)
print(count)
import sys;sys.setrecursionlimit(int(1e7))
from sys import stdin
from collections import deque
 
input = stdin.readline
N = int(input())
tree = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int,input().split())
    tree[a].append((b,c))
    tree[b].append((a,c))
 
Qn,K = map(int,input().split())
XY = [tuple(map(int,input().split())) for _ in range(Qn)]
 
depth = [None]*(N+1)
def DFS(v,p,d):
    depth[v] = d
    for e in tree[v]:
        if e[0] != p:
            DFS(e[0], v, d + e[1])
DFS(K,0,0)
 
for i in range(Qn):
    print(depth[XY[i][0]] + depth[XY[i][1]])

### FIXED CODE ###

S=list(map(int, input()))

if S[0]==S[2]:
    print('Yes')
else:
    print('No')
N,M = map(int, input().split())
appear_list = [[]for i in range(N)]
flag = 0
for i in range(M):
  s,c = map(int, input().split())
  appear_list[s-1].append(str(c))
ans = ""

for i in range(len(appear_list)):
  if len(set(appear_list[i])) > 1:
    flag = 1
    break
  elif len(set(appear_list[i])) == 1: 
    if appear_list[i][0] == "0" and len(ans) == 0:
      flag = 1
      break
    ans += appear_list[i][0]
  else:
    if len(ans) > 0:
	    ans += "0"
if flag == 0:
  print(int(ans))
else:
  print(-1)
  
  

### FIXED CODE ###

N,M = map(int, input().split())

appear_list = [[]for i in range(N)]
flag = 0
ans = ""
for i in range(M):
  s,c = map(int, input().split())
  if len(appear_list[s-1]) == 0:
    appear_list[s-1].append(str(c))
  if len(appear_list[s-1]) ==1 and appear_list[s-1][0] != str(c):
    flag = 1
    break

for i in range(len(appear_list)):
  if i == 0:
    if len(appear_list[i]) == 0 and i != N-1:
      ans += "1"
    elif len(appear_list[i]) == 0 and i == N-1:
      ans += "0"
    elif appear_list[i][0] == "0" and i != N-1:
      flag = 1
      break
    else:
      ans += appear_list[i][0]
  else:
    if len(appear_list[i]) == 0:
      ans += "0"
    else:
      ans += appear_list[i][0]
if flag == 0:
  print(int(ans))
else:
  print(-1)
def solve(n, traffic_list):
    min_val = min(traffic_list)
	if n % min_val == 0:
        return n // min_val + len(traffic_list) - 1
    else:
        return n // min_val + len(traffic_list)


def main():
    n = int(input())
    a = int(input())
    b = int(input())
    c = int(input())
    d = int(input())
    e = int(input())

    ans = solve(n, [a, b, c, d, e])
    print(ans)


main()


### FIXED CODE ###

def solve(n, traffic_list):
    min_val = min(traffic_list)
    if n % min_val == 0:
        return n // min_val + len(traffic_list) - 1
    else:
        return n // min_val + len(traffic_list)


def main():
    n = int(input())
    a = int(input())
    b = int(input())
    c = int(input())
    d = int(input())
    e = int(input())

    ans = solve(n, [a, b, c, d, e])
    print(ans)


main()
S=input()
T=input()
result=len(T)
for s in range(0,len(S)-len(T)+1):
	diff=0
	for i in range(0,len(T)):
		if T[i]!=S[s+i]:
			diff+=1
	result=min(ans,diff)

print(result)


### FIXED CODE ###

S=input()
T=input()
result=len(T)
for s in range(0,len(S)-len(T)+1):
	diff=0
	for i in range(0,len(T)):
		if T[i]!=S[s+i]:
			diff+=1
	result=min(result,diff)

print(result)
import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

ans = []
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[i-1],T[i]]:
            d1 = abs(x-s) + abs(s-t)
            d2 = abs(x-t) + abs(s-t)
            d = min(d,d1,d2)
    ans.append(d)
print(*ans,sep="\n")

### FIXED CODE ###

import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for i in range(A)]+[INF]
T = [-INF]+[int(input()) for i in range(B)]+[INF]
for q in range(Q):
    x = int(input())
    i = bisect.bisect_right(S,x)
    j = bisect.bisect_right(T,x)
    d = INF
    for s in [S[i-1],S[i]]:
        for t in [T[j-1],T[j]]:
            d1 = abs(s-x)+abs(s-t)
            d2 = abs(t-x)+abs(s-t)
            d = min(d,d1,d2)
    print(d)
x,n=map(int, input().split())
if n == 0:
  print(x)
  exit()
p=list(map(int, input().split()))
all=set(range(1,101))
s_p=set(p)

min_dis=100
for i in all-s_p:
  dis=abs(i-x)
  if min_dis > dis:
    min_i=i
    min_dis=dis
print(min_i)

### FIXED CODE ###

x,n=map(int, input().split())
if n == 0:
  print(x)
  exit()
p=list(map(int, input().split()))
all=set(range(-100,102))
s_p=set(p)
kouho=sorted(all-s_p)

min_dis=201
for i in kouho:
  dis=abs(i-x)
  if min_dis > dis:
    min_i=i
    min_dis=dis
print(min_i)
n = int(input())

for i in range(1, 9)
	for j in range(1, 9)
    	if n == i * j:
        	print("Yes")
            break

print("No")


### FIXED CODE ###

n = int(input())
ans = ('No')
for i in range(1,10):
    for j in range(1,10):
        if i * j == n:
            ans = ('Yes')
            break
print(ans)
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
D = 1000000007
n, m = map(int, input().split())
a = [0] * (10**5 + 3)
for i in range(m):
    a[int(input())] += 1

# 初期値
s0 = 1
if (a[1] == 1):
    s1 = 0
else:
    s1 = 1


def calc(l, acc1, acc2):
    # print("calc(l, acc1, acc2):" + str(l) + str(acc1) + str(acc2))
    if (l == 1):
        return acc2
    else:
        oldacc2 = acc2
        acc2 = (acc1 + acc2) % D if a[n - l + 2] == 0 else 0
        acc1 = oldacc2
        return calc(l - 1, acc1, acc2)


print(calc(n, s0, s1))


### FIXED CODE ###

#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
D = 1000000007
n, m = map(int, input().split())
a = [0] * (10**5 + 1)
for i in range(m):
    a[int(input())] += 1

# 初期値
s = [0] * (n + 1)
s[0] = 1
if (a[1] == 1):
    s[1] = 0
else:
    s[1] = 1

# 暫定結果を格納する配列


def calc(n):
    if(a[n] == 1):
        return 0
    else:
        return (s[n - 1] + s[n - 2]) % D


def solver(n):
    for i in range(2, n + 1):
        s[i] = calc(i)


solver(n)
print(s[n])
def nabeatsu(n):
    if(n % 3 == 0):
        return True
    temp = n
    while(temp > 0):
        if(temp % 10 == 3):
            return True
        temp = temp // 10
    return False

x = int(input())
ans = []
for i in range(x):
    if(nabeatsu(i + 1)):
        ans.append(i + 1)
print(' '.join(ans))


### FIXED CODE ###

def nabeatsu(n):
    if(n % 3 == 0):
        return True
    temp = n
    while(temp > 0):
        if(temp % 10 == 3):
            return True
        temp = temp // 10
    return False

x = int(input())
for i in range(x):
    if(nabeatsu(i + 1)):
        print(' ', end = '')
        print(i + 1, end = '')
print()
A, B = map(int, input().split())
S = input()
if is_integer(S[:A+1]) and S[A+1] == '-' and is_integer(S[A+2:]):
  print('Yes')
else:
  print('No')


### FIXED CODE ###

def is_int(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

A, B = map(int, input().split())
S = input()
if is_int(S[:A]) and S[A] == '-' and is_int(S[A+1:]):
  print('Yes')
else:
  print('No')
D, T, S = map(int, input.split())
if (D / S) <= T:
  print("Yes")
else:
  print("No")

### FIXED CODE ###

D, T, S = map(int, input().split())
print("Yes" if (D / S) <= T else "No")
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
loop = []
start = 0
index = 0
check = [-1] * N
fin = False
count = 0
while True:
    if check[index] != -1:
        start = check[index]
        break
    else:
        loop.append(index)
        if len(loop) == K + 1:
            print(index + 1)
            fin = True
            break
        check[index] = count
        count += 1
        index = a[index]

if not fin: 
    if (K + 1 - start) % len(loop[start:]) == 0:
        print(loop[-1] + 1)
    else:
        print(loop[start + (K + 1 - start) % len(loop[start:]) - 1] + 1)


### FIXED CODE ###

S = input()
T = input()
print('Yes' if T[:len(T) - 1] == S else 'No')
x = raw_input().split(" ")
a = x[0]
b = x[1]

print a * b, 2*(a + b)

### FIXED CODE ###

x = raw_input().split(" ")
a = int(x[0])
b = int(x[1])
print a * b, 2*(a + b)
# coding: utf-8

a, b, c = map(int, input().split())

x = a*10 + b
y = b*10 + c
z = c*10 + a

if x >= y and x >= z:
    print(x + c)
elif y >= x and y >= z:
    print(y + a)
else
    print(z + b)

### FIXED CODE ###

# coding: utf-8
 
a, b, c = map(int, input().split())
 
x = a*10 + b
y = b*10 + c
z = c*10 + a
 
if x >= y and x >= z:
    print(x + c)
elif y >= x and y >= z:
    print(y + a)
else:
    print(z + b)
n = input()
words_org = []
for i in range(int(n)):
  words_org.append(input())
print(words_org)

countDic = { k: words_org.count(k) for k in set(words_org) }
ranking = sorted([item for item in countDic.items()], key = lambda x:x[1], reverse=True)
order = 0
top = ranking[0][1]
candidates = []
while ranking[order][1] == top:
  candidates.append(ranking[order][0])
  order += 1
for word in sorted(candidates):
  print(word)

### FIXED CODE ###

N = input()
dic = {}
max = 0
for i in range(int(N)):
  word = input()
  if word not in dic.keys():
    
    if max == 0:
      max += 1
    dic[word] = 1
    
  else:
    
    if max == dic[word]:
      max += 1
    dic[word] += 1

candidates = sorted([word for word in dic.keys() if dic[word] == max])
for word in candidates:
  print(word)
N = int(input())
 S = str(input())

 white = 0
 black = 0

 first_black_index = 0

 while S[first_black_index] == ".":
   first_black_index += 1
   if first_black_index == N:
     print(0)
     break
 if first_black_index != N:
   S = S[first_black_index:N+1]

   if S[len(S)-1] == "#":
     N = N-1


   for i in range(len(S)):
     if S[i] == "#":
       black +=1
     else:
       white +=1

   print(min(white,black))


### FIXED CODE ###

N = int(input())
S = str(input())

white = S.count(".")
black = 0
ans = S.count(".")

for i in range(N):
  if S[i] == ".":
    white -=1
  else:
    black +=1
  ans = min(ans,white+black)

print(ans)
bun = input()
str_bun = list(bun)
str_bun[0,1,2,3,] = "2","0","1","8"
str_changed = "".join(str_bun)
print(str_bun)


### FIXED CODE ###

bun = input()
bunbun = bun.replace("2017", "2018")
print(bunbun)
#coding:utf-8
n = input()
h = input()
w = input()

print((n-h+1)*(n-w+1))


### FIXED CODE ###

#coding:utf-8
n = input()
h = input()
w = input()

print((int(n)-int(h)+1)*(int(n)-int(w)+1))
n, l = (int(i) for i in input().solit())
ans = 0
flg = 0
min = 1000
for i in range(n) :
  sum =  l + (i + 1) - 1
  if sum == 0 :
    flg = 1
  elif min > sum :
    min = sum
  ans += sum
if flg == 1 :
  print(ans)
else :
  print(ans - sum)

### FIXED CODE ###

n, l = (int(i) for i in input().split())
ans = 0
flg = 0
temp = 1000
for i in range(n) :
  sum =  l + (i + 1) - 1
  if sum == 0 :
    flg = 1
  elif temp > abs(sum) :
    temp = abs(sum)
    min = sum
  ans += sum
if flg == 1 :
  print(ans)
else :
  print(ans - min)
a, b, k = map(int, input().split())

ans =[]
for i in range(60, 0, -1):
    if a % i == 0 and b % i == 0:
        ans.append(i)
print(ans[k-1])


### FIXED CODE ###

a, b, k = map(int, input().split())

ans =[]
for i in range(1000, 0, -1):
    if a % i == 0 and b % i == 0:
        ans.append(i)
print(ans[k-1])
import sys
input = sys.stdin.readline
import math
import numpy as np
def print_ans(A, B):
    """Test Case
    >>> print_ans(2, 3)
    6
    >>> print_ans(123, 456)
    18696
    >>> print_ans(100000, 99999)
    9999900000
    """
    def lcm(a, b):
        g = math.gcd(a, b)
        c = a * b
        return c // g
    print(lcm(A, B))
if __name__ == '__main__':
    A, B = map(int, input().rstrip().split())
    print_ans(A, B)


### FIXED CODE ###

import sys
input = sys.stdin.readline
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
def print_ans(A, B):
    """Test Case
    >>> print_ans(2, 3)
    6
    >>> print_ans(123, 456)
    18696
    >>> print_ans(100000, 99999)
    9999900000
    """
    print(A * B // gcd(A, B))
if __name__ == '__main__':
    # test
    A, B = map(int, input().rstrip().split())
    print_ans(A, B)
# coding: utf-8

def North(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[3]
    new_dice[1] = dice[0]
    new_dice[2] = dice[2]
    new_dice[3] = dice[5]
    new_dice[4] = dice[4]
    new_dice[5] = dice[1]
    return new_dice

def East(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[4]
    new_dice[1] = dice[1]
    new_dice[2] = dice[0]
    new_dice[3] = dice[3]
    new_dice[4] = dice[5]
    new_dice[5] = dice[2]
    return new_dice

def West(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[2] #3
    new_dice[1] = dice[1] #5
    new_dice[2] = dice[5] #6
    new_dice[3] = dice[3] #2
    new_dice[4] = dice[0] #1
    new_dice[5] = dice[4] #4
    return new_dice

def South(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[1] #5
    new_dice[1] = dice[5] #6
    new_dice[2] = dice[2] #3
    new_dice[3] = dice[0] #1
    new_dice[4] = dice[4] #4
    new_dice[5] = dice[3] #2
    return new_dice

def Right(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[0] #1
    new_dice[1] = dice[4] #4
    new_dice[2] = dice[1] #5
    new_dice[3] = dice[2] #3
    new_dice[4] = dice[3] #2
    new_dice[5] = dice[5] #6
    return new_dice

def Left(dice):
    new_dice = [0,0,0,0,0,0]
    new_dice[0] = dice[0] #1
    new_dice[1] = dice[2] #3
    new_dice[2] = dice[3] #2
    new_dice[3] = dice[4] #4
    new_dice[4] = dice[1] #5
    new_dice[5] = dice[5] #6
    return new_dice

while True:
    n = int(raw_input())
    if n == 0:
        break
        dice = [1,5,3,2,4,6] # ??? ??? ??± ??? ?\? ??? ??????
        sum = 1
        for i in range(n):
            instruct = raw_input()
            if instruct == "North":
                dice = North(dice)
                elif instruct == "East":
                    dice = East(dice)
                elif instruct == "South":
                    dice = South(dice)
                elif instruct == "West":
                    dice = West(dice)
                elif instruct == "Right":
                    dice = Right(dice)
                elif instruct == "Left":
                    dice = Left(dice)
                #print("** {:} **".format(dice[0]))
                        sum += dice[0]
        print(sum)

### FIXED CODE ###

while(1):
    num=int(raw_input())
    if num==0:
        break
    else:
        d=[0,1,2,3,4,5,6]
        sum=1
        for i in range(num):
            dir=num=raw_input()
            if dir=="North":
                d=[0,d[2],d[6],d[3],d[4],d[1],d[5]]
            elif dir=="East":
                d=[0,d[4],d[2],d[1],d[6],d[5],d[3]]
            elif dir=="West":
                d=[0,d[3],d[2],d[6],d[1],d[5],d[4]]
            elif dir=="South":
                d=[0,d[5],d[1],d[3],d[4],d[6],d[2]]
            elif dir=="Right":
                d=[0,d[1],d[3],d[5],d[2],d[4],d[6]]
            else:
                d=[0,d[1],d[4],d[2],d[5],d[3],d[6]]
            sum=sum+d[1]
        print sum
# from line_profiler import LineProfiler
from collections import deque
from sys import stdin

def main():
    while(True):
        W, H = map(int, sys.stdin().split())
        if not W: break
        tx,ty = map(int, sys.stdin().split())
        kx,ky = map(int, sys.stdin().split())
        ma = [[False]*(W+2) for _ in range(H+2)]
        for i in range(1,H+1):
            for j,a in enumerate(sys.stdin().split()):
                ma[i][j+1] = bool(1-int(a))
        que = deque([[tx,ty,kx,ky,0]])
        pas = set()
        ans = "NA"
        while(que):
            tx,ty,kx,ky,c = que.popleft()
            if c > 100: break
            if tx == kx and ty == ky:
                ans = c
                break
            for dx,dy in [[-1,0],[0,-1],[1,0],[0,1]]:
                tdx, tdy = [ dx, dy] if ma[ty+dy][tx+dx] else [0,0]
                kdx, kdy = [-dx,-dy] if ma[ky-dy][kx-dx] else [0,0]
                if (tx+tdx,ty+tdy,kx+kdx,ky+kdy) in pas: continue
                que.append([tx+tdx,ty+tdy,kx+kdx,ky+kdy,c+1])
                pas.add((tx+tdx,ty+tdy,kx+kdx,ky+kdy))
        print(ans)

if __name__ == "__main__":
    # prf = LineProfiler()
    # prf.add_function(main)
    # prf.runcall(main)
    # prf.print_stats()
    main()


### FIXED CODE ###

# from line_profiler import LineProfiler
from collections import deque
from sys import stdin

def main():
    while(True):
        W, H = map(int, stdin.readline().split())
        if not W: break
        tx,ty = map(int, stdin.readline().split())
        kx,ky = map(int, stdin.readline().split())
        ma = [[False]+[bool(1-int(a)) for a in stdin.readline().split()]+[False] for i in range(H)]
        ma = [[False]*(W+2)] + ma + [[False]*(W+2)]
        que = deque([[tx,ty,kx,ky,0]])
        pas = set()
        ans = "NA"
        while(que):
            tx,ty,kx,ky,c = que.popleft()
            if tx == kx and ty == ky:
                ans = c
                break
            if c+1 > 100: continue
            for dx,dy in [[-1,0],[0,-1],[1,0],[0,1]]:
                tdx, tdy = ( dx, dy) if ma[ty+dy][tx+dx] else (0,0)
                kdx, kdy = (-dx,-dy) if ma[ky-dy][kx-dx] else (0,0)
                if (tx+tdx,ty+tdy,kx+kdx,ky+kdy) in pas: continue
                que.append([tx+tdx,ty+tdy,kx+kdx,ky+kdy,c+1])
                pas.add((tx+tdx,ty+tdy,kx+kdx,ky+kdy))
        print(ans)

if __name__ == "__main__":
    # prf = LineProfiler()
    # prf.add_function(main)
    # prf.runcall(main)
    # prf.print_stats()
    main()