import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String mask = sc.nextLine();
            String ip1 = sc.nextLine();
            String ip2 = sc.nextLine();
            String[] maskArr = mask.split("\\.");
            String[] ip1Arr = ip1.split("\\.");
            String[] ip2Arr = ip2.split("\\.");
            if (isErrorAddress(maskArr, ip1Arr, ip2Arr)) {
                System.out.println(1);
            } else if (isSameNet(maskArr, ip1Arr, ip2Arr)) {
                System.out.println(0);
            } else {
                System.out.println(2);
            }
        }
        sc.close();
    }

    private static boolean isSameNet(String[] maskArr, String[] ip1Arr, String[] ip2Arr) {
        String maskBinary = getBinaryString(maskArr);
        String ip1Binary = getBinaryString(ip1Arr);
        String ip2Binary = getBinaryString(ip2Arr);
        StringBuilder ip1AndMask = new StringBuilder();
        StringBuilder ip2AndMask = new StringBuilder();
        for (int i = 0; i < maskBinary.length(); i++) {
            int mask = Integer.parseInt(String.valueOf(maskBinary.charAt(i)));
            int ip1 = Integer.parseInt(String.valueOf(ip1Binary.charAt(i)));
            int ip2 = Integer.parseInt(String.valueOf(ip2Binary.charAt(i)));
            ip1AndMask.append(mask & ip1);
            ip2AndMask.append(mask & ip2);
        }
        if (ip1AndMask.toString().equals(ip2AndMask.toString())) {
            return true;
        }
        return false;
    }

    private static boolean isErrorAddress(String[] mask, String[] ip1, String[] ip2) {
        if (isError(mask) || isError(ip1) || isError(ip2)) {
            return true;
        }
        // 判断掩码是否非法
        String binarystr = getBinaryString(mask);
        if (binarystr.split("0").length > 1 || binarystr.lastIndexOf("1") > binarystr.lastIndexOf("0")) {
            return true;
        }
        return false;
    }

    private static String getBinaryString(String[] mask) {
        StringBuilder sb = new StringBuilder();
        for (String m : mask) {
            int number = Integer.parseInt(m);
            String binary = Integer.toBinaryString(number);
            if (binary.length() < 8) {
                for (int i = 0; i < 8 - binary.length(); i++) {
                    sb.append(0);
                }
            }
            sb.append(binary);
        }
        return sb.toString();
    }

    private static boolean isError(String[] address) {
        if (address.length != 4) {
            return true;
        }
        for (String m : address) {
            for (char c : m.toCharArray()) {
                if (!Character.isDigit(c)) {
                    return true;
                }
            }
            int number = Integer.parseInt(m);
            if (number < 0 || number > 255) {
                return true;
            }
        }
        return false;
    }
}