Problem: Let's play a new board game ``Life Line''.

The number of the players is greater than 1 and less than 10.

In this game, the board is a regular triangle in which many small regular triangles are arranged (See Figure l). The edges of each small triangle are of the same length.

<image>

Figure 1: The board

The size of the board is expressed by the number of vertices on the bottom edge of the outer triangle. For example, the size of the board in Figure 1 is 4.

At the beginning of the game, each player is assigned his own identification number between 1 and 9, and is given some stones on which his identification number is written.

Each player puts his stone in turn on one of the ``empty'' vertices. An ``empty vertex'' is a vertex that has no stone on it.

When one player puts his stone on one of the vertices during his turn, some stones might be removed from the board. The player gains points which is equal to the number of the removed stones of others, but loses points which is equal to the number of the removed stones of himself. The points of a player for a single turn is the points he gained minus the points he lost in that turn.

The conditions for removing stones are as follows:

* The stones on the board are divided into groups. Each group contains a set of stones whose numbers are the same and placed adjacently. That is, if the same numbered stones are placed adjacently, they belong to the same group.
* If none of the stones in a group is adjacent to at least one ``empty'' vertex, all the stones in that group are removed from the board.

<image>

Figure 2: The groups of stones

Figure 2 shows an example of the groups of stones.

Suppose that the turn of the player `4' comes now. If he puts his stone on the vertex shown in Figure 3a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 3b). The player gains 6 points, because the 6 stones of others are removed from the board (See Figure 3c).

<image>

Figure 3a

|

Figure 3b

|

Figure 3c

---|---|---

As another example, suppose that the turn of the player `2' comes in Figure 2. If the player puts his stone on the vertex shown in Figure 4a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 4b). The player gains 4 points, because the 4 stones of others are removed. But, at the same time, he loses 3 points, because his 3 stones are removed. As the result, the player's points of this turn is 4 - 3 = 1 (See Figure 4c).

<image>

Figure 4a

|

Figure 4b

|

Figure 4c

---|---|---

When each player puts all of his stones on the board, the game is over. The total score of a player is the summation of the points of all of his turns.

Your job is to write a program that tells you the maximum points a player can get (i.e., the points he gains - the points he loses) in his current turn.



Input

The input consists of multiple data. Each data represents the state of the board of the game still in progress.

The format of each data is as follows.


N C

S1,1
S2,1  S2,2
S3,1 S3,2 S3,3
...
SN,1    ...    SN,N


N is the size of the board (3 ≤ N ≤ 10).

C is the identification number of the player whose turn comes now (1 ≤ C ≤ 9) . That is, your program must calculate his points in this turn.

Si,j is the state of the vertex on the board (0 ≤ Si,j ≤ 9) . If the value of Si,j is positive, it means that there is the stone numbered by Si,j there. If the value of Si,j is 0, it means that the vertex is ``empty''.

Two zeros in a line, i.e., 0 0, represents the end of the input.

Output

For each data, the maximum points the player can get in the turn should be output, each in a separate line.

Examples

Input

4 4
   2
  2 3
 1 0 4
1 1 4 0
4 5
   2
  2 3
 3 0 4
1 1 4 0
4 1
   2
  2 3
 3 0 4
1 1 4 0
4 1
   1
  1 1
 1 1 1
1 1 1 0
4 2
   1
  1 1
 1 1 1
1 1 1 0
4 1
   0
  2 2
 5 0 7
0 5 7 0
4 2
   0
  0 3
 1 0 4
0 1 0 4
4 3
   0
  3 3
 3 2 3
0 3 0 3
4 2
   0
  3 3
 3 2 3
0 3 0 3
6 1
     1
    1 2
   1 1 0
  6 7 6 8
 0 7 6 8 2
6 6 7 2 2 0
5 9
    0
   0 0
  0 0 0
 0 0 0 0
0 0 0 0 0
5 3
    3
   3 2
  4 3 2
 4 4 0 3
