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

s = input()
s_list = list(s)
while True:
    l5 = s_list[-5:]
    l6 = s_list[-6:]
    l7 = s_list[-7:]
    s5 = s6 = s7 = ''

    for ch in l5:
        s5 += ch
#    print("s5 :", s5)
    for ch in l6:
        s6 += ch
#    print("s6 :", s6)
    for ch in l7:
        s7 += ch
#    print("s7 :", s7)

    if (s5 == 'erase') | (s5 == 'dream'): 
        s_list = s_list[:-5]
#       print("s_list:", s_list)
    elif s6 == 'eraser':
        s_list = s_list[:-6]
#       print("s_list:", s_list)
    elif s7 == 'dreamer':
        s_list = s_list[:-7]
#       print("s_list:", s_list)
    else:
        print('NO')
#       print("s_list:", s_list)
        exit()

    if  s_list == []:
        print('YES')
        exit()



### FIXED CODE ###

s = input()
while True:
    temp = s
    s5 = temp[-5:]
    s6 = temp[-6:]
    s7 = temp[-7:]

    if (s5 == 'erase') | (s5 == 'dream'): 
        s = temp[:-5]
    elif s6 == 'eraser':
        s = temp[:-6]
    elif s7 == 'dreamer':
        s = temp[:-7]
    else:
        print('NO')
        exit()

    if s == '':
        print('YES')
        exit()
### BUGGY CODE ###

import sys
N,K=map(int,sys.stdin.readline().split())
A=[[int(e) for e in sys.stdin.readline().split()] for _ in range(N)]
x,y=zip(*A)
X,Y=map(sorted,[x,y])
r=(X[-1]-X[0])*(Y[-1]-Y[0])
for i in range(N):
 for j in range(i+1,N):
  for k in range(N):
   for l in range(k+1,N):
    lx=X[i];rx=X[j];ly=Y[k];ry=Y[l]
    if K<=sum(lx<=x[z]<=rx and ly<=y[z]<=ry for z in range(N)): r=min(r,(rx-lx)*(ry-ly))
print(r)

### FIXED CODE ###

import sys
N,K=map(int,sys.stdin.readline().split())
A=sorted([int(e) for e in sys.stdin.readline().split()] for _ in range(N))
x,y=zip(*A)
Y=sorted(y)
r=(x[-1]-x[0])*(Y[-1]-Y[0])
for i in range(N):
 for j in range(i+1,N):
  for k in range(N):
   for l in range(k+1,N):
    ly=Y[k];ry=Y[l];q=(x[j]-x[i])*(ry-ly)
    if r>q and K<=(j-i+1) and K<=sum(ly<=y[z]<=ry for z in range(i,j+1)): r=q
print(r)
### BUGGY CODE ###

N,K,Q=map(int,input().split())
P=[K]*N
for i in range(Q):
    a=int(input())
    for n in range(N):
        if n!=a-1:P[n]-=1
for i in range(N):
    if P[i]>0:print("Yes")
    else:print("No")

### FIXED CODE ###

N,K,Q=map(int,input().split())
P=[K-Q]*N
for i in range(Q):
    a=int(input())
    P[a-1]+=1
for i in range(N):
    if P[i]>0:print("Yes")
    else:print("No")
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())
graph = [[] for _ in range(n)]
point = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
for _ in range(q):
    a,b = map(int,input().split())
    a = a -1
    point[a]+=b
def dfs(now, prev = -1):
    for next in graph[now]:
        if next == prev:
            continue
        point[next] += point[now]
        dfs(next, now)
dfs(0)
print(*point)

### FIXED CODE ###

n,q = map(int,input().split())
graph = [[] for _ in range(n)]
point = [0]*n
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
for _ in range(q):
    a,b = map(int,input().split())
    a = a -1
    point[a]+=b
stack = [];
stack.append(0)
visited = [False]*n
while stack:
    x = stack.pop()
    visited[x] = True
    for y in graph[x]:
        if visited[y] == False:
            point[y] += point[x]
            stack.append(y)
print(*point)
### BUGGY CODE ###

from fractions import gcd
 
k=int(input())
ans=0
for i in range(1,k+1):
  for j in range(1,k+1):
    for k in range(1,k+1):
      ans+=gcd(i,gcd(j,k))
