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

if __name__ == "__main__":
    N, M = list(map(int, (input().split(' '))))
    ura = 0
    if N == 1 and M != 1:
        arr = [0 for i in range(N)]
        ura = M - 2
    elif N != 1 and M == 1:
        arr = [0 for i in range(M)]
        ura = N - 2
    elif N == 1 and M == 1:
        ura = 1
    else:
        arr = [[0 for i in range(M)] for j in range(N)]
        for i in range(N):
            for j in range(M):
                for buf_i in range(-1, 2):
                    for buf_j in range(-1, 2):
                        if i-buf_i < 0 or i+buf_i >= N or j-buf_j < 0 or j+buf_j >= M:
                            continue
                        if arr[i+buf_i][j+buf_j] == 0:
                            arr[i+buf_i][j+buf_j] = 1
                        else:
                            arr[i+buf_i][j+buf_j] = 0
        for i in range(N):
            for j in range(M):
                if arr[i][j] == 1:
                    ura += 1
    print(ura)

### FIXED CODE ###

if __name__ == "__main__":
    N, M = list(map(int, (input().split(' '))))
    ura = 0
    if N == 1 and M != 1:
        arr = [0 for i in range(N)]
        ura = M - 2
    elif N != 1 and M == 1:
        arr = [0 for i in range(M)]
        ura = N - 2
    elif N == 1 and M == 1:
        ura = 1
    else:
#        arr = [[0 for i in range(M)] for j in range(N)]
#        for i in range(N):
#            for j in range(M):
#                for buf_i in range(-1, 2):
#                    for buf_j in range(-1, 2):
#                        if i-buf_i < 0 or i+buf_i >= N or j-buf_j < 0 or j+buf_j >= M:
#                            continue
#                        if arr[i+buf_i][j+buf_j] == 0:
#                            arr[i+buf_i][j+buf_j] = 1
#                        else:
#                            arr[i+buf_i][j+buf_j] = 0
#        for i in range(N):
#            for j in range(M):
#                if arr[i][j] == 1:
#                    ura += 1
        ura = N * M - (N * 2 + M * 2 - 4)
    print(ura)
### BUGGY CODE ###


import sys
sys.setrecursionlimit(int(1e+6))
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]

results = [0 for _ in range(N)]

def repeat(node, counter, pre_node):
    counter += counters[node]
    for candidate in branch[node]:
        if candidate == pre_node:
            continue
        else:
            repeat(candidate, counter, node)
    results[node] = counter

repeat(0, 0, 0)

print((" ").join([str(i) for i in results]))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
N, Q = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(N-1)]
px = [list(map(int, input().split())) for _ in range(Q)]

branch = [[] for _ in range(N)]
for i in range(N-1):
    branch[ab[i][0]-1].append(ab[i][1]-1)
    branch[ab[i][1]-1].append(ab[i][0]-1)

counters = [0 for _ in range(N)]
for i in range(Q):
    counters[px[i][0]-1] += px[i][1]
results = [0 for _ in range(N)]

seen = [False for _ in range(N)]
from collections import deque
seen[0] = True
todo = deque([0])
results[0] = counters[0]

while len(todo)>0:
    checking = todo.pop()
    candidates = branch[checking]
    for candidate in candidates:
        if seen[candidate] == False:
            todo.append(candidate)
            seen[candidate] = True
            results[candidate] = results[checking]+counters[candidate]


print((" ").join([str(i) for i in results]))
### BUGGY CODE ###

import bisect
import sys
input = sys.stdin.readline
 
n = int(input())
l = list(map(int, input().split()))
l.sort()

cnt = 0
for i in range(n):
  a = l[i]
  for j in range(n-i-1):
    b = l[j+i+1]
    cnt += len([c for c in l[j+i+2:] if c < a + b])
print(cnt)

### FIXED CODE ###

import bisect
import sys
input = sys.stdin.readline
 
n = int(input())
l = [int(li) for li in input().split(' ')]
l.sort()

cnt = 0
for i in range(n):
  for j in range(i+1, n):
    cnt += bisect.bisect_left(l, l[i]+l[j])-j-1
    
print(cnt)
### BUGGY CODE ###

