from collections import deque
import sys
input = sys.stdin.readline
n = int(input().strip())
dd = deque()
for _ in range(n):
k = input().rstrip()
if k == 'pop':
if dd:
print(dd.popleft())
else:
print(-1)
elif k == 'size':
print(len(dd))
elif k == 'empty':
if dd:
print(0)
else:
print(1)
elif k == 'front':
if dd:
a = dd.popleft()
print(a)
dd.appendleft(a)
else:
print(-1)
elif k == 'back':
if dd:
a = dd.pop()
print(a)
dd.append(a)
else:
print(-1)
else:
k, a = k.split()
a = int(a)
dd.append(a)