print(ans)

### FIXED CODE ###

from math import gcd
def solve():
    K = int(input())

    ans = 0
    for a in range(1,K+1):
        for b in range(1,K+1):
            for c in range(1,K+1):
                ans += gcd(gcd(a,b), c)
    
    print(ans)

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

import sys,queue,copy,heapq
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

N,K = inpl()

hlist = inpl()

dp = [[] for _ in range(N)]
for i in range(N):
    heapq.heapify(dp[i])


for i in range(N):
    if i == 0 :
        heapq.heappush(dp[i],0)
        continue
    for j in range(1,K+1):
        if i-j < 0:
            break
        heapq.heappush(dp[i],(dp[i-j][0]+abs(hlist[i]-hlist[i-j])))

print(dp[N-1][0])


### FIXED CODE ###

import numpy as np
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
INF = float('inf')

N,K = inpl()

hlist = np.array(inpl())

dp = np.full(N,INF)

dp[0]=0

for i in range(1,N):
    s = max(0,i-K)
    dp[i] = np.min(dp[s:i]+abs(hlist[i]-hlist[s:i]))

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

import sys

sys.setrecursionlimit(10 ** 9)


def rec(arr, l, lst):
    # lstは操作の逆順
    # print(arr, l, lst[::-1])
    if l == 0:
        for p in lst[::-1]:
            print(p)
        exit()
        return
    for i, aa in enumerate(arr, 1):
        # 1-indexed
        if aa == i:
            # print(aa, '除去')
            # 最後に入れた数は、何番目と数の大きさが一致する
            rec(arr[:i - 1] + arr[i:], l - 1, lst + [i])
    return


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

rec(b, N, [])

print(-1)

### FIXED CODE ###

import sys

sys.setrecursionlimit(10 ** 9)


def rec(arr, l, lst):
    # lstは操作の逆順
    # print(arr, l, lst[::-1])
    if l == 0:
        for p in lst[::-1]:
            print(p)
        exit()
        return

    for i in sorted(range(1, l + 1), reverse=True):
        # 1-indexed
        if arr[i-1] == i:
            # print(aa, '除去')
            # 最後に入れた数は、何番目と数の大きさが一致する
            rec(arr[:i - 1] + arr[i:], l - 1, lst + [i])
        elif arr[i-1] > i:
            # 何番目よりも数が大きい
            return
    return


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

rec(b, N, [])

print(-1)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

XOR=[0]
for a in A:
    XOR.append(XOR[-1]^a)

ZEROS=[0]
for xor in XOR:
    if xor==0:
        ZEROS.append(ZEROS[-1]+1)
    else:
        ZEROS.append(ZEROS[-1])

if XOR[-1]!=0:
    SUBJ=XOR[-1]
    SUBJ_LIST=[]

    for j in range(N+1):
        if XOR[j]==SUBJ:
            SUBJ_LIST.append(j)

    #print(SUBJ_LIST)

    ZERO=1
    NOW=0

    for i in range(len(SUBJ_LIST)):
        ZERO+=NOW*(ZEROS[SUBJ_LIST[i]]-ZEROS[SUBJ_LIST[i-1]])
        NOW+=ZERO

        #print(NOW,ZERO)

    print(ZERO%mod)

else:
    SET=set(XOR)
    
    from collections import defaultdict
    SUBJ=defaultdict(list)
    for j in range(1,N+1):
        SUBJ[XOR[j]].append(j)

    ANS=1<<(ZEROS[-1]-2)

    for subj in SUBJ:
        if subj==0:
            continue
        ZERO=1
        NOW=0

        for i in range(len(SUBJ[subj])):
            
            ZERO=(ZERO+NOW*(ZEROS[SUBJ[subj][i]]-ZEROS[SUBJ[subj][i-1]]))%mod
            NOW=(NOW+ZERO)%mod

        #print(ZERO,SUBJ[subj],subj)
        ANS=(ANS+NOW)%mod

    print(ANS%mod)


### FIXED CODE ###

import sys
input = sys.stdin.readline

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

XOR=[0]
for a in A:
    XOR.append(XOR[-1]^a)

ZEROS=[0]
for xor in XOR:
    if xor==0:
        ZEROS.append(ZEROS[-1]+1)
    else:
        ZEROS.append(ZEROS[-1])

