How CNNs learn to model grid-like data