Similar Items: A first-order logic characterization of safety and co-safety languages