Language: C++

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.IntBinaryOperator;


public class Main {
    public static void main(String[] args) {
        FastScanner fsc = new FastScanner();
        int max = 2000;
        char[] a = fsc.next().toCharArray();
        char[] b = fsc.next().toCharArray();
        char[] c = fsc.next().toCharArray();
        int la = a.length;
        int lb = b.length;
        int lc = c.length;
        boolean[] ab = new boolean[100000];
        boolean[] ac = new boolean[100000];
        boolean[] bc = new boolean[100000];
        int bias = 50000;
        Arrays.fill(ab, true);
        Arrays.fill(ac, true);
        Arrays.fill(bc, true);
        for (int ai = 0; ai < la; ai++) {
            for (int bj = 0; bj < lb; bj++) {
                if (!ok(a[ai], b[bj])) {
                    ab[ai - bj + bias] = false;
                }
            }
        }
        for (int ai = 0; ai < la; ai++) {
            for (int cj = 0; cj < lc; cj++) {
                if (!ok(a[ai], c[cj])) {
                    ac[ai - cj + bias] = false;
                }
            }
        }
        for (int bi = 0; bi < lb; bi++) {
            for (int cj = 0; cj < lc; cj++) {
                if (!ok(b[bi], c[cj])) {
                    bc[bi - cj + bias] = false;
                }
            }
        }
        int ans = 3 * max;
        for (int bs = -2 * max; bs <= 2 * max; bs++) {
            for (int cs = -2 * max; cs <= 2 * max; cs++) {
                if (ab[bs + bias] && ac[cs + bias] && bc[cs - bs + bias]) {
                    int left = IntUtil.min(0, bs, cs);
                    int right = IntUtil.max(la, bs + lb, cs + lc);
                    ans = Math.min(ans, right - left);
                }
            }
        }
        System.out.println(ans);
    }

    public static boolean ok(char x, char y) {
        return x == y || x == '?' || y == '?';
    }
}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte()) {
            return buffer[ptr++];
        } else {
            return -1;
        }
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
            ptr++;
        }
        return hasNextByte();
    }

    public String next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
            throw new NumberFormatException();
        }
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


class IntUtil {

    private IntUtil(){}
    
    public static int min(int... a) {
        int ret = Const.IINF;
        for (int c : a) ret = Math.min(ret, c);
        return ret;
    }

    public static int max(int... a) {
        int ret = -Const.IINF;
        for (int c : a) ret = Math.max(ret, c);
        return ret;
    }

    /**
     * Caluculate the ceil of a/b. Returns the smallest integer greater than or
     * equal to a/b while 'a/b' rounds fractional parts to ZERO.
     * 
     * @param a
     * @param b
     * @return the smallest integer greater than or equal to a/b
     */
    public static int cld(int a, int b) {
        if (a > 0 && b > 0) return (a + b - 1) / b;
        else if (a < 0 && b < 0) return (a + b + 1) / b;
        else return a / b;
    }

    /**
     * Caluculate the floor of a/b. Returns the largest integer less than or equal
     * to a/b while 'a/b' rounds fractional parts to ZERO.
     * 
     * @param a
     * @param b
     * @return the largest integer less than or equal to a/b
     */
    public static int fld(int a, int b) {
        if (a <= 0 && b > 0) return (a - b + 1) / b;
        else if (a > 0 && b <= 0) return (a - b - 1) / b;
        else return a / b;
    }

    public static int fold(IntBinaryOperator func, int e, int... a) {
        int ret = e;
        for (int c : a) ret = func.applyAsInt(ret, c);
        return ret;
    }
}

class Const {
    public static final long MOD7 = 1_000_000_007;
    public static final long MOD9 = 1_000_000_009;
    public static final long MOD99 = 998_244_353;

    public static final long LINF = 1_000_000_000_000_000_000l;
    public static final int IINF = 1_000_000_000;
    public static final double DINF = 1e150;

    public static final double DELTA = 0.000_000_000_1;
    public static final double LDELTA = 0.000_001;

    public static final String YES = "YES";
    public static final String NO = "NO";
    public static final String Yes = "Yes";
    public static final String No = "No";
    public static final String POSSIBLE = "POSSIBLE";
    public static final String IMPOSSIBLE = "IMPOSSIBLE";
    public static final String Possible = "Possible";
    public static final String Impossible = "Impossible";

    public static final int[] dx8 = {1, 0, -1, 0, 1, -1, -1, 1};
    public static final int[] dy8 = {0, 1, 0, -1, 1, 1, -1, -1};
    public static final int[] dx = {1, 0, -1, 0};
    public static final int[] dy = {0, 1, 0, -1};

    public static long MOD = MOD7;

    public static void setMod(long mod) {
        MOD = mod;
    }

