using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bitscreen { class Program { static int modoru(int cur, int goal, ref int step_counter, ref int modoru_counter) { int modoru_num = bitCounter(cur); modoru_num = cur - modoru_num; int ret = 0; if (modoru_num < 0) { modoru_counter++; return -2; } else if (modoru_num == goal) { step_counter++; return 0; } else { step_counter++; modoru_counter++; ret = susumu(modoru_num, goal, ref step_counter, ref modoru_counter); if(ret != 0) { return -2; } return ret; } } static int susumu(int cur, int goal, ref int step_counter, ref int modoru_counter) { int susumu_num = bitCounter(cur); susumu_num += cur; if(susumu_num > goal) { return -1; } else if (susumu_num == goal) { step_counter++; return 0; } else { step_counter++; int ret; if (susumu_num == 0) susumu_num++; ret = susumu(susumu_num, goal, ref step_counter, ref modoru_counter); if (ret == -1) { if(modoru_counter == 1) { return -1; } ret = modoru(susumu_num, goal, ref step_counter, ref modoru_counter); if (ret == -2) { return -3; } } return ret; } } static int bitCounter(int n) { int bitmask = 0x00000001; int data = n; int count = 0; for (int j = 0; j < 32; j++) { if ((data & bitmask) == 1) count++; data = data >> 1; } return count; } static void Main(string[] args) { string s = Console.ReadLine(); int step_counter = 0; int ret = 0; int modoru_counter = 0; ret = susumu(0,int.Parse(s), ref step_counter, ref modoru_counter); if (ret == 0) { Console.WriteLine(step_counter); } else { Console.WriteLine(-1); } } } }