if XOR[-1]!=0:
    SUBJ=XOR[-1]
    SUBJ_LIST=[]

    for j in range(N+1):
        if XOR[j]==SUBJ:
            SUBJ_LIST.append(j)

    #print(SUBJ_LIST)

    ZERO=1
    NOW=0

    for i in range(len(SUBJ_LIST)):
        ZERO+=NOW*(ZEROS[SUBJ_LIST[i]]-ZEROS[SUBJ_LIST[i-1]])
        NOW+=ZERO

        #print(NOW,ZERO)

    print(ZERO%mod)

else:
    SET=set(XOR)
    
    from collections import defaultdict
    SUBJ=defaultdict(list)
    for j in range(1,N+1):
        SUBJ[XOR[j]].append(j)

    ANS=1<<(ZEROS[-1]-2)

    for subj in SUBJ:
        if subj==0:
            continue
        ZERO=1
        NOW=0

        for i in range(len(SUBJ[subj])):
            
            ZERO=(ZERO+NOW*(ZEROS[SUBJ[subj][i]]-ZEROS[SUBJ[subj][i-1]]))%mod
            NOW=(NOW+ZERO)%mod

        #print(ZERO,SUBJ[subj],subj)
        ANS=(ANS+NOW)%mod

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

value = 0
value_list = []
result_list = []
number_of_test = int(input())
while True:
	try:
		value = int(input())
		value_list.append(value)
		if value_list.count(value) == 2:
			while value in value_list: value_list.remove(value)
	except EOFError:
		break
print(len(value_list))


### FIXED CODE ###

import collections

cnt = int(input())
array = []
for i in range(cnt):
    array.append(int(input()))
cnt_dict = collections.Counter(array)

res = 0
for j in cnt_dict.values():
    if j % 2 != 0:
        res += 1
print(res)
### BUGGY CODE ###

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

dp=[[-1]*(W+1) for i in range(N+1)]
dp[0][0]=0
for i in range(N):
    for w_sum in range(W+1):
        #品物を入れる場合
        if 0<=w_sum-w[i]<=W and dp[i][w_sum-w[i]]!=-1:
            dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum-w[i]]+v[i])
        #入れない場合
        dp[i+1][w_sum]=max(dp[i+1][w_sum],dp[i][w_sum])
print(max(dp[-1]))
#print(dp)

### FIXED CODE ###

n,w=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
#[w,v]
#重さに対応するインデックスに価値を保存する
dp=[[0]*(w+1) for i in range(n+1)]
 
for i in range(n):
    for sum_w in range(1,w+1):
        if sum_w-l[i][0]>=0:
            dp[i+1][sum_w]=max(dp[i+1][sum_w],dp[i][sum_w-l[i][0]]+l[i][1])
        #i番目の品を入れる場合
    
        dp[i+1][sum_w]=max(dp[i][sum_w],dp[i+1][sum_w])
        #i番目の品を入れない場合
print(dp[-1][-1])
### BUGGY CODE ###

# import bisect
from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    X=int(input())
    d=deque([[0,0]])

    while True:
        dd=d.popleft()
        if dd[0]==X:
            print(dd[1])
            break
        else:
            d.append([dd[1]+1+dd[0],dd[1]+1])
            d.append([dd[0],dd[1]+1])
            d.append([-(dd[1]+1)+dd[0],dd[1]+1])






resolve()

### FIXED CODE ###

def resolve():

    X=int(input())
    cnt=1
    while True:
        val=cnt*(cnt+1)//2
        if val>=X:
            print(cnt)
            break
        else:
            cnt += 1


resolve()
### BUGGY CODE ###

import sys
from fractions import gcd
import bisect
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

k, s = na()
ans = 0
for x in range(k+1):
        for y in range(k+1):
                for z in range(k+1):
                        if x + y + z == s:
                                ans += 1
                                break
print(ans)




### FIXED CODE ###

import sys
from fractions import gcd
import bisect
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

k, s = na()
ans = 0
for x in range(k+1):
        for y in range(k+1):
                z = s - x - y
                if z <= k and z >= 0:
                        ans += 1
print(ans)
### BUGGY CODE ###

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

MOD = 10 ** 9 + 7

