Program Problem3;

Uses CRT;

Var
   s : string;
   i,x : integer;
   ifbalance,bCont : boolean;
   b1, b2, b3 : integer;
   counter : integer;
   bcont1, bcont2, bcont3 : boolean;
   InFile, OutFile : text;
   CH : Char;
   blah : integer;

   InfoIn, InfoOut : Array [1..5] of String;

Begin {MAIN}

b1 := 0;
b2 := 0;
b3 := 0;
bCont1 := True;
bCont2 := True;
bCont3 := True;
Counter := 0;

assign(outfile, 'out4.txt');
rewrite(outfile);

Assign(InFile, 'DATA4.TXT');

Reset(InFIle);
for Blah := 1 to 5 do
begin
readln(infile,s);

for x  := 1 to length(s) do
    begin
    if bCont1 = True then
       Begin
       if s[x] = '(' then
          b1 := b1 + 1;
       if s[x] = ')' then
          b1 := b1 - 1;
       end;
    if bcont2 = true then
       begin
       if s[x] = '[' then
          b2 := b2 + 1;
       if s[x] = ']' then
          b2 := b2 - 1;
       end;
    if bcont3 = true then
       begin
       if s[x] = '{' then
          b3 := b3 + 1;
       if s[x] = '}' then
          b3 := b3 - 1;
       end;

       If b1 < 0 then
          bcont1 := false;
       if b2 < 0 then
          bcont2 := false;
       if b3 < 0 then
          bcont3 := false;

    End;

If (b1 <> 0) or (b2 <> 0) or (b3 <> 0) then
    ifbalance := false;
If (b1 = 0) and (b2 = 0) and (b3 = 0) then
    ifbalance := true;

If ifbalance = false then
   writeln(outfile, 'not balanced')
else
    writeln(outfile, 'balanced');

{writeln('b1:',b1);
writeln('b2:',b2);
writeln('b3:',b3);
writeln('ifbalance:',ifbalance);
}
end; {blah 1 to 5}

close(outfile);
close(infile);

End. {MAIN}

