[Python] Top down dp.
\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        def dist(n1, n2): \n            if n1 is None:    \n                return 0    \n 
```\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        def dist(n1, n2): \n            if n1 is None:    \n                return 0    \n            x1, y1 = divmod(ord(n1) - ord(\'A\'), 6)\n            x2, y2 = divmod(ord(n2) - ord(\'A\'), 6) \n            return abs(x1 - x2) + abs(y1 - y2)\n        \n        @lru_cache(None) \n        def two_fingers(i, l, r):\n            if i == len(word):    \n                return 0\n            n1 = dist(l, word[i]) + two_fingers(i + 1, word[i], r) \n            n2 = dist(r, word[i]) + two_fingers(i + 1, l, word[i]) \n            return min(n1, n2)\n        \n        return two_fingers(0, None, None)\n```
[]