import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class Main { public static final long MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); final char[] chs = sc.next().toCharArray(); int[] score = new int[chs.length]; for(int i = 0; i < chs.length; i++){ if(chs[i] == 'A'){ score[i] = 1; }else{ score[i] = -1; } } for(int i = 1; i < score.length; i++){ score[i] += score[i - 1]; } final int min = Math.min(0, Arrays.stream(score).min().getAsInt()); final int max = Math.max(0, Arrays.stream(score).max().getAsInt()); final int size = max - min + 1; int[] prev = new int[size]; Arrays.fill(prev, score.length); prev[0 - min] = -1; int answer = 0; for(int i = 0; i < score.length; i++){ final int s = score[i] - min; if(prev[s] < score.length){ answer = Math.max(answer, i - prev[s]); }else{ prev[s] = i; } } System.out.println(answer); } }