import java.util.Objects; import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
        String ipMark = sc.nextLine();
        String ip1 = sc.nextLine();
        String ip2 = sc.nextLine();
        System.out.println(isSameMark(ipMark, ip1, ip2));
    }
}

private static int isSameMark(String ipMark, String ip1, String ip2) {
    if (notIp(ipMark) || notIp(ip1) || notIp(ip2)) {
        return 1;
    }
    StringBuilder ipMarkSb = new StringBuilder();
    if (notIpMark(ipMark, ipMarkSb)) {
        return 1;
    }
    StringBuilder ip1Sb = new StringBuilder();
    getIpSb(ip1, ip1Sb);

    StringBuilder ip2Sb = new StringBuilder();
    getIpSb(ip2, ip2Sb);

    return isSame(ipMarkSb, ip1Sb, ip2Sb) ? 0 : 2;
}

private static boolean isSame(StringBuilder ipMarkSb, StringBuilder ip1Sb,
                              StringBuilder ip2Sb) {
    StringBuilder ip1Result = new StringBuilder();
    for (int i = 0; i < ipMarkSb.length(); i++) {
        if (Character.isDigit(ipMarkSb.charAt(i)) &&
                Character.isDigit(ip1Sb.charAt(i))) {
            ip1Result.append(Integer.parseInt(ipMarkSb.charAt(i) + "") & Integer.parseInt(
                                 ip1Sb.charAt(i) + ""));
        }
    }

    StringBuilder ip2Result = new StringBuilder();
    for (int i = 0; i < ipMarkSb.length(); i++) {
        if (Character.isDigit(ipMarkSb.charAt(i)) &&
                Character.isDigit(ip2Sb.charAt(i))) {
            ip2Result.append(Integer.parseInt(ipMarkSb.charAt(i) + "") & Integer.parseInt(
                                 ip2Sb.charAt(i) + ""));
        }
    }

    return Objects.equals(ip1Result.toString(), ip2Result.toString());
}

private static void getIpSb(String ipStr, StringBuilder ipSb) {
    String[] ips = ipStr.split("\\.");
    for (String s : ips) {
        StringBuilder ipValue = new StringBuilder(Integer.toBinaryString(
                    Integer.parseInt(s)));
        for (int i = ipValue.length(); i < 8; i++) {
            ipValue.insert(0, "0");
        }
        ipSb.append(ipValue).append(".");
    }
}

private static boolean notIpMark(String ipMark, StringBuilder ipSb) {
    String[] ips = ipMark.split("\\.");
    for (String s : ips) {
        StringBuilder ip = new StringBuilder(Integer.toBinaryString(Integer.parseInt(
                s)));
        for (int i = ip.length(); i < 8; i++) {
            ip.insert(0, "0");
        }
        ipSb.append(ip).append(".");
    }
    return ipSb.lastIndexOf("1") > ipSb.indexOf("0");
}

private static boolean notIp(String ip) {
    String[] ips = ip.split("\\.");
    if (ips.length != 4) {
        return true;
    }
    for (String s : ips) {
        if (!s.matches("\\d+") || Integer.parseInt(s) < 0 ||
                Integer.parseInt(s) > 255) {
            return true;
        }
    }
    return false;
}

}