lst = [0] * 65
for a in A:
    tmp = a
    for i in range(61):
        lst[i] += tmp % 2
        tmp //= 2
        if tmp == 0:
            break

ans = 0
for a in A:
    tmp = a
    for i in range(61):
        n = tmp % 2
        tmp //= 2
        if n == 1:
            ans += pow(2, i) * (N - lst[i])
        else:
            ans += pow(2, i) * lst[i]

print (ans // 2 % MOD)

### FIXED CODE ###

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

MOD = 10 ** 9 + 7

lst = [0] * 65
for a in A:
    tmp = a
    for i in range(61):
        lst[i] += tmp % 2
        tmp //= 2
        if tmp == 0:
            break

calc = [1]
for i in range(65):
    calc.append((calc[-1] * 2) % MOD)

ans = 0
for a in A:
    tmp = a
    for i in range(61):
        n = tmp % 2
        tmp //= 2
        if n == 1:
            ans += calc[i] * (N - lst[i])
        else:
            ans += calc[i] * lst[i]
    ans %= MOD

def modinv(a, m): #mod m でのaの逆元 a^{-1}を計算する
    b = m
    u = 1
    v = 0
    while b:
        t = a//b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    
    u %= m
    if u < 0:
        u += m
    return u

# print (ans)
ans *= modinv(2, MOD)
print (ans % MOD)
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
DP = [1] * (2 * 10 ** 6)
DP[0] = A[0]
for i in range(1, K):
    DP[i] = DP[i - 1] * A[i]


for i in range(K, N):
    DP[i] = DP[i - 1] * A[i] // A[i - K]
    if DP[i] > DP[i - 1]:
        print("Yes")
    else:
        print("No")


### FIXED CODE ###

import sys
#import numpy as np
N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
# DP = [1] * (2 * 10 ** 6)
# DP[K - 1] = np.prod(A[0:K])

for i in range(K, N):
    if A[i] / A[i - K] > 1:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

a = int(input())
fanwei = []

for i in range(a):
    one = input().split()
    one = [int(i) for i in one]
    two = [one[0] - one[1], one[0] + one[1]]
    fanwei.append(two)

fanwei = sorted(fanwei, key=lambda student: student[0] + 100000000000000000000 * student[1])

maxer = 0
for i in range(len(fanwei)):
    now = -100000000000000000000
    ans = 0
    for i in fanwei[i:]:
        down = i[0]
        if down >= now:
            ans += 1
            now = i[1]
    maxer = max(ans, maxer)

print(maxer)


### FIXED CODE ###

a = int(input())
fanwei = []

for i in range(a):
    one = input().split()
    one = [int(i) for i in one]
    two = [one[0] - one[1], one[0] + one[1]]
    fanwei.append(two)

fanwei = sorted(fanwei, key=lambda student: student[0] + 100000000000000000000 * student[1])

now = -100000000000000000000
ans = 0
for i in fanwei[:]:
    down = i[0]
    if down >= now:
        ans += 1
        now = i[1]


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

n,k=map(int,input().split())
a=list(map(int,input().split()))
count=0
ans=None
d=0
for i in range(n-k+1):
  if d==0:
    d=1
    gakki=1
    for j in range(count,k+count):
      gakki*=a[j]
      
  else:
    gakki/=a[count]
    gakki*=a[k+count]
    count+=1
  
  if ans is None:
    ans=gakki
  else:
    if ans<gakki:
      print('Yes')
      ans=gakki
    else:
      print("No")
      ans=gakki

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n-k):
  if a[i]<a[k+i]:
    print('Yes')
  else:
    print("No")
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    d={chr(i+a): i for i in range(26)}
    s=list(map(lambda x:d[x],input()))
    t=list(map(lambda x:d[x],input()))
    l=[[-1]*26 for _ in range(len(s))]
    ap=[0]*26
    for i,a in enumerate(s*2):
        for j in range(ap[a],len(s)):
            l[j][a]=i-j
        ap[a]=i
    ans=0
    index=len(s)-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        ans+=n
        index+=n
        index%=len(s)
    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,input()))
    N=len(s)
    t=list(map(lambda x:ord(x)-a,input()))
    l=[[-1]*26 for _ in range(len(s))]
    for i in range(N-1,-N,-1):
        for j in range(26):
            if s[i]==j:
                l[i-1][j]=i%N
            else:
                l[i-1][j]=l[i][j]
    ans=0
    index=N-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        if n>index:
            ans+=n-index
        else:
            ans+=n+N-index
        index=n
    print(ans)

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

