Under construction

Still being built. Things may change

IPvX

Time limit2sMemory limit1024 MB

Summary
Given taken IPvX addresses and starting addresses a < b, find the fewest addresses to occupy so the two assigned addresses become adjacent, or -1.
Level

Medium7 of 10

Topics
Implementation, Hash map, Greedy
Solved
No attempts yet

Statement

Jumanji is an interactive game, so almost anything can happen in it. This time our heroes took a wrong turn and were captured by Doctor DHCP, the chief cyber-scientist villain of Jumanji. The heroes do not interest the Doctor, so he will let them go if they help with a problem that has resisted him for several days.

Doctor DHCP has nn computers in his lab, and the ii-th one has IPvX address cic_i. An IPvX address is a regular IPv4 address with one change: it has xx groups instead of 4. So each address has the form a1.a2.a3.….axa_1.a_2.a_3. \ldots .a_x, where every aia_i is between 0 and 255 inclusive. Addresses are ordered as follows: a1.a2.….axa_1.a_2. \ldots .a_x is less than b1.b2.….bxb_1.b_2. \ldots .b_x if for some 0≤k<x0 \le k < x, a1=b1a_1 = b_1, …\ldots, ak=bka_k = b_k and ak+1<bk+1a_{k+1} < b_{k+1}.

In this problem, the next address is the smallest address greater than the current one, and the previous address is the largest address less than the current one. For example, the next address after 0.123.123.1230.123.123.123 is 0.123.123.1240.123.123.124, after 0.123.123.2550.123.123.255 it is 0.123.124.00.123.124.0, and after 0.255.255.2550.255.255.255 it is 1.0.0.01.0.0.0.

Doctor DHCP wants to add two new computers to his local network. The network assigns IPvX addresses as follows. The first computer first receives address aa. While this address is taken, it switches to the next address, if one exists. After the first computer gets its address, the second computer receives address bb and follows a similar procedure: if its current address is taken, it tries the previous address, if one exists, and so on.

The goal is that after both computers receive their addresses, the address of the second computer is the next address after the address of the first computer. This is not always possible, so the Doctor has added fictitious occupation: he can send a request to the local network to occupy any address. That address then counts as taken, even though no computer owns it. The Doctor dislikes this operation and wants to use it as few times as possible.

Given aa and bb, find the minimum number of fictitious occupations needed so that, after both computers receive addresses, the two addresses are adjacent. If it is impossible, print −1-1.

Input

The first line has nn and xx (0≤n≤1050 \le n \le 10^5, 1≤x≤81 \le x \le 8). Each of the next nn lines gives the address cic_i of one computer in the form p1.p2.p3.….pxp_1.p_2.p_3. \ldots .p_x (0≤pj≤2550 \le p_j \le 255). Lines n+2n+2 and n+3n+3 contain the initial addresses aa and bb of the first and second computers, in the same format. All nn computer addresses are pairwise distinct, and aa is less than bb.

Output

Print the minimum number of fictitious occupations, or −1-1 if it cannot be done.

Hint

In the first test, occupying address 123.123.123.124 is enough. Then the computers take addresses 123.123.123.121 and 123.123.123.122. The test cases in the statement do not belong to the first test group, but they must pass to be accepted.

Examples2

  1. Example 1

    Input
    2 4
    123.123.123.123
    123.123.123.125
    123.123.123.121
    123.123.123.125
    
    Expected output
    1
    
  2. Example 2

    Input
    2 4
    123.123.123.123
    123.123.123.125
    123.123.123.122
    123.123.123.125
    
    Expected output
    -1