------
## 最终结论
```python
import heapq

def minCost(grid):
    m, n = len(grid), len(grid[0])
    # Directions: right, left, down, up
    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    
    # Priority queue for Dijkstra's algorithm
    pq = [(0, 0, 0)]  # (cost, x, y)
    # Cost matrix to track minimum cost to reach each cell
    cost_matrix = [[float('inf')] * n for _ in range(m)]
    cost_matrix[0][0] = 0
    
    while pq:
        cost, x, y = heapq.heappop(pq)
        
        # If we reached the bottom-right corner
        if x == m - 1 and y == n - 1:
            return cost
        
        # Explore neighbors based on current direction
        for i in range(4):
            nx, ny = x + directions[i][0], y + directions[i][1]
            if 0 <= nx < m and 0 <= ny < n:
                new_cost = cost + (1 if grid[x][y] != i + 1 else 0)
                if new_cost < cost_matrix[nx][ny]:
                    cost_matrix[nx][ny] = new_cost
                    heapq.heappush(pq, (new_cost, nx, ny))
    
    return -1  # In case there's no valid path
```