num = list (map (int, input().strip().split()))
n = num[0]
k = num[1]
v = list (map (int, input().strip().split()))
sum = 1
for i in range(k):
    sum *= v[i]
for i in range(k, n):
    nsum = sum / v[i - k]
    nsum *= v[i]
    if(sum < nsum):
        print("Yes")
    else:
        print("No")
    sum /= v[i - k]
    sum *= v[i]


### FIXED CODE ###

num = list (map (int, input().strip().split()))
n = num[0]
k = num[1]
v = list (map (int, input().strip().split()))
for i in range(k, n):
    if(v[i - k] < v[i]):
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

def calc_sub(arg, start):
  global n,k
  sub = 0
  for i in range(n):
    if i != 0:
      idx = start + i
      if idx < n:
        sub += arg[idx] - arg[idx-1]
      else:
        idx = idx - n
        if idx == 0:
          sub += arg[0] + (k - arg[n-1])
        else:
          sub += arg[idx] - arg[idx-1]
        
  return sub

def calc_sub_r(arg, start):
  global n,k
  sub = 0
  for i in reversed(range(0, n)):
    idx = start + i
    idx = idx - n if idx >= n else idx
    if idx == n-1:
      sub += (k - arg[idx]) + arg[0]
    else:
      sub += arg[idx+1] - arg[idx]
  return sub


k, n = map(int, input().split())
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

#aaa = calc_sub(l_si_i, 2)
#bbb = calc_sub_r(l_si_i, 2)
#print(aaa)
#print(bbb)


min = 10000000
for i in range(n):
  ret = calc_sub(l_si_i,i)
  if min > ret:
    min = ret
    
  ret = calc_sub_r(l_si_i, i)
  if min > ret:
    min = ret
#  l_si_i.append(l_si_i.pop(0))

print(min)


### FIXED CODE ###

k, n = map(int, input().split())
a = input().split()
l_si_i = [int(s) for s in a]
#print(l_si_i)

num_list = []
for i in range(n):
  if i == 0:
    sub = l_si_i[0] + (k - l_si_i[n-1])
  else:
    sub = l_si_i[i] - l_si_i[i-1]
  
  num_list.append(sub)
  
#print(num_list)
#print(sum(num_list))

idx = num_list.index(max(num_list))
#print(idx)
num_list.pop(idx)
print(sum(num_list))
### BUGGY CODE ###

N = int(input())
names = []
words = {}
for i in range(N):
    name = input()
    if name not in names:
        words[name] = 1
        names.append(name)
    elif name in names:
        words[name] += 1

max_name_list = []
max_num = max(words.values());
for nm in words.items():
    if nm[1] == max_num:
        max_name_list.append(nm[0])

max_name_list = sorted(max_name_list)
for i in range(len(max_name_list)):
    print(max_name_list[i])


### FIXED CODE ###

from collections import Counter
N = int(input())

counter = Counter()
for i in range(N):
    s = input()
    counter[s] += 1


lst = list(counter.most_common())
max_num = lst[0][1]

for l in sorted(lst):
    if l[1] == max_num:
        print(l[0])
### BUGGY CODE ###

from collections import defaultdict
N, P = map(int,input().split())
S = input()

dp = [defaultdict(int) for i in range(N)]


dp[0][int(S[0]) % P] = 1

for i in range(N-1):
    n = int(S[i+1])
    #print(n)
    for k,v in dp[i].items():
        dp[i+1][(10*k+n) % P] += v
    dp[i+1][n % P] += 1
    #print(dp[i+1])

ans = sum([dp[i][0] for i in range(N)])
print(ans)




### FIXED CODE ###

from collections import Counter
N, P = map(int,input().split())
S = input()

'''
N, P  = 10, 7
S = '1234567890'
tans = 0
for i in range(N):
    for j in range(i+1,N+1):
        t = int(S[i:j])
        tans += ((t % P) == 0)
print(tans)
'''

S = list(map(int,S))

ans = 0
if P == 2:
    for i,s in enumerate(S):
        if s % 2 == 0:
            ans += i + 1