h,w=map(int,input().split())
T=[["#"]*(w+2)]
T.extend([["#"]+[s for s in input()]+["#"] for _ in range(h)])
T.append(["#"]*(w+2))
L=[[0]*w for _ in range(h)]
R=[[0]*w for _ in range(h)]
U=[[0]*w for _ in range(h)]
D=[[0]*w for _ in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if T[i][j]==".":
      if j==1:
        L[i-1][j-1]=1
      else:
        L[i-1][j-1]=L[i-1][j-2]+1
      if i==1:
        U[i-1][j-1]=1
      else:
        U[i-1][j-1]=U[i-2][j-1]+1
    if T[h+1-i][w+1-j]==".":
      if j==1:
        R[h-i][w-j]=1
      else:
        R[h-i][w-j]=R[h-i][w-j+1]+1
      if i==1:
        D[h-i][w-j]=1
      else:
        D[h-i][w-j]=D[h-i+1][w-j]+1
ans=0
for i in range(h):
  for j in range(w):
    ans=max(ans,L[i][j]+R[i][j]+D[i][j]+U[i][j]-3)
print(ans)

### FIXED CODE ###

h,w=map(int,input().split())
T=[["#"]*(w+2)]
T.extend([["#"]+[s for s in input()]+["#"] for _ in range(h)])
T.append(["#"]*(w+2))
L=[[0]*w for _ in range(h)]
R=[[0]*w for _ in range(h)]
U=[[0]*w for _ in range(h)]
D=[[0]*w for _ in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if T[i][j]==".":
      if j==1:
        L[i-1][j-1]=1
      else:
        L[i-1][j-1]=L[i-1][j-2]+1
      if i==1:
        U[i-1][j-1]=1
      else:
        U[i-1][j-1]=U[i-2][j-1]+1
    if T[h+1-i][w+1-j]==".":
      if j==1:
        R[h-i][w-j]=1
      else:
        R[h-i][w-j]=R[h-i][w-j+1]+1
      if i==1:
        D[h-i][w-j]=1
      else:
        D[h-i][w-j]=D[h-i+1][w-j]+1
ans=0
for i in range(h):
  for j in range(w):
    ans=max(ans,L[i][j]+R[i][j]+D[i][j]+U[i][j]-3)
print(ans)
### BUGGY CODE ###

from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [i for i in range(N+1)]
    PX = list()
    G[1] = 0  # 終点
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[b] = a  # 逆辺を張る
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [-1 for i in range(N+1)]
    values = deque()
    for p, x in PX:
        C[p] += x
    for i in range(1, N+1):
        node = i
        v = 0
        while node > 0:
            if V[node] > 0:  # use cache
                v += V[node]
                break
            v += C[node]
            node = G[node]
        V[i] = v 
        values.append(v)
    return ' '.join(map(str, values))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))


### FIXED CODE ###

import sys
sys.setrecursionlimit(200000)
from collections import deque


def read():
    N, Q = list(map(int, input().strip().split()))
    G = [list() for i in range(N+1)]
    PX = list()
    for i in range(N-1):
        a, b = list(map(int, input().strip().split()))
        G[a].append(b)
        G[b].append(a)
    for i in range(Q):
        px = list(map(int, input().strip().split()))
        PX.append(px)
    return N, Q, G, PX


def bfs(parent, G, C, V):
    q = deque()
    q.append(1)
    while len(q) > 0:
        parent = q.pop()
        V[parent] = True
        for child in G[parent]:
            if not V[child]:
                C[child] += C[parent]
                q.append(child)
    

def solve(N, Q, G, PX):
    C = [0 for i in range(N+1)]
    V = [False for i in range(N+1)]
    for p, x in PX:
        C[p] += x
    bfs(1, G, C, V)
    return ' '.join(map(str, C[1:]))
    

if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))
### BUGGY CODE ###

import sys
import numpy as np

input = sys.stdin.readline

# N 品物の数、 W 合計の重さ
n, w = map(int, input().split())

# (W V) W : 品物の重さ V：品物の価値
lst = [list(map(int, input().split())) for ii in range(n)]

# 価値 v を最大にするので、0に初期化
# １次元は品物（インデックス0には ALL0）、２次元は重さの合計
dp = np.zeros([n+1, w+1])

def retmax(a, b):
    if a > b:
        return a
    return b

for i, ln in enumerate(lst):
    # 品物の数分
    for j in range(w+1):
        # 重みの数まで
        # 選ばない。１つ前からセットする。
        dp[i+1, j] = retmax(dp[i+1, j], dp[i, j])
        if j >= ln[0]:
            # 品物を選べる
            dp[i+1, j] = retmax(dp[i+1, j], dp[i, j-ln[0]]+ln[1])
print(int(dp[n, w]))

### FIXED CODE ###

import sys
import numpy as np

input = sys.stdin.readline

# N 品物の数、 W 合計の重さ
n, w = map(int, input().split())

