program latex_fractions;

type
    ttoken = (_frac, _lbrace, _rbrace, _other, _eof);

const
    eof_char = #26;

var
    token: ttoken;
    c: char;
    s, d: longint;

procedure readc(var c: char);
begin
    if eof then
        c := eof_char
    else
        read(c);
end;

procedure nexttoken;
var
    r: string;
begin
    while (c <= ' ') and (c <> eof_char) do
        readc(c);
    if c = '\' then
    begin
        r := '';
        read(c);
        while c in ['a'..'z', 'A'..'Z'] do
        begin
            r := r + c;
            readc(c);
        end;
        if r = 'frac' then
            token := _frac
        else 
            token := _other;
    end else if c = '{' then begin
        token := _lbrace;
        read(c);
    end else if c = '}' then begin
        token := _rbrace;
        read(c);
    end else if c = eof_char then begin
        token := _eof;
    end else begin
        token := _other;
        read(c);
    end;
end;

function calc: longint;
var
    tmp, top, btm: longint;
    braces: longint;
begin
    top := s;
    btm := 0;
    braces := 0;
    while true do
    begin
        if token = _frac then
        begin
            nexttoken;
            if token = _lbrace then
            begin
                nexttoken;
                tmp := calc;
                if tmp > top then 
                    top := tmp;
            end;
            nexttoken;
            if token = _lbrace then
            begin
                nexttoken;
                tmp := calc;
                if tmp > btm then 
                    btm := tmp;
            end else begin
                if s > btm then
                    btm := s;
            end;
        end else if token = _rbrace then begin
            dec(braces)
        end else if token = _lbrace then begin
            inc(braces)
        end else if token = _eof then
            break;

        if braces < 0 then
            break;

        nexttoken;
    end;

    if btm > 0 then
        tmp := top + btm + d
    else
        tmp := s;
{    writeln(tmp);}
    calc := tmp;
end;

begin
{    assign(input, 'frac.in');
    reset(input);
    assign(output, 'frac.out');
    rewrite(output);}

    read(s, d);

    read(c);
    read(c);
    read(c);
    nexttoken;
    writeln(calc);

    close(input);
    close(output);
end.
