using System; using System.Collections.Generic; using System.Linq; using static System.Console; class Program { static void Main() { var S = ReadLine(); var s = new int[S.Length]; var dic = new Dictionary(); for (int i = 1; i < s.Length; i++) { s[i] = s[i - 1] + (S[i] == 'A' ? 1 : -1); dic[s[i]] = i; } var ans = 0; for (int i = 0; i < s.Length; i++) { if (dic.ContainsKey(s[i])) { ans = Math.Max(ans, dic[s[i]] - i); } } WriteLine(ans); } }