Sort Array by Moving Items to an Empty Cache Slot

A cache has n slots labeled 0 through n - 1. Slot 0 is the empty slot, and every other slot contains exactly one item whose label is also between 0 and n - 1. The array slots describes the current state: slots[i] is the item currently in slot i, and slots[i] == 0 identifies the empty slot.

In one operation, choose any non-empty slot and exchange its item with the empty slot. The empty slot therefore moves to the chosen slot. An item is in its final position when its label equals its slot index.

Return the minimum number of operations needed to transform slots into [0, 1, 2, ..., n - 1].

The input is guaranteed to be a permutation of 0 through n - 1, so there is exactly one empty slot. You may not exchange two occupied slots directly, and an operation must move an item into the empty slot.

Examples
Input: [0]
Output: 0
Hints

Sort Array by Moving Items to an Empty Cache Slot

A cache has `n` slots labeled `0` through `n - 1`. Slot `0` is the empty slot, and every other slot contains exactly one item whose label is also between `0` and `n - 1`. The array `slots` describes the current state: `slots[i]` is the item currently in slot `i`, and `slots[i] == 0` identifies the empty slot.