elif P == 5:
    for i,s in enumerate(S):
        if s % 5 == 0:
            ans += i + 1
else:
    res = []
    n = 0
    k = 1
    for s in S[::-1]:
        n = (s * k + n) % P
        k = (k * 10) % P
        res.append(n)

    res.append(0) # right
    #print(N,P,S)
    #print(res)

    cnts = Counter(res)
    for ind, cnt in cnts.items():
        if cnt == 1:
            continue
        ans += cnt * (cnt - 1) // 2

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

class MyList(list):
    def __init__(self):
        self.__init__ = list

    def cmd(self,command):
        if command == 'deleteFirst':
            self.pop(0)
        elif command == 'deleteLast':
            self.pop(-1)
        else:
            ope, num = command.split()
            num = int(num)
            if ope == 'insert':
                self.insert(0,num)
            else:
                if num in self:
                    self.remove(num)

def pr(x):
    print x,


if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    map(pr,l[:-1])
    print l[-1]

### FIXED CODE ###

from collections import deque

class MyList(deque):
    def __init__(self):
        self.__init__ = deque
        self.d = {'deleteFirst':self.delF, 'deleteLast':self.delL, 'insert':self.insX, 'delete':self.delX}
    def cmd(self,command):
        if ' ' in command:
            ope, num = command.split()
            num = int(num)
            self.d[ope](num)
        else:
            self.d[command]()
            
    def delF(self):
        self.popleft()

    def delL(self):
        self.pop()

    def insX(self,num):
        self.appendleft(num)

    def delX(self,num):
        if num in self:
            self.remove(num)

if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    q = deque(l)
    for i in range(len(l)-1):
        print q.popleft(),
    print l[-1]
### BUGGY CODE ###

k, n = map(int, input().split())
house = list(map(int, input().split()))
load = [0]*k

for i in house:
    load[i] = 1


def check(lst):
    return k - lst.index(1) -1


best_way = k
for i in house:
    right = load[i+1:]
    left = load[:i]
    a_way = right + left
    way = min(check(a_way), check(a_way[::-1]))
    if way < best_way:
        best_way = way

print(best_way)


### FIXED CODE ###

k, n = map(int, input().split())
a = list(map(int, input().split()))
 
max_dist = 0
for i in range(n):
    nxt = (i+1) % n
    distance = a[nxt] - a[i]
    if distance < 0:
        distance += k
    max_dist = max(max_dist, distance)
 
print(k-max_dist)
### BUGGY CODE ###

n = int(input())
s = input()

ans = [0 for i in range(n + 1)]

for i in range(n):
    for j in range(n + 1):
        if j <= i:
            if s[i] == '.':
                ans[j] += 1
        else:
            if s[i] == '#':
                ans[j] += 1
print(min(ans))



### FIXED CODE ###


n = int(input())
s = input()

b = 0
w = 0
tb = 0
tw = 0

for i in reversed(range(n)):
    if s[i] == '#':
        tb += 1
        if tb > tw:
            b += tb
            w += tw
            tb = 0
            tw = 0
    else:
        tw += 1
print(tb + w)
### BUGGY CODE ###

import sys
import math
N = int(input())
for n in range(int(math.sqrt(N)),N+1):
  if N%n==0:
    print(max(int(math.log10(n)), int(math.log10(N//n)))+1)
    sys.exit()
    


### FIXED CODE ###

import sys
import math
N = int(input())
for n in range(int(math.sqrt(N))+1,0,-1):
  if N%n==0:
    print(max(int(math.log10(n)), int(math.log10(N//n)))+1)
    sys.exit()
### BUGGY CODE ###

S=input()
Q=int(input())
Query=[[j for j in input().split()] for i in range(Q)]

c=0
S0=list(S)

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S0.insert(0, Query[i][2])
        else:
            S0.append(Query[i][2])

if c%2==0:
    print(''.join(S0))
else:
    print(''.join(reversed(S0)))

### FIXED CODE ###

from collections import deque

S=input()
Q=int(input())
Query=[[j for j in input().split()] for i in range(Q)]

c=0
S0=deque(S)

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S0.appendleft(Query[i][2])
        else:
            S0.append(Query[i][2])

if c%2==0:
    print(''.join(S0))
else:
    print(''.join(reversed(S0)))