Quality factor of superconducting radio-frequency (SRF) cavities is degraded whenever magnetic flux is trapped in the cavity walls during the cooldown. In this contribution we study how the trapped flux sensitivity, defined as the trapped flux surface resistance normalized for the amount of flux trapped, depends on the mean free path. A variety of 1.3 GHz cavities with different surface treatments (EP, 120 C bake and different N-doping) were studied in order to cover the largest range of mean free path nowadays achievable, from few to thousands of nanometers. A bell shaped trend appears for the range of mean free path studied. Over doped cavities falls at the maximum of this curve defining the largest values of sensitivity. In addition, we have also studied the trend of the BCS surface resistance contribution as a function of mean free path, revealing that N-doped cavities follow close to the theoretical minimum of the BCS surface resistance as a function of the mean free path. Adding these results together we unveil that optimal N-doping treatment allows to maximize Q-factor at 2 K and 16 MV/m until the magnetic field fully trapped during the cavity cooldown stays below 10 mG.