import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { string s; readV(s); int[int] h; auto a = 0; foreach (int i, c; s) { a += c == 'A' ? +1 : -1; h[a] = i; } a = 0; auto l = 0; foreach (int i, c; s) { if (a in h && h[a] > i) l = max(l, h[a]-i+1); a += c == 'A' ? +1 : -1; } writeln(l); }