Peter likes to travel by train. He likes it so much that on the train he falls asleep. 

Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.

The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.

At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.

Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.

Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.

Input

The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order. 

The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order. 

Output

Output one of the four words without inverted commas: 

  * «forward» — if Peter could see such sequences only on the way from A to B; 
  * «backward» — if Peter could see such sequences on the way from B to A; 
  * «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A; 
  * «fantasy» — if Peter could not see such sequences. 

Examples

Input

atob
a
b


Output

forward


Input

aaacaaa
aca
aa


Output

both

Note

It is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.

Difficulty: EASY
Tags: String algorithms

Starter code:

Solutions:
import re
I = input
(s, first, second) = (I(), I(), I())
forward = re.fullmatch('\\w*' + first + '\\w*' + second + '\\w*', s)
backward = re.fullmatch('\\w*' + first + '\\w*' + second + '\\w*', s[::-1])
if forward:
	print('both' if backward else 'forward')
else:
	print('backward' if backward else 'fantasy')


def isTrue(flags, first, second):
	try:
		index1 = flags.index(first)
	except:
		index1 = len(flags) + 1
	try:
		index2 = flags.rindex(second)
	except:
		index2 = -1
	return index1 + len(first) - 1 < index2
flags = input()
first = input()
second = input()
forward = isTrue(flags, first, second)
flags = flags[len(flags)::-1]
backward = isTrue(flags, first, second)
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


s = input()
rs = s[::-1]
a = input()
b = input()
(forw, back) = (False, False)
fa = s.find(a)
if fa != -1:
	fb = s.find(b, fa + len(a))
	if fb != -1:
		forw = True
	else:
		forw = False
else:
	forw = False
fa = rs.find(a)
if fa != -1:
	fb = rs.find(b, fa + len(a))
	if fb != -1:
		back = True
	else:
		back = False
else:
	back = False
if forw == False and back == False:
	print('fantasy')
elif forw == True and back == True:
	print('both')
elif forw == True and back == False:
	print('forward')
else:
	print('backward')


s = input()
s1 = input()
s2 = input()
answer = 'fantasy'
l = len(s1) - 1
i = s.find(s1)
j = s.rfind(s2)
if i > -1 and j > -1 and (i + l < j):
	answer = 'forward'
s = s[::-1]
i = s.find(s1)
j = s.rfind(s2)
if i > -1 and j > -1 and (i + l < j):
	if answer == 'fantasy':
		answer = 'backward'
	else:
		answer = 'both'
print(answer)


s = input()
a = input()
b = input()
ab = a + b
sin = s[::-1]
if (s.find(a) >= 0 and s[s.find(a) + len(a):].find(b) >= 0) and (sin.find(a) >= 0 and sin[sin.find(a) + len(a):].find(b) >= 0):
	print('both')
elif s.find(a) >= 0 and s[s.find(a) + len(a):].find(b) >= 0:
	print('forward')
elif sin.find(a) >= 0 and sin[sin.find(a) + len(a):].find(b) >= 0:
	print('backward')
else:
	print('fantasy')


s = input()
first = input()
second = input()
a = s[::-1]
forward = False
backward = False
for i in range(0, len(s) - len(first) + 1):
	if s[i:i + len(first)] == first:
		j = i + len(first)
		if second in s[j:]:
			forward = True
		break
for i in range(0, len(a) - len(first) + 1):
	if a[i:i + len(first)] == first:
		k = i + len(first)
		if second in a[k:]:
			backward = True
		break
if forward == True and backward == True:
	print('both')
if forward == False and backward == False:
	print('fantasy')
if forward == True and backward == False:
	print('forward')
if forward == False and backward == True:
	print('backward')


def search(word, subword):
	a = word.find(subword)
	if a == -1:
		return [False, 0]
	else:
		return [True, a + len(subword)]

def cut(word, begin):
	c = ''
	for i in range(begin, len(word)):
		c += word[i]
	return c

def reverse(word):
	c = ''
	for i in range(len(word) - 1, -1, -1):
		c += word[i]
	return c

def is_forward(road, sight1, sight2):
	a = search(road, sight1)
	if a[0]:
		if a[1] < len(road):
			if search(cut(road, a[1]), sight2)[0]:
				return True
			else:
				return False
		else:
			return False
	else:
		return False

def is_backward(road, sight1, sight2):
	return is_forward(reverse(road), sight1, sight2)
(road,) = [str(x) for x in input().split()]
(sight1,) = [str(x) for x in input().split()]
(sight2,) = [str(x) for x in input().split()]
forward = is_forward(road, sight1, sight2)
backward = is_backward(road, sight1, sight2)
if forward and backward:
	print('both')
if forward and (not backward):
	print('forward')
if not forward and backward:
	print('backward')
if not forward and (not backward):
	print('fantasy')


s = input()
s1 = input()
s2 = input()
forward = False
backward = False
matchfound = False
s_rev = s[::-1]
i = 0
while i < len(s):
	if matchfound == False and s1 == s[i:i + len(s1)]:
		matchfound = True
		i += len(s1)
	if matchfound == True and s2 == s[i:i + len(s2)]:
		forward = True
		break
	i += 1
matchfound = False
i = 0
while i < len(s_rev):
	if matchfound == False and s1 == s_rev[i:i + len(s1)]:
		matchfound = True
		i += len(s1)
	if matchfound == True and s2 == s_rev[i:i + len(s2)]:
		backward = True
		break
	i += 1
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


def check(flags: str, first: str, second: str) -> bool:
	if first not in flags or second not in flags:
		return False
	f_f_ind = flags.index(first)
	l_s_ind = flags.rindex(second)
	return f_f_ind + len(first) - 1 < l_s_ind
(flags, first, second) = (input(), input(), input())
f = check(flags, first, second)
flags = flags[::-1]
b = check(flags, first, second)
if f and b:
	print('both')
elif f:
	print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


(i, ii, iii) = [input() for i in range(3)]

def check(a):
	try:
		c = a.index(ii)
		cc = a.index(iii, c + len(ii))
	except:
		return (0, 0)
	return (c, cc)
c = check(i)
cc = check(i[::-1])
if c[0] < c[1] and cc[0] < cc[1]:
	print('both')
elif c[0] < c[1]:
	print('forward')
elif cc[0] < cc[1]:
	print('backward')
else:
	print('fantasy')


l = input()
first = input()
second = input()
l2 = [first, second]
(m, n) = (-1, len(l))
(f, b) = (0, 0)
for i in range(len(l)):
	j = len(l) - i
	if i >= m and f <= 1 and (l[i:i + len(l2[f])] == l2[f]):
		m = i + len(l2[f])
		f += 1
	if j <= n and b <= 1 and (l[j - len(l2[b]):j] == l2[b][::-1]):
		n = j - len(l2[b])
		b += 1
if f == 2 and b == 2:
	print('both')
elif f == 2:
	print('forward')
elif b == 2:
	print('backward')
else:
	print('fantasy')


colors = input()
f = input()
s = input()
forw = 0
if f in colors:
	f_idx = colors.index(f)
	_f = f_idx + len(f)
	if s in colors[_f:]:
		forw = 1
way_back = colors[::-1]
back = 0
if f in way_back:
	f_idx = way_back.index(f)
	_f = f_idx + len(f)
	if s in way_back[_f:]:
		back = 1
if forw == 1 and back == 1:
	print('both')
elif forw == 1 and back == 0:
	print('forward')
elif forw == 0 and back == 1:
	print('backward')
elif forw == 0 and back == 0:
	print('fantasy')


def main():
	flags = input()
	first = input()
	second = input()
	skip = len(first)
	possible = 0
	at = flags.find(first)
	if at >= 0:
		at += skip
		if flags.find(second, at) >= at:
			possible += 1
	flags = flags[::-1]
	at = flags.find(first)
	if at >= 0:
		at += skip
		if flags.find(second, at) >= at:
			possible += 2
	if possible == 1:
		print('forward')
	elif possible == 2:
		print('backward')
	elif possible == 3:
		print('both')
	else:
		print('fantasy')
main()


from sys import stdin, stdout
import math, bisect, heapq
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	print(' '.join(list(map(str, a))))

def solve():
	x = input()
	y = input()
	z = input()
	(a, b) = (0, 0)
	n = len(x)
	ind = n
	for i in range(n):
		if x[i:i + len(y)] == y:
			ind = i + len(y)
			break
	for i in range(ind, n):
		if x[i:i + len(z)] == z:
			ind = -1
	if ind == -1:
		a = 1
	x = x[::-1]
	ind = n
	for i in range(n):
		if x[i:i + len(y)] == y:
			ind = i + len(y)
			break
	for i in range(ind, n):
		if x[i:i + len(z)] == z:
			ind = -1
	if ind == -1:
		b = 1
	if a + b == 2:
		print('both')
	elif a == 1:
		print('forward')
	elif b == 1:
		print('backward')
	else:
		print('fantasy')
for _ in range(1):
	solve()


t = input()
a = input()
b = input()
answer = 'fantasy'
l = len(a) - 1
i = t.find(a)
j = t.rfind(b)
if i > -1 and j > -1 and (i + l < j):
	answer = 'forward'
t = t[::-1]
i = t.find(a)
j = t.rfind(b)
if i > -1 and j > -1 and (i + l < j):
	if answer == 'fantasy':
		answer = 'backward'
	else:
		answer = 'both'
print(answer)


flags = input()
first = input()
second = input()
n = len(flags)
a = len(first)
b = len(second)

def check(colors):
	i = 0
	f = False
	answer = False
	while i < n:
		x = i
		if f:
			if colors[i] == second[0]:
				k = 0
				while k < b and i < n:
					if second[k] == colors[i]:
						k += 1
						i += 1
					else:
						i = x + 1
						break
				if k == b:
					answer = True
			else:
				i += 1
		elif colors[i] == first[0]:
			j = 0
			while j < a and i < n:
				if first[j] == colors[i]:
					j += 1
					i += 1
				else:
					i = x + 1
					break
			if j == a:
				f = True
		else:
			i += 1
	return answer