3 3 3 0 3
0 0


Output

6
5
1
-10
8
-1
0
1
-1
5
0
5


Input

4 4
2
2 3
1 0 4
1 1 4 0
4 5
2
2 3
3 0 4
1 1 4 0
4 1
2
2 3
3 0 4
1 1 4 0
4 1
1
1 1
1 1 1
1 1 1 0
4 2
1
1 1
1 1 1
1 1 1 0
4 1
0
2 2
5 0 7
0 5 7 0
4 2
0
0 3
1 0 4
0 1 0 4
4 3
0
3 3
3 2 3
0 3 0 3
4 2
0
3 3
3 2 3
0 3 0 3
6 1
1
1 2
1 1 0
6 7 6 8
0 7 6 8 2
6 6 7 2 2 0
5 9
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
5 3
3
3 2
4 3 2
4 4 0 3
3 3 3 0 3
0 0


Output

6
5
1
-10
8
-1
0
1
-1
5
0
5

Solution (in CPP):

Solution:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};
 
static const double EPS = 1e-8;

  //              0
  //            1   2
  //           3  4  5 
  //          6  7  8  9 
  //        10 11 12 13 14
  //       15 16 17 18 19 20
  //      21 22 23 24 25 26 27
  //    28 29 30 31 32 33 34 35
  //   36 37 38 39 40 41 42 43 44
  //  45 46 47 48 49 50 51 52 53 54
const string edges_str[] = {"1 2", // 0
			    "0 2 4 3","0 1 4 5", //1 2
			    "1 4 6 7","1 2 3 5 7 8","2 4 8 9", //3 4 5
			    "3 7 10 11","3 4 6 8 11 12","4 5 7 9 12 13","5 8 13 14", //6 7 8 9
			    "6 11 15 16","6 7 10 12 16 17","7 8 11 13 17 18"," 8 9 12 14 18 19","9 13 19 20",//10 11 12 13 14
			    "10 16 21 22","10 11 15 17 22 23","11 12 16 18 23 24","12 13 17 19 24 25","13 14 18 20 25 26","14 19 26 27",//15 16 17 18 19 20
			    "15 22 28 29","15 16 21 23 29 30","16 17 22 24 30 31","17 18 23 25 31 32","18 19 24 26 32 33","19 29 25 27 33 34","20 26 34 35",//21 22 23 24 25 26 27
			    "21 29 36 37","21 22 28 30 37 38","22 23 29 31 38 39","23 24 30 32 39 40","24 25 31 33 40 41","25 26 32 34 41 42","26 27 33 35 42 43","27 34 43 44",//28 29 30 31 32 33 34 35
			    "28 37 45 46","28 29 36 38 46 47","29 30 37 39 47 48","30 31 38 40 48 49","31 32 39 41 49 50","32 33 40 42 50 51","33 34 41 43 51 52","34 35 42 44 52 53","35 43 53 54",//36 37 38 39 40 41 42 43 44
			    "36 46","36 37 45 47","37 38 46 49","38 39 47 49","39 40 48 50","40 41 49 51","41 42 50 52","42 43 51 53","43 44 52 54","44 53",//45 46 47 48 49 50 51 52 53 54
};
  

class Stage{
 
public:
  int size;
  vector< vector<int> > edges;
  int nodes[55];
  int prev_nodes[55];
  int latest_stone;
  Stage(int _s) : size(_s){
    for(int i=0;i<(1+size)*size/2;i++){
      vector<int> tmp = split(" ",edges_str[i]);
      edges.push_back(tmp);
    }
    memset(nodes,0,sizeof(nodes));
    memset(prev_nodes,0,sizeof(prev_nodes));

    latest_stone = -1;
  }

  bool set_stone(int node_idx,int num){
    if(nodes[node_idx] != 0) return false;

    latest_stone = num;
    nodes[node_idx] = num;
    return true;
  }

