#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+100;

struct Node {
    int depth, Min_depth, Max_depth, Max_lv, Max_rv, Max_lrv;
}node[maxn<<2];

int n, m;
char parenthesis[maxn];

void init() {
    scanf("%d%d%s", &n, &m, parenthesis+1);
}

Node get_left_parenthesis() {
    return Node{1, 0, 1, 0, 1, 1};
}

Node get_right_parenthesis() {
    return Node{-1, -1, 0, 2, 1, 1};
}

Node shifted_left(int depth, int root) {
    return Node{node[root].depth + depth, node[root].Min_depth+depth, node[root].Max_depth+depth,
                node[root].Max_lv-depth, node[root].Max_rv-depth, node[root].Max_lrv};
}

void merge(int root) {
    int chl = root<<1;
    int chr = root<<1|1;

    Node rhs_shifted = shifted_left(node[chl].depth, chr);

    node[root].depth = rhs_shifted.depth;
    node[root].Min_depth = min(node[chl].Min_depth, rhs_shifted.Min_depth);
    node[root].Max_depth = max(node[chl].Max_depth, rhs_shifted.Max_depth);
    node[root].Max_lv = max(node[chl].Max_lv, max(rhs_shifted.Max_lv, node[chl].Max_depth - 2*rhs_shifted.Min_depth));
    node[root].Max_rv = max(node[chl].Max_rv, max(rhs_shifted.Max_rv, rhs_shifted.Max_depth - 2*node[chl].Min_depth));
    node[root].Max_lrv = max(max(node[chl].Max_lrv, rhs_shifted.Max_lrv), max(node[chl].Max_lv + rhs_shifted.Max_depth,
            rhs_shifted.Max_rv + node[chl].Max_depth));
}

void build_tree(int root, int l, int r) {
    if(l == r) {
        if(parenthesis[l] == '(') node[root] = get_left_parenthesis();
        else node[root] = get_right_parenthesis();
        return ;
    }

    int mid = l + r >> 1;
    build_tree(root<<1, l, mid);
    build_tree(root<<1|1, mid+1, r);

    merge(root);
}

int query_max() {
    return node[1].Max_lrv;
}

void change(int root, int l, int r, int find_pos, int change_pos) {
    if(l == r) {
        if(parenthesis[change_pos] == '(') node[root] = get_left_parenthesis();
        else node[root] = get_right_parenthesis();
        return ;
    }

    int mid = l + r >> 1;
    if(find_pos <= mid) {
        change(root<<1, l, mid, find_pos, change_pos);
    } else {
        change(root<<1|1, mid+1, r, find_pos, change_pos);
    }

    merge(root);
}

void change(int pos1, int pos2) {
    change(1, 1, 2*n-2, pos1, pos2);
    change(1, 1, 2*n-2, pos2, pos1);
}

