Derive Intuition through Paper & Pen
Code
![WhatsApp Image 2025-01-21 at 3.29.08 AM (1).jpeg](https://assets.leetcode.com/users/images/b4d682e9-adea-4b8d-9058-3ed92d6a45a2_1737410673.5139878.jpeg)


# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {

         HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < arr.length; i++) {
            map.put(arr[i], i);
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < mat.length; i++) {

            int max = -1;

            for (int j = 0; j < mat[0].length; j++) {

                int ind = map.get(mat[i][j]);

                max = Math.max(ind, max);
            }
            min = Math.min(max, min);
        }

        for (int i = 0; i < mat[0].length; i++) {

            int max = -1;

            for (int j = 0; j < mat.length; j++) {

                int ind = map.get(mat[j][i]);

                max = Math.max(ind, max);

            }
            min = Math.min(max, min);

        }

        return min;

    }
}
```
['Matrix', 'Java']