Implemented the extended euclidean algorithm for every case