------
## 最终结论
```python
def minCost(houses, cost, m, n, target):
    import sys
    from functools import lru_cache

    @lru_cache(None)
    def dfs(i, k, last_color):
        if i == m:
            return 0 if k == target else sys.maxsize
        
        if k > target:
            return sys.maxsize
        
        if houses[i] != 0:
            return dfs(i + 1, k + (houses[i] != last_color), houses[i])
        
        min_cost = sys.maxsize
        for color in range(1, n + 1):
            current_cost = cost[i][color - 1]
            new_k = k + (color != last_color)
            min_cost = min(min_cost, current_cost + dfs(i + 1, new_k, color))
        
        return min_cost

    result = dfs(0, 0, -1)
    return -1 if result == sys.maxsize else result

# Example usage:
print(minCost([0,0,0,0,0], [[1,10],[10,1],[10,1],[1,10],[5,1]], 5, 2, 3)) # Output: 9
print(minCost([0,2,1,2,0], [[1,10],[10,1],[10,1],[1,10],[5,1]], 5, 2, 3)) # Output: 11
print(minCost([0,0,0,0,0], [[1,10],[10,1],[1,10],[10,1],[1,10]], 5, 2, 5)) # Output: 5
print(minCost([3,1,2,3], [[1 ,1 ,1],[1 ,1 ,1],[1 ,1 ,1],[1 ,1 ,1]],4 ,3 ,3))   # Output: -1
```