package oj.test;
import java.math.BigInteger;
import java.util.*;
public class Demo11 {
/**
* @判断两个IP是否属于同一子网
* 输入参数: String Mask: 子网掩码,格式:“255.255.255.0”;
* String ip1: 计算机1的IP地址,格式:“192.168.0.254”;
* String ip2: 计算机2的IP地址,格式:“192.168.0.1”;
* 返回值: 0:IP1与IP2属于同一子网络; 1:IP地址或子网掩码格式非法; 2:IP1与IP2不属于同一子网络
*
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String mask = sc.nextLine();
String ip1 = sc.nextLine();
String ip2 = sc.nextLine();
if(isOk(mask) && isOk(ip1) && isOk(ip2)){
String str1 = func(mask,ip1);
String str2 = func(mask,ip2);
if(str1.equals(str2))
sop("0");
else
sop("2");
}
else
sop("1");
}
private static String func(String mask, String ip) {
String[] m = mask.split("\\.");
String[] ipp = ip.split("\\.");
mask = "";
ip = "";
for(int i=0;i<4;i++){
BigInteger big1 = new BigInteger(m[i]);
m[i] = big1.toString(2);
BigInteger big2 = new BigInteger(ipp[i]);
ipp[i] = big2.toString(2);
while(m[i].length()!=8){
m[i]="0"+m[i];
}
while(ipp[i].length()!=8){
ipp[i]="0"+ipp[i];
}
mask = mask+m[i];
ip = ip+ipp[i];
}
//sop(mask);
//sop(ip);
char[] ch = new char[32];
for(int i=0;i<32;i++){
ch[i] = (char) (mask.charAt(i)&ip.charAt(i));
}
return new String(ch);
}
private static boolean isOk(String str) {
String[] s = str.split("\\.");
if(s.length!=4)
return false;
for(int i=0;i<4;i++){
if(Integer.parseInt(s[i])<0 || Integer.parseInt(s[i])>255)
return false;
}
return true;
}
public static void sop(Object o){
System.out.println(o);
}
}