forward = check(flags)
backward = check(flags[::-1])
if forward and backward:
	print('both')
elif forward and (not backward):
	print('forward')
elif backward and (not forward):
	print('backward')
else:
	print('fantasy')


def check(s, p, q):
	return q in s[s.index(p) + len(p):] if p in s else False
(s, p, q) = (input(), input(), input())
print(('fantasy', 'forward', 'backward', 'both')[check(s, p, q) + check(s[::-1], p, q) * 2])


from sys import stdin
s = next(stdin).strip()
s1 = next(stdin).strip()
s2 = next(stdin).strip()
f = False
b = False
if s1 in s and s2 in s.split(s1, 1)[1]:
	f = True
if s1 in s[::-1] and s2 in s[::-1].split(s1, 1)[1]:
	b = True
if f:
	if b:
		print('both')
	else:
		print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


s = input().strip()
a = input().strip()
b = input().strip()
t = 0
if a in s and b in s:
	if s.index(a) + len(a) <= s.rindex(b):
		t += 1
s = s[::-1]
if a in s and b in s:
	if s.index(a) + len(a) <= s.rindex(b):
		t += 2
print(['fantasy', 'forward', 'backward', 'both'][t])


(color, s1, s2, atob, btoa) = (input(), input(), input(), False, False)
for i in range(len(color) - len(s1) - len(s2) + 1):
	if color[i:i + len(s1)] == s1:
		for j in range(i + len(s1), len(color) - len(s2) + 1):
			if color[j:j + len(s2)] == s2:
				atob = True
				break
		break
color = color[::-1]
for i in range(len(color) - len(s1) - len(s2) + 1):
	if color[i:i + len(s1)] == s1:
		for j in range(i + len(s1), len(color) - len(s2) + 1):
			if color[j:j + len(s2)] == s2:
				btoa = True
				break
		break
if atob and btoa:
	print('both')
elif atob:
	print('forward')
elif btoa:
	print('backward')
else:
	print('fantasy')


flags = input()
p1 = input()
p2 = input()
ab = False
ba = False
if p1 in flags:
	if p2 in flags.split(p1, 1)[1]:
		ab = True
lflags = list(flags)
lflags.reverse()
rflags = ''.join(lflags)
if p1 in rflags:
	if p2 in rflags.split(p1, 1)[1]:
		ba = True
if ab and ba:
	print('both')
elif ab and (not ba):
	print('forward')
elif not ab and ba:
	print('backward')
else:
	print('fantasy')


x = input()
x_reversed = ''.join(list(reversed(x)))
seq1 = input()
seq2 = input()
if len(seq1) + len(seq2) > len(x):
	print('fantasy')
else:
	forward = False
	backward = False
	x_find_seq1 = x.find(seq1)
	if x_find_seq1 > -1:
		x_find_seq2 = x[x_find_seq1 + len(seq1):].find(seq2)
		if x_find_seq2 > -1:
			x_find_seq2 += x_find_seq1 + len(seq1)
	else:
		x_find_seq2 = -1
	x_reversed_find_seq1 = x_reversed.find(seq1)
	if x_reversed_find_seq1 > -1:
		x_reversed_find_seq2 = x_reversed[x_reversed_find_seq1 + len(seq1):].find(seq2)
		if x_reversed_find_seq2 > -1:
			x_reversed_find_seq2 += x_reversed_find_seq1 + len(seq1)
	else:
		x_reversed_find_seq2 = -1
	if x_find_seq1 != -1 and x_find_seq2 != -1:
		if x_find_seq1 <= x_find_seq2:
			forward = True
	if x_reversed_find_seq1 != -1 and x_reversed_find_seq2 != -1:
		if x_reversed_find_seq1 <= x_reversed_find_seq2:
			backward = True
	if forward and backward:
		print('both')
	elif forward:
		print('forward')
	elif backward:
		print('backward')
	else:
		print('fantasy')


c = input()
s = input()
t = input()
try:
	i = c.index(s)
	f = t in c[i + len(s):]
except ValueError:
	f = 0
c = c[::-1]
try:
	i = c.index(s)
	b = t in c[i + len(s):]
except ValueError:
	b = 0
print([['fantasy', 'backward'], ['forward', 'both']][f][b])


flags = input()
first = input()
second = input()
forward = backward = 0
if first in flags:
	start = flags.index(first)
	end = flags[start + len(first):]
	if second in end:
		forward = 1
reverse = flags[::-1]
if first in reverse:
	start = reverse.index(first)
	end = reverse[start + len(first):]
	if second in end:
		backward = 1
if forward == 1 and backward == 1:
	print('both')
elif forward == 1 and backward == 0:
	print('forward')
elif forward == 0 and backward == 1:
	print('backward')
elif forward == 0 and backward == 0:
	print('fantasy')


def flags_on_stations(way, first, second):
	if first in way:
		if second in way.split(first, 1)[1]:
			return True
		else:
			return False
	else:
		return False

def flags_on_both_ways(way, first, second):
	return flags_on_stations(way, first, second) and flags_on_stations(way[::-1], first, second)

def get_result(way, first, second):
	if flags_on_both_ways(way, first, second):
		return 'both'
	elif flags_on_stations(way, first, second):
		return 'forward'
	elif flags_on_stations(way[::-1], first, second):
		return 'backward'
	else:
		return 'fantasy'
print(get_result(input(), input(), input()))


s = input()
a = input()
b = input()
f = 0
back = 0
if a in s and b in s:
	k = s.index(a)
	if b in s[k + len(a):]:
		f += 1
s = list(s)
s.reverse()
s = str(''.join(s))
if a in s and b in s:
	k = s.index(a)
	if b in s[k + len(a):]:
		back += 1
if f == 1 and back == 0:
	print('forward')
elif f == 1 and back == 1:
	print('both')
elif f == 0 and back == 1:
	print('backward')
else:
	print('fantasy')


s = input()
ss = input()
sss = input()

def f(s, ss, sss):
	n = 0
	ok = False
	for i in range(len(s) - len(ss) + 1):
		n = i + len(ss)
		if s[i:len(ss) + i] == ss:
			ok = True
			break
	if not ok:
		return False
	for n in range(n, len(s) - len(sss) + 1):
		if s[n:len(sss) + n] == sss:
			return True
	return False
x = f(s, ss, sss)
y = f(s[::-1], ss, sss)
if x and y:
	print('both')
if not x and y:
	print('backward')
if x and (not y):
	print('forward')
if not x and (not y):
	print('fantasy')


d = {(True, True): 'both', (True, False): 'forward', (False, True): 'backward', (False, False): 'fantasy'}
(p, a, b) = (input().strip(), input().strip(), input().strip())
(ka, kb) = (p.find(a), p.rfind(b))
forward = ka + len(a) <= kb and ka >= 0 and (kb >= 0)
p = p[::-1]
(ka, kb) = (p.find(a), p.rfind(b))
backward = ka + len(a) <= kb and ka >= 0 and (kb >= 0)
print(d[forward, backward])


s = input()
s1 = input()
s2 = input()
k = s.find(s1)
if k == -1 or k + len(s1) == len(s):
	forward = False
else:
	forward = False if s[k + len(s1):].find(s2) == -1 else True
s = s[::-1]
k = s.find(s1)
if k == -1 or k + len(s1) == len(s):
	backward = False
else:
	backward = False if s[k + len(s1):].find(s2) == -1 else True
if forward:
	print('both' if backward else 'forward')
else:
	print('backward' if backward else 'fantasy')


import string

def main():
	flags = input()
	s1 = input()
	s2 = input()
	revFlags = flags[::-1]
	foundF = canSee(flags, s1, s2)
	foundRevF = canSee(revFlags, s1, s2)
	if foundF == True:
		if foundRevF == True:
			print('both')
		else:
			print('forward')
	elif foundRevF == True:
		print('backward')
	else:
		print('fantasy')

def canSee(flags, s1, s2):
	foundS1 = flags.find(s1)
	if foundS1 != -1:
		rem = flags[foundS1 + len(s1):]
		if rem.find(s2) != -1:
			return True
	return False
main()


a = input()
b = input()
c = input()
d = 0
i = a.find(b)
if i >= 0:
	j = a[i + len(b):].find(c)
	if j >= 0:
		d = 1
m = 0
a = a[::-1]
i = a.find(b)
if i >= 0:
	j = a[i + len(b):].find(c)
	if j >= 0:
		m = 1
if m and d:
	print('both')
elif d:
	print('forward')
elif m:
	print('backward')
else:
	print('fantasy')


import string
s = input()
x = input()
y = input()
a = False
b = False
if x in s:
	if y in s[s.find(x) + len(x):]:
		a = True
s = s[::-1]
if x in s:
	if y in s[s.find(x) + len(x):]:
		b = True
if a and b:
	print('both')
elif a:
	print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


def findfirst(s, t):
	return s.find(t)

def findlast(s, t):
	v = s[::-1].find(t[::-1])
	if v == -1:
		return -1
	else:
		return len(s) - v - len(t)
matcht = input()
x = input()
y = input()
canForward = False
a = findfirst(matcht, x)
b = findlast(matcht, y)
if a != -1 and b != -1 and (a + len(x) <= b):
	canForward = True
c = findfirst(matcht[::-1], x)
d = findlast(matcht[::-1], y)
canBackward = False
if c != -1 and d != -1 and (c + len(x) <= d):
	canBackward = True
if canForward and canBackward:
	print('both')
elif canForward:
	print('forward')
elif canBackward:
	print('backward')
else:
	print('fantasy')


