import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); char[] arr = sc.next().toCharArray(); int[] sums = new int[arr.length + 1]; HashMap> map = new HashMap<>(); TreeSet first = new TreeSet<>(); first.add(0); map.put(0, first); for (int i = 1; i <= arr.length; i++) { sums[i] = sums[i - 1]; if (arr[i - 1] == 'A') { sums[i]++; } else { sums[i]--; } if (map.containsKey(sums[i])) { map.get(sums[i]).add(i); } else { TreeSet set = new TreeSet<>(); set.add(i); map.put(sums[i], set); } } int max = 0; for (TreeSet set : map.values()) { max = Math.max(max, set.last() - set.first()); } System.out.println(max); } }