#define F for
#define R return
#define E if(i/M||j/M)R-1;
#define A(X)isalpha(o[X+x])
enum{O
=40,C
,M
=3999};char*T
,Q
[M
],D
[M
],t
[M
],Z
,v
,*o
=Q
,*d
=D
;m
,n
,s
,c
,w
,x
,y
;K
(i
,j
,k
){!Z
?Z
=t
[O
]=1,t
[C
]=-1:0;E
;if(!o
[i
])R d
[j
]=0;if((c
=t
[o
[i
]]+t
[o
[i
+1]])-2||o
[i
+2]-92){d
[j
++]=o
[i
++];R K
(i
,j
,i
);}F
(i
+=2,y
=w
=0;i
<M
&&o
[i
]&&c
;y
=o
[i
++]-46||y
?y
:i
+1)c
+=t
[o
[i
]],w
=w
||c
-1?w
:i
;E
;if(c
){F
(;d
[j
++]=o
[i
++];)E
;R
0;}F
(c
=y
;c
<w
;++c
)if(o
[c
]==92)F
(n
=0,m
=w
+2;m
<i
;++m
){if(o
[m
]==o
[c
+2]){F
(x
=0;o
[m
+x
]&&A
(m
)&&o
[m
+x
]==o
[c
+2+x
];++x
);if(o
[c
+2+x
]-46||A
(m
))continue;if(v
>90)R
-1;F
(n
=c
+2;n
<w
;++n
)if(o
[n
]==o
[m
]){F
(x
=0;o
[m
+x
]&&A
(m
)&&o
[m
+x
]==o
[n
+x
];++x
);memset(o
+n
,v
,x
*(o
[m
+x
]==46&&!A
(n
)));}++v
;}}F
(c
=y
;c
<w
&&j
<M
;++c
){F
(x
=0;o
[c
+x
]&&o
[c
+x
]==o
[k
+4+x
]&&A
(c
);++x
);if(o
[k
+4+x
]==46&&!A
(c
)){F
(m
=w
+2;m
<i
-1&&j
<M
;)d
[j
++]=o
[m
++];c
+=x
-1;}else d
[j
++]=o
[c
];}E
;Z
=2;R K
(i
,j
,i
);}*L
(char*a
){F
(s
=n
=0;n
<M
?o
[n
]=a
[n
]:0;++n
);if(n
==M
)R
0;F
(v
=65;++s
<M
;){n
=K
(0,0,Z
=0);if(Z
==2&&~n
)T
=d
,d
=o
,o
=T
;else break;}R~n
&&s
<M
?d
:0;}
#define P printf
main()
{char*r[]={ "((\\ abc. (\\ b. (abc (abc (abc b))))) (\\ cc. (\\ dd. (cc (cc dd)))))",
"((\\ fa. (\\ abc. (fa abc))) (\\ yy. (\\ abc. yy)))",
"((\\ x. x) z)",
"((\\ x. x) (\\ y. (\\ z. z)))",
"(\\ x. ((\\ y. y) x))",
"((\\ x. (\\ y. x)) (\\ a. a))",
"(((\\ x. (\\ y. x)) (\\ a. a)) (\\ b. b))",
"((\\ x. (\\ y. y)) (\\ a. a))",
"(((\\ x. (\\ y. y)) (\\ a. a)) (\\ b. b))",
"((\\ x. (x x)) (\\ x. (x x)))",
"(((\\ x. (\\ y. x)) (\\ a. a)) ((\\ x. (x x)) (\\ x. (x x))))",
0}, *p;
int w;
for(w=0;r[w];++w){
p=L(r[w]);
P("o=%s d=%s\n",r[w],p==0?"Error ":p);
}
R 0;
}
I2RlZmluZSBGIGZvcgojZGVmaW5lIFIgcmV0dXJuCiNkZWZpbmUgRSBpZihpL018fGovTSlSLTE7CiNkZWZpbmUgQShYKWlzYWxwaGEob1tYK3hdKQplbnVte089NDAsQyxNPTM5OTl9O2NoYXIqVCxRW01dLERbTV0sdFtNXSxaLHYsKm89USwqZD1EO20sbixzLGMsdyx4LHk7SyhpLGosayl7IVo/Wj10W09dPTEsdFtDXT0tMTowO0U7aWYoIW9baV0pUiBkW2pdPTA7aWYoKGM9dFtvW2ldXSt0W29baSsxXV0pLTJ8fG9baSsyXS05Mil7ZFtqKytdPW9baSsrXTtSIEsoaSxqLGkpO31GKGkrPTIseT13PTA7aTxNJiZvW2ldJiZjO3k9b1tpKytdLTQ2fHx5P3k6aSsxKWMrPXRbb1tpXV0sdz13fHxjLTE/dzppO0U7aWYoYyl7Rig7ZFtqKytdPW9baSsrXTspRTtSIDA7fUYoYz15O2M8dzsrK2MpaWYob1tjXT09OTIpRihuPTAsbT13KzI7bTxpOysrbSl7aWYob1ttXT09b1tjKzJdKXtGKHg9MDtvW20reF0mJkEobSkmJm9bbSt4XT09b1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IChcXCBiLiBiKSkiLAogICAgICAgICAgICAiKChcXCB4LiAoXFwgeS4geSkpIChcXCBhLiBhKSkiLAogICAgICAgICAgICAiKCgoXFwgeC4gKFxcIHkuIHkpKSAoXFwgYS4gYSkpIChcXCBiLiBiKSkiLCAgICAgICAgICAgICAKICAgICAgICAgICAgIigoXFwgeC4gKHggeCkpIChcXCB4LiAoeCB4KSkpIiwKICAgICAgICAgICAgIigoKFxcIHguIChcXCB5LiB4KSkgKFxcIGEuIGEpKSAoKFxcIHguICh4IHgpKSAoXFwgeC4gKHggeCkpKSkiLAogICAgICAgICAgICAwfSwgKnA7CiBpbnQgdzsKIGZvcih3PTA7clt3XTsrK3cpewogIHA9TChyW3ddKTsKICBQKCJvPSVzIGQ9JXNcbiIsclt3XSxwPT0wPyJFcnJvciAiOnApOwogfQogUiAwOwp9