Data hiding is a technique to conceal secret data in a cover image. Least significant bit (LSB) substitution method is a simple way to generate a stego-image by hiding secret data in a cover image. To improve the quality of the generated stego-image, Wang et al. used the genetic algorithm to search for a bijective mapping from the set of the distinct secret bits to another set of the distinct secret bits. The secret data are rearranged for more safely hiding in the host image by the bijective mapping. However, the bijective mapping in their method may be local optimal for hiding secret data. This paper proposes a data hiding method based on a two-layer optimal structure. The proposed two-layer structure finds an optimal bijective mapping. Experimental results show that the quality of the generated stego-image is better than previous LSB-based hiding methods.