using System; using System.Collections.Generic; using System.Linq; using static System.Console; class Program { static void Main() { var S = ReadLine(); if (!S.Contains('A') || !S.Contains('B')) { WriteLine(0); return; } var s = new int[S.Length + 1]; var dic = new Dictionary(); for (int i = 1; i < s.Length; i++) { s[i] = s[i - 1] + (S[i - 1] == '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); } }