s = input()
a = input()
b = input()
r = a + '.*' + b
import re
print(['fantasy', 'forward', 'backward', 'both'][(re.search(r, s) != None) + 2 * (re.search(r, s[::-1]) != None)])


a = input()
b = input()
c = input()
d = -1
f = -1
a1 = 0
a2 = 0
h = 0
if len(b) + len(c) > len(a):
	print('fantasy')
	h = 1
if h == 0:
	for i in range(0, len(a) - len(b) + 1):
		if a[i:i + len(b)] == b:
			d = i
			break
	for i in range(0, len(a) - len(c) + 1):
		if a[i:i + len(c)] == c:
			f = i
	if d + len(b) - 1 < f and d > -1:
		a1 = 1
	d = -1
	f = -1
	a = list(a)
	a.reverse()
	a = ''.join(a)
	for i in range(0, len(a) - len(b) + 1):
		if a[i:i + len(b)] == b:
			d = i
			break
	for i in range(0, len(a) - len(c) + 1):
		if a[i:i + len(c)] == c:
			f = i
	if d + len(b) - 1 < f and d > -1:
		a2 = 1
	if a1 == 1 and a2 == 0:
		print('forward')
	if a1 == 0 and a2 == 1:
		print('backward')
	if a1 == 1 and a2 == 1:
		print('both')
	if a1 == 0 and a2 == 0:
		print('fantasy')


def main():
	(s, a, b) = (input(), input(), input())
	sh = pi = False
	(la, ls, lb) = (len(a), len(s), len(b))
	fi = s.find(a)
	if fi != -1:
		if ls - (fi + la) >= lb and s.find(b, fi + la) != -1:
			sh = True
	s = ''.join(reversed(s))
	fi = s.find(a)
	if fi != -1:
		if ls - (fi + la) >= lb and s.find(b, fi + la) != -1:
			pi = True
	if sh != True and pi != True:
		print('fantasy')
	elif sh == True and pi == True:
		print('both')
	elif sh == True:
		print('forward')
	elif pi == True:
		print('backward')
main()


string = input()
a = input()
b = input()
forward = False
first_index = string.find(a)
if first_index != -1:
	second_index = string[first_index + len(a):].find(b)
	forward = first_index != -1 and second_index != -1
else:
	forward = False
string = string[::-1]
backward = False
first_index = string.find(a)
if first_index != -1:
	second_index = string[first_index + len(a):].find(b)
	backward = first_index != -1 and second_index != -1
else:
	backward = False
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


s = input()
a = input()
b = input()

def check(p):
	j = p.find(a)
	k = p.find(b, len(a) + j)
	return j != -1 and k != -1
f = check(s)
r = check(s[-1::-1])
if f and r:
	print('both')
elif f:
	print('forward')
elif r:
	print('backward')
else:
	print('fantasy')


flags = input()
sleep1 = input()
sleep2 = input()
f1 = None
f2 = None
r1 = None
r2 = None
try:
	f1 = flags.index(sleep1)
except ValueError:
	pass
if type(f1) == int:
	try:
		f2 = flags[f1 + len(sleep1):].index(sleep2)
		f2 = True
	except ValueError:
		pass
flags = flags[::-1]
try:
	r1 = flags.index(sleep1)
except ValueError:
	pass
if type(r1) == int:
	try:
		r2 = flags[r1 + len(sleep1):].index(sleep2)
		r2 = True
	except ValueError:
		pass
if f2 and r2:
	print('both')
elif f2:
	print('forward')
elif r2:
	print('backward')
else:
	print('fantasy')


x = input()
x_reversed = ''.join(list(reversed(x)))
seq1 = input()
seq2 = input()
if len(seq1) + len(seq2) > len(x):
	print('fantasy')
else:
	forward = False
	backward = False
	if seq1 in x:
		if seq2 in x[x.find(seq1) + len(seq1):]:
			forward = True
	if seq1 in x_reversed:
		if seq2 in x_reversed[x_reversed.find(seq1) + len(seq1):]:
			backward = True
	if forward and backward:
		print('both')
	elif forward:
		print('forward')
	elif backward:
		print('backward')
	else:
		print('fantasy')


seq = input()
w1 = input()
w2 = input()
seq1 = ''.join(reversed(seq))
if len(w1) + len(w2) <= len(seq):
	if w1 in seq and w2 in seq[seq.index(w1) + 1:] and (w1 in seq1) and (w2 in seq1[seq1.index(w1) + 1:]):
		print('both')
	elif w1 in seq and w2 in seq[seq.index(w1) + 1:]:
		print('forward')
	elif w1 in seq1 and w2 in seq1[seq1.index(w1) + 1:]:
		print('backward')
	else:
		print('fantasy')
else:
	print('fantasy')


actual = input()
memorized1 = input()
memorized2 = input()
index1 = actual.find(memorized1)
index2 = actual.find(memorized2, index1 + len(memorized1))
index3 = actual[::-1].find(memorized1)
index4 = actual[::-1].find(memorized2, index3 + len(memorized1))
maybe_ans = ''
if (index1 == -1 or index2 == -1) and (index3 == -1 or index4 == -1):
	maybe_ans = 'fantasy'
elif index1 < index2 and index3 < index4:
	maybe_ans = 'both'
elif index1 < index2:
	maybe_ans = 'forward'
elif index3 < index4:
	maybe_ans = 'backward'
else:
	maybe_ans = 'fantasy'
print(maybe_ans)


def z(a, b, c):
	(x, y) = (a.find(b), a.rfind(c))
	return x != -1 and y != -1 and (x + len(b) <= y)
(a, b, c) = (input(), input(), input())
(k1, k2) = (z(a, b, c), z(a[::-1], b, c))
if k1 == False and k2 == False:
	print('fantasy')
elif k1 == False:
	print('backward')
elif k2 == False:
	print('forward')
else:
	print('both')


def train_and_peter():
	flags = input()
	a = input()
	b = input()
	i = flags.find(a)
	if i == -1:
		forward = False
	else:
		forward = flags.find(b, i + len(a)) != -1
	reversed_flags = flags[::-1]
	j = reversed_flags.find(a)
	if j == -1:
		backward = False
	else:
		backward = reversed_flags.find(b, j + len(a)) != -1
	if forward and backward:
		print('both')
	elif forward:
		print('forward')
	elif backward:
		print('backward')
	else:
		print('fantasy')
train_and_peter()


s = input()
a = input()
b = input()
l = len(s)
af = s.find(a)
res_f = 0
res_b = 0
c = s[af:l]
if af >= 0 and s[af + len(a):l].find(b) >= 0:
	res_f = 1
s = s[::-1]
ab = s.find(a)
if ab >= 0 and s[ab + len(a):l].find(b) >= 0:
	res_b = 1
if res_f and res_b:
	print('both')
elif res_f:
	print('forward')
elif res_b:
	print('backward')
else:
	print('fantasy')


s = input()
s1 = s[::-1]
a = input()
b = input()
(c, d, e) = (0, 0, 0)
if a in s:
	if b in s[s.index(a) + len(a):]:
		c += 1
if a in s1:
	if b in s1[s1.index(a) + len(a):]:
		d += 1
if c == 1 and d == 1:
	print('both')
elif c == 1:
	print('forward')
elif d == 1:
	print('backward')
else:
	print('fantasy')


def check(flags, first, second):
	if first not in flags or second not in flags:
		return False
	(fIndex, secLastIndex) = (flags.index(first), flags.rindex(second))
	return fIndex + len(first) - 1 < secLastIndex
(flags, first, second) = (input(), input(), input())
f = check(flags, first, second)
flags = flags[::-1]
b = check(flags, first, second)
if f and b:
	print('both')
elif f:
	print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


s = input()
f = input()
l = input()
r = s[::-1]
ff = rr = 0
if f in s:
	j = s.index(f) + len(f)
	if l in s[j:]:
		ff = 1
if f in r:
	jj = r.index(f) + len(f)
	if l in r[jj:]:
		rr = 1
if ff == 0 and rr == 0:
	print('fantasy')
elif ff == 1 and rr == 0:
	print('forward')
elif ff == 0 and rr == 1:
	print('backward')
else:
	print('both')


a = input()
b = input()
c = input()
k = 0
p = 0
if a.find(b) != -1 and a.find(b) <= a.rfind(c) - len(b):
	k = 1
a = a[::-1]
if a.find(b) != -1 and a.find(b) <= a.rfind(c) - len(b):
	p = 1
if k and p:
	print('both')
elif k:
	print('forward')
elif p:
	print('backward')
else:
	print('fantasy')


def main():
	(a, b, c) = [input() for _ in 'abc']
	x = 0
	for i in (1, 2):
		(u, v) = (a.find(b), a.rfind(c))
		if 0 <= u <= v - len(b):
			x += i
		a = a[::-1]
		if i == 2:
			break
	print(('fantasy', 'forward', 'backward', 'both')[x])
main()


flags = input()
sleep1 = input()
sleep2 = input()
forward1 = None
forward2 = None
reverse1 = None
reverse2 = None
try:
	forward1 = flags.index(sleep1)
except ValueError:
	pass
if type(forward1) == int:
	try:
		forward2 = flags[forward1 + len(sleep1):].index(sleep2)
		forward2 = True
	except ValueError:
		pass
flags = flags[::-1]
try:
	reverse1 = flags.index(sleep1)
except ValueError:
	pass
if type(reverse1) == int:
	try:
		reverse2 = flags[reverse1 + len(sleep1):].index(sleep2)
		reverse2 = True
	except ValueError:
		pass
if forward2 and reverse2:
	print('both')
elif forward2:
	print('forward')
elif reverse2:
	print('backward')
else:
	print('fantasy')


flags = input()
flags2 = flags[::-1]
period1 = input()
ans = 0
period2 = input()
if period1 in flags:
	flags = flags.replace(period1, '1', 1)
	flags = flags[flags.index('1') + 1:]
	if period2 in flags:
		ans = ans + 1
