In the famous game "Fall Guys:Ultimate Knockout", there's a level called "Perfect Match". We have simplified the rule of the game as follows: You stand on an
n×n grid and there are
m kinds of fruits. Also, each grid is labeled with some kind of fruit.
It is guaranteed that each kind of fruit appears on at least 1 and at most 20 grids. You can choose to stand on any grid initially, then a fruit will appear on the big screen in the game, you need to run to a grid such that the fruit labeled on it is the same as the fruit on the screen, within the countdown. In the game, you can only move horizontally or vertically, i.e. if you stand on
(x1,y1) and move to
(x2,y2), the distance you move is
∣x1−x2∣+∣y1−y2∣.
You want to select a grid to stand on initially, to minimize the distance you need to move in the worst case of all
m possible fruits to appear on the big screen, so that hopefully you will not be knocked out even in the worst case! Calculate this minimized distance.