Peripheral synthesis of medium-ring nitrogen heterocycles by displacement reactions