if period1 in flags2:
	flags2 = flags2.replace(period1, '1', 1)
	flags2 = flags2[flags2.index('1') + 1:]
	if period2 in flags2:
		ans = ans + 10
if ans == 1:
	print('forward')
elif ans == 10:
	print('backward')
elif ans == 11:
	print('both')
else:
	print('fantasy')


a = input()
b = input()
c = input()
k = 0
if -a.find(b) + a.rfind(c) >= len(b) and a.find(b) != -1 and (a.rfind(c) != -1):
	k += 1
n = len(a)
s = ''
for i in range(n):
	s += a[n - i - 1]
if -s.find(b) + s.rfind(c) >= len(b) and s.find(b) != -1 and (s.rfind(c) != -1):
	k += 2
if k == 3:
	print('both')
if k == 2:
	print('backward')
if k == 1:
	print('forward')
if k == 0:
	print('fantasy')


strA = input()
strB = input()
strC = input()
k = 0
(x, y) = (strA.find(strB), strA.rfind(strC))
if x > -1 and y > -1 and (x + len(strB) < y + 1):
	k += 1
strA = strA[::-1]
(x, y) = (strA.find(strB), strA.rfind(strC))
if x > -1 and y > -1 and (x + len(strB) < y + 1):
	k += 2
print(['fantasy', 'forward', 'backward', 'both'][k])


from sys import stdin
__author__ = 'artyom'

def read_line():
	return stdin.readline().strip()

def contains(st, a, b):
	try:
		st.index(b, st.index(a) + len(a))
		return 1
	except ValueError:
		return 0
route = read_line()
route_back = route[::-1]
seen1 = read_line()
seen2 = read_line()
res = 0
if contains(route, seen1, seen2):
	res = 1
if contains(route_back, seen1, seen2):
	res |= 2
print(['fantasy', 'forward', 'backward', 'both'][res])


def checkSeq(seq1, seq2):
	global forward, backward
	try:
		if seq.index(seq1) + len(seq1) <= seq.rfind(seq2):
			forward = True
		else:
			forward = False
	except ValueError:
		forward = False
	try:
		if seq[::-1].index(seq1) + len(seq1) <= seq[::-1].rfind(seq2):
			backward = True
		else:
			backward = False
	except ValueError:
		backward = False
seq = input()
checkSeq(input(), input())
if forward:
	if backward:
		print('both')
	else:
		print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


a = input()
b = input()
c = input()
d = a.find(b)
e = a[d + len(b):].find(c)
f = 0
if e != -1 and d != -1:
	f += 1
a = a[::-1]
d = a.find(b)
e = a[d + len(b):].find(c)
if e != -1 and d != -1:
	f += 2
if f == 3:
	print('both')
elif f == 2:
	print('backward')
elif f == 1:
	print('forward')
else:
	print('fantasy')


import sys

def str_in_sequence(sequence, str1, str2):
	start1 = sequence.find(str1)
	if start1 == -1:
		return False
	start2 = sequence.find(str2, start1 + len(str1))
	if start2 == -1:
		return False
	return True

def main(sinp=sys.stdin, sout=sys.stdout):
	(flags, vision1, vision2) = sinp.read().split()
	forward = str_in_sequence(flags, vision1, vision2)
	backward = str_in_sequence(flags[::-1], vision1, vision2)
	if forward:
		if backward:
			result = 'both'
		else:
			result = 'forward'
	elif backward:
		result = 'backward'
	else:
		result = 'fantasy'
	sout.write(result)
main()


def reverse(s):
	return s[::-1]
s = input()
sReverse = reverse(s)
a = input()
b = input()
import re
allStr = '.*'
expression = allStr + a + allStr + b + allStr
matchResultForward = re.compile(expression).match(s)
matchResultBackward = re.compile(expression).match(sReverse)
forward = True if matchResultForward != None else False
backward = True if matchResultBackward != None else False
if forward == True:
	if backward == True:
		print('both')
	else:
		print('forward')
elif backward == True:
	print('backward')
else:
	print('fantasy')


clrs = input()
s1 = input()
s2 = input()
forward = False
backward = False
if s1 in clrs:
	if s2 in clrs[clrs.find(s1) + len(s1):]:
		forward = True
clrs = clrs[::-1]
if s1 in clrs:
	if s2 in clrs[clrs.find(s1) + len(s1):]:
		backward = True
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


a = input()
b = input()
c = input()
f1 = True
f2 = True
k1 = 0
k2 = 0
k = 0
i = 0
v1 = [0] * len(b)
for i in range(1, len(b)):
	w = v1[i - 1]
	while w > 0 and b[w] != b[i]:
		w = v1[w - 1]
	if b[w] == b[i]:
		w = w + 1
	v1[i] = w
v2 = [0] * len(c)
for i in range(1, len(c)):
	w = v2[i - 1]
	while w > 0 and c[w] != c[i]:
		w = v2[w - 1]
	if c[w] == c[i]:
		w = w + 1
	v2[i] = w
i = 0
while i < len(a):
	if f1:
		while k1 > 0 and b[k1] != a[i]:
			k1 = v1[k1 - 1]
		if a[i] == b[k1]:
			k1 += 1
		if k1 == len(b):
			k1 = 0
			f1 = False
	else:
		while k1 > 0 and c[k1] != a[i]:
			k1 = v2[k1 - 1]
		if a[i] == c[k1]:
			k1 += 1
		if k1 == len(c):
			k1 = 0
			if k % 2 == 0:
				k += 1
	i += 1
i = 0
while i < len(a):
	if f2:
		while k2 > 0 and b[k2] != a[len(a) - 1 - i]:
			k2 = v1[k2 - 1]
		if a[len(a) - 1 - i] == b[k2]:
			k2 += 1
		if k2 == len(b):
			k2 = 0
			f2 = False
	else:
		while k2 > 0 and c[k2] != a[len(a) - 1 - i]:
			k2 = v2[k2 - 1]
		if a[len(a) - 1 - i] == c[k2]:
			k2 += 1
		if k2 == len(c):
			k2 = 0
			if k < 2:
				k += 2
	i += 1
if k == 3:
	print('both')
if k == 2:
	print('backward')
if k == 1:
	print('forward')
if k == 0:
	print('fantasy')


def f(a, b, c):
	(i, j) = (a.find(b), a.rfind(c))
	return i != -1 and j != -1 and (i + len(b) <= j)
(a, b, c) = (input(), input(), input())
print(('fantasy', 'backward', 'forward', 'both')[2 * f(a, b, c) + f(a[::-1], b, c)])


texto = input()
cadena1 = input()
cadena2 = input()
forward = False
backward = False
rpta = 'fantasy'
indice_c1 = None
indice_c2 = None
indice_c1 = texto.find(cadena1)
if indice_c1 != -1:
	indice_c2 = texto.find(cadena2, indice_c1 + len(cadena1))
	if indice_c2 != -1:
		forward = True
indice_c1 = texto[::-1].find(cadena1)
if indice_c1 != -1:
	indice_c2 = texto[::-1].find(cadena2, indice_c1 + len(cadena1))
	if indice_c2 != -1:
		backward = True
if forward and backward:
	rpta = 'both'
elif forward:
	rpta = 'forward'
elif backward:
	rpta = 'backward'
print(rpta)


a = input()
b = input()
c = input()
d = a[::-1]
lenb = len(b)
lenc = len(c)
bf = cf = bb = cb = 1000000
couf = coub = 0
for i in range(len(a)):
	if b == a[i:i + lenb]:
		bf = min(i + lenb - 1, bf)
	if c == a[i:i + lenc]:
		cf = i
	if bf < cf and bf != 1000000 and (cf != 1000000):
		couf = 1
		break
for i in range(len(a)):
	if b == d[i:i + lenb]:
		bb = min(i + lenb - 1, bb)
	if c == d[i:i + lenc]:
		cb = i
	if bb < cb and bb != 1000000 and (cb != 1000000):
		coub = 1
		break
if coub + couf == 2:
	print('both')
elif couf == 1:
	print('forward')
elif coub == 1:
	print('backward')
else:
	print('fantasy')


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
import re
a = ip()
b = a[::-1]
x = ip()
y = ip()
a = re.match(f'(.*){x}(.*){y}', a)
b = re.match(f'(.*){x}(.*){y}', b)
if a and b:
	print('both')
elif a and (not b):
	print('forward')
elif not a and b:
	print('backward')
else:
	print('fantasy')


(sequence, seen1, seen2, theway) = (input(), input(), input(), 'fantasy')
sequenceback = ''.join(list(reversed(sequence)))
if seen1 in sequence and seen2 in sequence:
	if seen2 in sequence[sequence.index(seen1) + len(seen1):]:
		theway = 'forward'
if seen1 in sequenceback and seen2 in sequenceback:
	if seen2 in sequenceback[sequenceback.index(seen1) + len(seen1):]:
		if theway == 'forward':
			theway = 'both'
		else:
			theway = 'backward'
print(theway)


a = input()
b = input()
c = input()
one = a.find(b)
if one != -1:
	one = a[one + len(b):].find(c)
b = ''.join(list(reversed(b)))
c = ''.join(list(reversed(c)))
two = a.rfind(b)
if two != -1:
	two = a[:two].rfind(c)
if one != -1 and two != -1:
	print('both')
elif one != -1:
	print('forward')
elif two != -1:
	print('backward')
else:
	print('fantasy')


(s, a, b) = (input(), input(), input())
(fd, bk) = (0, 0)
if s.find(a) != -1:
	if s[s.find(a) + len(a):].find(b) != -1:
		fd = 1
s = s[::-1]
if s.find(a) != -1:
	if s[s.find(a) + len(a):].find(b) != -1:
		bk = 1
