A Bass formula for Gorenstein injective dimension