  bool update(){
    bool has_space[55];
    bool visited[55];
    bool hold[55];

    //init
    memcpy(prev_nodes,nodes,sizeof(int)*55);
    memset(has_space,false,sizeof(has_space));
    memset(visited,false,sizeof(visited));
    memset(hold,false,sizeof(hold));

    //marking
    for(int i=0;i<(1+size)*size/2;i++){
      check_dfs(i,nodes[i],visited,has_space);
    }

    bool isok = false;

    //overwrite
    for(int node_idx=0;node_idx<(1+size)*size/2;node_idx++){
      if(has_space[node_idx]){
	hold_dfs(node_idx,nodes[node_idx],hold);
      }
    }

    for(int node_idx=0;node_idx<(1+size)*size/2;node_idx++){
      if(!hold[node_idx]){
	nodes[node_idx] = 0;
	isok = true;
      }
    }

    return isok;
  }
  
  int compute_score(){
        //count score
    int score = 0;
    for(int i=0;i<(1+size)*size/2;i++){
      if(nodes[i] == 0 && prev_nodes[i] != 0){
	if(prev_nodes[i] != latest_stone){
	  score++;
	}
	else if(prev_nodes[i] == latest_stone){
	  score--;
	}
      }
    }
    return score;
  }

  void check_dfs(int node_idx,int num,bool visited[55],bool has_space[55]){
    if(visited[node_idx]) return;
    
    visited[node_idx] = true;

    for(int i=0;i<edges[node_idx].size();i++){
      int next_node = edges[node_idx][i];
      if(next_node >= (1+size)*size/2) continue;
      if(nodes[next_node] == 0){
	has_space[node_idx] = true;
	return;
      }
      if(nodes[next_node] != num) continue;
      if(visited[next_node]) continue;
      check_dfs(next_node,num,visited,has_space);
    }
  }

  void hold_dfs(int node_idx,int num,bool hold[55]){
    hold[node_idx] = true;
    for(int i=0;i<edges[node_idx].size();i++){
      int next_node = edges[node_idx][i];
      if(next_node >= (1+size)*size/2) continue;
      if(nodes[next_node] == 0) continue;
      if(nodes[next_node] != num) continue;
      if(hold[next_node]) continue;
      hold_dfs(next_node,num,hold);
    }
  }
    
  vector<int> split(string delim,string str){
    vector<int> res;
    str += delim;
    for(int i=0;i<str.size();i++){
      for(int j=1;i+j<=str.size();j++){
	if(str.substr(i,j).find(delim) != string::npos){
	  int num = atoi(str.substr(i,j-1).c_str());
	  if(str.substr(i,j-1).size() >0 ) res.push_back(num);
	  i+=j-1;
	  break;
	}
      }
    }
    
    return res;
  }

  void dump(){
    int idx = 0;
    for(int i=1;i<=size;i++){
      for(int j=0;j<i;j++){
	printf("%d",nodes[idx]);
	idx++;
      }
      printf("\n");
    }
  }

  bool operator==(const Stage& s) const{
    for(int i=0;i<(1+size)*size/2;i++){
      if(s.nodes[i] != nodes[i]) return false;
    }
    return true;
  }
};

int main(){
  int stage_size,current_turn;
  while(~scanf("%d %d",&stage_size,&current_turn)){
    if(stage_size == 0) break;
    Stage current(stage_size);
    int idx = 0;
    for(int i=1;i<=stage_size;i++){
      for(int j=0;j<i;j++){
	int num;
	scanf("%d",&num);
	current.set_stone(idx,num);
	idx++;
      }
    }

    Stage next(stage_size);
    next = current;
    int res = -INF;
    for(int idx=0;idx<(1+current.size)*current.size/2;idx++){
      if(!next.set_stone(idx,current_turn)) continue;

      // printf("prev\n");
      // next.dump();

      next.update();

      // printf("after update\n");
      // next.dump();

      int score = next.compute_score();
      res = max(res,score);
      next = current;
    }
    printf("%d\n",res);
  }
}