if fd and bk:
	print('both')
elif fd:
	print('forward')
elif bk:
	print('backward')
else:
	print('fantasy')


def rr():
	return input().rstrip()

def rri():
	return int(rr())

def rrl():
	return list(map(int, rr().split()))

def rrt():
	return tuple(map(int, rr().split()))
from collections import defaultdict

def mus(d=lambda : 0):
	return defaultdict(lambda : defaultdict(d))

def dd0(d=lambda : 0):
	return defaultdict(d)

def ms(x, y, d=0):
	return [[0] * y for i in range(x)]

def ar(x, d=0):
	return [d] * x

def ppm(m, n=0, x=0, y=0):
	print('\n'.join(('\t'.join((str(m[j][i]) for j in range(y or n))) for i in range(x or n))))

def ppa(a, n):
	print('\t'.join(map(str, a[0:n])))

def ppl(a=0, x=[1]):
	x[0] = a or x[0] + 1
	print('-' + '- -' * 14 + '-# ' + str(x[0] - 2) + ' #-' + '- -' * 14 + '-')

def fltn(a):
	(i for sub in a for i in sub)

def yn(x):
	return 'YES' if x else 'NO'
from itertools import accumulate
from itertools import groupby

def grp(x):
	return ((i, sum((1 for _ in g))) for (i, g) in groupby(x))
import math

def rnar():
	return (*rrl(), rrl())

def rn():
	return (*rrl(),)

def dpp(a, b=''):
	print('[{}]o[{}]'.format(a, b))

def read():
	return (rr(), rr(), rr())

def solve(t, a, b):
	p1 = t.find(a)
	p2 = t.find(b, p1 + len(a))
	if p1 != -1 and p2 != -1:
		fwd = True
	else:
		fwd = False
	t = t[::-1]
	p1 = t.find(a)
	p2 = t.find(b, p1 + len(a))
	if p1 != -1 and p2 != -1:
		bwd = True
	else:
		bwd = False
	if bwd and fwd:
		ans = 'both'
	elif fwd:
		ans = 'forward'
	elif bwd:
		ans = 'backward'
	else:
		ans = 'fantasy'
	return ans
test_count = 1
for _ in range(test_count):
	input_data = read()
	result = solve(*input_data)
	print(result)


s = input()
s1 = input()
s2 = input()
flag1 = flag2 = 0
pos1 = s.find(s1)
if pos1 >= 0:
	pos2 = s[pos1 + len(s1):].find(s2)
	if pos2 >= 0:
		flag1 = 1
s = s[::-1]
pos1 = s.find(s1)
if pos1 >= 0:
	pos2 = s[pos1 + len(s1):].find(s2)
	if pos2 >= 0:
		flag2 = 1
if flag1 == 1 and flag2 == 1:
	print('both')
elif flag1 == 1:
	print('forward')
elif flag2 == 1:
	print('backward')
else:
	print('fantasy')


s = input()
rev = s[::-1]
start = input()
end = input()
(f, r) = (0, 0)
if start in s and end in s:
	if s.find(start) <= s.find(end, s.find(start) + len(start)):
		f = 1
if start in rev and end in rev:
	if rev.find(start) <= rev.find(end, rev.find(start) + len(start)):
		r = 1
if f == 1 and r == 1:
	print('both')
elif f == 1 and r == 0:
	print('forward')
elif f == 0 and r == 1:
	print('backward')
else:
	print('fantasy')


flags = input()
rev_flags = flags[::-1]
forward = False
backward = False
both = False
arr_1 = input()
arr_2 = input()
if arr_1 in flags:
	if arr_2 in flags[flags.index(arr_1) + len(arr_1):]:
		forward = True
if arr_1 in rev_flags:
	if arr_2 in rev_flags[rev_flags.index(arr_1) + len(arr_1):]:
		if forward:
			both = True
		else:
			backward = True
if both:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


def tryexcept(flags, first):
	try:
		index = flags.index(first)
	except:
		index = len(flags) + 1
	return index

def rtryexcept(flags, first):
	try:
		index = flags.rindex(first)
	except:
		index = -1
	return index
flags = input()
first = input()
second = input()
startToFinish1 = tryexcept(flags, first)
startToFinish2 = rtryexcept(flags, second)
flags = flags[len(flags)::-1]
finishToStart1 = tryexcept(flags, first)
finishToStart2 = rtryexcept(flags, second)
if startToFinish1 + len(first) - 1 < startToFinish2 and finishToStart1 + len(first) - 1 < finishToStart2:
	print('both')
elif startToFinish1 + len(first) - 1 < startToFinish2:
	print('forward')
elif finishToStart1 + len(first) - 1 < finishToStart2:
	print('backward')
else:
	print('fantasy')


s = input()
s1 = s[::-1]
first = input()
sec = input()
flag1 = [0, 0]
flag2 = [0, 0]
n1 = len(first)
n2 = len(sec)
n = len(s)
i = 0
j = 0
while True:
	if i == n or j == n:
		break
	if flag1[0] == 1 and s[i:min(i + n2, n)] == sec:
		flag2[0] = 1
	if flag1[1] == 1 and s1[j:min(j + n2, n)] == sec:
		flag2[1] = 1
	if s[i:min(i + n1, n)] == first:
		i += n1 - 1
		flag1[0] = 1
	if s1[j:min(j + n1, n)] == first:
		j += n1 - 1
		flag1[1] = 1
	j += 1
	i += 1
if flag1 == [0, 0] or flag2 == [0, 0]:
	print('fantasy')
elif flag1 == [1, 1] and flag2 == [1, 1]:
	print('both')
elif flag1 == [1, 0] and (flag2 == [1, 0] or flag2 == [1, 1]) or ((flag1 == [1, 0] or flag1 == [1, 1]) and flag2 == [1, 0]):
	print('forward')
elif flag2 == [0, 1] and (flag1 == [0, 1] or flag1 == [1, 1]) or ((flag2 == [0, 1] or flag2 == [1, 1]) and flag1 == [0, 1]):
	print('backward')
else:
	print('fantasy')


__author__ = 'Obriel Muga'
banderas = input()
primera = input()
segunda = input()
banderas_vuelta = banderas[::-1]
index = 0
forward = False
backward = False
if primera in banderas:
	seccion = banderas.find(primera)
	banderas_siguiente = banderas[seccion + len(primera):]
	if segunda in banderas_siguiente:
		forward = True
if primera in banderas_vuelta:
	seccion = banderas_vuelta.find(primera)
	banderas_siguiente = banderas_vuelta[seccion + len(primera):]
	if segunda in banderas_siguiente:
		backward = True
if backward and forward:
	print('both')
elif backward and (not forward):
	print('backward')
elif forward and (not backward):
	print('forward')
else:
	print('fantasy')


s1 = input()
s2 = input()
s3 = input()
res = [0, 0]
i = 0
state = 0
while i < len(s1):
	if state == 0 and i + len(s2) <= len(s1) and (s1[i:i + len(s2)] == s2):
		state = 1
		i += len(s2)
	elif state == 1 and i + len(s3) <= len(s1) and (s1[i:i + len(s3)] == s3):
		res[0] = 1
		break
	else:
		i += 1
s1 = s1[::-1]
i = 0
state = 0
while i < len(s1):
	if state == 0 and i + len(s2) <= len(s1) and (s1[i:i + len(s2)] == s2):
		state = 1
		i += len(s2)
	elif state == 1 and i + len(s3) <= len(s1) and (s1[i:i + len(s3)] == s3):
		res[1] = 1
		break
	else:
		i += 1
if res[0] == res[1] == 1:
	print('both')
elif res[0] == res[1] == 0:
	print('fantasy')
elif res[0] == 1:
	print('forward')
elif res[1] == 1:
	print('backward')


Way = input()
L1 = input()
L2 = input()
WayB = Way[::-1]
forw = True
back = True
if (Way.find(L1) == -1 or Way.rfind(L2) == -1) and (WayB.find(L1) == -1 or WayB.rfind(L2) == -1):
	print('fantasy')
else:
	if Way.find(L1) + len(L1) > Way.rfind(L2):
		forw = False
	if WayB.find(L1) + len(L1) > WayB.rfind(L2):
		back = False
	if forw and back:
		print('both')
	if not forw and back:
		print('backward')
	if forw and (not back):
		print('forward')
	if not forw and (not back):
		print('fantasy')


def h(f, s):
	(lf, ls) = (len(f), len(s))
	if ls < lf:
		return (False, '')
	for i in range(ls - lf + 1):
		if s[i:i + lf] == f:
			return (True, s[i + lf:])
	return (False, '')
(s, s1, s2) = (input(), input(), input())
(f, b) = (h(s2, h(s1, s)[1])[0], h(s2, h(s1, s[::-1])[1])[0])
if f:
	if b:
		print('both')
	else:
		print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


(a, b, c) = (input(), input(), input())

def check(a):
	i = a.find(b)
	return i != -1 and a.find(c, i + len(b)) != -1
f = check(a)
b = check(a[::-1])
if f and b:
	print('both')
elif f:
	print('forward')
elif b:
	print('backward')
else:
	print('fantasy')


colors = input()
first = input()
second = input()
forward = False
backward = False
if first in colors:
	idx_1_forward = colors.index(first)
	if second in colors[idx_1_forward + len(first):]:
		forward = True
if second[::-1] in colors:
	idx_1_backward = colors.index(second[::-1])
	if first[::-1] in colors[idx_1_backward + len(second):]:
		backward = True
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


truepath = input()
firstwake = input()
secondwake = input()
d = -1
f = -1
a1 = 0
a2 = 0
h = 0
if len(firstwake) + len(secondwake) > len(truepath):
	print('fantasy')
	h = 1
