import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String S = scan.next(); scan.close(); int len = S.length(); Map mapL = new HashMap(); Map mapR = new HashMap(); int k = 0; mapL.put(0, 0); for(int i = 0; i < len; i++) { char c = S.charAt(i); if(c == 'A') { k++; }else { k--; } if(!mapL.containsKey(k)) { mapL.put(k, i + 1); }else { mapR.put(k, i + 1); } } int max = 0; for(int key : mapL.keySet()) { if(mapR.containsKey(key)) { int t = mapR.get(key) - mapL.get(key); max = Math.max(max, t); } } System.out.println(max); } }