Pass 1: count maximum possible number of scopes
Pass 2: count non-duplicated scopes, and guess for templates