int main() {
//    freopen("1.in", "r", stdin);
    init();
    build_tree(1, 1, 2*n-2);

    printf("%d\n", query_max());
    while(m--) {
        int pos1, pos2; scanf("%d%d",&pos1, &pos2);
        change(pos1, pos2);
        swap(parenthesis[pos1], parenthesis[pos2]);
        printf("%d\n", query_max());
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(a) ((int)a.size())

const int oo = 1e9;
const int maxn = 1e5 + 5;

// 0 - max f(x)
// 1 - max f(x) - 2 * f(r)
// 2 - max f(x) - 2 * f(l)
// 3 - max f(x) - 2 * f(m) + f(y)
// 4 - min f(x)
int val[maxn * 8][5];
int _lz[maxn * 8];
int n, q;
string s;

void upd_node(int x, int v) {
    _lz[x] += v;
    val[x][0] += v;
    val[x][1] -= v;
    val[x][2] -= v;
    val[x][4] += v;
}

void merge_node(int x, int y, int z) {
    val[x][0] = max(val[y][0], val[z][0]);
    val[x][1] = max(val[y][1], val[z][1]);
    val[x][1] = max(val[x][1], val[y][0] - 2 * val[z][4]);
    val[x][2] = max(val[y][2], val[z][2]);
    val[x][2] = max(val[x][2], val[z][0] - 2 * val[y][4]);
    val[x][3] = max(val[y][3], val[z][3]);
    val[x][3] = max(val[x][3], val[y][1] + val[z][0]);
    val[x][3] = max(val[x][3], val[y][0] + val[z][2]);
    val[x][4] = min(val[y][4], val[z][4]);
}

void pushdown(int x) {
    upd_node(2 * x, _lz[x]);
    upd_node(2 * x + 1, _lz[x]);
    _lz[x] = 0;
    merge_node(x, 2 * x, 2 * x + 1);
}

void upd(int x, int L, int R, int l, int r, int v) {
    if (l > R || L > r) return;
    if (l <= L && R <= r) {
        upd_node(x, v);
        return;
    }
    if (L < R) {
        pushdown(x);
        int M = (L + R) >> 1;
        upd(2 * x, L, M, l, r, v);
        upd(2 * x + 1, M + 1, R, l, r, v);
        merge_node(x, 2 * x, 2 * x + 1);
    }
}

void build(int x, int L, int R) {
    if (L == R) {
        val[x][0] = val[x][4] = 0;
        val[x][1] = 0;
        val[x][2] = 0;
        val[x][3] = 0;
        return;
    }
    int M = (L + R) >> 1;
    build(2 * x, L, M);
    build(2 * x + 1, M + 1, R);
}

void deb(int x, int L, int R) {
    if (L == R) {
        cout << L << " " << val[x][0] << endl;
        return;
    }
    pushdown(x);
    int M = (L + R) >> 1;
    deb(2 * x, L, M);
    deb(2 * x + 1, M + 1, R);
}

int main() {
    cin >> n >> q;
    int N = 2 * (n - 1) - 1;

    cin >> s;

    build(1, 0, N);
    rep(i, 0, sz(s)) {
        int v = (s[i] == '(' ? 1 : -1);
        upd(1, 0, N, i, N, v);
    }

//    cout << "Case 0" << endl;
//    deb(1, 0, N);
    cout << val[1][3] << endl;
    rep(i, 0, q) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        if (s[x] == '(') {
            upd(1, 0, N, x, N, -1);
            upd(1, 0, N, y, N, 1);
        } else {
            upd(1, 0, N, x, N, 1);
            upd(1, 0, N, y, N, -1);
        }
        if (s[y] == ')') {
            upd(1, 0, N, y, N, 1);
            upd(1, 0, N, x, N, -1);
        } else {
            upd(1, 0, N, y, N, -1);
            upd(1, 0, N, x, N, 1);
        }
        swap(s[x], s[y]);
//        cout << "Case " << i + 1 << endl;
//        deb(1, 0, N);
        cout << val[1][3] << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(a) ((int)a.size())

const int oo = 1e9;
const int maxn = 1e5 + 5;

// 0 - max f(x)
// 1 - max f(x) - 2 * f(r)
// 2 - max f(x) - 2 * f(l)
// 3 - max f(x) - 2 * f(m) + f(y)
// 4 - min f(x)
int val[maxn * 8][5];
int _lz[maxn * 8];
int n, q;
string s;

void upd_node(int x, int v) {
    _lz[x] += v;
    val[x][0] += v;
    val[x][1] -= v;
    val[x][2] -= v;
    val[x][4] += v;
}

void merge_node(int x, int y, int z) {
    val[x][0] = max(val[y][0], val[z][0]);
    val[x][1] = max(val[y][1], val[z][1]);
    val[x][1] = max(val[x][1], val[y][0] - 2 * val[z][4]);
    val[x][2] = max(val[y][2], val[z][2]);
    val[x][2] = max(val[x][2], val[z][0] - 2 * val[y][4]);
    val[x][3] = max(val[y][3], val[z][3]);
    val[x][3] = max(val[x][3], val[y][1] + val[z][0]);
    val[x][3] = max(val[x][3], val[y][0] + val[z][2]);
    val[x][4] = min(val[y][4], val[z][4]);
}

void pushdown(int x) {
    upd_node(2 * x, _lz[x]);
    upd_node(2 * x + 1, _lz[x]);
    _lz[x] = 0;
    merge_node(x, 2 * x, 2 * x + 1);
}

void upd(int x, int L, int R, int l, int r, int v) {
    if (l > R || L > r) return;
    if (l <= L && R <= r) {
        upd_node(x, v);
        return;
    }
    if (L < R) {
        pushdown(x);
        int M = (L + R) >> 1;
        upd(2 * x, L, M, l, r, v);
        upd(2 * x + 1, M + 1, R, l, r, v);
        merge_node(x, 2 * x, 2 * x + 1);
    }
}

void build(int x, int L, int R) {
    if (L == R) {
        val[x][0] = val[x][4] = 0;
        val[x][1] = 0;
        val[x][2] = 0;
        val[x][3] = 0;
        return;
    }
    int M = (L + R) >> 1;
    build(2 * x, L, M);
    build(2 * x + 1, M + 1, R);
}

void deb(int x, int L, int R) {
    if (L == R) {
        cout << L << " " << val[x][0] << endl;
        return;
    }
    pushdown(x);
    int M = (L + R) >> 1;
    deb(2 * x, L, M);
    deb(2 * x + 1, M + 1, R);
}

int main() {
    freopen("inp.txt", "r", stdin);
    cin >> n >> q;
    int N = 2 * (n - 1) - 1;

    cin >> s;

    build(1, 0, N);
    rep(i, 0, sz(s)) {
        int v = (s[i] == '(' ? 1 : -1);
        upd(1, 0, N, i, N, v);
    }

//    cout << "Case 0" << endl;
//    deb(1, 0, N);
    cout << val[1][3] << endl;
    rep(i, 0, q) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        if (s[x] == '(') {
            upd(1, 0, N, x, N, -1);
            upd(1, 0, N, y, N, 1);
        } else {
            upd(1, 0, N, x, N, 1);
            upd(1, 0, N, y, N, -1);
        }
        if (s[y] == ')') {
            upd(1, 0, N, y, N, 1);
            upd(1, 0, N, x, N, -1);
        } else {
            upd(1, 0, N, y, N, -1);
            upd(1, 0, N, x, N, 1);
        }
        swap(s[x], s[y]);
//        cout << "Case " << i + 1 << endl;
//        deb(1, 0, N);
        cout << val[1][3] << '\n';
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

#define Rep(i,l,r) for (int i = (l); i <= (r); ++ i)
#define RepD(i,r,l) for (int i = (r); i >= (l); -- i)
#define RepG(i,x) for (int i = hd[x]; i; i = e[i].nxt)
#define ls (o<<1)
#define rs (o<<1|1)

const int N = 2e5+5;

int n, q; char s[N];
struct node {int s, x, z, xz, zy, d;} tr[N<<2];

void upd (int o) {
	int S = tr[ls].s, lx = tr[ls].x, lz = tr[ls].z, lxz = tr[ls].xz, lzy = tr[ls].zy;
	int rx = tr[rs].x+S, rz = tr[rs].z-2*S, rxz = tr[rs].xz-S, rzy = tr[rs].zy-S;
	tr[o].s = S+tr[rs].s;
	tr[o].x = max (lx, rx), tr[o].z = max (lz, rz);
	tr[o].xz = max (max (lxz, rxz), lx+rz);
	tr[o].zy = max (max (lzy, rzy), lz+rx);
	tr[o].d = max (max (tr[ls].d, tr[rs].d), max (lxz+rx, lx+rzy));
}

void mdf (int o, int l, int r, int p, int c) {
	if (l==r) {tr[o] = {c,c,-2*c,-c,-c,0}; return;}
	int mid = (l+r)>>1;
	if (p<=mid) mdf (ls, l, mid, p, c);
	else mdf (rs, mid+1, r, p, c);
	upd (o);
}

int main () {
	scanf ("%d%d%s", &n, &q, s+1);
	Rep(i,1,2*n-2) mdf (1, 1, 2*n-2, i, s[i]=='('? 1: -1);
	printf ("%d\n", tr[1].d);
	while (q --) {
		int x, y; scanf ("%d%d", &x, &y);
		swap (s[x], s[y]);
		mdf (1, 1, 2*n-2, x, s[x]=='('? 1: -1);
		mdf (1, 1, 2*n-2, y, s[y]=='('? 1: -1);
		printf ("%d\n", tr[1].d);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int inf = 0x3f3f3f3f;
int n, q, sum[MAXN];
char s[MAXN];

int Max[MAXN<<2], Min[MAXN<<2], lz[MAXN<<2];
int Ans[MAXN<<2], L[MAXN<<2], R[MAXN<<2];

inline void upd(int i) {
    Max[i] = max(Max[i<<1], Max[i<<1|1]);
    Min[i] = min(Min[i<<1], Min[i<<1|1]);

    L[i] = max(max(L[i<<1], L[i<<1|1]), Max[i<<1] - 2*Min[i<<1|1]);
    R[i] = max(max(R[i<<1], R[i<<1|1]), Max[i<<1|1] - 2*Min[i<<1]);

    Ans[i] = max(max(Ans[i<<1], Ans[i<<1|1]), max(Max[i<<1] + R[i<<1|1], Max[i<<1|1] + L[i<<1]));
}

void build(int i, int l, int r) {
    if(l == r) {
        Max[i] = Min[i] = sum[l];
        Ans[i] = 0;
        L[i] = R[i] = -sum[l]; return;
    }
    int mid = (l + r) >> 1;
    build(i<<1, l, mid);
    build(i<<1|1, mid+1, r);
    upd(i);
}

inline void give(int i, int v) {
    Max[i] += v, Min[i] += v;
    L[i] -= v, R[i] -= v;
    lz[i] += v;
}

inline void mt(int i) {
    if(lz[i]) {
        give(i<<1, lz[i]);
        give(i<<1|1, lz[i]);
        lz[i] = 0;
    }
}

void ins(int i, int l, int r, int x, int y, int v) {
    if(l == x && r == y) {
        give(i, v);
        return;
    }
    mt(i);
    int mid = (l + r) >> 1;
    if(y <= mid) ins(i<<1, l, mid, x, y, v);
    else if(x > mid) ins(i<<1|1, mid+1, r, x, y, v);
    else ins(i<<1, l, mid, x, mid, v), ins(i<<1|1, mid+1, r, mid+1, y, v);
    upd(i);
}

int main() {
    scanf("%d%d", &n, &q);
    n-=1; n<<=1;
    scanf("%s", s+1);
    for(int i = 1; i <= n; ++i)
        sum[i] = sum[i-1] + (s[i] == '(' ? 1 : -1);
    build(1, 1, n);
    int x, y;
    printf("%d\n", Ans[1]);
    while(q--) {
        scanf("%d%d", &x, &y);
        if(x > y) swap(x, y);
        if(s[x] == '(' && s[y] == ')') {
            swap(s[x], s[y]);
            ins(1, 1, n, x, y-1, -2);
        }
        else if(s[x] == ')' && s[y] == '(') {
            swap(s[x], s[y]);
            ins(1, 1, n, x, y-1, 2);
        }
        printf("%d\n", Ans[1]);
    }
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void maximize(int &a,int b)
{
  a=max(a,b);
}

int absolut(int x)
{
  if (x<0) return -x;
  return x;
}

const int tope=1<<19;
const int first=1<<18;

struct elem {
  int ls,lH,lh,lb;
  int rs,rH,rh,rb;
  int sol;
};

elem v[tope];

void recompute(int pos)
{
  elem &e=v[pos];
  elem &e0=v[2*pos];
  elem &e1=v[2*pos+1];
  e.ls=e0.ls+e1.ls;
  e.lH=max(e0.lH,e0.ls+e1.lH);
  e.lh=min(e0.lh,e0.ls+e1.lh);
  e.lb=max(e0.lb,-e0.ls+e1.lb);
  if (e0.lh<e0.ls+e1.lH)
    maximize(e.lb,-2*e0.lh+e0.ls+e1.lH);
  e.rs=e0.rs+e1.rs;
  e.rH=max(e1.rH,e0.rH+e1.rs);
  e.rh=min(e1.rh,e0.rh+e1.rs);
  e.rb=max(e1.rb,e0.rb-e1.rs);
  if (e1.rh<e0.rH+e1.rs)
    maximize(e.rb,-2*e1.rh+e1.rs+e0.rH);
  e.sol=max(max(e0.sol,e1.sol),max(e0.rb+e1.lH,e0.rH+e1.lb));
}

void insert(int pos,int val)
{
  pos+=first;
  elem &e=v[pos];
  e.ls=val;
  e.lH=max(0,val);
  e.lh=min(0,val);
  e.lb=absolut(val);
  val*=-1;
  e.rs=val;
  e.rH=max(0,val);
  e.rh=min(0,val);
  e.rb=absolut(val);
  e.sol=absolut(val);
  while (pos>1) {
    pos/=2;
    recompute(pos);
  }
}

int n,cases;
string s;

void insert(int pos)
{
  insert(pos,s[pos]=='('?1:-1);
}

int main()
{
  cin>>n>>cases>>s;
  s=" "+s;
  for (int i=1;i<int(s.size());i++)
    insert(i);
  cout<<v[1].sol<<endl;
  for (int cas=0;cas<cases;cas++) {
    int pos0,pos1;
    cin>>pos0>>pos1;
    swap(s[pos0],s[pos1]);
    insert(pos0);
    insert(pos1);
    cout<<v[1].sol<<endl;
  }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *rstrstr(char *__restrict s1, char *__restrict s2)
{
  size_t  s1len = strlen(s1);
  size_t  s2len = strlen(s2);
  char *s;

  if (s2len > s1len)
    return NULL;
  for (s = s1 + s1len - s2len; s >= s1; --s)
    if (strncmp(s, s2, s2len) == 0)
      return s;
  return NULL;
}

int main()
{
    char v[100111], a1[111], a2[111];
    scanf("%s", &v);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2)) && (v-strstr(v,a1)>=0 || v-strstr(v,a2)>=0) && ((strstr(v,a1)-strstr(v,a2)>=0) || (strstr(v,a2)-strstr(v,a1))>=0)){
        if(strstr(v, a1)>=strstr(v,a2) && rstrstr(v, a1)<=rstrstr(v, a2) && (strstr(v,a1)!=rstrstr(v,a1) || strstr(v,a2)!=rstrstr(v,a2)))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *rstrstr(char *__restrict s1, char *__restrict s2)
{
  size_t  s1len = strlen(s1);
  size_t  s2len = strlen(s2);
  char *s;

  if (s2len > s1len)
    return NULL;
  for (s = s1 + s1len - s2len; s >= s1; --s)
    if (strncmp(s, s2, s2len) == 0)
      return s;
  return NULL;
}

int main()
{
    char v[100000], a1[100], a2[100];
    scanf("%s", &v);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2))){
        if(strstr(v, a1)>=strstr(v,a2) && rstrstr(v, a1)<=rstrstr(v, a2))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char v[100000], a1[100], a2[100], nv[100000];
    gets(v);
    strcpy(nv, v);
    strrev(nv);
    gets(a1);
    gets(a2);
    if(strstr(v, a1) != NULL){
        if(strstr(v, a1)>strstr(v,a2) && strstr(nv, a1)>strstr(nv, a2))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if ((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb )) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
  if (sequance[1000]=='a'){

       printf("%d lenseq\n", lenseq);
       printf("%d lena\n", lena);
      printf("%d lenb\n", lenb);
   }
   for (int j=0;j<=lenseq-lena;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+lena;break;}
   }

   for (int j=current;j<=lenseq-lenb;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2=j+lenb;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+lena;break;}
   }

   for (int j=current;j<=lenseq-lenb;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=lena-1; j--){
       if (sequance[j]==a[0]) {
          n3=1;
           for (int i=1;i<lena;i++){

               if(sequance[j-i]==a[i]){
                   n3++;
               } else break;
           }
       }
       if (n3==lena) {current2=j-lena;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[0]) {
           n4=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j-i]==b[i]){
                   n4 ++;

               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+lena;break;}
   }

   for (int j=current;j<=lenseq-lenb;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=lena-1; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2=j-lena;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
  if (sequance[1000]=='a'){

       printf("%d lenseq\n", lenseq);
       printf("%d lena\n", lena);
      printf("%d lenb\n", lenb);
   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   if (sequance[1000]=='a'){

       printf("%d lenseq\n", lenseq);
       printf("%d lena\n", lena);
       printf("%d lenb\n", lenb);
   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   if (sequance[1000]=='a'){
       printf("%d", lenseq);
   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000000];
   scanf("%s", sequance);
   char a[1000];
   char b[1000];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   if (sequance[100]=='a'){
       printf("%d", lenseq)
   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   if (sequance[100]=='a'){
       printf("%d", lenseq);
   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[1000000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100000];
   scanf("%s", sequance);
   char a[100];
   char b[100];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[1000000];
   scanf("%s", sequance);
   char a[1000];
   char b[1000];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[1000000];
   scanf("%s", sequance);
   char a[120];
   char b[120];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
  long long int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena+1;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+1;break;}
   }

   for (int j=current+1;j<=lenseq-lenb+1;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=0; j--){
       if (sequance[j]==a[lena-1]) {
           n3=1;
           int m=1;
           for (int i=lena-2;i>=0;i--){

               if(sequance[j-m]==a[i]){
                   n3 ++;
                   m++;
               } else break;
           }
       }
       if (n3==lena) {current2= j;break;}
   }

   for (int j=current2+1;j>=0; j--){
       if (sequance[j]==b[lenb-1]) {
           n4=1;
           int m=1;
           for (int i=lenb-2;i>=0;i--){
               if(sequance[j-m]==b[i]){
                   n4 ++;
                   m++;
               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+lena;break;}
   }

   for (int j=current;j<=lenseq-lenb;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=lena-1; j--){
       if (sequance[j]==a[0]) {

           for (int i=1;i<lena;i++){

               if(sequance[j-i]==a[i]){
                   n3++;
               } else break;
           }
       }
       if (n3==lena) {current2=j-lena;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[0]) {
           n4=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j-i]==b[i]){
                   n4 ++;

               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}


#include<stdio.h>
#include<string.h>

char s[100005],sa[105],sb[105];
int main()
{
    while(scanf("%s%s%s",s,sa,sb)!=EOF)
    {
        int l=strlen(s);
        int la=strlen(sa);
        int lb=strlen(sb);
        int flag1=0,pos=0,flag2=0,flag3=0,flag4=0;
        for(int i=0;i<l;i++)
        {
            flag1=1;
            for(int j=0;j<la;j++)
            {
                if(s[i+j]!=sa[j])
                {
                    flag1=0;
                    break;
                }
            }
            if(flag1==1)
            {
                pos=i+la;
                break;
            }
        }
        if(flag1==1)
        {
            for(int i=pos;i<l;i++)
            {
                flag2=1;
                for(int j=0;j<lb;j++)
                {
                    if(s[i+j]!=sb[j])
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1)
                break;
            }
        }
        //反着找
        for(int i=l-1;i>=0;i--)
        {
            flag3=1;
            for(int j=0;j<la;j++)
            {
                if(s[i-j]!=sa[j])
                {
                    flag3=0;
                    break;
                }
            }
            if(flag3==1)
            {
                pos=i-la;
                break;
            }
        }
        for(int i=pos;i>=0;i--)
        {
            flag4=1;
            for(int j=0;j<lb;j++)
            {
                if(s[i-j]!=sb[j])
                {
                    flag4=0;
                    break;
                }
            }
            if(flag4==1)
            break;
        }
        if(flag2==1 && flag4==1)
        {
            printf("both\n");
        }
        else if(flag2==1)
        {
            printf("forward\n");
        }
        else if(flag4==1)
        {
            printf("backward\n");
        }
        else
        printf("fantasy\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[10000], Reverse[10000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }
    //printf("%s ", Reverse);
    if(good(Reverse, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    else if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
     else if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <string.h>

#define A 100000
#define B 100
#define C 100

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

void reverse(int n, char *s) {
	int i, j;

	for (i = 0, j = n - 1; i < j; i++, j--) {
		char tmp;

		tmp = s[i];
		s[i] = s[j];
		s[j] = tmp;
	}
}

int main() {
	static char s1[A + 1], s2[B + 1], s3[C + 1];
	int a, b, c, forward, backward;

	scanf("%s%s%s", s1, s2, s3);
	a = strlen(s1);
	b = strlen(s2);
	c = strlen(s3);
	forward = 0;
	if (good(s1, s2, s3))
		forward = 1;
	reverse(a, s1);
	backward = 0;
	if (good(s1, s2, s3))
		backward = 1;
	printf(forward && backward ? "both\n" : forward ? "forward\n" : backward ? "backward\n" : "fantasy\n");
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[10000], Reverse[10000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
   strrev(Primul);

    //printf("%s ", Reverse);
    if(good(Primul, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    else if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
     else if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

void reverse(int n, char *s) {
	int i, j;

	for (i = 0, j = n - 1; i < j; i++, j--) {
		char tmp;

		tmp = s[i];
		s[i] = s[j];
		s[j] = tmp;
	}
}

int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[10000], Reverse[10000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array

    reverse(strlen(Primul), Primul);

    //printf("%s ", Reverse);
    if(good(Primul, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    else if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
     else if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[1000], Reverse[1000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    for (i = 0, j = strlen(Primul) - 1; i < j; i++, j--) {
		char temp;
		temp = Primul[i];
		Primul[i] = Primul[j];
		Primul[j] = Primul;
	}

    if(good(Primul, A, B)){
        inapoi=1;
    }
    if(inainte==1 && inapoi==0){
        printf("forward");
    }
    else if(inainte==0 && inapoi==1){
        printf("backward");
    }
    else if(inainte==1 && inapoi==1){
        printf("both");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[10000], Reverse[1z0000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }
    //printf("%s ", Reverse);
    if(good(Reverse, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    else if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
     else if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[100000], Reverse[100000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }

    if (good(Primul, A, B)){
        inainte=1;
    }

    if(good(Reverse, A, B)){
        inapoi=1;
    }
    if(inainte==1 && inapoi==0){
        printf("forward");
    }
    else if(inainte==0 && inapoi==1){
        printf("backward");
    }
    else if(inainte==1 && inapoi==1){
        printf("both");
    }
    else printf("fantasy");
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int n,m;
    int i,j;
    int flag=1;
    //citirea variabilelor
    scanf("%d%d",&n,&m);

    char a[n][m];
    //citirea arraiului
    for(i=0;i<n;i++){
        scanf("%s",&a[i]);
    }
    //daca 2 numere nu sunt egale pe rand atuncti flag primesste 0 si se opreste
    for(i=0; i<n; i++){
        for(j=0; j<m-1; j++){
            if(a[i][j]!=a[i][j+1]){
                flag=0;
                break;
            }
        }
        //iesirea din functie daca gasim o eroare
         if(flag==0){
            break;
        }

    }

   for(i=0; i<n; i++){
        for(j=0; j<m-1; j++){
            //verificam daca randurile adiacente sut diferite
            if(a[i][0]==a[i+1][0]){
            flag=0;
            break;
        }
        }
        //iesirea din functie daca gasim o eroare
         if(flag==0){
            break;
        }

    }

    if(flag==1){
        printf("YES");
    }
    else{
        printf("NO");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[1000], Reverse[1000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }
    //printf("%s ", Reverse);
    if(good(Reverse, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
    if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[1000], Reverse[1000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }
    printf("%s ", Reverse);
    if(good(Primul, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
    if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[10000], Reverse[10000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
   strrev(Primul);

    //printf("%s ", Reverse);
    if(good(Primul, A, B)){
        inapoi=1;
    }

    printf(inainte && inapoi ? "both\n" : inainte ? "forward\n" : inapoi ? "backward\n" : "fantasy\n");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[1000], Reverse[1000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    for (i = 0, j = strlen(Primul) - 1; i < j; i++, j--) {
		char temp;
		temp = Primul[i];
		Primul[i] = Primul[j];
		Primul[j] = Primul;
	}

    if(good(Primul, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
    if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
     static char Primul[10000], Reverse[10000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
   strrev(Primul);

    //printf("%s ", Reverse);
    if(good(Primul, A, B)){
        inapoi=1;
    }

    printf(inainte && inapoi ? "both\n" : inainte ? "forward\n" : inapoi ? "backward\n" : "fantasy\n");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}
int main()
{

    int i, flag, j;
    int inainte=0, inapoi=0;
    char Primul[1000], Reverse[1000];
    scanf("%s", Primul);
    char A[100], B[100];
    scanf("%s", A);
    scanf("%s", B);

    if (good(Primul, A, B)){
        inainte=1;
    }

    //iversul primul array
    j=0;
    for(i=strlen(Primul)-1; i>=0; i--){
        Reverse[j]=Primul[i];
        j++;
    }
    //printf("%s ", Reverse);
    if(good(Reverse, A, B)){
        inapoi=1;
    }

    if(inainte==1 && inapoi==1){
        printf("both");
        return 0;
    }
    else if(inainte==1 && inapoi==0){
        printf("forward");
        return 0;
    }
     else if(inainte==0 && inapoi==1){
        printf("backward");
        return 0;
    }

    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <string.h>

#define A 100000
#define B 100
#define C 100

int good(int a, int b, int c, char *s1, char *s2, char *s3) {
	int i, j, yes1, yes2;

	yes1 = 0;
	for (i = 0; i + b - 1 < a; i++)
		if (strncmp(s1 + i, s2, b) == 0) {
			yes1 = 1;
			break;
		}
	yes2 = 0;
	for (j = i + b; j + c - 1 < a; j++)
		if (strncmp(s1 + j, s3, c) == 0) {
			yes2 = 1;
			break;
		}
	return yes1 && yes2;
}

void reverse(int n, char *s) {
	int i, j;

	for (i = 0, j = n - 1; i < j; i++, j--) {
		char tmp;

		tmp = s[i];
		s[i] = s[j];
		s[j] = tmp;
	}
}

int main() {
	static char s1[A + 1], s2[B + 1], s3[C + 1];
	int a, b, c, forward, backward;

	scanf("%s%s%s", s1, s2, s3);
	a = strlen(s1);
	b = strlen(s2);
	c = strlen(s3);
	forward = 0;
	if (good(a, b, c, s1, s2, s3))
		forward = 1;
	reverse(a, s1);
	backward = 0;
	if (good(a, b, c, s1, s2, s3))
		backward = 1;
	printf(forward && backward ? "both\n" : forward ? "forward\n" : backward ? "backward\n" : "fantasy\n");
	return 0;
}


/* https://codeforces.com/contest/8/submission/17393175 (rainboy) */
#include <stdio.h>
#include <string.h>

#define A 100000
#define B 100
#define C 100

int good(char *s1, char *s2, char *s3) {
	return (s1 = strstr(s1, s2)) != NULL && strstr(s1 + strlen(s2), s3) != NULL;
}

void reverse(int n, char *s) {
	int i, j;

	for (i = 0, j = n - 1; i < j; i++, j--) {
		char tmp;

		tmp = s[i];
		s[i] = s[j];
		s[j] = tmp;
	}
}

int main() {
	static char s1[A + 1], s2[B + 1], s3[C + 1];
	int a, b, c, forward, backward;

	scanf("%s%s%s", s1, s2, s3);
	a = strlen(s1);
	b = strlen(s2);
	c = strlen(s3);
	forward = 0;
	if (good(s1, s2, s3))
		forward = 1;
	reverse(a, s1);
	backward = 0;
	if (good(s1, s2, s3))
		backward = 1;
	printf(forward && backward ? "both\n" : forward ? "forward\n" : backward ? "backward\n" : "fantasy\n");
	return 0;
}


#include <stdio.h>
#include <string.h>

#define A 100000
#define B 100
#define C 100

int main() {
	static char s1[A + 1], s2[B + 1], s3[C + 1];
	int a, b, c, i, j, x, y, forward, backward;

	scanf("%s%s%s", s1, s2, s3);
	a = strlen(s1);
	b = strlen(s2);
	c = strlen(s3);
	x = 0;
	for (i = 0; i < a; i++)
		if (s1[i] == s2[x]) {
			x++;
			if (x == b)
				break;
		}
	y = 0;
	for (j = i + 1; j < a; j++)
		if (s1[j] == s3[y]) {
			y++;
			if (y == c)
				break;
		}
	forward = x == b && y == c;
	x = 0;
	for (i = a - 1; i >= 0; i--)
		if (s1[i] == s2[x]) {
			x++;
			if (x == b)
				break;
		}
	y = 0;
	for (j = i - 1; j >= 0; j--)
		if (s1[j] == s3[y]) {
			y++;
			if (y == c)
				break;
		}
	backward = x == b && y == c;
	printf(forward && backward ? "both\n" : forward ? "forward\n" : backward ? "backward\n" : "fantasy\n");
	return 0;
}


#include <stdio.h>
#include <string.h>

#define A 100000
#define B 100
#define C 100

int good(int a, int b, int c, char *s1, char *s2, char *s3) {
	int i, j, yes1, yes2;

	yes1 = 0;
	for (i = 0; i < a; i++)
		if (strncmp(s1 + i, s2, b) == 0) {
			yes1 = 1;
			break;
		}
	yes2 = 0;
	for (j = i + 1; j < a; j++)
		if (strncmp(s1 + j, s3, c) == 0) {
			yes2 = 1;
			break;
		}
	return yes1 && yes2;
}

void reverse(int n, char *s) {
	int i, j;

	for (i = 0, j = n - 1; i < j; i++, j--) {
		char tmp;

		tmp = s[i];
		s[i] = s[j];
		s[j] = tmp;
	}
}

int main() {
	static char s1[A + 1], s2[B + 1], s3[C + 1];
	int a, b, c, forward, backward;

	scanf("%s%s%s", s1, s2, s3);
	a = strlen(s1);
	b = strlen(s2);
	c = strlen(s3);
	forward = 0;
	if (good(a, b, c, s1, s2, s3))
		forward = 1;
	reverse(a, s1);
	backward = 0;
	if (good(a, b, c, s1, s2, s3))
		backward = 1;
	printf(forward && backward ? "both\n" : forward ? "forward\n" : backward ? "backward\n" : "fantasy\n");
	return 0;
}


#include <stdio.h>
#include <string.h>

char s[100001], b[101], c[101];

void copiefragmentsir(char *s, char *q, int i, int k)
{
    // se presupune ca fragmentul copiat nu depaseste sirul initial
    int j;
    for (j = i; j < i + k; j++ )  q[j - i] = s[j];
    q[j - i] = '\0';

    return;
}

int cautqins(char *q, char *s, int k)
{
    char tmp[101];
    int ls = strlen(s);
    int lq = strlen(q);
    if (ls <= lq) return -1;
    for (int i = k; i <= ls - lq; i++)
      if(s[i] == q[0])
        {

            copiefragmentsir(s, tmp, i, lq);
            if (strcmp(q, tmp) == 0) return i;
        }
    return -1;
}

int este(char *s, char *a, char *b)
{
    int pa = cautqins(a,s,0);
    if (pa < 0) return -1;
    int pb = cautqins(b,s,pa + strlen(a));
    if (pb < 0) return -1;
    else return 1;
}

void inverse (char *z)
{
    int tz, n = strlen(z);
    for (int i = 0; i < n / 2; i++)
    {
        tz = z[n - i - 1];
        z[n - i - 1] = z[i];
        z[i] = tz;
    }
}

int main()
{
    scanf("%s", s);
    scanf("%s %s", b, c);
    int flag1 = 0, flag2 = 0;
    if( este (s, b, c) > 0)  flag1 = 1;
    inverse(s);
    if( este (s, b, c) > 0 ) flag2 = 1;

    if (flag1 == 1 && flag2 == 1) printf("both");
    if (flag1 == 1 && flag2 == 0) printf("forward");
    if (flag1 == 0 && flag2 == 1) printf("backward");
    if (flag1 == 0 && flag2 == 0) printf("fantasy");
    return 0;
}


#include<stdio.h>
#include<string.h>
int findsub(char s[100002], char s2[102], int x)
{
    int pos,i,j,k,count,len,len2;
    len=strlen(s);
    len2=strlen(s2);
    //printf("%d ",len2);
    for(i=x;i<len;i++)
    {
        if(s[i]==s2[0])
        {
            count=1;
            pos=i;
            k=i+1;
            j=1;
            while(s[k]==s2[j] && (s2[j]!='\0' && s[k]!='\0'))
            {
                count++;
                k++;
                j++;
            }
            if(count==len2)
                return pos;
            else
             count=0;
        }
    }
    return -1;
}

int main()
{
    char str[100002],rev[100002],s1[102],s2[102],*x;
    int fwd=0, back=0,len,len1,len2, pos1,pos2,y;
    scanf(" %s",str);
    scanf(" %s",s1);
    scanf(" %s",s2);
    strcpy(rev,str);
    strrev(rev);
    len=strlen(str);
    len1=strlen(s1);
    len2=strlen(s2);
    y=len-(len1+len2);

    x=strstr(str,s1);
    if(x)
    {
        pos1=x-str;
        pos2=findsub(str,s2,pos1+1);
        //printf("%d %d\n",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            fwd=1;
    }

    x=strstr(rev,s1);
    if(x)
    {
        pos1=x-rev;
        pos2=findsub(rev,s2,pos1+1);
        //printf("%d %d",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            back=1;
    }

    if((fwd&&back) && y>=0)
        printf("both\n");
    else if(fwd && y>=0)
        printf("forward\n");
    else if(back && y>=0)
        printf("backward\n");
    else
        printf("fantasy\n");

    return 0;
}


#include<stdio.h>
#include<string.h>
int findsub(char s[100002], char s2[102], int x)
{
    int pos,i,j,k,count,len,len2;
    len=strlen(s);
    len2=strlen(s2);
    //printf("%d ",len2);
    for(i=x;i<len;i++)
    {
        if(s[i]==s2[0])
        {
            count=1;
            pos=i;
            k=i+1;
            j=1;
            while(s[k]==s2[j] && (s2[j]!='\0' && s[k]!='\0'))
            {
                count++;
                k++;
                j++;
            }
            if(count==len2)
                return pos;
            else
             count=0;
        }
    }
    return -1;
}

int main()
{
    char str[100002],rev[100002],s1[102],s2[102],*x;
    int fwd=0, back=0,len,len1,len2, pos1,pos2,y;
    scanf(" %s",str);
    scanf(" %s",s1);
    scanf(" %s",s2);
    strcpy(rev,str);
    strrev(rev);
    len=strlen(str);
    len1=strlen(s1);
    len2=strlen(s2);
    y=len-(len1+len2);

    x=strstr(str,s1);
    if(x)
    {
        pos1=x-str;
        pos2=findsub(str,s2,pos1+1);
        //printf("%d %d\n",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            fwd=1;
    }

    x=strstr(rev,s1);
    if(x)
    {
        pos1=x-rev;
        pos2=findsub(rev,s2,pos1+1);
        //printf("%d %d",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            back=1;
    }

    if((fwd&&back) && y>0)
        printf("both\n");
    else if(fwd && y>0)
        printf("forward\n");
    else if(back && y>0)
        printf("back\n");
    else
        printf("fantasy\n");

    return 0;
}


#include<stdio.h>
#include<string.h>
int findsub(char s[100002], char s2[102], int x)
{
    int pos,i,j,k,count,len,len2;
    len=strlen(s);
    len2=strlen(s2);
    //printf("%d ",len2);
    for(i=x;i<len;i++)
    {
        if(s[i]==s2[0])
        {
            count=1;
            pos=i;
            k=i+1;
            j=1;
            while(s[k]==s2[j] && (s2[j]!='\0' && s[k]!='\0'))
            {
                count++;
                k++;
                j++;
            }
            if(count==len2)
                return pos;
            else
             count=0;
        }
    }
    return -1;
}

int main()
{
    char str[100002],rev[100002],s1[102],s2[102],*x;
    int fwd=0, back=0,len,len1,len2, pos1,pos2,y;
    scanf(" %s",str);
    scanf(" %s",s1);
    scanf(" %s",s2);
    strcpy(rev,str);
    strrev(rev);
    len=strlen(str);
    len1=strlen(s1);
    len2=strlen(s2);
    y=len-(len1+len2);

    x=strstr(str,s1);
    if(x)
    {
        pos1=x-str;
        pos2=findsub(str,s2,pos1+1);
        //printf("%d %d\n",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            fwd=1;
    }

    x=strstr(rev,s1);
    if(x)
    {
        pos1=x-rev;
        pos2=findsub(rev,s2,pos1+1);
        //printf("%d %d",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            back=1;
    }

    if((fwd&&back) && y>0)
        printf("both\n");
    else if(fwd && y>0)
        printf("forward\n");
    else if(back && y>0)
        printf("backward\n");
    else
        printf("fantasy\n");

    return 0;
}


#include<stdio.h>
#include<string.h>
int findsub(char s[100002], char s2[102], int x)
{
    int pos,i,j,k,count,len,len2;
    len=strlen(s);
    len2=strlen(s2);
    //printf("%d ",len2);
    for(i=x;i<len;i++)
    {
        if(s[i]==s2[0])
        {
            count=1;
            pos=i;
            k=i+1;
            j=1;
            while(s[k]==s2[j] && (s2[j]!='\0' && s[k]!='\0'))
            {
                count++;
                k++;
                j++;
            }
            if(count==len2)
                return pos;
            else
             count=0;
        }
    }
    return -1;
}

int main()
{
    char str[100002],rev[100002],s1[102],s2[102],*x;
    int fwd=0, back=0,len,len1,len2, pos1,pos2,y;
    scanf(" %s",str);
    scanf(" %s",s1);
    scanf(" %s",s2);
    strcpy(rev,str);
    strrev(rev);
    len=strlen(str);
    len1=strlen(s1);
    len2=strlen(s2);
    y=len-(len1+len2);

    x=strstr(str,s1);
    if(x)
    {
        pos1=x-str;
        pos2=findsub(str,s2,pos1+1);
        //printf("%d %d\n",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            fwd=1;
    }

    x=strstr(rev,s1);
    if(x)
    {
        pos1=x-rev;
        pos2=findsub(rev,s2,pos1+1);
        //printf("%d %d",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            back=1;
    }

    if((fwd&&back) && y>=0)
        printf("both\n");
    else if(fwd && y>=0)
        printf("forward\n");
    else if(back && y>=0)
        printf("back\n");
    else
        printf("fantasy\n");

    return 0;
}


#include<stdio.h>
#include<string.h>
int findsub(char s[100002], char s2[102], int x)
{
    int pos,i,j,k,count,len,len2;
    len=strlen(s);
    len2=strlen(s2);
    //printf("%d ",len2);
    for(i=x;i<len;i++)
    {
        if(s[i]==s2[0])
        {
            count=1;
            pos=i;
            k=i+1;
            j=1;
            while(s[k]==s2[j] && (s2[j]!='\0' && s[k]!='\0'))
            {
                count++;
                k++;
                j++;
            }
            if(count==len2)
                return pos;
            else
             count=0;
        }
    }
    return -1;
}

int main()
{
    char str[100002],rev[100002],s1[102],s2[102],*x;
    int fwd=0, back=0,len, pos1,pos2,y;
    scanf(" %s",str);
    scanf(" %s",s1);
    scanf(" %s",s2);
    strcpy(rev,str);
    strrev(rev);

    x=strstr(str,s1);
    if(x)
    {
        pos1=x-str;
        pos2=findsub(str,s2,pos1+1);
        //printf("%d %d\n",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            fwd=1;
    }

    x=strstr(rev,s1);
    if(x)
    {
        pos1=x-rev;
        pos2=findsub(rev,s2,pos1+1);
        //printf("%d %d",pos1,pos2);
        if(pos2!=-1 && pos1<pos2)
            back=1;
    }

    if(fwd&&back)
        printf("both\n");
    else if(fwd)
        printf("forward\n");
    else if(back)
        printf("back\n");
    else
        printf("fantasy\n");

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int len(char *str)
{
    int i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}
int main()
{
    char base[100001];
    char first[101];
    char second[101];
    int count = 0;

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0;
    int foundFirst = 0;
    for (int i = 0; i < len(base); i++)
    {
        for (int j = 0; j < len(first) && i + j < len(base); j++)
        {
            if (base[i + j] != first[j])
            {
                break;
            }

            if (j + 1 == len(first))
            {
                foundFirst = 1;
            }
        }

        if (foundFirst == 1)
        {
            i += len(first);
            for (int j = 0; j < len(second) && i + j < len(base); j++)
            {
                if (base[i + j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    forward = 1;
                }
            }
        }
    }

    int backward = 0;
    foundFirst = 0;
    for (int i = len(base) - 1; i >= 0; i--)
    {
        for (int j = 0; j < len(first) && i - j >= 0; j++)
        {
            if (base[i - j] != first[j])
            {
                break;
            }

            if (j + 1 == len(first))
            {
                foundFirst = 1;
            }
        }

        if (foundFirst == 1)
        {
            i -= len(first);
            for (int j = 0; j < len(second) && i - j >= 0; j++)
            {
                if (base[i - j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    backward = 1;
                }
            }
        }
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

// returns length of string
long len(char *str)
{
    long i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}

// returns true if X and Y are same
int compare(char *X, char *Y)
{
    while (*X && *Y)
    {
        if (*X != *Y)
            return 0;

        X++;
        Y++;
    }

    return (*Y == '\0');
}

// Function to implement strstr() function
long pos(char *X, char *Y)
{
    long i = 0;
    while (X[i] != '\0')
    {
        if ((X[i] == Y[0]) && compare(X + i, Y))
            return i;
        i++;
    }

    return -1;
}

// reverses a string
void reverse(char s[])
{
    long length = len(s);
    long c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main()
{
    char base[100001];
    char first[101];
    char second[101];

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0, backward = 0;

    long forwardFirstPos = pos(base, first);
    long forwardSecondPos = pos(base + forwardFirstPos + len(first), second);
    if (forwardSecondPos >= 0)
    {
        forwardSecondPos += forwardFirstPos + len(first);
    }
    if (forwardFirstPos >= 0 && forwardSecondPos >= 0 && forwardFirstPos < forwardSecondPos)
    {
        forward = 1;
    }

    reverse(base);

    long backwardFirstPos = pos(base, first);
    long backwardSecondPos = pos(base + backwardFirstPos + len(first), second);
    if (backwardSecondPos >= 0)
    {
        backwardSecondPos += backwardFirstPos + len(first);
    }
    if (backwardFirstPos >= 0 && backwardSecondPos >= 0 && backwardFirstPos < backwardSecondPos)
    {
        backward = 1;
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int len(char *str)
{
    int i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}
int main()
{
    char base[100001];
    char first[101];
    char second[101];
    int count = 0;

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0;
    int foundFirst = 0;
    for (int i = 0; i < len(base); i++)
    {
        if (foundFirst != 1)
        {
            for (int j = 0; j < len(first) && i + j < len(base); j++)
            {
                if (base[i + j] != first[j])
                {
                    break;
                }

                if (j + 1 == len(first))
                {
                    foundFirst = 1;
                    i += len(first) - 1;
                }
            }
        }
        else
        {
            for (int j = 0; j < len(second) && i + j < len(base); j++)
            {
                if (base[i + j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    forward = 1;
                }
            }
        }
    }

    int backward = 0;
    foundFirst = 0;
    for (int i = len(base) - 1; i >= 0; i--)
    {
        if (foundFirst != 1)
        {
            for (int j = 0; j < len(first) && i - j >= 0; j++)
            {
                if (base[i - j] != first[j])
                {
                    break;
                }

                if (j + 1 == len(first))
                {
                    foundFirst = 1;
                    i -= len(first) - 1;
                }
            }
        }
        else
        {
            for (int j = 0; j < len(second) && i - j >= 0; j++)
            {
                if (base[i - j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    backward = 1;
                }
            }
        }
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

// returns length of string
long len(char *str)
{
    long i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}

// returns true if X and Y are same
int compare(char *X, char *Y)
{
    while (*X && *Y)
    {
        if (*X != *Y)
            return 0;

        X++;
        Y++;
    }

    return (*Y == '\0');
}

// Function to implement strstr() function
long pos(char *X, char *Y)
{
    long i = 0;
    while (X[i] != '\0')
    {
        if ((X[i] == Y[0]) && compare(X + i, Y))
            return i;
        i++;
    }

    return -1;
}

// reverses a string
void reverse(char s[])
{
    long length = len(s);
    long c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main()
{
    char base[100001];
    char first[101];
    char second[101];

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0, backward = 0;

    long forwardFirstPos = pos(base, first);
    long forwardSecondPos = pos(base + len(first), second);
    if (forwardFirstPos >= 0 && forwardSecondPos >= 0 && forwardFirstPos < forwardSecondPos)
    {
        forward = 1;
    }

    reverse(base);

    long backwardFirstPos = pos(base, first);
    long backwardSecondPos = pos(base + len(first), second);
    if (backwardFirstPos >= 0 && backwardSecondPos >= 0 && backwardFirstPos < backwardSecondPos)
    {
        backward = 1;
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

// returns length of string
long len(char *str)
{
    long i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}

// returns true if X and Y are same
int compare(char *X, char *Y)
{
    while (*X && *Y)
    {
        if (*X != *Y)
            return 0;

        X++;
        Y++;
    }

    return (*Y == '\0');
}

// Function to implement strstr() function
long pos(char *X, char *Y)
{
    long i = 0;
    while (X[i] != '\0')
    {
        if ((X[i] == Y[0]) && compare(X + i, Y))
            return i;
        i++;
    }

    return -1;
}

// reverses a string
void reverse(char s[])
{
    long length = len(s);
    long c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main()
{
    char base[100001];
    char first[101];
    char second[101];

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0, backward = 0;

    long forwardFirstPos = pos(base, first);
    long forwardSecondPos = pos(base + forwardFirstPos + len(first), second) + forwardFirstPos + len(first);
    if (forwardFirstPos >= 0 && forwardSecondPos >= 0 && forwardFirstPos < forwardSecondPos)
    {
        forward = 1;
    }

    reverse(base);

    long backwardFirstPos = pos(base, first);
    long backwardSecondPos = pos(base + backwardFirstPos + len(first), second) + backwardFirstPos + len(first);
    if (backwardFirstPos >= 0 && backwardSecondPos >= 0 && backwardFirstPos < backwardSecondPos)
    {
        backward = 1;
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

long len(char *str)
{
    long i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}
int main()
{
    char base[100001];
    char first[101];
    char second[101];

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0;
    int foundFirst = 0;
    for (long i = 0; i < len(base); i++)
    {
        if (foundFirst != 1)
        {
            for (long j = 0; j < len(first) && i + j < len(base); j++)
            {
                if (base[i + j] != first[j])
                {
                    break;
                }

                if (j + 1 == len(first))
                {
                    foundFirst = 1;
                    i += len(first) - 1;
                }
            }
        }
        else
        {
            for (long j = 0; j < len(second) && i + j < len(base); j++)
            {
                if (base[i + j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    forward = 1;
                }
            }
        }
    }

    int backward = 0;
    foundFirst = 0;
    for (long i = len(base) - 1; i >= 0; i--)
    {
        if (foundFirst != 1)
        {
            for (long j = 0; j < len(first) && i - j >= 0; j++)
            {
                if (base[i - j] != first[j])
                {
                    break;
                }

                if (j + 1 == len(first))
                {
                    foundFirst = 1;
                    i -= len(first) - 1;
                }
            }
        }
        else
        {
            for (long j = 0; j < len(second) && i - j >= 0; j++)
            {
                if (base[i - j] != second[j])
                {
                    break;
                }

                if (j + 1 == len(second))
                {
                    backward = 1;
                }
            }
        }
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include<stdio.h>
#include<string.h>

char s[100005],sa[105],sb[105];
int main()
{
    while(scanf("%s%s%s",s,sa,sb)!=EOF)
    {
        int l=strlen(s);
        int la=strlen(sa);
        int lb=strlen(sb);
        int flag1=0,pos=0,flag2=0,flag3=0,flag4=0;
        for(int i=0;i<l;i++)
        {
            flag1=1;
            for(int j=0;j<la;j++)
            {
                if(s[i+j]!=sa[j])
                {
                    flag1=0;
                    break;
                }
            }
            if(flag1==1)
            {
                pos=i+la;
                break;
            }
        }
        if(flag1==1)
        {
            for(int i=pos;i<l;i++)
            {
                flag2=1;
                for(int j=0;j<lb;j++)
                {
                    if(s[i+j]!=sb[j])
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1)
                    break;
            }
        }

        for(int i=l-1;i>=0;i--)
        {
            flag3=1;
            for(int j=0;j<la;j++)
            {
                if(s[i-j]!=sa[j])
                {
                    flag3=0;
                    break;
                }
            }
            if(flag3==1)
            {
                pos=i-la;
                break;
            }
        }
        for(int i=pos;i>=0;i--)
        {
            flag4=1;
            for(int j=0;j<lb;j++)
            {
                if(s[i-j]!=sb[j])
                {
                    flag4=2;
                    break;
                }
            }
            if(flag4==1)
                break;
        }
        if(flag2==1 && flag4==1)
        {
            printf("both\n");
        }
        else if(flag2==1)
        {
            printf("forward\n");
        }
        else if(flag4==1)
        {
            printf("backward\n");
        }
        else
            printf("fantasy\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

char s[100005],sa[105],sb[105];
int main()
{
    while(scanf("%s%s%s",s,sa,sb)!=EOF)
    {
        int l=strlen(s);
        int la=strlen(sa);
        int lb=strlen(sb);
        int flag1=0,pos=0,flag2=0,flag3=0,flag4=0;
        for(int i=0;i<l;i++)
        {
            flag1=1;
            for(int j=0;j<la;j++)
            {
                if(s[i+j]!=sa[j])
                {
                    flag1=0;
                    break;
                }
            }
            if(flag1==1)
            {
                pos=i+la;
                break;
            }
        }
        if(flag1==1)
        {
            for(int i=pos;i<l;i++)
            {
                flag2=1;
                for(int j=0;j<lb;j++)
                {
                    if(s[i+j]!=sb[j])
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1)
                    break;
            }
        }

        for(int i=l-1;i>=0;i--)
        {
            flag3=1;
            for(int j=0;j<la;j++)
            {
                if(s[i-j]!=sa[j])
                {
                    flag3=0;
                    break;
                }
            }
            if(flag3==1)
            {
                pos=i-la;
                break;
            }
        }
        for(int i=pos;i>=0;i--)
        {
            flag4=1;
            for(int j=0;j<lb;j++)
            {
                if(s[i-j]!=sb[j])
                {
                    flag4=0;
                    break;
                }
            }
            if(flag4==4)
                break;
        }
        if(flag2==1 && flag4==1)
        {
            printf("both\n");
        }
        else if(flag2==1)
        {
            printf("forward\n");
        }
        else if(flag4==1)
        {
            printf("backward\n");
        }
        else
            printf("fantasy\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		int j;
		for(j=0;j<lenb;j++)
		{
			if(a[i+j]!=b[j])
				break;
		}
		if(j==lenb)
		{flag=1;i+=lenb;break;}
	}
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		int j;
		for(j=0;j<lenc;j++)
		{
			if(a[i+j]!=c[j])
				break;
		}
		if(j==lenc)
		{temp1=1;break;}
		i++;
	}
}
void backward()
{
	int i=lena-1,flag=0;
	for(;i>=0;i--)
	{
		int j;
		for(j=lenb-1;j>=0;j--)
		{
			if(a[i-j+1]==b[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{flag=1;i-=lenb;break;}
	}
	if(flag==0)
		return;
	while(i>=0)
	{
		int j;
		for(j=lenc-1;j>=0;j--)
		{
			if(a[i+j]==c[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{temp2=1;break;}
		i--;
	}
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}


#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		int j;
		for(j=0;j<lenb;j++)
		{
			if(a[i+j]!=b[j])
				break;
		}
		if(j==lenb)
		{flag=1;i+=lenb;break;}
	}
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		int j;
		for(j=0;j<lenc;j++)
		{
			if(a[i+j]!=c[j])
				break;
		}
		if(j==lenc)
		{temp1=1;break;}
		i++;
	}
}
void backward()
{
	int i=lena-1,temp=lenb-1,flag=0;
	for(;i>=0;i--)
	{
		int j;
		for(j=lenb-1;j>=0;j--)
		{
			if(a[i+j-1]==b[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{flag=1;i-=lenb;break;}
	}
	if(flag==0)
		return;
	while(i>=0)
	{
		int j;
		for(j=lenc-1;j>=0;j--)
		{
			if(a[i+j]==c[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{temp2=1;break;}
		i--;
	}
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}


#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		int j;
		for(j=0;j<lenb;j++)
		{
			if(a[i+j]!=b[j])
				break;
		}
		if(j==lenb)
		{flag=1;i++;break;}
	}
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		int j;
		for(j=0;j<lenc;j++)
		{
			if(a[i+j]!=c[j])
				break;
		}
		if(j==lenc)
		{temp1=1;break;}
		i++;
	}
}
void backward()
{
	int i=lena-1,temp=lenb-1,flag=0;
	for(;i>=0;i--)
	{
		int j;
		for(j=lenb-1;j>=0;j--)
		{
			if(a[i+j]==b[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{flag=1;i--;break;}
	}
	if(flag==0)
		return;
	while(i>=0)
	{
		int j;
		for(j=lenc-1;j>=0;j--)
		{
			if(a[i+j]==c[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{temp2=1;break;}
		i--;
	}
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}


#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		if(temp==lenb)
		{
			flag=1;
			break;
		}
		if(a[i]==b[temp])
			temp++;
	}
//	printf("%d\n",i);
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		if(temp==lenc)
		{
			temp1=1;
			flag=1;
			break;
		}
		if(a[i++]==c[temp])
		{	temp++;}
	}
		if(temp==lenc)
		{
			temp1=1;
			return;
		}
//	printf("%d\n",temp1);
}
void backward()
{
	int i=lena-1,temp=lenb-1,flag=0;
//	printf("%d\n",temp);
	for(;i>=0;i--)
	{
		if(temp==-1)
		{
			flag=1;
			break;
		}
//		printf("%c %c\n",a[i],b[temp]);
		if(a[i]==b[temp])
			temp--;
	}
//	printf("%d\n",temp);
	if(flag==0)
		return;
	temp=lenc-1;
	while(i>=0)
	{
		if(temp==-1)
		{
			temp2=1;
			return;
		}
//		printf("%c %c\n",a[i],c[temp]);
		if(a[i]==c[temp])
			temp--;
		i--;
	}
		if(temp==-1)
		{
			temp2=1;
			return;
		}
//	printf("%d\n",temp);
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}