Under construction

Still being built. Things may change

Space Walls

Time limit15sMemory limit2048 MB

Summary
Given a union of axis-aligned unit cubes and robots walking on its exposed faces, find the first time two robots share a face or swap positions.
Level

Medium7 of 10

Topics
Simulation, Implementation, Graph
Solved
No attempts yet

Problem

Place-Y Technology Corp. plans to launch a new space station soon. The company CEO is known for being obsessed with perfection. For example, he insists that all the outer surfaces of the space station are regularly polished and cleaned of what he calls "space debris," mainly so the station looks good in photos. The engineering team tried to convince the CEO that this was not needed, but failed. So instead they developed a new technology that maintains the surfaces while minimizing human work outside the station. Several small robots perform the maintenance by moving over the station surface, much like robotic vacuum cleaners. Before the first flight, Place-Y needs to assess the risk of collisions during robot operation. That is where you come in.

In this problem, the space station is modeled as a collection of axis-aligned unit cubes. The cubes do not need to be connected. Each robot starts at time t=0t = 0 at the center of an exposed face of one of the station's unit cubes. An exposed face is a face that is not shared with another station cube. The robot faces one of the four directions parallel to an edge of that face. Every time unit, the robot moves straight ahead to another cube face. It may pivot 90 degrees across a station edge so that it always stays in contact with the station. If two cubes share an edge, the robot cannot pass between them, because there is no gap.

Figure K.1: Illustration of Sample Input 1. The dots mark the starting points of two robots.

Given the station layout and the starting positions of all robots, find the time of the earliest collision, if any. A collision happens at a time unit when two or more robots are inside the same cube face, or when two robots try to swap locations. See Sample Input 3 for the swap case.

Input

The first line contains two integers nn and kk. Here nn (1≤n≤1001 \le n \le 100) is the number of regions that describe the station shape, and kk (0≤k≤1000 \le k \le 100) is the number of robots.

Each of the next nn lines contains six integers x1x_1, y1y_1, z1z_1, x2x_2, y2y_2, and z2z_2 with 0≤x1<x2≤1060 \le x_1 < x_2 \le 10^6, 0≤y1<y2≤1060 \le y_1 < y_2 \le 10^6, and 0≤z1<z2≤1060 \le z_1 < z_2 \le 10^6. Each line describes one region. Every point (x,y,z)(x, y, z) with x1≤x≤x2x_1 \le x \le x_2, y1≤y≤y2y_1 \le y \le y_2, and z1≤z≤z2z_1 \le z \le z_2 belongs to the station. A unit cube may be covered by more than one region.

Each of the next kk lines gives the starting position of one robot. The line contains coordinates xx, yy, zz, and two directions f→\overrightarrow{f} and d→\overrightarrow{d}. The robot starts on the face of the unit cube (x,y,z)(x, y, z)-(x+1,y+1,z+1)(x+1, y+1, z+1) determined by f→\overrightarrow{f}. Its initial direction of movement is d→\overrightarrow{d}. Each direction is one of the strings x+, x-, y+, y-, z+, or z-. For example, x+ means the positive x-axis direction (1,0,0)(1, 0, 0). The axis letters of f→\overrightarrow{f} and d→\overrightarrow{d} are always different. The starting cube belongs to the station, and the given face is exposed.

Output

Print the time of the first collision. If no collision ever happens, print ok.

Examples3

  1. Example 1

    Input
    9 2
    1 1 1 7 7 7
    0 0 0 3 3 3
    5 0 0 8 3 3
    0 5 0 3 8 3
    0 0 5 3 3 8
    5 5 0 8 8 3
    5 0 5 8 3 8
    0 5 5 3 8 8
    5 5 5 8 8 8
    0 1 0 z- x+
    3 5 1 z- y+
    
    Expected output
    44
    
  2. Example 2

    Input
    1 3
    0 0 0 1 1 1
    0 0 0 x+ z+
    0 0 0 y+ x+
    0 0 0 z- y+
    
    Expected output
    ok
    
  3. Example 3

    Input
    1 2
    0 0 0 2 1 1
    0 0 0 y+ x+
    1 0 0 y+ x-
    
    Expected output
    0