Software-Defined Networks (SDNs) are characterized by dividing a network architecture in a data plane (i.e., any packet-relaying nodes like switches or routers) and a control plane, where specialized controllers assign forwarding decisions to the underlying data plane, and must do so in a very short timeframe. Thus, controllers play a key role in SDNs and the Controller Placement Problem (CPP) becomes a critical issue, affecting network delays and synchronization. If there are significant propagation delays between controllers and nodes, or among controllers, their ability to quickly react to network events is affected, degrading reliability. In this work, we propose a comprehensive mathematical formalization of the CPP, which constrains propagation latency and controller capacity, and determines simultaneously the minimum number of controllers, their location and the assignment of nodes to each, while keeping a balanced load distribution among controllers. As CPP is NP-hard, a heuristic approach is also presented. Simulations for 60 network scenarios show that this approach obtains balanced and resilient solutions, in negligible time, which are proven to be optimal or near optimal for 90% of the evaluated cases.