The aim of this study is to evaluate the value of suitable distance due to prevent the impact between two irregular adjacent buildings when earthquake is caused to occur large lateral displacement and damage the elements of buildings. For this purpose, by using a mathematical program based on neural network, the number of stories, the period and height of investigated models, PGD, PGV and PGA of earthquake records are defined and the nonlinear lateral displacements of different structures are determined in order to use in the program. Thus, the results of displacements based on all inputs are listed and the minimum critical distance is approximately estimated based on especial regression. For instance, a 3-4 story model is numerically investigated by Tabas earthquake record, which is suggested to provide required gap size about 70 cm. In fact, each model has to observe a 35 cm gap. A newly developed program based on mathematical equations are applied for determining the lateral displacements of each story. A new mathematical formula is proposed by neural network, which shows the least distance between irregular adjacent buildings. For investigating the accuracy of formula, two different ways are performed and the results of analyses confirm suggested equation. For this challenge, a 2-4 story model is considered and three different critical distances are calculated to be 59, 62 and 75 cm which show the last gap size is able to provide safety gap size, determined by suggested formula.