In the 22nd Century, more than one hundred million people attend the Mahjong Sports all over the world. Professional Mahjong players are getting more and more attentions. In many high schools, there’ll be a large-scale Mahjong match every year to select talented students to attend the National Olympiad in Mahjong. For high scores to prove themselves and eager to be a professional Mahjong player someday, students compete with each other for the honor of the Mahjong.
Saki, a girl whose mother and elder sister are professional Mahjong players, will attend the National Olympiad in Mahjong in province (NOMP) this year. To beat other high school, achieve the first prize and attend the National Olympiad in Mahjong (NOM), she and her friends decide to make a camp to train their Mahjong skill.
A very important skill of Mahjong match is to control power of the game. A good Mahjong player could control the game, even all tiles of the game, just like Amae Koromo, whose favorite skill is "catching the moon from the bottom of the sea". This time, Saki and her friends are training their control skill of the Mahjong tiles via the following way.
Saki got a lot of colored Mahjong tiles, and put them into an n * n matrix. Assume there are no more than two Mahjong tiles with the same color. Saki wants to use the Psychic force to control the tiles to the specified state. She could do two kinds of operation as follows:
A. To choose a row arbitrarily, and then swap two tiles' position in this row. This operation will cost CA magic power.
B. To choose a column arbitrarily, and then swap two tiles' position in this column. This operation will cost CB magic power.
Moreover, if you do the same kind of operation as you just did, the operation will cost no power. For example, Saki could make operation BAAAAA but just cost CB+CA magic power. Saki wants to know how to do the operations to finish the task with the least cost.