/*
 * File: JingleBalls.cpp # After Contest
 * Author: Magica
 * Date: 2014.09.01
 ****************************************/
#include <bits/stdc++.h>
using namespace std;
#define Multicase for (int TestCase = 1, TestCaseSum = xint(); TestCase <= TestCaseSum; ++TestCase)
#define __cin__ { cin.sync_with_stdio(0); cin.tie(0); }
#define inject(x) { cerr << "Function: " << __FUNCTION__ << ", Line: " << __LINE__ << ", " << #x << ": " << (x) << endl; }
#define mp make_pair
#define pb push_back
#define init(range, val) memset(range, val, sizeof(range))
typedef pair<int, int> pii;
typedef long long ll;
char buf;
inline char xchar() { while (buf = getchar(), isspace(buf)); return buf; }
inline int xint() { while (buf = getchar(), buf < '0' || buf > '9'); int x = buf - '0'; for (; buf = getchar(), buf >= '0' && buf <= '9'; x = x * 10 + buf - '0'); return x; }
inline ll xll() { while (buf = getchar(), buf < '0' || buf > '9'); ll x = buf - '0'; for (; buf = getchar(), buf >= '0' && buf <= '9'; x = x * 10 + buf - '0'); return x; }
inline string xstring() { while (buf = getchar(), buf == ' ' || buf == '\n'); string x = ""; for (x += buf; buf = getchar(), buf != ' ' && buf != '\n' && buf != '\r'; x += buf); return x; }
inline string xline() { while (buf = getchar(), buf == ' ' || buf == '\n'); string x = ""; for (x += buf; buf = getchar(), buf != '\n' && buf != '\r'; x += buf); return x; }
#define ROOT 1
#define Mint 1000000000
int f[2005][2005], l[2005], r[2005], sum[2005];
bool leaf[2005], ball[2005];
char s[5005];
int tail, tot;
void ParseTree(int Root)
{
    assert(s[tail] == '(');
    ++tail;
    if (s[tail] == 'B') {
        ball[Root] = true;
        sum[Root] = 1;
        ++tail;
        assert(s[tail] == ')');
    }
    if (s[tail] == ')') {
        leaf[Root] = true;
        ++tail;
        return;
    }
    ParseTree(l[Root] = ++tot);
    ParseTree(r[Root] = ++tot);
    assert(s[tail] == ')');
    ++tail;
    sum[Root] = sum[l[Root]] + sum[r[Root]];
}
int Eval(int s, int cur)
{
    if (f[s][cur] != -1)
        return f[s][cur];
    f[s][cur] = Mint + 5;
    if (!l[s]) {
        f[s][0] = ball[s];
        f[s][1] = 0;
        return f[s][cur];
    }
    int a = cur >> 1;
    int b = cur - a;
    f[s][cur] = min(f[s][cur], Eval(l[s], a) + Eval(r[s], b));
    f[s][cur] = min(f[s][cur], Eval(l[s], b) + Eval(r[s], a));
    return f[s][cur];
}
int main()
{
    int res;
    while (scanf("%s", s + 1) != EOF) {
        tail = tot = 1;
        init(f, -1);
        init(l, 0);
        init(r, 0);
        init(sum, 0);
        init(leaf, 0);
        init(ball, 0);
        ParseTree(ROOT);
        res = Eval(ROOT, sum[ROOT]);
        if (res > Mint)
            puts("impossible");
        else
            printf("%d\n", res);
    }
    return 0;
}
