import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int ans = 0; int p = 0; HashMap map = new HashMap(); map.put(0, 0); for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == 'A') p++; if(s.charAt(i) == 'B') p--; if(map.containsKey(p)) { ans = Math.max(ans, i + 1 - map.get(p)); } else { map.put(p, i + 1); } } System.out.println(ans); } }