# (W V) W : 品物の重さ V：品物の価値
lst = [list(map(int, input().split())) for ii in range(n)]

# 価値 v を最大にするので、0に初期化
# １次元は品物（インデックス0には ALL0）、２次元は重さの合計
dp = np.zeros([n+1, w+1])

for i, ln in enumerate(lst):
    # 自分の重さが入るまでは１つ前の
    dp[i+1, :ln[0]] = dp[i, :ln[0]]
    # 自分を加えられる。選ばない時と価値の高いほうをセット
    dp[i+1, ln[0]:] = np.max([dp[i, ln[0]:], dp[i, :w-ln[0]+1]+ln[1]], axis=0)

print(int(dp[n, w]))
### BUGGY CODE ###

from sys import stdin


S = stdin.readline().rstrip()
T = stdin.readline().rstrip()

N = len(S)

ret = "Yes"

for i in range(0, N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            if T[i] != T[j]:
                # print("type1:",i,j, S[j],T[j])
                ret = "No"
        if T[i] == T[j]:
            if S[i] != S[j]:
                # print("type2:",i,j, S[j],T[j])
                ret = "No"

print(ret)


### FIXED CODE ###

S = input()
T = input()

d1 = dict()
d2 = dict()

ret = "Yes"

for s, t in zip(S, T):
    #print("{}-->{}".format(s, t))
    tmp1 = d1.get(s, None)
    tmp2 = d2.get(t, None)
    if (tmp1 == None or tmp1 == t) and (tmp2 == None or tmp2 == s):
        d1[s] = t
        d2[t] = s
    else:
        ret = "No"

print(ret)
### BUGGY CODE ###

import collections


def search(data, w, h, start, end):
    maps = [[0] * w for i in range(h)]
    move = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    que = collections.deque([(start[1], start[0])])
    maps[start[1]][start[0]] = 0
    while que:
        y, x = que.popleft()
        num = maps[y][x]
        for m in move:
            if 0 <= y+m[0] < h and 0 <= x+m[1] < w:
                if maps[y+m[0]][x+m[1]] != 0:
                    continue
                if data[y+m[0]][x+m[1]] == '#':
                    continue
                elif data[y+m[0]][x+m[1]] == '.':
                    maps[y+m[0]][x+m[1]] = num+1
                    que.append((y+m[0], x+m[1]))
    return maps[end[1]][end[0]]


def main():
    h, w = map(int, input().split())
    data = []
    for i in range(h):
        tmp = input()
        data.append([j for j in tmp])

    ans = 0
    for i in range(h*w-1):
        for j in range(i+1, h*w):
            if data[i//w][i % w] == '.' and data[j//w][j % w] == '.':
                ans = max(search(data, w, h, (i % w, i//w), (j % w, j//w)), ans)
    print(ans)


main()


### FIXED CODE ###

import collections


def search(data, w, h, start):
    maps = [[-1] * w for i in range(h)]
    move = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    que = collections.deque([(start[1], start[0])])
    maps[start[1]][start[0]] = 0
    result = 0
    while que:
        y, x = que.popleft()
        num = maps[y][x]
        for m in move:
            if 0 <= y+m[0] < h and 0 <= x+m[1] < w:
                if maps[y+m[0]][x+m[1]] != -1:
                    continue
                if data[y+m[0]][x+m[1]] == '#':
                    continue
                elif data[y+m[0]][x+m[1]] == '.':
                    maps[y+m[0]][x+m[1]] = num+1
                    result = max(result, maps[y+m[0]][x+m[1]])
                    que.append((y+m[0], x+m[1]))
    return result


def main():
    h, w = map(int, input().split())
    data = []
    for i in range(h):
        tmp = input()
        data.append([j for j in tmp])

    ans = 0
    for i in range(h*w-1):
        if data[i//w][i % w] == '.':
            ans = max(search(data, w, h, (i % w, i//w)), ans)
    print(ans)


main()
### BUGGY CODE ###


def main():
  import sys
  input = sys.stdin.readline

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

  from fractions import gcd
  from functools import reduce

  def lcm(x, y):
    return x * y // gcd(x, y)

  L = reduce(lcm, A)

  B = [None] * N

  for i in range(N):
    B[i] = (L // A[i]) % mod

  print(sum(B) % mod)

if __name__ == '__main__':
    main()

### FIXED CODE ###


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

from fractions import gcd

L = A[0]
for i in range(1,N):
  L=(L*A[i])//gcd(L,A[i])

ans = 0

for i in range(N):
  ans += (L // A[i])

print(ans % mod)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
from itertools import accumulate
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


H, N = zz()
A, B = [], []
for i in range(N):
    a, b = zz()
    A.append(a)
    B.append(b)
# B, A = zip(*sorted(zip(B, A)))

max_a = max(A)
dp = [[100000000]*(H + max_a + 1)] * (N+1)  # dp[i][j]: 魔法i種類で、HPをj減らすのに必要な魔力
#  初期化
for j in range(H):
    if (j % A[0] == 0):
        dp[0][j] = (j//A[0])*B[0]
    else:
        dp[0][j] = (j//A[0]+1)*B[0]
# print(dp)
# print()

for j in range(1, H + max_a + 1):
    for i in range(0, N):

        if(j < A[i]):
            dp[i][j] = min(dp[i-1][j], B[i])
        else:
            # if (j > 8):
            #     print(dp[i - 1][j],
            #           dp[i][j - A[i]] + B[i],
            #           dp[i-1][j-A[i]] + B[i])

            dp[i][j] = min(dp[i - 1][j],
                           dp[i][j - A[i]] + B[i],
                           dp[i-1][j-A[i]] + B[i])


# print(dp)
print(dp[N-1][H])


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
from itertools import accumulate
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


H, N = zz()
A, B = [], []
for i in range(N):
    a, b = zz()
    A.append(a)
    B.append(b)
# B, A = zip(*sorted(zip(B, A)))

max_a = max(A)
dp = [[100000000]*(H + max_a + 1)] * (N+1)  # dp[i][j]: 魔法i種類で、HPをj減らすのに必要な魔力
#  初期化
for j in range(H):
    if (j % A[0] == 0):
        dp[0][j] = (j//A[0])*B[0]
    else:
        dp[0][j] = (j//A[0]+1)*B[0]
# print(dp)
# print()

for j in range(1, H + max_a + 1):
    for i in range(0, N):

        if(j < A[i]):
            dp[i][j] = min(dp[i-1][j], B[i])
        else:
            # if (j > 8):
            #     print(dp[i - 1][j],
            #           dp[i][j - A[i]] + B[i],
            #           dp[i-1][j-A[i]] + B[i])

            dp[i][j] = min(dp[i - 1][j],
                           dp[i][j - A[i]] + B[i],
                           dp[i-1][j-A[i]] + B[i])


# print(dp)
print(dp[N-1][H])
### BUGGY CODE ###

N = int(input())
A = [0] + list(map(int, input().split())) + [0]
cost = [abs(A[i] - A[i + 1]) for i in range(N + 1)]
result = ""
for n in range(N):
    result += str(sum(cost[:n]) + abs(A[n] - A[n + 2]) + sum(cost[n + 2:]))
    if n != N - 1:
        result += '\n'
print(result)


### FIXED CODE ###

N = int(input())
A = [0] + list(map(int, input().split())) + [0]
cost = [abs(A[i] - A[i + 1]) for i in range(N + 1)]
start = 0
end = sum(cost[2:])
for n in range(N):
    print(start + abs(A[n] - A[n + 2]) + end)
    if n != N - 1:
        start += cost[n]
        end -= cost[n + 2]
### BUGGY CODE ###

N,M=map(int, input().split())
lis=[]
for i in range(M):
    lis.append(list(map(int,input().split())))
l_2=sorted(lis, key=lambda x: x[1])
dic={}
for i,j in l_2:
    dic[i]=dic.get(i,0)+1
    res='{:0=6}{:0=6}'.format(i,dic[i])
    lis[lis.index([i,j])]=res
for i in lis:print(i)

### FIXED CODE ###

def main():
    n, m = map(int, input().split())
    lines = [input() for j in range(m)]
 
    cities = {}
    for i in range(m):
        p, y = map(int, lines[i].split())
        if p in cities:
            cities[p].append((i, y))
        else:
            cities[p] = [(i, y)]
 
    outputs = [None] * m
    for pref, city_list in cities.items():
        city_list.sort(key=lambda x: x[1])
        #print(pref, city_list)
        city_no = 1
        for city in city_list:
            outputs[city[0]] = "{:06d}{:06d}".format(pref, city_no)
            city_no += 1
 
    for output in outputs:
        print(output)
    return
 
 
main()
### BUGGY CODE ###

s = input()
q = int(input())

rev = False

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                s = s+c
            else:
                s = c+s
        else:
            if rev:
                s = c+s
            else:
                s = s+c

print(s if not rev else s[::-1])

### FIXED CODE ###

s = input()
q = int(input())

rev = False

forward = ''
back = ''

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                back += c
            else:
                forward += c
        else:
            if rev:
                forward += c
            else:
                back += c

s = forward[::-1] + s + back
if rev:
    s = s[::-1]

print(s)
### BUGGY CODE ###

import numpy as np
from numba import njit, i8, u1, void, b1
import heapq

@njit(void(i8,i8,i8,i8,b1[:,:]),cache=True)
def solve(ch,cw,dh,dw,road_map):

    h,w = road_map.shape

    queue = [[0,ch*w+cw]]
    used_map = np.full(h*w,-1,dtype=np.int64)

    #move walk
    while used_map[dh*w+dw] == -1 and len(queue) != 0:
        dot = heapq.heappop(queue)
        tl,n = dot[0],dot[1]
        th,tw = divmod(n,w)
        if used_map[n] > -1 and used_map[n] <= tl:
            continue

        used_map[n] = tl

        for x in range(-2,3,1):
            for y in range(-2,3,1):
                if not(th+ x < 0 or tw+y < 0 or th+x >= h or tw+y >= w):
                    if used_map[(th+x)*w+tw+y] == -1 and road_map[th+x][tw+y] == False:
                        if x == y == 0:
                            continue
                        if abs(x)+abs(y) <= 1:
                            heapq.heappush(queue,[tl,(th+x)*w+tw+y])
                        else:
                            heapq.heappush(queue,[tl+1,(th+x)*w+tw+y])

    print(used_map[dh*w+dw])

h,w = map(int,input().split())
ch,cw = map(int,input().split())
ch,cw = ch-1,cw-1
dh,dw = map(int,input().split())
dh,dw = dh-1,dw-1
road_map = np.array([[c=="#" for c in input()] for _ in range(h)])

solve(ch,cw,dh,dw,road_map)



### FIXED CODE ###

#shotoyooさんのコードをお借りしてテスト

import numpy as np
from numba import njit, b1, i4, i8, f8
from heapq import heappop as hpp, heappush as hp

@njit((b1[:, :],i8,i8,i8,i8), cache=True)
def main(ss,ch,cw,dh,dw):
    inf = 1<<30
    h,w = ss.shape
    start = ch*w+cw
    goal = dh*w+dw
    n = h*w
    seen = np.full(n, inf, np.int64)
    seen[start] = 0
    q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置
    while q:
        pnum,pu = hpp(q)
        ux,uy = divmod(pu,w)
        if pu==goal:
            break
        for xx in range(-2, 3):
            for yy in range(-2, 3):
                if xx==yy==0:
                    continue
                if abs(xx)+abs(yy)<=1:
                    vv = 0
                else:
                    vv = 1
                x,y = ux+xx, uy+yy
                u = x*w + y
                num = pnum+vv
                if x<0 or y<0 or x>=h or y>=w or ss[x][y]:
                    continue
        #         print(x,y)
                if seen[u]>num:
                    seen[u] = num
                    hp(q,(num, u))
                
    val = seen[goal]
    if val==inf:
        return -1
    else:
        return val
      

h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh,dw = map(int, input().split())
ch -= 1
cw -= 1
dh -= 1
dw -= 1
ss = np.array([[c=="#" for c in input()] for _ in range(h)])

print(main(ss,ch,cw,dh,dw))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
total = 0
for i in range(n - 1):
    for j in range(i+1, n):
        total += (a[i] * a[j])
        if total >= 10 ** 9 + 7:
            total = total % (10 ** 9 + 7)
ans = total % (10 ** 9 + 7)
print(ans)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
a_sum = sum(a)
a_list = []
for num in a:
    a_sum -= num
    a_list.append(a_sum)
total = 0
for i in range(n-1):
    ans = a[i] * a_list[i]
    total += ans
    if total > 10 ** 9 + 7:
        total = total % (10 ** 9 + 7)
print(total)
### BUGGY CODE ###

import itertools
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

N=int(input())
data=prime_factorize(N)

ans=10**12
for i in range((len(data))//2+1):
  for subset in itertools.permutations(data, i):
    a=1
    for e in subset:
      a*=e
    b=N//a
    ans=min(ans,a+b)
print(ans-2)


### FIXED CODE ###

N=int(input())
import math
SQ=int(math.sqrt(N))
minval=10**12
a=1
b=1
for i in range(1,SQ+1):
  if N%i==0:
    newa=i
    newb=N//i
    if abs(newa-newb) < minval:
      minval=abs(newa-newb)
      a=newa
      b=newb

print(a+b-2)