if h == 0:
	for i in range(0, len(truepath) - len(firstwake) + 1):
		if truepath[i:i + len(firstwake)] == firstwake:
			d = i
			break
	for i in range(0, len(truepath) - len(secondwake) + 1):
		if truepath[i:i + len(secondwake)] == secondwake:
			f = i
	if d + len(firstwake) - 1 < f and d > -1:
		a1 = 1
	d = -1
	f = -1
	truepath = list(truepath)
	truepath.reverse()
	truepath = ''.join(truepath)
	for i in range(0, len(truepath) - len(firstwake) + 1):
		if truepath[i:i + len(firstwake)] == firstwake:
			d = i
			break
	for i in range(0, len(truepath) - len(secondwake) + 1):
		if truepath[i:i + len(secondwake)] == secondwake:
			f = i
	if d + len(firstwake) - 1 < f and d > -1:
		a2 = 1
	if a1 == 1 and a2 == 0:
		print('forward')
	if a1 == 0 and a2 == 1:
		print('backward')
	if a1 == 1 and a2 == 1:
		print('both')
	if a1 == 0 and a2 == 0:
		print('fantasy')


(a, b, c) = (input(), input(), input())
try:
	f = c in a[a.index(b) + len(b):]
except ValueError:
	f = False
a = a[-1::-1]
try:
	r = c in a[a.index(b) + len(b):]
except ValueError:
	r = False
if f:
	if r:
		print('both')
	else:
		print('forward')
elif r:
	print('backward')
else:
	print('fantasy')


a = input()
s1 = input()
s2 = input()
k = ['fantasy', 'forward', 'backward', 'both']
ans = 0
if a.find(s1) != -1 and a.find(s2, a.find(s1) + len(s1)) != -1:
	ans = 1
a = a[::-1]
if a.find(s1) != -1 and a.find(s2, a.find(s1) + len(s1)) != -1:
	ans += 2
print(k[ans])


import re
z = input()
a = input()
b = input()
regex = re.compile(a + '.*' + b)
if bool(regex.search(z)) and bool(regex.search(z[::-1])):
	print('both')
elif bool(regex.search(z)):
	print('forward')
elif bool(regex.search(z[::-1])):
	print('backward')
else:
	print('fantasy')


ss = input()
ns = ss[::-1]
a = input()
b = input()
f1 = 0
f2 = 0
k1 = ss.find(a)
k2 = ss.find(b, k1 + len(a))
if (k1 > -1) & (k2 > -1):
	f1 = 1
k1 = ns.find(a)
k2 = ns.find(b, k1 + len(a))
if (k1 > -1) & (k2 > -1):
	f2 = 1
if f1 + f2 == 2:
	print('both')
elif f1 == 1:
	print('forward')
elif f2 == 1:
	print('backward')
else:
	print('fantasy')


forwardStations = input()
firstStations = input()
lastStation = input()

def getDirection(stations):
	if firstStations in stations:
		indexOfFirsChar = stations.index(firstStations)
		indexOfLastChar = indexOfFirsChar + len(firstStations) - 1
		if lastStation in stations[indexOfLastChar + 1:]:
			return True
	return False
forward = getDirection(forwardStations)
backward = getDirection(forwardStations[::-1])
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


trip = input()
seq1 = input()
seq2 = input()
n1 = len(seq1)
n2 = len(seq2)
m = len(trip)

def roadtrip(trip):
	n1f = 0
	n2f = 0
	forward = 0
	for i in range(m - n1 + 1):
		if seq1 == trip[i:i + n1]:
			n1f = 1
			index1 = i + n1
			break
	if n1f == 1:
		for i in range(index1, m - n2 + 1):
			if seq2 == trip[i:i + n2]:
				n2f = 1
				break
		if n2f == 1:
			forward = 1
	return forward
a1 = roadtrip(trip)
a2 = roadtrip(trip[::-1])
if a1 == 1 and a2 == 1:
	print('both')
elif a1 == 1 and a2 == 0:
	print('forward')
elif a1 == 0 and a2 == 1:
	print('backward')
elif a1 == 0 and a2 == 0:
	print('fantasy')


flags = input()
a = input()
b = input()
fow = 0
back = 0
if flags.find(a) != -1 and flags.find(b, flags.find(a) + len(a)) != -1:
	fow = 1
flags = flags[::-1]
if flags.find(a) != -1 and flags.find(b, flags.find(a) + len(a)) != -1:
	back = 2
lst = ['fantasy', 'forward', 'backward', 'both']
print(lst[fow + back])


f = input()
s1 = input()
s2 = input()
fw = 0
bw = 0
if s1 in f:
	a = f.index(s1)
	g = f[a + len(s1):]
	if s2 in g:
		fw = 1
h = f[::-1]
if s1 in h:
	a = h.index(s1)
	g = h[a + len(s1):]
	if s2 in g:
		bw = 1
if fw == 1 and bw == 1:
	print('both')
elif fw == 1 and bw == 0:
	print('forward')
elif fw == 0 and bw == 1:
	print('backward')
elif fw == 0 and bw == 0:
	print('fantasy')


def CF_8A():
	flag = input()
	one = input()
	two = input()
	rev_flag = flag[::-1]
	forward = 0
	a = flag.find(one)
	b = flag.rfind(two)
	if a != -1 and b != -1 and (a + len(one) <= b):
		forward = 1
	backward = 0
	a = rev_flag.find(one)
	b = rev_flag.rfind(two)
	if a != -1 and b != -1 and (a + len(one) <= b):
		backward = 1
	if forward and backward:
		print('both')
	elif forward:
		print('forward')
	elif backward:
		print('backward')
	else:
		print('fantasy')
	return
CF_8A()


s = input()
s1 = input()
s2 = input()
forw = backw = False
tmp = s.find(s1)
if ~tmp:
	if ~s[tmp + len(s1):].find(s2):
		forw = True
s = s[::-1]
tmp = s.find(s1)
if ~tmp:
	if ~s[tmp + len(s1):].find(s2):
		backw = True
if forw and backw:
	print('both')
elif forw:
	print('forward')
elif backw:
	print('backward')
else:
	print('fantasy')


flags_atob = input()
first = input()
second = input()
n = len(flags_atob)

def isTrue(flags, first_pattern, last_pattern):
	try:
		first_index_first = flags.index(first_pattern)
		second_index_last = n - 1 - flags[::-1].index(last_pattern[::-1])
		if first_index_first + len(first_pattern) - 1 < second_index_last - len(last_pattern) + 1:
			return True
	except:
		pass
	return False
forward = isTrue(flags_atob, first, second)
backward = isTrue(flags_atob[::-1], first, second)
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


s = input()
s1 = input()
s2 = input()
index1 = s.find(s1)
i = 0
temp = index1
forward = 0
backward = 0
if index1 != -1:
	for x in range(temp, len(s)):
		if i < len(s1):
			if s1[i] == s[x]:
				index1 = index1 + 1
				i = i + 1
			else:
				break
		else:
			break
	index2 = s.find(s2, index1)
	if index2 != -1:
		forward = 1
s = s[::-1]
index1 = s.find(s1)
i = 0
temp = index1
if index1 != -1:
	for x in range(temp, len(s)):
		if i < len(s1):
			if s1[i] == s[x]:
				index1 = index1 + 1
				i = i + 1
			else:
				break
		else:
			break
index2 = s.find(s2, index1)
if index2 != -1:
	backward = 1
if forward == 1 and backward == 1:
	print('both')
elif forward == 0 and backward == 0:
	print('fantasy')
elif forward == 1:
	print('forward')
else:
	print('backward')


import string

def stringMatch(str1, str2, str3):
	m = len(str1)
	n = len(str2)
	l = len(str3)
	for i in range(l - m + 1):
		if str3[i:i + m] == str1:
			for j in range(i + m, l - n + 1):
				if str3[j:j + n] == str2:
					return True
	return False

def main():
	strA = str(input())
	strB = str(input())
	strC = str(input())
	a = stringMatch(strB, strC, strA)
	b = stringMatch(strB, strC, strA[::-1])
	if a and b:
		print('both')
	elif a and (not b):
		print('forward')
	elif not a and b:
		print('backward')
	else:
		print('fantasy')
main()


a = input()
b = input()
c = input()
a1 = len(a)
b1 = len(b)
c1 = len(c)
d = 0
e = 0
f = -1
l = -1
for i in range(a1 - b1 + 1):
	if a[i:i + b1] == b:
		if f == -1:
			f = i
			break
for i in range(a1 - c1 + 1):
	if a[i:i + c1] == c:
		l = i
if f != -1 and l != -1:
	if l > f + b1 - 1:
		d = 1
a = a[::-1]
f = -1
l = -1
for i in range(a1 - b1 + 1):
	if a[i:i + b1] == b:
		if f == -1:
			f = i
			break
for i in range(a1 - c1 + 1):
	if a[i:i + c1] == c:
		l = i
if f != -1 and l != -1:
	if l > f + b1 - 1:
		e = 1
if d == 1:
	if e == 1:
		print('both')
	else:
		print('forward')
elif e == 1:
	print('backward')
else:
	print('fantasy')


import sys

def main():
	solve()

def solve():
	data = sys.stdin.readlines()
	(stations_a_to_b, first, second) = parse(data)
	output = calc_trip(stations_a_to_b, first, second)
	print(output)

def parse(data):
	return tuple((s.rstrip() for s in data))

def calc_trip(stations_a_to_b, first, second):
	a_to_b = is_2_substr_of(first, second, stations_a_to_b)
	b_to_a = is_2_substr_of(first, second, stations_a_to_b[::-1])
	if a_to_b:
		if b_to_a:
			return 'both'
		else:
			return 'forward'
	elif b_to_a:
		return 'backward'
	return 'fantasy'

def is_2_substr_of(ss1, ss2, text):
	a = text.find(ss1)
	if a == -1:
		return False
	return text.find(ss2, a + len(ss1)) != -1