    private Const(){}
}


Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.io.IOException;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.io.InputStream;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.util.Arrays;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:4:1: error: 'import' does not name a type
    4 | import java.util.NoSuchElementException;
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:1: error: 'import' does not name a type
    5 | import java.util.function.IntBinaryOperator;
      | ^~~~~~
a.cc:5:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:8:1: error: expected unqualified-id before 'public'
    8 | public class Main {
      | ^~~~~~
a.cc:66:12: error: expected ':' before 'final'
   66 |     private final InputStream in = System.in;
      |            ^~~~~~
      |            :
a.cc:66:13: error: 'final' does not name a type
   66 |     private final InputStream in = System.in;
      |             ^~~~~
a.cc:67:12: error: expected ':' before 'final'
   67 |     private final byte[] buffer = new byte[1024];
      |            ^~~~~~
      |            :
a.cc:67:13: error: 'final' does not name a type
   67 |     private final byte[] buffer = new byte[1024];
      |             ^~~~~
a.cc:68:12: error: expected ':' before 'int'
   68 |     private int ptr = 0;
      |            ^~~~
      |            :
a.cc:69:12: error: expected ':' before 'int'
   69 |     private int buflen = 0;
      |            ^~~~
      |            :
a.cc:71:12: error: expected ':' before 'boolean'
   71 |     private boolean hasNextByte() {
      |            ^~~~~~~~
      |            :
a.cc:71:13: error: 'boolean' does not name a type; did you mean 'bool'?
   71 |     private boolean hasNextByte() {
      |             ^~~~~~~
      |             bool
a.cc:88:12: error: expected ':' before 'int'
   88 |     private int readByte() {
      |            ^~~~
      |            :
a.cc:96:12: error: expected ':' before 'static'
   96 |     private static boolean isPrintableChar(int c) {
      |            ^~~~~~~
      |            :
a.cc:96:20: error: 'boolean' does not name a type; did you mean 'bool'?
   96 |     private static boolean isPrintableChar(int c) {
      |                    ^~~~~~~
      |                    bool
a.cc:100:11: error: expected ':' before 'boolean'
  100 |     public boolean hasNext() {
      |           ^~~~~~~~
      |           :
a.cc:100:12: error: 'boolean' does not name a type; did you mean 'bool'?
  100 |     public boolean hasNext() {
      |            ^~~~~~~
      |            bool
a.cc:107:11: error: expected ':' before 'String'
  107 |     public String next() {
      |           ^~~~~~~
      |           :
a.cc:107:12: error: 'String' does not name a type
  107 |     public String next() {
      |            ^~~~~~
a.cc:120:11: error: expected ':' before 'long'
  120 |     public long nextLong() {
      |           ^~~~~
      |           :
a.cc:147:11: error: expected ':' before 'int'
  147 |     public int nextInt() {
      |           ^~~~
      |           :
a.cc:155:11: error: expected ':' before 'double'
  155 |     public double nextDouble() {
      |           ^~~~~~~
      |           :
a.cc:158:2: error: expected ';' after class definition
  158 | }
      |  ^
      |  ;
a.cc: In member function 'int FastScanner::readByte()':
a.cc:89:13: error: 'hasNextByte' was not declared in this scope
   89 |         if (hasNextByte()) {
      |             ^~~~~~~~~~~
a.cc:90:20: error: 'buffer' was not declared in this scope; did you mean 'buflen'?
   90 |             return buffer[ptr++];
      |                    ^~~~~~
      |                    buflen
a.cc: In member function 'long int FastScanner::nextLong()':
a.cc:121:14: error: 'hasNext' was not declared in this scope
  121 |         if (!hasNext()) {
      |              ^~~~~~~
a.cc:122:23: error: expected type-specifier before 'NoSuchElementException'
  122 |             throw new NoSuchElementException();
      |                       ^~~~~~~~~~~~~~~~~~~~~~
a.cc:125:9: error: 'boolean' was not declared in this scope; did you mean 'bool'?
  125 |         boolean minus = false;
      |         ^~~~~~~
      |         bool
a.cc:128:13: error: 'minus' was not declared in this scope
  128 |             minus = true;
      |             ^~~~~
a.cc:132:23: error: expected type-specifier before 'NumberFormatException'
  132 |             throw new NumberFormatException();
      |                       ^~~~~~~~~~~~~~~~~~~~~
a.cc:138:36: error: 'isPrintableChar' was not declared in this scope
  138 |             } else if (b == -1 || !isPrintableChar(b)) {
      |                                    ^~~~~~~~~~~~~~~
a.cc:139:24: error: 'minus' was not declared in this scope
  139 |                 return minus ? -n : n;
      |                        ^~~~~
a.cc:141:27: error: expected type-specifier before 'NumberFormatException'
  141 |                 throw new NumberFormatException();
      |                           ^~~~~~~~~~~~~~~~~~~~~
a.cc: In member function 'int FastScanner::nextInt()':
a.cc:149:18: error: 'Integer' was not declared in this scope
  149 |         if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
      |                  ^~~~~~~
a.cc:150:23: error: expected type-specifier before 'NumberFormatException'
  150 |             throw new NumberFormatException();
      |                       ^~~~~~~~~~~~~~~~~~~~~
a.cc: In member function 'double FastScanner::nextDouble()':
a.cc:156:16: error: 'Double' was not declared in this scope; did you mean 'double'?
  156 |         return Double.parseDouble(next());
      |                ^~~~~~
      |                double
a.cc:156:35: error: 'next' was not declared in this scope; did you mean 'nextInt'?
  156 |         return Double.parseDouble(next());
      |                                   ^~~~
      |                                   nextInt
a.cc: At global scope:
a.cc:163:12: error: expected ':' before 'IntUtil'
  163 |     private IntUtil(){}
      |            ^~~~~~~~
      |            :
a.cc:165:11: error: expected ':' before 'static'
  165 |     public static int min(int... a) {
      |           ^~~~~~~
      |           :
a.cc:165:34: error: expansion pattern 'int' contains no parameter packs
  165 |     public static int min(int... a) {
      |                                  ^
a.cc:171:11: error: expected ':' before 'static'
  171 |     public static int max(int... a) {
      |           ^~~~~~~
      |           :
a.cc:171:34: error: expansion pattern 'int' contains no parameter packs
  171 |     public static int max(int... a) {
      |                                  ^
a.cc:185:11: error: expected ':' before 'static'
  185 |     public static int cld(int a, int b) {
      |           ^~~~~~~
      |           :
a.cc:199:11: error: expected ':' before 'static'
  199 |     public static int fld(int a, int b) {
      |           ^~~~~~~
      |           :
a.cc:205:11: error: expected ':' before 'static'
  205 |     public static int fold(IntBinaryOperator func, int e, int... a) {
      |           ^~~~~~~
      |           :
a.cc:205:28: error: 'IntBinaryOperator' has not been declared
  205 |     public static int fold(IntBinaryOperator func, int e, int... a) {
      |                            ^~~~~~~~~~~~~~~~~
a.cc:205:66: error: expansion pattern 'int' contains no parameter packs
  205 |     public static int fold(IntBinaryOperator func, int e, int... a) {
      |                                                                  ^
a.cc:210:2: error: expected ';' after class definition
  210 | }
      |  ^
      |  ;
a.cc: In static member function 'static int IntUtil::min(...)':
a.cc:166:19: error: 'Const' was not declared in this scope; did you mean 'const'?
  166 |         int ret = Const.IINF;
      |                   ^~~~~
      |                   const
a.cc:167:22: error: 'a' was not declared in this scope
  167 |         for (int c : a) ret = Math.min(ret, c);
      |                      ^
a.cc:167:31: error: 'Math' was not declared in this scope
  167 |         for (int c : a) ret = Math.min(ret, c);
      |                               ^~~~
a.cc: In static member function 'static int IntUtil::max(...)':
a.cc:172:20: error: 'Const' was not declared in this scope; did you mean 'const'?
  172 |         int ret = -Const.IINF;
      |                    ^~~~~
      |                    const
a.cc:173:22: error: 'a' was not declared in this scope
  173 |         for (int c : a) ret = Math.max(ret, c);
      |                      ^
a.cc:173:31: error: 'Math' was not declared in this scope
  173 |         for (int c : a) ret = Math.max(ret, c);
      |                               ^~~~
a.cc: In static member function 'static int IntUtil::fold(...)':
a.cc:207:22: error: 'a' was not declared in this scope
  207 |         for (int c : a) ret = func.applyAsInt(ret, c);
      |                      ^
a.cc:207:36: error: request for member 'applyAsInt' in 'func', which is of non-class type 'int'
  207 |         for (int c : a) ret = func.applyAsInt(ret, c);
      |                                    ^~~~~~~~~~
a.cc: At global scope:
a.cc:213:11: error: expected ':' before 'static'
  213 |     public static final long MOD7 = 1_000_000_007;
      |           ^~~~~~~
      |           :
a.cc:213:19: error: 'final' does not name a type
  213 |     public static final long MOD7 = 1_000_000_007;
      |                   ^~~~~
a.cc:214:11: error: expected ':' before 'static'
  214 |     public static final long MOD9 = 1_000_000_009;
      |           ^~~~~~~
      |           :
a.cc:214:19: error: 'final' does not name a type
  214 |     public static final long MOD9 = 1_000_000_009;
      |                   ^~~~~
a.cc:215:11: error: expected ':' before 'static'
  215 |     public static final long MOD99 = 998_244_353;
      |           ^~~~~~~
      |           :
a.cc:215:19: error: 'final'