EASY SOLUTION O(M*N)
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Pair{
    int i,j;
    Pair(int i, int j) {
        this.i = i;
        this.j = j;
    }
}
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.length, n = mat[0].length;

        List<HashSet<Integer>> rowSets = new ArrayList<>();
        for (int i =0; i < m; i++) rowSets.add(new HashSet<>());

        List<HashSet<Integer>> colSets = new ArrayList<>();
        for (int i = 0; i < n; i++) colSets.add(new HashSet<>());

        Pair[] table = new Pair[m*n+1];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                table[mat[i][j]] = new Pair(i, j);
            }
        }

        for (int i = 0; i < arr.length; i++) {
            int r = table[arr[i]].i;
            int c = table[arr[i]].j;
            HashSet<Integer> set1 = rowSets.get(r);
            set1.add(arr[i]);

            if (set1.size() == n) return i;

            HashSet<Integer> set2 = colSets.get(c);
            set2.add(arr[i]);

            if (set2.size() == m) return i;

            rowSets.set(r, set1);
            colSets.set(c, set2);
        }

        return -1;
    }
}
```
['Java']