main()


s = input()
a = input()
b = input()
fw = False
rw = False
r = s[::-1]
if a in s:
	pa = s.index(a)
	if b in s[pa + len(a):]:
		fw = True
if a in r:
	pa = r.index(a)
	if b in r[pa + len(a):]:
		rw = True
if fw and rw:
	print('both')
elif fw and rw == False:
	print('forward')
elif fw == False and rw:
	print('backward')
else:
	print('fantasy')


s = input()
a = input()
b = input()
ans = 0
for x in range(2):
	i = s.find(a)
	j = s.rfind(b)
	if i != -1 and j != -1 and (i + len(a) <= j):
		ans ^= 1 << x
	s = ''.join(reversed(s))
print(['fantasy', 'forward', 'backward', 'both'][ans])


s = input()
a = input()
b = input()
n1 = len(a)
x1 = s.find(a)
y1 = s.rfind(b)
s = s[::-1]
x2 = s.find(a)
y2 = s.rfind(b)
if x1 > -1:
	x1 += n1 - 1
if x2 > -1:
	x2 += n1 - 1
if x1 != -1 and y1 != -1 and (x2 != -1) and (y2 != -1):
	if x1 < y1 and x2 < y2:
		print('both')
	elif x1 < y1 and x2 >= y2:
		print('forward')
	elif x1 >= y1 and x2 < y2:
		print('backward')
	else:
		print('fantasy')
elif x1 == -1 or (y1 == -1 and x2 != -1 and (y2 != -1)):
	if x2 < y2:
		print('backward')
	else:
		print('fantasy')
elif x1 != -1 and y1 != -1 and (x2 == -1) or y2 == -1:
	if x1 < y1:
		print('forward')
	else:
		print('fantasy')
else:
	print('fantasy')


def print_ans(forward, backward):
	if forward and backward:
		print('both')
		return
	if forward:
		print('forward')
		return
	if backward:
		print('backward')
		return
	print('fantasy')

def both_seq_in_flags(flags, seq1, seq2):
	f1 = flags.find(seq1)
	if f1 != -1 and seq2 in flags[f1 + len(seq1):]:
		return True
	return False
flags = input()
(seq1, seq2) = (input(), input())
flags_rev = ''.join(list(flags)[::-1])
(forward, backward) = (False, False)
if both_seq_in_flags(flags, seq1, seq2):
	forward = True
if both_seq_in_flags(flags_rev, seq1, seq2):
	backward = True
print_ans(forward, backward)


input_seq = input()
reverse_seq = input_seq[::-1]
seq_a = input()
seq_b = input()
mark = [0, 0]
seq = [input_seq, reverse_seq]
for i in range(2):
	op = seq[i]
	pos_a = pos_b = None
	for j in range(len(op) - len(seq_a)):
		if op[j:j + len(seq_a)] == seq_a:
			pos_a = j
			break
	for j in range(len(op) - len(seq_b), -1, -1):
		if op[j:j + len(seq_b)] == seq_b:
			pos_b = j
			break
	if pos_a != None and pos_b != None and (pos_a + len(seq_a) <= pos_b):
		mark[i] = 1
if mark[0] and mark[1]:
	print('both')
elif mark[0]:
	print('forward')
elif mark[1]:
	print('backward')
else:
	print('fantasy')


s = input().strip()
s1 = input().strip()
s2 = input().strip()
p = ''
if s1 in s:
	i = s.find(s1)
	if s2 in s[i + len(s1):]:
		p += 'forward'
s_rev = s[-1::-1]
if s1 in s_rev:
	i = s_rev.find(s1)
	if s2 in s_rev[i + len(s1):]:
		p += 'backward'
if p == '':
	print('fantasy')
elif p == 'forward':
	print('forward')
elif p == 'backward':
	print('backward')
else:
	print('both')


def func(v, r, l):
	(i, j) = (v.find(r), v.rfind(l))
	return i != -1 and j != -1 and (i + len(r) <= j)
(v, r, l) = (input(), input(), input())
print(('fantasy', 'backward', 'forward', 'both')[2 * func(v, r, l) + func(v[::-1], r, l)])


def StartFinish(flags, travel, i):
	for i in range(i, len(flags)):
		if flags[i:i + len(travel)] == travel:
			return i
	return -1
flags = input()
first = input()
second = input()
i = 0
startToFinish1 = int(StartFinish(flags, first, i))
i = startToFinish1
if startToFinish1 != -1:
	startToFinish2 = int(StartFinish(flags, second, i + len(first)))
flags = flags[len(flags)::-1]
i = 0
finishToStart1 = int(StartFinish(flags, first, i))
i = finishToStart1
if finishToStart1 != -1:
	finishToStart2 = int(StartFinish(flags, second, i + len(first)))
ans = 0
if startToFinish1 != -1 and startToFinish1 < startToFinish2:
	ans += 1
if finishToStart1 != -1 and finishToStart1 < finishToStart2:
	ans += 2
if ans == 1:
	print('forward')
if ans == 2:
	print('backward')
if ans == 3:
	print('both')
if ans == 0:
	print('fantasy')


import re
from typing import Tuple

def _solve(haystack: str, a: str, b: str) -> bool:
	a_index = haystack.find(a)
	b_index = haystack.rfind(b)
	if a_index > -1 and b_index > -1 and (b_index >= a_index + len(a)):
		return True
	return False

def solve(flags: str, a: str, b: str) -> str:
	is_forward = _solve(flags, a, b)
	is_backward = _solve(flags[::-1], a, b)
	if is_forward and is_backward:
		return 'both'
	elif is_forward:
		return 'forward'
	elif is_backward:
		return 'backward'
	else:
		return 'fantasy'

def parse_input() -> Tuple[str, str, str]:
	return (input(), input(), input())
(flags, a, b) = parse_input()
print(solve(flags, a, b))


s = input()
(a, b) = (input(), input())
ind = s.find(a)
(first, second) = (0, 0)
if ind != -1:
	ind = s[ind + len(a):].find(b)
	if ind != -1:
		first = 1
s = s[::-1]
ind = s.find(a)
if ind != -1:
	ind = s[ind + len(a):].find(b)
	if ind != -1:
		second = 1
if first and second:
	print('both')
elif first:
	print('forward')
elif second:
	print('backward')
else:
	print('fantasy')


def backward_or_forward(string_fun, first_flags_fun, last_flags_fun):
	if first_flags_fun in string_fun:
		indexOfFirsChar = string_fun.index(first_flags_fun)
		indexOfLastChar = indexOfFirsChar + len(first_flags_fun) - 1
		if last_flags_fun in string_fun[indexOfLastChar + 1:]:
			return True
	return False
string = input()
first_flags = input()
last_flags = input()
forward = backward_or_forward(string, first_flags, last_flags)
backward = backward_or_forward(string[::-1], first_flags, last_flags)
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


def can(s, l, r):
	if l not in s:
		return 0
	i = s.index(l) + len(l)
	if r not in s[i:]:
		return 0
	return 1
s = input()
a = input()
b = input()
v1 = can(s, a, b)
v2 = can(s[::-1], a, b)
if v1 and v2:
	print('both')
elif v1:
	print('forward')
elif v2:
	print('backward')
else:
	print('fantasy')


s = input()
invs = s[::-1]
a = input()
inva = a[::-1]
b = input()
invb = b[::-1]
sa = invsinvb = -1
if a in s:
	sa = s.index(a)
if invb in invs:
	invsinvb = invs.index(invb)
invsa = sinvb = -1
if a in invs:
	invsa = invs.index(a)
if invb in s:
	sinvb = s.index(invb)
forward = backward = 0
if sa != -1 and invsinvb != -1 and (sa + len(a) <= len(s) - invsinvb - len(b)):
	forward = 1
if invsa != -1 and sinvb != -1 and (invsa + len(a) <= len(s) - sinvb - len(b)):
	backward = 1
if forward:
	if backward:
		print('both')
	else:
		print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


def has_subseqs(text: str, seq1: str, seq2: str) -> bool:
	seq1_start = text.find(seq1)
	if seq1_start != -1:
		return text.find(seq2, seq1_start + len(seq1)) != -1
	else:
		return False
colors = input()
seq1 = input()
seq2 = input()
forward = has_subseqs(colors, seq1, seq2)
backward = has_subseqs(colors[::-1], seq1, seq2)
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


(s, x, y, t) = (input(), input(), input(), 0)
e = s[::-1]
if x in s:
	q = s.index(x) + len(x)
	if y in s[q:]:
		t += 1
if x in e:
	q = e.index(x) + len(x)
	if y in e[q:]:
		t += 2
print(['fantasy', 'forward', 'backward', 'both'][t])


allflags = input()
flags1 = input()
flags2 = input()
searchresult = [0] * 2
res1 = allflags.find(flags1)
if res1 != -1:
	res2 = allflags.find(flags2, res1 + len(flags1))
	if res2 != -1:
		searchresult[0] = 1
flags = list(allflags)
flags.reverse()
flagsrev = ''.join(flags)
res1 = flagsrev.find(flags1)
if res1 != -1:
	res2 = flagsrev.find(flags2, res1 + len(flags1))
	if res2 != -1:
		searchresult[1] = 1
if searchresult[0] == 1 and searchresult[1] == 1:
	print('both')
elif searchresult[0] == 1:
	print('forward')
elif searchresult[1] == 1:
	print('backward')
else:
	print('fantasy')


m1 = input()
a = input()
b = input()
length = len(m1)
m2 = ''
r1 = False
r2 = False
for i in range(length):
	m2 = m2 + m1[length - i - 1]
for i in range(length):
	if m1[i:i + len(a)] == a:
		r1 = True
	if r1 == True and m1[i + len(a):i + len(a) + len(b)] == b:
		r2 = True
		break
