python top-down
\nimport functools\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        \n        def distance(a, b):\n            return abs(int(a/6) - 
```\nimport functools\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        \n        def distance(a, b):\n            return abs(int(a/6) - int(b/6)) + abs(a%6 - b%6)\n        \n        @lru_cache(None)\n        def f(l, r, k):\n            \n            if k >= len(word):\n                return 0\n            \n            o = ord(word[k]) - 65\n            left = f(o, r, k+1) + (0 if l == -1 else distance(o, l))\n            right = f(l, o, k+1) + (0 if r == -1 else distance(o, r))\n            \n            return min(left, right)\n        \n        return f(-1, -1, 0)\n```
[]