import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author silviase */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); longestABSubString solver = new longestABSubString(); solver.solve(1, in, out); out.close(); } static class longestABSubString { public void solve(int testNumber, Scanner in, PrintWriter out) { String s = in.next(); int l = s.length(); int[] dif = new int[l * 2 + 2]; int now = l; Arrays.fill(dif, -1); dif[now] = 0; int res = 0; for (int i = 0; i < l; i++) { if (s.charAt(i) == 'A') { now++; } else { now--; } if (dif[now] == -1) { // 更新する dif[now] = i + 1; } else { // 比較する res = Math.max(res, i + 1 - dif[now]); } } out.println(res); } } }