uses crt;
var fin,fout:text;
    p1,i,p2,p3,p4,p5,p6,z,acheck,bcheck,ccheck:integer;
    str:string;
    check,scheck:boolean;
begin
clrscr;
  assign(fin,'DATA4.txt');
  reset(fin);
  assign(fout,'OUT4.txt');
  rewrite(fout);
  for z:=1 to 5 do
  begin
    check:=true;
    p1:=0;
    p3:=0;
    p5:=0;
    readln(fin,str);
    scheck:=true;
    acheck:=0;
    bcheck:=0;
    ccheck:=0;
    for i:=1 to length(str) do
    begin
      if str[i]='(' then
      begin
        acheck:=acheck+1;
        p1:=p1+1;
      end;
      if str[i]=')' then
      begin
        if acheck=0 then
          scheck:=false;
        p1:=p1-1;
        acheck:=acheck-1;
      end;
      if str[i]='[' then
      begin
        bcheck:=bcheck+1;
        p3:=p3+1;
      end;
      if str[i]=']' then
       begin
        if bcheck=0 then
          scheck:=false;
        p3:=p3-1;
        bcheck:=bcheck-1;
       end;
      if str[i]='{' then
      begin
        ccheck:=ccheck+1;
        p5:=p5+1;
      end;
      if str[i]='}' then
      begin
        if ccheck=0 then
          scheck:=false;
        p5:=p5-1;
        ccheck:=ccheck-1;
      end;


    end;
    if (p1<>0) or (p3<>0) or (p5<>0) or (scheck=false) then
        check:=false;
    if check=true then
      writeln(fout,'balanced');
    if check=false then
      writeln(fout,'not balanced');
  end;
  close(fin);
  close(fout);
end.

