import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(string s) { int ans = 0; int[int] d = [0: -1]; int x = 0; for (int i = 0; i < s.length; i++) { switch (s[i]) { case 'A': x++; break; case 'B': x--; break; default: break; } if (x in d) { ans = max(ans, i - d[x]); } else { d[x] = i; } } return ans; } void main() { string s = readln.chomp; auto ans = calc(s); writeln(ans); }