/**
 * HJ39 判断两个IP是否属于同一子网
 */
public class HJ039 {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String mask = sc.nextLine();
            String ip1 = sc.nextLine();
            String ip2 = sc.nextLine();
            String[] _mask = mask.split("\\.");
            String[] iip1 = ip1.split("\\.");
            String[] iip2 = ip2.split("\\.");
            StringBuilder mask_sb = new StringBuilder();
            StringBuilder ip1_sb = new StringBuilder();
            StringBuilder ip2_sb = new StringBuilder();
            boolean flag = true;  //判断是否有大于255的不规范
            for (int i = 0; i < 4; i++) {
                int num = Integer.parseInt(_mask[i]);
                if (num > 255 || num < 0) {
                    flag = false;
                    break;
                }
                String num2 = Integer.toBinaryString(num);
                while (num2.length() < 8) {
                    num2 = "0" + num2;
                }
                mask_sb.append(num2);

                num = Integer.parseInt(iip1[i]);
                if (num > 255 || num < 0) {
                    flag = false;
                    break;
                }
                num2 = Integer.toBinaryString(num);
                while (num2.length() < 8) {
                    num2 = "0" + num2;
                }
                ip1_sb.append(num2);

                num = Integer.parseInt(iip2[i]);
                if (num > 255 || num < 0) {
                    flag = false;
                    break;
                }
                num2 = Integer.toBinaryString(num);
                while (num2.length() < 8) {
                    num2 = "0" + num2;
                }
                ip2_sb.append(num2);
            }
            if (!flag) {
                System.out.println(1);
                continue;
            }
            mask = mask_sb.toString();
            ip1 = ip1_sb.toString();
            ip2 = ip2_sb.toString();
            int left = 0, right = 31;
            while (mask.charAt(left) == '1') {
                left++;
            }
            while (mask.charAt(right) == '0') {
                right--;
            }
            if (left != right + 1) {  //判断掩码是否规范
                System.out.println(1);
                continue;
            }

            boolean ans = true;
            for (int i = 0; i < 32 && mask.charAt(i) == '1'; i++) {
                if (ip1.charAt(i) != ip2.charAt(i)) {
                    ans = false;
                    break;
                }
            }
            if (ans) {
                System.out.println(0);
            } else {
                System.out.println(2);
            }
        }
        sc.close();
    }
}