r3 = False
r4 = False
for i in range(length):
	if m2[i:i + len(a)] == a:
		r3 = True
	if r3 == True and m2[i + len(a):i + len(b) + len(a)] == b:
		r4 = True
		break
if r2 == True and r4 == True:
	print('both')
if r2 == True and r4 == False:
	print('forward')
if r2 == False and r4 == True:
	print('backward')
if r2 == False and r4 == False:
	print('fantasy')


n = input()
a = input()
b = input()
m = n[::-1]
i = n.find(a)
x = False
if i != -1:
	n = n[i + len(a)::1]
	j = n.find(b)
	if j != -1:
		x = True
i = m.find(a)
y = False
if i != -1:
	m = m[i + len(a)::1]
	j = m.find(b)
	if j != -1:
		y = True
if x and y:
	print('both')
elif x:
	print('forward')
elif y:
	print('backward')
else:
	print('fantasy')


__author__ = 'Darren'

def solve():
	(flags, first, second) = (input(), input(), input())
	len_first = len(first)
	(index_first, index_second) = (flags.find(first), flags.rfind(second))
	forward = False
	if index_first > -1 and index_second > -1 and (index_first + len_first <= index_second):
		forward = True
	flags = flags[::-1]
	(index_first, index_second) = (flags.find(first), flags.rfind(second))
	backward = False
	if index_first > -1 and index_second > -1 and (index_first + len_first <= index_second):
		backward = True
	if forward and backward:
		print('both')
	elif forward:
		print('forward')
	elif backward:
		print('backward')
	else:
		print('fantasy')
solve()


def main():
	(l, p1, p2) = (input(), input(), input())
	(fw, bw) = (False, False)
	pos = l.find(p1)
	if pos != -1 and l.find(p2, pos + len(p1)) != -1:
		fw = True
	l = l[::-1]
	pos = l.find(p1)
	if pos != -1 and l.find(p2, pos + len(p1)) != -1:
		bw = True
	if fw and bw:
		print('both')
	elif fw:
		print('forward')
	elif bw:
		print('backward')
	else:
		print('fantasy')
main()


s = input()
a = input()
b = input()
x = y = 0
if a in s:
	if b in s[s.find(a) + len(a):]:
		x = 1
s = s[::-1]
if a in s:
	if b in s[s.find(a) + len(a):]:
		y = 1
if x == 1 and y == 1:
	print('both')
elif x == 1:
	print('forward')
elif y == 1:
	print('backward')
else:
	print('fantasy')


import sys
import math
import bisect
import re

def solve(s, a, b):
	forward = False
	backward = False
	if re.match('.*{0}.*{1}.*'.format(a, b), s):
		forward = True
	if re.match('.*{0}.*{1}.*'.format(a, b), s[::-1]):
		backward = True
	if forward and backward:
		return 'both'
	if forward:
		return 'forward'
	if backward:
		return 'backward'
	return 'fantasy'

def main():
	s = input()
	a = input()
	b = input()
	ans = solve(s, a, b)
	print(ans)
main()


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	a = insr()
	b = insr()
	c = insr()
	index1 = a.find(b)
	found_forward = False
	if index1 >= 0:
		if a.find(c, index1 + len(b)) >= 0:
			found_forward = True
	a = a[::-1]
	found_backward = False
	index1 = a.find(b)
	if index1 >= 0:
		if a.find(c, index1 + len(b)) >= 0:
			found_backward = True
	if found_forward and found_backward:
		print('both')
	elif found_forward:
		print('forward')
	elif found_backward:
		print('backward')
	else:
		print('fantasy')


s = input()
t = list(s)
t.reverse()
sr = ''.join(t)
rez = [0] * 2
s1 = input()
s2 = input()
k1 = s.find(s1)
if k1 != -1:
	k2 = s.find(s2, k1 + len(s1))
	if k2 != -1:
		rez[0] = 1
k1 = sr.find(s1)
if k1 != -1:
	k2 = sr.find(s2, k1 + len(s1))
	if k2 != -1:
		rez[1] = 1
if rez[0] == 1 and rez[1] == 1:
	print('both')
elif rez[0] == 1:
	print('forward')
elif rez[1] == 1:
	print('backward')
else:
	print('fantasy')


s = str(input())
t = s[::-1]
p = str(input())
q = str(input())
a = s.find(p)
b = s.find(q, a + len(p))
c = t.find(p)
d = t.find(q, c + len(p))
if a != -1 and b != -1 and (c != -1) and (d != -1):
	print('both')
elif a != -1 and b != -1:
	print('forward')
elif c != -1 and d != -1:
	print('backward')
else:
	print('fantasy')


(sn, s1, s2) = (input(), input(), input())
sr = sn[::-1]
if (sn.find(s1) != -1 and sn.find(s2, sn.find(s1) + len(s1)) != -1) and (sr.find(s1) != -1 and sr.find(s2, sr.find(s1) + len(s1)) != -1):
	print('both')
elif sn.find(s1) != -1 and sn.find(s2, sn.find(s1) + len(s1)) != -1:
	print('forward')
elif sr.find(s1) != -1 and sr.find(s2, sr.find(s1) + len(s1)) != -1:
	print('backward')
else:
	print('fantasy')


(s, a, b) = (input(), input(), input())
(fo, ba) = (0, 0)
if s.find(a) != -1 and s.find(b, s.find(a) + len(a)) != -1:
	fo = 1
s = s[::-1]
if s.find(a) != -1 and s.find(b, s.find(a) + len(a)) != -1:
	ba = 2
print('fantasy,forward,backward,both'.split(',')[fo + ba])


s = input()
s1 = input()
s2 = input()
b1 = False
b2 = False
if s1 in s:
	if s2 in s[s.find(s1) + len(s1):]:
		b1 = True
s = s[::-1]
if s1 in s:
	if s2 in s[s.find(s1) + len(s1):]:
		b2 = True
if b1 and b2:
	print('both')
elif b1:
	print('forward')
elif b2:
	print('backward')
else:
	print('fantasy')


def main():
	flags = input().replace(' ', '')
	fir = input().replace(' ', '')
	sec = input().replace(' ', '')
	fin = ini = 0
	if flags.find(fir) != -1 and flags.find(sec, flags.find(fir) + len(fir)) != -1:
		ini = 1
	flags = flags[::-1]
	if flags.find(fir) != -1 and flags.find(sec, flags.find(fir) + len(fir)) != -1:
		fin = 2
	print('fantasy,forward,backward,both'.split(',')[ini + fin])
main()


(s, a, b) = (input(), input(), input())
(forward, backward) = (False, False)
if s.find(a) != -1 and s.find(b, s.find(a) + len(a)) != -1:
	forward = True
s = s[::-1]
if s.find(a) != -1 and s.find(b, s.find(a) + len(a)) != -1:
	backward = True
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


from re import match
flag = input()
p1 = input()
p2 = input()
pattern = '.*' + p1 + '.*' + p2 + '.*'
forward = match(pattern, flag) is not None
backward = match(pattern, flag[::-1]) is not None
if forward and backward:
	print('both')
elif forward:
	print('forward')
elif backward:
	print('backward')
else:
	print('fantasy')


import re
input_seq = input()
reverse_seq = input_seq[::-1]
mark = [0, 0]
seq_a = input()
seq_b = input()
reg = re.compile(seq_a + '.*' + seq_b)
if re.search(reg, input_seq):
	mark[0] = 1
if re.search(reg, reverse_seq):
	mark[1] = 1
if mark[0] == 0 and mark[1] == 0:
	print('fantasy')
elif mark[0] == 1 and mark[1] == 1:
	print('both')
elif mark[0] == 1:
	print('forward')
else:
	print('backward')


w = input()
a = input()
b = input()
v = n = 0
if w.find(a) != -1 and w.find(a) <= w.rfind(b) - len(a):
	v = 1
w = w[::-1]
if w.find(a) != -1 and w.find(a) <= w.rfind(b) - len(a):
	n = 1
if v and n:
	print('both')
elif v:
	print('forward')
elif n:
	print('backward')
else:
	print('fantasy')


def main():

	def check(seq, A, B):
		fA = seq.find(A)
		return fA >= 0 and seq.find(B, fA + len(A)) >= 0
	flags = input()
	fmem = input()
	smem = input()
	forw = check(flags, fmem, smem)
	back = check(flags[::-1], fmem, smem)
	if forw and back:
		print('both')
	elif forw:
		print('forward')
	elif back:
		print('backward')
	else:
		print('fantasy')
main()


(t, a, b) = (input(), input(), input())
(k, l) = (0, len(a) - 1)
(i, j) = (t.find(a), t.rfind(b))
if i > -1 and j > -1 and (i + l < j):
	k += 1
t = t[::-1]
(i, j) = (t.find(a), t.rfind(b))
if i > -1 and j > -1 and (i + l < j):
	k += 2
print(['fantasy', 'forward', 'backward', 'both'][k])


import sys
import re
import logging
logging.root.setLevel(level=logging.WARNING)
flags = sys.stdin.readline().strip()
first = sys.stdin.readline().strip()
second = sys.stdin.readline().strip()
pattern = first + '.*' + second
forward = re.findall(pattern, flags)
logging.info(f'foreard={forward}')
backward = re.findall(pattern, flags[::-1])
logging.info(f'backward={backward}')
cases = (bool(backward) << 1) + bool(forward)
ans = ['fantasy', 'forward', 'backward', 'both']
print(ans[cases])


def solve():
	id1 = s.find(a)
	if id1 == -1:
		return False
	id2 = s.rfind(b)
	if id2 == -1:
		return False
	return id1 + len(a) - 1 < id2
s = input()
a = input()
b = input()
fl1 = solve()
s = s[::-1]
fl2 = solve()
if fl1:
	if fl2:
		print('both')
	else:
		print('forward')
elif fl2:
	print('backward')
else:
	print('fantasy')