#include<bits/stdc++.h>
#define ll long long
using namespace std;
int count_bit(int value)
{
	int count = 0;
	while(value)
	{
		count++;
		value = value & (value - 1);
	}
	return count;
}
int main()
{
    int n,p;scanf("%d%d",&n,&p);
    if(n<=p)puts("-1");
    else {
        int x;
        if(p>0){
           x = n/p;
        if(count_bit(n-x*p)>x) {puts("-1");return 0;}
        }
        for(int i=1;i<=10000;i++)
        {
          ll tmp = i*p;
          ll res;
           res = n-i*p;
           if(res<i) {
                puts("-1");break;
            }
            if(count_bit(res)<=i){
            printf("%d\n",i);break;
            }

        }
    }

}

 /*                    (
                     (  ) (
                      )    )
         |||||||     (  ( (
        ( O   O )        )
 ____oOO___(_)___OOo____(
(_______________________)
           JOINT           ░█─░█ ░█▀▀▀ ▀█▀ ░█▀▀▀█ ░█▀▀▀ ░█▄─░█ ░█▀▀█ ░█▀▀▀ ░█▀▀█ ░█▀▀█ █ ░█▀▀▀█ 　 ░█▀▀█ ░█▀▀▀█ ░█▀▀▄ ░█▀▀▀
                           ░█▀▀█ ░█▀▀▀ ░█─ ─▀▀▀▄▄ ░█▀▀▀ ░█░█░█ ░█▀▀▄ ░█▀▀▀ ░█▄▄▀ ░█─▄▄ ─ ─▀▀▀▄▄ 　 ░█─── ░█──░█ ░█─░█ ░█▀▀▀
                           ░█─░█ ░█▄▄▄ ▄█▄ ░█▄▄▄█ ░█▄▄▄ ░█──▀█ ░█▄▄█ ░█▄▄▄ ░█─░█ ░█▄▄█ ─ ░█▄▄▄█ 　 ░█▄▄█ ░█▄▄▄█ ░█▄▄▀ ░█▄▄▄*/

#include<bits/stdc++.h>
using namespace std;

 // Ordered Set
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loop1(i,a,b) for(int i=a;i<=b;i++)
#define rloop(i,a,b) for(int i=a;i>b;i--)
#define rloop1(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define ff first
#define ss second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
//#define a^b=a+b-2*(a&b)
/*
//POWER FUNCTION FOR LARGE NO.
ll M=1000000007LL,fact[100010];

ll power(ll a, ll b, ll mod){
	ll x = 1LL, y = a;
	while (b > 0LL){
		if (b%2LL){
			x = (x*y);
			if(x>mod) x%=mod;
		}
		y = (y*y);
		if(y>mod) y%=mod;
		b /= 2LL;
	}
	return x;
}*/

/*
EULER'S FUNCTION FOR TAKING COMBINATION OF LARGE NO.
ll mod_inv(ll n, ll mod){
	return power(n, mod-2LL, mod);
}

ll nCr(ll n, ll k, ll mod){
	return (fact[n]* ((mod_inv(fact[k],mod)*mod_inv(fact[n-k],mod))%mod) )%mod;
}
 fact[0]=1LL;
    for(ll i=1LL;i<=100005LL;i++)
    {
        fact[i]=(fact[i-1]*i)%M;

    }*/

//___________________(⌐⊙_⊙)__________<CHAL GURU HOJA SHURU >__________(⊙_⊙⌐)______________________________________________

signed main()
{
int n,p;
cin>>n>>p;
if(p>=0){
        int ans=-1;
    loop(i,0,32){
       int temp;
       temp=n;
       temp-=i*p;
       int y= __builtin_popcount(temp);
       if(i<=temp&&i>=y){
        ans=i;
        break;
       }

}
 cout<<ans<<endl;
}
else{
      int ans=-1;
    loop(i,0,32){
       int temp;
       temp=n;
              temp+=i*(-1*p);
       int y= __builtin_popcount(temp);
       if(i<=temp&&i>=y){
        ans=i;
        break;
       }

}
  cout<<ans<<endl;
 }
}

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;

#define int ll
#define size(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back

const int MAXN = 2e6 + 1;
const int INF = 2e9 + 1;
const int MOD = (1e9 + 7);

void bye(string s = "") {
	cout << s << '\n';
	exit(0);
}

signed main() {
	#ifdef _ARTUR

		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		//freopen("input.txt", "r", stdin);
		//freopen("output.txt", "w", stdout);
	#endif
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n, p;
	cin >> n >> p;
	int ans = INF;
	for (int i = 1; i < 5e6; i++) {
		if (n - p * i <= 0) continue;
		int lol = __builtin_popcountll(n - p * i);
		if (lol && lol <= i && n - p * i >= i) {
			cout << i;
			bye();
		}
	}
	cout << -1;
	bye();
}

#include <bits/stdc++.h>
#define ll  long long
#define C continue
#define B break
#define R return
#define mod 1000000007
#define F first
#define S second

using namespace std;
ll a[2000100];
ll vis[2001000];
//queue < ll > q[200010];
ll x,n,k,d,ans2,y,m;
/*string s;
*/
ll poww(ll po , ll ba)
{
    if(ba==0) R 1;
    if(ba==1) R po;
    if(ba%2==0) R ((poww(po,ba/2)%mod)*(poww(po,ba/2)%mod)%mod);
    R (((poww(po,ba/2)%mod)*(poww(po,ba/2)%mod)%mod)*(po%mod))%mod;
}
ll sq(ll in)
{
    ll sum=0;
    //if(in==0) R 0;
    for(int i=29;i>=0;i--)
    {
        if(in==0) B;
        if( in/poww(2,i) > 0)
        {
        sum++;
        in-=poww(2,i);
        }
    }
    R sum;
}
ll p,ans=mod;
int main()
{
    //cout<<sq(27)<<endl;
    cin>>n>>p;

    if(p!=0)
    for(ll k=1; n-k*p > 0 ;k++)
    {

        if(sq(n-k*p)<=k && n-k*p>=k ) {ans=k; B;}
    }
    else ans=sq(n);
    if(ans==mod)
    {
        cout<<-1<<endl;
        R 0;
    }
    cout<<ans<<endl;
    return 0;
}

/*

6
5
1 2 3 4 5
3
2 3 1
6
4 6 2 1 5 3
1
1
4
3 4 1 2
5
5 1 2 4 3

*/

//Code by rp_o_02
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;

const int MAX_INT = 2147483647;
const ll MAX_LL = 9223372036854775807;

bool Possible(ll n, int k)
{
	int sum = k;

	vll A(k, 1);

	for (int i = k - 1; i >= 0; --i)
	{
		while (sum + A[i] <= n)
		{
			sum += A[i];
			A[i] *= 2;
		}
	}

	return sum == n;
}

int main()
{
	ll n;
	int p;
	cin >> n >> p;

	int ans = MAX_INT;

	if (n == p)
	{
		cout << -1 << endl;
		return 0;
	}

	for (int i = 1; i < 64; i++)
	{
		if (Possible(n - p * (ll)i, i))
		{
			cout << i << endl;
			return 0;
		}
	}

	cout << -1 << endl;

	return 0;
}

#include <iostream>

using namespace std;

int main() {
	int n, p;
	cin >> n >> p;
	for (int k = 0; k <= 31; ++k) {
		int m = n - k * p, cnt = 0;
		for (int num = m; num > 0; num >>= 1)
			cnt += (num & 1);
		if (cnt <= k && k <= m) {
			cout << k;
			return 0;
		}
	}
	cout << -1;
	return 0;
}

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
#include<set>
#define ll long long
using namespace std;
ll n,p;
int main()
{
    scanf("%lld%lld",&n,&p);
    for(int i=0;i<200000;i++)
    {
        ll k=n-p*i;
        ll u=0;
        while(k)
        {
            u+=k%2;
            k/=2;
        }
        if(u<=i&&i<=n-p*i)
        {
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
/*using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/#define int long long
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define rep(i,l,r) for(int i=l;i<r;i++)
#define bep(i,l,r) for(int i=l;i>=r;i--)
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define el '\n'
#define sz(s) (int)s.size()
#define all(v) v.begin(),v.end()
#define mii map<int,int>
#define mr  make_pair

int mod = 998244353;
int inf = 4e18;
const int N = 5e5+5;
//int powmod(int a,int b) {int res=1;if(a>=mod)a%=mod;for(;b;b>>=1){if(b&1)res=res*a;if(res>=mod)res%=mod;a=a*a;if(a>=mod)a%=mod;}return res;}
int right(int x)
{
    int ct=0;
    int i=0;
    while(x)
    {
        if(x&1){
            i = ct;
            break;
        }
        x>>=1;
        ct++;
    }
    return ct;
}
signed main()
{
    io
    int tt = 1;
    //cin>>tt;
    while(tt--)
    {
        int n,p;
        cin>>n>>p;
        //vector<pii> v(60);
        int ans = -100;
        rep(i,0,60)
        {
            int y = n-(i*p);
            if(y>0)
            {
                int xxx = __builtin_popcount(y);
                if(i>=xxx&&i<=y)
                {
                    ans = i;
                    break;
                }
            }
        }
        if(ans==-100)
            cout<<-1<<el;
        else
            cout<<ans<<el;
       // cout<<right(28);

    }
    return 0;
}

#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
using uint = unsigned int;
using usize = std::size_t;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); }
template<typename T> constexpr T msbp1(const T u) { return log2p1(u); }
template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); }
template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); }
template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; }
template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); }
template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); }
template<typename T> constexpr bool btest(const T mask, const usize ind) { return ((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); }
template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
constexpr unsigned int mod                  = 1000000007;
template<typename T> constexpr T inf_v      = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T>
T read()
{
    T v;
    return std::cin >> v, v;
}
template<typename T>
std::vector<T> read_vec(const std::size_t size)
{
    std::vector<T> v(size);
    for (auto& e : v) { std::cin >> e; }
    return v;
}
template<typename... Types>
auto read_vals() { return std::tuple<std::decay_t<Types>...>{read<Types>()...}; }
#define SHOW(...) static_cast<void>(0)
template<typename T>
std::vector<T> make_v(const std::size_t size, T v) { return std::vector<T>(size, v); }
template<class... Args>
auto make_v(const std::size_t size, Args... args) { return std::vector<decltype(make_v(args...))>(size, make_v(args...)); }
int main()
{
    auto [n, p] = read_vals<ll, ll>();
    for (ll l = 0; l <= 40; l++) {
        const ll m = n - p * l;
        if (m >= l and popcount(m) <= l) { return std::cout << l << std::endl, 0; }
    }
    std::cout << -1 << std::endl;
    return 0;
}

#include<iostream>
using namespace std;
int n, p;
int cnt(int n) {
	int ans = 0;
	while (n != 0)
	{
		ans++;
		n = n&(n - 1);
	}
	return ans;
}

int main() {
	cin >> n >> p;
	if (p == 0) {
		cout << cnt(n);
		return 0;
	}
	//if (n <= p) {
	//	cout << -1;
	//	return 0;
	//}
	int i = 1;
	while (n > p) {
		n -= p;
		if (cnt(n) <= i && i <= n) {
			cout << i;
			return 0;
		}
		i++;
	}
	cout << -1;
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

#define LL long long
const int N = 1e6;
LL n,p,ans = -1;
bool checker(LL u,int k){
    if(!u) return false;
    int lb = 0,rb = 1,i = 0;
    while(u){
        if(u & 1) lb++,rb += i;
        u >>= 1,i++;
    }
    return lb <= k && k <= rb;
}
int main()
{
    scanf("%I64d%I64d",&n,&p);
    for(int k=1;k<=N;k++){
        LL c = n - 1ll * k * p;
        if(c < 0) break;
        if(checker(c,k)){
            ans = 1ll * k;
            break;
        }
    }
    printf("%I64d",ans);
}

#include <cstdio>
#define ll long long
using namespace std;
ll n,p;
int main(){
  scanf("%lld%lld",&n,&p);
  for(int i=1;i<=42;++i){
    ll tmp=n-p*i;
    if(tmp<=0)break;
    int now=0;
    for(int j=tmp;j;j-=j&-j)++now;
    if(now>i)continue;
    if(now<i){
      if(i-now<((tmp>>1)<<1)){
        printf("%d\n",i);
        return 0;
      }
    }
    else if(now==i){
      printf("%d\n",i);
      return 0;
    }
  }
  puts("-1");
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0; i<=n-m*i; i++)
        if(i>=__builtin_popcount(n-m*i))
        {
            printf("%d\n",i);
            return 0;
        }
    printf("-1\n");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i,a,n) for(ll i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define UB upper_bound
#define LB lower_bound
const int NUM = 2e5 + 5;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int num_tests=1;
    //cin>>num_tests;
    while(num_tests-->0){
        ll n,p; cin >> n >> p;
        ll ans = -1;
        for(ll i = 31; i >= 0; i--){
            if(n-i*p>=i and __builtin_popcountll(n - i*p) <= i){
                ans = i;
            }
        }
        cout << ans;
    }
}

#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define mx LLONG_MAX
#define mn LLONG_MIN
#define pii pair<int,int>
#define vi vector<int>
#define vll vector< long long >
#define pb push_back
#define pop pop_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define f(i,a,n) for( i = a ; i < n ; i++)
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen ("input.txt","r",stdin);
        freopen ("output.txt","w",stdout);
    #endif
    fastIO;
    int TESTS = 1;
    //cin>>TESTS;
    while(TESTS--)
    {
        ll i,n,p; cin>>n>>p;

        ll d = n,ans = -1;

        if(p == 0)
        {
            bitset<30> b(d);
            cout<<b.count()<<endl;
            continue;
        }

        while(true && d>0)
        {
            int pd = abs(p);
            if((abs(d-n))%pd == 0 && (abs(d-n))/pd > 0)
            {
                bitset<30> b(d);
                if((b.count() <= (abs(d-n))/pd ) &&( (abs(d-n))/pd <= d))
                {
                    ans = (abs(d-n))/pd;
                    break;
                }
                else
                    d-=p;
            }
            else
                d-=p;

            //cout<<ans<<endl;
        }

        cout<<ans<<endl;

    }
}

#include <bits/stdc++.h>
#include <string>
#include <cstdlib>

using namespace std;

int to_binary_one_num (int x) {
    int ans = 0;
    while (x) {
        ans = ans + x % 2;
        x = x / 2;
    }
    return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, p;
    cin >> n >> p;
    int ans = 2e9, step = 1;
    int bin = to_binary_one_num(n - step * p);
    if (p != 0) {
        while (n - step * p >= (int) pow(2, step - 1)) {
            if (bin == step) {
                if (step < ans) {
                    ans = step;
                }
            } else if (step > bin) {
                if (step < ans) {
                    ans = step;
                }
            }
            // cout << n - step * p << " " << step << " " << bin << endl;
            step = step + 1;
            bin = to_binary_one_num(n - step * p);
        }
    } else {
        ans = to_binary_one_num(n);
    }

    if (ans == 2e9) {
        cout << -1;
    } else {
        cout << ans;
    }

    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define pf push_fornt
#define ft first
#define sc second
using namespace std;
const ll maxn = 100010;
int main() {
    cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false);
    ll n, p;
    cin >> n >> p;
    for (int d = 1; d < 500; d++) {
        ll n1 = n - d * p;
        ll cnt = 0;
        if (n1 < d) {
            continue;
        }
        for (int i = 60; i >= 0; i--) {
            ll k = (1ll << i);
            if (n1 >= k) {
                cnt++;
                n1 -= k;
            }
        }
        if (cnt <= d && n1 == 0 && cnt > 0) {
            cout << d << "\n";
            return 0;
        }
    }
    cout << -1;
}

#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
    int n,p;scanf("%d%d",&n,&p);
    for(int i = 0;i <= 1000000;i++)
    {
        int tmp = n - p * i;
        int cnt = 0;
        while(tmp)
        {
            cnt += tmp % 2;
            tmp /= 2;
        }
        if(cnt <= i && i <= n - p * i)//i == n-p*i的时候，意味着n-p*i全部分成1都刚好分为i份。
        {//cnt <= i的时候，我们可以通过二进制拆解的方法匹配上i。
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define md(a, b)		((a) % b + b) % b
#define mod(a)			md(a, MOD)
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
//using namespace __gnu_pbds;

typedef long long 		ll;
typedef long double ld;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest  order_of_key <
const int tam = 100010;
const int MOD = 1000000007;
const int MOD1 = 988244353;
const double EPS = 1e-9;
const double PI = acos(-1);
bool can(ll n, int con)
{
	int uns = 0;
	ll forms = 0;
	ll cons = 1;
	while(n > 0)
	{
		if(n & 1)
		{
			uns++;
			forms += cons;
		}
		cons *= 2;
		n /= 2;
	}
	//cout<<uns<<' '<<forms<<'\n';
	return con >= uns && con <= forms;

}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	ll n, p;
	cin>>n>>p;
	if(p == 0)
	{
		cout<<__builtin_popcount(n)<<'\n';
		return 0;
	}

	if(p > 0)
	{
		int con = 1;
		n -= p;
		while(n > 0)
		{
			if(can(n, con))
			{
				cout<<con<<'\n';
				return 0;
			}
			con++;
			n -= p;
		}
		cout<<-1<<'\n';
	}
	else
	{
		int con = 1;
		n -= p;
		while(n < 1e17)
		{
			//cout<<"cheche "<<n<<' '<<con<<'\n';
			if(can(n, con))
			{
				cout<<con<<'\n';
				return 0;
			}
			con++;
			n -= p;
		}
		cout<<-1<<'\n';
	}
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	ll n, p;
	cin >> n >> p;
	if (p <= 0) {
		for (ll i=0;; i++) {
			ll x = n - p*i;
			ll pc = __builtin_popcountll(x);
			if (pc <= i && i <= x) {
				cout << i << '\n';
				return 0;
			}
		}
	} else {
		for (ll i=0; i<=1000000; i++) {
			ll x = n - p*i;
			if (x < 0)
				break;
			ll pc = __builtin_popcountll(x);
			if (pc <= i && i <= x) {
				cout << i << '\n';
				return 0;
			}
		}
		cout << "-1\n";
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define elif else if
#define int long long
#define real long double
#define f first
#define s second
#define all(v) v.begin(), v.end()

int n, p;

int check(int d){
	int k = n - p * d;
	if (k <= 0) return -1;
	int c = 0;
	for (int i=62; i>=0; --i){
		if ((1LL<<i) <= k){
			//cout << (1LL<<i) << "\n";
			k -= (1LL<<i);
			++c;
		}
	}
	//cout << c << " " << k << " " << d <<  "\n";
	return c <= d and n - p * d >= d;
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> p;
	int k = 1;
	while (true){
		int res = check(k);
		if (res == -1){
			cout << -1;
			return 0;
		}
		if (res) break;
		++k;
	}
	cout << k;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
using namespace std;
#define Fio                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define f(i, n) for (long long int i = 0; i < n; i++)
#define ll long long int
#define fo(i, a, b) for (long long int i = a; i <= b; i++)
#define w(t)  \
    int t;    \
    cin >> t; \
    while (t--)
#define vi vector<int>
#define vl vector<long long int>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long int>>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define newl cout<<"\n"
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const ll inf = 1e9 + 7;
const ll modc = 998244353;
#define MAX 100002

ll count1bit(ll n){
	ll cnt = 0;
	while(n){
		cnt += n&1;
		n = n/2;
	}
	return cnt;
}

int main(){
	Fio;
	ll n,p;
	cin>>n>>p;
	if(p==0){
		cout<<count1bit(n);
		return 0;
	}
	int flag = 0;
	fo(i,1,31){
		ll num = n-p*i;
		if(num<=0)break;
		//if(totbits(n)!=totbits())continue;
		ll c = count1bit(num);
		if(i>=c && num>=i){
			flag=1;
			cout<<i;
			break;
		}
	}
	if(flag==0)cout<<"-1";
	return 0;
}

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1e6+7, P = 998244353;

#define dd c=getchar()
int read() {int s=0,w=1; char c; while (dd, c>'9' || c<'0') if (c=='-') w=-1; while (c>='0' && c<='9') s=s*10+c-'0', dd; return s*w;}
#undef dd
void write(int x) {if (x<0) putchar('-'), x=-x; if (x>=10) write(x/10); putchar(x%10|'0');}
void wsp(int x) {write(x); putchar(' ');} void wln(int x) {write(x); putchar('\n');}

int n, p;
int b[32];
signed main() {
//	freopen("hh.in", "r", stdin);
	n = read(); p = read();
	for (int i = 1; i; i++) {
		int x = n-i*p, s = 0;
		if (x <= 0) return puts("-1"), 0;
		memset(b, 0, sizeof b);
		for (int i = 0; i <= 31; i++) {
			b[i] = x&1, x>>=1;
			if (b[i] == 1) s++;
		}
		for (int j = 31; j && s < i; j--) {
			while (b[j]) {
				s++; b[j]--; b[j-1]++;
				if (s >= i) break;
			}
		}
		if (s == i) return printf("%d\n", i), 0;
	}
}

#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

#define WHATS(var)cout<<__LINE__<<' '<<#var<<"="<<var<<endl;

template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	}
	return os;
}
using ll =long long ;
vector<vector<int>>chs;
vector<int>ds;
vector<int>answers;
vector<int>ops;
void dfs(int now){
	ds[now]=0;
	for(auto e:chs[now]){
		dfs(e);
		ds[now]=max(ds[now],1+ds[e]);
	}

}
void solve(int now,int next){
	answers.push_back(now);
	vector<pair<int,int>>ps;
	for(auto e:chs[now]){
		ps.push_back(make_pair(ds[e],e));
	}
	sort(ps.begin(),ps.end());
	for(int i=0;i<ps.size();++i){
		int an=-1;
		if(i!=ps.size()-1)an=ps[i+1].second;
		else{
			an=next;
		}
		solve(ps[i].second,an);
	}
	if(next!=-1)ops.push_back(next);

}

int main() {
	ios::sync_with_stdio(false);
	ll N;
	int K;cin>>N>>K;
	int ans=-1;
	for(int i=1;i<50;++i){
		ll need=N-K*i;
		if(need<=0)continue;
		else{
			int xx=__builtin_popcount(need);
			if(need>=i&&xx<=i){
				ans=i;
				break;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include <iostream>

using namespace std;

int BitsDB(long long x)
{
    long long y = 1, db = 0;
    while(y <= x)
    {
        if((x&y) != (long long)0)
            db++;
        y *= (long long)2;
    }
    return db;
}

long long Maximal(long long x)
{
    long long y = 1, ret = 0;
    while(y <= x)
    {
        if((x&y) != (long long)0)
            ret += y;
        y *= 2;
    }
    return ret;
}

int main()
{
    cin.sync_with_stdio(false);
    cin.tie(0);

    long long n, p;
    cin>>n>>p;

    for(int i = 0; i < 35; i++)
    {
        if(n-i*p <= 0)
            break;
        if(BitsDB(n-i*p) <= i && i <= Maximal(n-i*p))
        {
            cout<<i<<"\n";
            return 0;
        }
    }

    cout<<"-1\n";

    return 0;
}

#include <bits/stdc++.h>
#define popcnt __builtin_popcount
using namespace std;
int n, p;
int main(){
    cin>>n>>p;
    for (int i=0;i<=n-p*i;i++)
        if (i>=popcnt(n-p*i)){
            cout<<i<<endl;;
            return 0;
        }
    cout<<"-1"<<endl;;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n, m, p;
int get_num(int x){
    int res = 0;
    while(x){
        if(x & 1) res++;
        x >>= 1;
    } return res;
}
int main()
{
    cin >> n >> p;
    int cnt = 0;
    for(int i = 1; i*p <= n; i++)
    {
        cnt++;
        if(cnt > 1000) break;
        m = n - p*i;
        int num = get_num(m);
        if(num <= i && m >= i)
        {
            cout << i << endl;
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, p;
    cin >> n >> p;
    forn (i, 0, 32) {
        int cur = n - p * i;
        //cout << cur << " ";
        if (cur < i)
            continue;
        if ( __builtin_popcount(cur) <= i) {
            cout << i;
            return 0;
        }
    }
    cout << -1;
    return 0;
}

#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 1
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif

void Main() {
  int n, p;
  cin >> n >> p;
  if (p >= n) cout << -1 << endl;
  else {
    for (int k = 1; ; k++) {
      int x = n - k * p;
      if (x < 0) break;
      int c = 0, maxc = 0;
      for (int i = 0; i < 30; i++) {
        if ((1 << i) & x) c++, maxc += 1 << i;
      }
      if (c <= k && maxc >= k) {
        cout << k << endl;
        return;
      }
    }
    cout << -1 << endl;
  }
}

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
using namespace std;

int main(int argc, char* argv[])
{
    int n,p;
    scanf("%d%d", &n, &p);

    int curr=n;
    int i;
    for(i=1; i<=1000; i++) {
        curr-=p;
        if(curr<0) break;

        int tmp=curr;
        int ans=0;
        while(tmp) {
            ans+=tmp%2;
            tmp/=2;
        }
        if(ans<=i && i<=curr) {
            printf("%d\n", i); return 0;
        }
    }
    printf("-1\n");

    return 0;
}

    #include <bits/stdc++.h>
    using namespace std;
    #define dude ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define endl "\n"

    #define ll long long
    #define pb push_back
    void fun()
    {
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        #endif
    }
   int mod=1000000000+7;
ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}

int32_t main(){
fun();
dude;
 	int n,m,i;
 	cin>>n>>m;
 	i=0;
 	if(n-m<=0)
 	{
 		cout<<-1;
 		return 0;
 	}
 	if( m==0)
 	{
 		cout<<__builtin_popcount(n);
 		return 0;
 	}

 	while(1)
 	{
 		n-=m;
 		if(n<=0)
 		{
 			cout<<-1;
 			return 0;
 		}
 		i++;
 		if((__builtin_popcount(n)==i ) || (__builtin_popcount(n)<=i && n>=pow(2,i)))
 			break;
 	}
 	cout<<i;
 	return 0;
 }

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define dbg(n) cout<<#n<<' '<<n<<endl;
int main() {
	ll n,k;
	cin>>n>>k;
	ll ans;
	int f=0;
	int i=0;
	if(k==0){
		cout<<__builtin_popcountll(n)<<endl;
	}
	else{
		for(;i<=32;i++){
			ans=n-i*k;
			if(ans<0){
				f=1;
				break;
			}
			int count=0,bit=0;
			for(int j=0;j<=32;j++){
				if(ans&(1LL<<j)){
					bit++;
					count+=j;
				}
			}
			if(bit<=i){
				if(count>=i||bit==i)
					break;
			}
		}
		if(!f)
			cout<<i<<endl;
		else
			cout<<-1<<endl;
	}
}

#include <bits/stdc++.h>

#define ll long long

using namespace std;
ll binpow (ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1)
        return binpow (a, n-1) * a;
    else {
        ll b = binpow (a, n/2);
        return b * b;
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, p, v = 0, a = -1;
    cin >> n >> p;
    for (ll i = 1; i <= 1e5; ++i) {
        ll n1 = n - p * i;
        vector<ll>ans;
        while(n1 != 0){
            ans.push_back(n1 % 2);
            n1 /= 2;
        }
        ll cont = 0, p2 = 0;
        for (ll j = 0; j < ans.size(); ++j) {
            if(ans[j] == 1){
                    p2 += 1;
                cont += binpow(2, j);
            }
        }
        if(cont >= i && p2 <= i){
            a = i;
            break;
        }
    }
    cout << a;
    return 0;
}

/*
     AUTHOR : Ankit Kumar
     BRANCH : Information Technology
     INSTITUTE : Birla Institute of Technology
*/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define MP make_pair
#define SET(a,val) memset(a,val,sizeof(a))
#define deci(n)         cout<<fixed<<setprecision(n);
#define show(a,l,r) for(int i=l;i<r;i++) cout<<a[i]<<" ";
#define all(a)    a.begin(),a.end()
#define pi pair<int,int>
const int N=1000004;
const int dx[] = {-1, 0, 1, 0, -1,-1, 1, 1};
const int dy[] = { 0, 1, 0,-1, -1, 1,-1, 1};
char dir[]={'L','D','R','U'};
//int dx[]={2,-2,1,-1,2,-2,1,-1};//for knights
//int dy[]={1,1,2,2,-1,-1,-2,-2};
typedef long long int ll;
const ll mod=(ll)1e9+7;
const ll inf=1e18+100;
int BIT[1000005],n;
int prime[17],spf[17];
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<typename T> T min3(T a,T b,T c) {return min(a,min(b,c));}
template<typename T> T max3(T a,T b,T c) {return max(a,max(b,c));}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
ll mInv(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
return x;
}
ll power(ll x, ll y)
{
    ll res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x)% mod;
    }
    return res;
}
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
long long ExtendedGcd (long long a, long long b, long long *x, long long *y) {
    if (!a) {
        *x = 0;
        *y = 1;
        return b;
    }
    long long nx, ny;
    long long d = ExtendedGcd (b % a, a, &nx, &ny);
    long long k = b / a;
    *y = nx;
    *x = -k * nx + ny;
    return d;
}
void update(int x, int delta)
{
    if(!x)
    {
        BIT[x]+=delta;
        return;
    }
      for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
ll query(int x)
{
     ll sum = BIT[0];
     for(; x > 0; x -= x&-x)
        sum += BIT[x];
     return sum;
}
void Sieve()
{
    for(int i=1;i<=N;i++)
        prime[i]=1;
    prime[0]=0;
    prime[1]=0;
    for (ll p=2; p*p<=N; p++)
    {
        if (prime[p] == 1)
        {
            for (ll i=p*p; i<=N; i += p)
                prime[i] = 0;
        }
    }
    for(int p=3;p<=N;p++)
        prime[p]+=prime[p-1];
}
void sieve()
{
    spf[1] = 1;
    for (int i=2; i<N; i++)
        spf[i] = i;
    for (int i=4; i<N; i+=2)
        spf[i] = 2;
    for (ll i=3; i*i<N; i++)
    {
        if (spf[i] == i)
        {
            for (ll j=i*i; j<N; j+=i)
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
string least_rotation(string s)
{
	s += s;
	vector<int> f(s.size(), -1);
	int k = 0;
	for(int j = 1; j < s.size(); j++)
	{
		char sj = s[j];
		int i = f[j - k - 1];
		while(i != -1 && sj != s[k + i + 1])
		{
			if(sj < s[k + i + 1])
				k = j - i - 1;
			i = f[i];
		}
		if(sj != s[k + i + 1])
		{
			if(sj < s[k])
				k = j;
			f[j - k] = -1;
		}
		else
			f[j - k] = i + 1;
	}
	return s.substr(k, s.size() / 2);
}
struct cmp {
    bool operator() (const pair<int,int>&x, const pair<int,int>&y) const {
        if(x.first!=y.first)
        return x.first<y.first;
        return x.second>y.second;
    }
};
void gInput(int m,vector<int>adj[])
{
    int u,v;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
}
vector<ll>fac;
void factorial()
{
    int N=(int)1e6;
    fac.resize(N);
    fac[0]=1;
    for(ll i=1;i<=N;i++)
    {
        fac[i]=((fac[i-1]%mod)*i)%mod;
    }
}
ll nCr(ll n,ll r)
{
    ll x=fac[n];
    ll y=fac[r];
    ll z=fac[n-r];
    ll pr=((y%mod)*(z%mod))%mod;
    pr=mInv(pr,mod);
    ll ncr=((x%mod)*(pr%mod))%mod;
    return ncr;
}
                         /******************Lets Code*******************/

int main()
{
    fast
    int t=1;
   //cin>>t;
    while(t--)
    {
       ll n,p;
       cin>>n>>p;
       //cout<<n<<" "<<p<<"\n";
       if(n<=p)
       {
          // cout<<"hi";
           cout<<-1<<"\n";
           return 0;
       }
       int ans=-1;
       for(int i=1;i<=1000000;i++)
       {
           ll j=i*p;
           ll num=n-j;
           if(num<0)
           break;
           int k=__builtin_popcountll(num);
           //cout<<k<<" "<<i<<"\n";
           if(k==i or (num>=i and k<i))
           {
               ans=i;break;
           }
       }
       cout<<ans<<"\n";
    }
   	return 0;
}

//minamoto
#include<bits/stdc++.h>
#define R register
#define pc(x) __builtin_popcount(x)
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
int n,p,res;
int main(){
	scanf("%d%d",&n,&p),res=23333;
	fp(i,0,32){
		if(n<=0)break;
		if(pc(n)<=i&&n>=i){res=i;break;}
		n-=p;
	}
	printf("%d\n",res==23333?-1:res);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    //freopen("inc.txt", "r", stdin);
    int n, p, ans = 0, last_n = 0;
    cin >> n >> p;
    for (int i = 1; i < 2000000; i++) {
        long long int nn = n - p * i;
        if (nn <= 0 || nn == 1 && i != 1) {
            continue;
        }
        int cnt1 = 1;
        int nnn = nn;
        while (nn != 1) {
            cnt1 += nn % 2;
            nn /= 2;
        }//*/
        if (cnt1 <= i && nnn >= i) {
            cout << i;
            return 0;
        }
    }
    cout << -1;
}

#include <bits/stdc++.h>
using namespace std;
int n,m;
int solve(){
    for(int i=1;i<=10000;++i){
        n-=m;
        if(__builtin_popcount(n)<=i&&i<=n) return i;
    }
    return -1;
}
int main(){
    scanf("%d %d",&n,&m);
    printf("%d\n",solve());
}

//Code by : Y-k-y
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <map>
#define int long long
const int N=100010;
#define lowbit(x) x&(-x)
using namespace std;
inline int rnd(){
	int res=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){res=res*10+ch-'0';ch=getchar();}
	return res*f;
}
inline void wr(int x){
	if(x<0){putchar('-');x=-x;}if(x>9) wr(x/10);putchar(x%10+'0');
}
int n,p;
int ans;
int fac[45];
int t[45];
signed main(){
	cin>>n>>p;
	fac[1]=2;fac[0]=1;
	for(int i=2;i<=35;i++){
		fac[i]=fac[i-1]*2;
	}
	for(int i=1;i<=35;i++){
		memset(t,0,sizeof(t));
		int m=n-i*p;int num=0;
		while(m>0){
			m-=lowbit(m);num++;
		}
		int down=num,up=n-i*p;
		if(i>=down&&i<=up){
			wr(i);return 0;
		}
	}
	wr(-1);
	return 0;
}

#include<bits/stdc++.h>
typedef long long int ll;
typedef double db;
using namespace std;
#define forn(i,n) for(int i = 0; i < int(n); ++i)
int n,m,i,j;

int num_in_2pow(int num)
{
	int pow_num=0;
	while(num>0)
	{
		if(num&1) pow_num++;
		num>>=1;
	}
	return pow_num;
}

int main()
{
	int p;
	cin>>n>>p;
	int num=n;
	if(num-p <=0)
	{
		cout<<"-1";
		return 0;
	}

	i=1;
	while(i)
	{
		num-=p;

		j=num_in_2pow(num);

		if(i>=j && i<=num)
		{
			cout<<i;
			return 0;
		}

		if(num<0)
		{	cout<<"-1"; return 0;}
		i++;
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define dd double
#define MOD 1000000007
#define nl cout<<endl
#define rt ll Q[100005]={0}
#define mp make_pair
#define test() ull t;cin>>t;while(t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define s(ar,n) sort(ar,ar+n)
#define rs(ar,n) sort(ar, ar+n, greater<ll>())
#define oa(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";nl
#define cn(a,n) for(ll i=0;i<n;i++)cin>>a[i];
#define ov(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";nl
#define maxa(ar,N) *max_element(ar,ar+N)
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
ll powe(ll n,ll m){if(m==0)return 1;ll t=powe(n,m/2);if(m%2==0)return (t*t);return (((t*t))*n);}
ll logtwo(ll n){if(n==1)return 0;return logtwo(n/2)+1;}
bool binary(ll n,ll p)
{
    ll k=p;
    ll ko=n;
	while(n!=0)
	{
		p-=n%2;
		n/=2;
	}
	if(k-p<=k&&ko>=k)
		return true;
	return false;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("outpu.txt", "w", stdout);
    #endif
    fastio();
    ll N,M;
    cin>>N>>M;
    ll c=0;
    bool k=false;
    while(N-M>0)
    {
    	N-=M;
    	c++;
    	//cout<<N<<" "<<M<<" "<<c<<endl;
    	k=binary(N,c);
    	//cout<<k<<endl;
    	if(k)
    	{
    		cout<<c;
    		return 0;
    	}
    }
    cout<<-1<<endl;
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
int bits(int a) {
	int res = 0;
	while (a > 0) {
		res += a % 2;
		a /= 2;
	}
	return res;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, P;
	cin >> N >> P;
	int res = -1;
	for (int i = 0; i <= 50; i++) {
		if (i <= N - P * i && bits(N - P * i) <= i) {
			res = i;
			break;
		}
	}
	cout << res << endl;
}

#include <bits/stdc++.h>
#define ll long long
#define MAXN 200005
using namespace std;
int num1(ll n) {
	if(n < 1) return -1;
	int ans = 0;
	while(n) {
		if(n & 1) ans++;
		n >>= 1;
	}
	return ans;
}
int main() {
	ll n, p;
	cin >> n >> p;
	int min1 = 100001;
	for(int i = 1; i <= 100000; i++) {
		if(num1(n - (ll)i * p) <= i && (n - (ll)i * p) >= i) min1 = min(min1, i);
	}
	if(min1 == 100001) min1 = -1;
	cout << min1 << endl;
}

#include<bits/stdc++.h>
#define  mod 1000000007
using namespace std;
using ll = long long int;

vector<ll>v(30, 1);

int check(ll n)
{
	int cnt = 0;
	for (int i = 29; i >= 0 ; i--)
	{
		if (v[i] <= n)
		{
			n -= v[i];
			cnt++;
		}
	}
	if (n != 0)cnt = 0;
	return cnt;
}

int main()
{
#ifndef ONLINE_JUDGE
	// for getting input from input.txt
	freopen("input.txt", "r", stdin);
	// for writing output to output.txt
	freopen("output.txt", "w", stdout);
#endif

	for (int i = 1; i < 30; i++)
	{
		v[i] = 2 * v[i - 1];

	}

	ll t = 1;
	//cin >> t;
	while (t--)
	{
		ll n, p;
		cin >> n >> p;
		int ans;
		int flag = 0;
		for (int i = 1; i < 30; i++)
		{
			int x = INT_MAX;
			if (n - p * i >= i)x = check(n - (p * i));

			//cout << (n - (p * i)) << " " << x << endl;
			if (x <= i)
			{

				flag = 1;
				ans = i;
				break;
			}
		}
		if (flag == 0)cout << "-1";
		else
			cout << ans;

	}
	return 0;

}

#include "bits/stdc++.h"
using namespace std;
#define all(v) (v).begin(), (v).end()
#define io ios::sync_with_stdio(0)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rson rt << 1 | 1, mid + 1, r
#define lson rt << 1, l, mid
#define lll __int128
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-12
const int mod =998244353;
inline int ksm(int a, int b)
{
    int ans = 1;
    for (; b; b >>= 1, a = a * a % mod)
        if (b & 1)
            ans = ans * a % mod;
    return ans;
}

signed main()
{
    int n,p;cin>>n>>p;
    int ans = 1e18;
    for(int i=1;i<=100;i++){
        int N = n-i*p;
        if(i<=0)continue;
        bitset<63> t = N;
        int minn = t.count();
        int maxx = N;
        if(i<=maxx && i>=minn) ans=min(ans,i);
    }
    if(ans == 1e18)cout<<-1<<endl;
    else cout<<ans<<endl;

    getchar();getchar();
    return 0;
}

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<set>
#include<map>
#include<bitset>
#include<ctime>
#include<queue>
using namespace std;
#define int long long
#define mn 100005
#define inf 4444444444444444444
#define mod 1000000009
#define FLN "test"
#define pii pair <int, int>
#define XX first
#define YY second

int n, p;

string toBin(int u)
{
    string ret="";
    while (u>0)
    {
        ret+=('0'+(u%2));
        u/=2;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

int cntBin(int u)
{
    int ret=0;
    // cerr<<u<<" ";
    while (u>0)
    {
        ret+=u%2;
        u/=2;
    }
    // cerr<<ret<<"\n";
    return ret;
}

signed main()
{
#ifdef lowie
    // freopen(FLN".inp", "r", stdin);
    // freopen(FLN".out", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>p;
    for (int ans=1; ans<=32; ans++)
    {
        if (cntBin(n-p*ans)<=ans && ans<=n-p*ans)
        {
            cout<<ans;
            return 0;
        }
    }

    cout<<-1;
}

// PLEASE REMOVE cout AND cerr DEBUG LINES BEFORE SUBMITTING PROBLEMS
// Code by low_
// Contact me via mail: dung.totuan01@gmail.com
// ...or codeforces: www.codeforces.com/profiles/low_
// ...or if you're interested in food: www.instagram.com/lowie_review/

#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
#define pb push_back
#define mp  make_pair
#define loop(a,n) for(long long i=a;i<n;i++)
#define vil vector<long long int>
#define vi vector<int>
#define sz(v) v.size()
using namespace std;
ll gcd(ll a,ll b){
    if(a==0)return b;
    return gcd(b%a,a);
}
bool isperfect(ll n){
    ll y=sqrt(n);
    if(n%y==0&&y*y==n)return true;
    return false;
}
bool comp(pair<ll,ll> a,pair<ll,ll> b){
    return a.second<b.second;
}

int main() {
  int n,p;
  cin>>n>>p;
  int m=n;
  for(int i=1;i<31;i++){
        m=m-i*p;
        int y=m;
        if(m<=0)continue;
        int cnt=0;
        while(m>0){
            if(m%2!=0)cnt++;
            m=(m>>1);
        }
        if(cnt<=i&&i<=y){
            cout<<i;
            return 0;
        }
        else m=n;
  }
  cout<<"-1";
}

#include<bits/stdc++.h>
using namespace std;
typedef  long long int ll;

#define  prec(n)        fixed<<setprecision(n)
#define  optimize       ios::sync_with_stdio(0); cin.tie(0);
#define  PI             acos(-1.0)
#define  RESET(a, b)    memset(a, b, sizeof(a))
#define  pb             push_back
#define  sz(v)          v.size()
#define  pii            pair<int,int>
#define  ff             first
#define  ss             second
#define  vv             vector<int>
#define  all(v)         v.begin(),v.end()

int const MOD=(int) 998244353;
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
inline ll mod(ll a, ll m){ if(a<0) return m-(abs(a)%m); else if(a<m) return a; else return (a%m); }
const int N = 1000000;
const int MAX=1000000;

int cal(ll n){
    int cnt=0;
    for(int i=0;i<64;i++){
        if((n>>i)&1==1){
            cnt++;
        }
    }
    return cnt;
}
int main()
{
    optimize
    ll n,p;
    cin>>n>>p;
    if(n<=p){
        cout<<"-1"<<endl;
        return 0;
    }
    if(p==0){
        cout<<cal(n)<<endl;
        return 0;
    }
    int i=1,flag=0;
    while(1){
        if(n-i*p <= 0){
            flag=1;
            break;
        }
        int d=cal(n-i*p);
        if(d<=i && (n-i*p)>=i){
            cout<<i<<endl;
            break;
        }
        i++;
    }
    if(flag==1) cout<<"-1"<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);  cin.tie(0);   cout.tie(0);
typedef long long ll;
const ll N = 1e6+1;

bool fun(ll n, ll cnt){
    if(n<=0) return false;

    ll num=n;
    ll numBits=0;
    while(n>0){
        numBits += (n&1);
        n = n>>1;
    }

    ll minBitsRequired=numBits;
    ll maxBitsRequired=num;

    return ((cnt>=minBitsRequired)&&(cnt<=maxBitsRequired));
}

int main() {
    fast
    ll n,p;
    cin >>n >>p;

    ll answer = -1;

    for(ll i=N;i>=0;i--){
        if(fun(n-i*p,i)==1){
            answer = i;
        }
    }

    cout<<answer;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=x;i<=y;i++)
#define per(i,x,y) for (int i=x;i>=y;i--)
#define quicker ios::sync_with_stdio(false);
#define first fi
#define second sec
typedef long double ld;
typedef long long ll;
const ll HOLLAND_PIG=1;
const long long INF=2147483647;

inline int read()
{
    int k=0;
    char f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
            c=getchar();
        }
    }
    while(c<='9'&&c>='0')
    {
        k=k*10+c-'0';
        c=getchar();
    }
    return k*f;
}

ll max(ll x,ll y)
{
	if (x>y)
	{
		return x;
	}
	else
	{
		return y;
	}
}

ll min(ll x,ll y)
{
	if (x<y)
	{
		return x;
	}
	else
	{
		return y;
	}
}
ll n,d,kk,xx;
int main()
{
	cin>>n>>d;rep(i,HOLLAND_PIG,1000000){
		kk=n-d*i;
		if(kk>=i){xx=0;kk-=i;

			per(q,31,1)
			{
				while(((HOLLAND_PIG<<q)-HOLLAND_PIG)<=kk){++xx;kk-=(HOLLAND_PIG<<q)-HOLLAND_PIG;}}if(xx<=i){cout<<i<<endl;return 0;}}}
	cout<<"-1"<<endl;
	return 0;
}

/*
 * =template=.cpp
 * Copyright (C) 2017 hzw <hzw@huzhenweideMacBook.local>
 *
 * Distributed under terms of the MIT license.
 */
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<unordered_map>
#define FR first
#define SC second
#define MP make_pair
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define pb push_back
#define ws wss
#define left lll
#define right rrr
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
template<class T> void upmax(T &a,T b) { if (a<b) a=b;}
template<class T> void upmin(T &a,T b) { if (a>b) a=b;}
void read(int &x)
{
	char ch;int fu=1;
	while ((ch=getchar())<=32);
	x=0;
	if (ch=='-') fu=-1;else x=ch-48;
	while ((ch=getchar())>32) x=x*10+ch-48;
	x*=fu;
}
void read(unsigned long long &x)
{
	char ch;int fu=1;
	while ((ch=getchar())<=32);
	x=0;
	if (ch=='-') fu=-1;else x=ch-48;
	while ((ch=getchar())>32) x=x*10+ch-48;
	x*=fu;
}
const double pi=acos(-1);
void upmax(int &a,int b) { if (a<b) a=b;}
void upmin(int &a,int b) { if (a>b) a=b;}

int gcd(int a,int b) { return b>0?gcd(b,a%b):a;}
//-----------------------
const int N=110000;//,p=1000000007,inv2=(p+1)/2;
int cas,n;
int f[N];
int main()
{
	int p;
	scanf("%d%d",&n,&p);
	for (int i=0;i<32;i++)
	{
		int m=n-i*p;
		if (m<0||m<i) continue;
		int k=0;
		while (m)
		{
			k+=m%2;
			m/=2;
		}
		if (k<=i)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	puts("-1");
}

//by xay5421 2449670833@qq.com
#include<set>
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define int long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define debug(...) fprintf(stderr,__VA_ARGS__)
//#define debug(...) ((void)0)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;typedef unsigned long long ull;typedef std::pair<int,int> pii;
template<typename T>void rd(T&x){int f=0,c;while((c=getchar())<48||57<c)f^=!(c^45);x=(c&15);while(47<(c=getchar())&&c<58)x=x*10+(c&15);if(f)x=-x;}
template<typename T>inline void pt(T x){if(x<0)x=-x,putchar('-');if(x>9)pt(x/10);putchar(x%10+48);}
template<typename T>inline void pt(T x,int c){pt(x),putchar(c);}
template<typename T>inline T max(const T&x,const T&y){return x<y?y:x;}
template<typename T>inline T min(const T&x,const T&y){return x<y?x:y;}

int n,p;

signed main(){
	rd(n),rd(p);
	rep(i,1,1000){
		n-=p;
		if(n<=0)break;
		int x=__builtin_popcountll(n);
		if(x<=i&&i<=n){
			pt(i,'\n');
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include<bits/stdc++.h>
#define re register
#define inc(i,j,k) for(re int i=j;i<=k;++i)
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=200010;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=x*10+(ch^48),ch=getchar();}
	return x*f;
}
int n,p;
int lowbit(int x) {return (x&-x);}
int cnt(int x)
{
    int res=0;
    while(x) x-=lowbit(x),res++;
    return res;
}
int main()
{
    cin>>n>>p;
    int flag=0,ans=0;
    for(int i=1;;i++)
    {
        if(n-i*p<i) break;
        if(cnt(n-i*p)>i) continue;
//		ans++;
		ans=i;
		flag=1;
        break;
    }
    if(flag) cout<<ans<<endl;
    else puts("-1");
    return 0;
}
/*
24 0
24 1
24 -1
4 -7
1 1
*/// 2 3 4 2 -1

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lowbit(x) (x&-x)
int n,m;
inline int calc(int x)
{
	int cnt=0;
	while(x)cnt++,x-=lowbit(x);
	return cnt;
}
signed main()
{
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=10000;i++)
	{
		int tmp=n-i*m;
		if(tmp>=i&&calc(tmp)<=i){printf("%lld",i);return 0;}
	}
	puts("-1");
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
bool F(int n, int i, int cont)
{
	if (n==0 && i==0)
	return true;
	if(i==0)
	return false;
    if(i>n && i>cont)
    return false;
	int j=0;
	while(pow(2, j)<=n)
	{
		j++;
	}

	return F(n-pow(2, j-1), i-1, cont+pow(2,j-1));

}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long int n;
	int p;
	bool find=false;
    cin>>n>>p;
    if (p>=0)
    {

    	for ( int j=1; n-p*j>0 && !find; j++)
    	{
    		if(F(n-p*j, j, 0))
			{
    		cout<<j;
    		find=true;

    	   	}
		}
	}
	else
	{

		for (int i=1; !find; i++)
		{
			if (F(n-p*i, i, 0))
			{
				cout<<i;
				find=true;
			}
		}

	}
	if(!find)
	cout<<'-'<<'1';

}

#include <bits/stdc++.h>
using namespace std;
// #include <ext/rope> //header with rope
// using namespace __gnu_cxx;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

/// Typedef
typedef long double ld;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector< pii > vii;

#define pb                  push_back
#define ppb                 pop_back
#define MP                  make_pair
#define ff                  first
#define ss                  second
#define sf                  scanf
#define pf                  printf
#define FOR(i, x, y)        for(int i=int(x); i<int(y); i++)
#define ROF(i, x, y)        for(int i=int(x)-1; i>=int(y); i--)
#define Binary(x,b)         std::bitset< b >( x ).to_string()
#define fastIO              ios::sync_with_stdio(false);    cin.tie(nullptr);   cout.tie(nullptr)
#define ran()               (((((1ULL*rand())<<31)|(1ULL*rand()))<<13)^((((1ULL*rand())<<32)|(1ULL*rand()))))
#define rran(a, b)          ((ran() % ((b) - (a) + 1)) + (a))
#define CLR(x, y)           memset(x, y, sizeof(x))
#define sc(n)               sf("%d",&n)
#define sc2(a,b)            sf("%d%d",&a,&b)
#define sc3(a,b,c)          sf("%d%d%d",&a,&b,&c)
#define sc4(a,b,c,d)        sf("%d%d%d%d",&a,&b,&c,&d)
#define scll(n)             sf("%lld",&n)
#define scll2(a,b)          sf("%lld%lld",&a,&b)
#define scll3(a,b,c)        sf("%lld%lld%lld",&a,&b,&c)
#define scll4(a,b,c,d)      sf("%lld%lld%lld%lld",&a,&b,&c,&d)
#define bitCheck(N,in)      ((bool)(N&(1LL<<(in))))
#define bitOff(N,in)        (N&(~(1LL<<(in))))
#define bitOn(N,in)         (N|(1LL<<(in)))
#define bitFlip(a,k)        (a^(1LL<<(k)))
#define bitCount(a)         __builtin_popcount(a)
#define bitCountLL(a)       __builtin_popcountll(a)
#define bitRightMost(a)     __builtin_ctzll(a)
#define bitLeftMost(a)      (63-__builtin_clzll(a))
#define SQR(x)              ((x)*(x))
#define mxEq(a,b)           (a = max(a,b))
#define all(c)              c.begin(), c.end()
#define unq(v)              sort(all(v)), (v).resize(unique(all(v)) - v.begin())
#define common(a,b)         sort(all(a)), sort(all(b)), a.erase(set_intersection(all(a),all(b),a.begin()),a.end())
#define uncommon(a,b)       sort(all(a)), sort(all(b)), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end())

// uniform random generator -->
// uniform_int_distribution<int> dist(0, 1);
// default_random_engine gen;

// *X.find_by_order(k) //returns the kth largest element.(0-based)
// X.order_of_key(val) //returns the no. of values less than val
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // C++98
// template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2, class T3> inline T3 bigMod(T1 b, T2 p, T3 M){ if(!p) return 1; if(p&1) return b*bigMod(b,p-1,M)%M; T3 h = bigMod(b,p>>1,M); return h*h%M;}
template <class T1, class T2> inline T2 modInverse(T1 a,T2 M){return bigMod(a%M,M-2,M);}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T lcm(T a,T b) {return (a/gcd(a,b))*b;}
template <class T> inline string int2String(T a){ostringstream str; str<<a; return str.str();}
const int dr[] = { 0,  1,  0, -1,        -1,  1,  1, -1,     -2, -2,  2,  2, -1, -1,  1,  1};
const int dc[] = { 1,  0, -1,  0,         1,  1, -1, -1,     -1,  1, -1,  1, -2,  2, -2,  2};

/// Constants
#define MOD                 (ll)(1e9+7)
#define eps                 1e-9
#define INF                 0x3f3f3f3f3f3f3f3f
#define inf                 0x3f3f3f3f // CLR(63)
#define PI                  (2.0*acos(0.0))  // 3.1415926535897932
#define Log(b,x)            (log(x)/log(b))

/// Debug
#define out(a,T,str)        copy(a.begin(),a.end()-1,ostream_iterator<T>(cout,str)),cout<<a.back()<<endl
#ifdef VAMP
    #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define dbg(...)
#endif

/// Priority Queue custom compare
// #define PQ_TYPE pair<pair<ll,ll>,int>
// bool cmp(const PQ_TYPE &a, const PQ_TYPE &b){     return a.ff.ff*b.ff.ss < b.ff.ff*a.ff.ss;}
// std::priority_queue< PQ_TYPE , std::vector< PQ_TYPE >, decltype(&cmp) > pq(cmp);
ll a[50];
bool solve(int n, int k){
    if(n <= 0) return 0;
    priority_queue<int> pq;
    ROF(i,32,0){
        if(n >= a[i]) n -= a[i], pq.push(i);
    }
    while(pq.size() < k){
        auto i = pq.top(); pq.pop();
        if(i == 0) break;
        i--;
        pq.push(i);
        pq.push(i);
    }
    return pq.size() == k;
}
int main(){
    #ifdef VAMP
        int start_s=clock();
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    // fastIO;
    // srand((rand()-time(NULL))%MOD);

    FOR(i,0,32) a[i] = (1LL<<i);//, cout << a[i] << endl;
    int n,p;
    while(sc2(n,p) == 2){
        int ans = -1;
        FOR(i,1,32){
            // pf("%d. %3d %3d\n", i, solve(n-i*p,i), n-i*p);
            if(solve(n-i*p, i)){
                ans = i;
                break;
            }
        }
        pf("%d\n", ans);
    }

    #ifdef VAMP
        int end_s=clock();
        printf("\nTime :: %.2lf\n", (double)(end_s-start_s)/CLOCKS_PER_SEC);
    #endif
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define foe(i,a) for(__typeof(a.begin())i=a.begin();i!=a.end();++i)
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
typedef double ld;
typedef long long L;
typedef vector<int> VI;
typedef pair<int,int> pa;
#ifdef flukehn
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

L n,p;
int main(){
#ifdef flukehn
	freopen("a.in","r",stdin);
#endif
	while(cin>>n>>p){
		int i;
		for(i=1;i<33333;++i){
			L m=n-i*p;
			if(m<=0||i>m)continue;
			if(__builtin_popcountll(m)<=i)break;
		}
		if(i<33333)cout<<i<<endl;
		else cout<<-1<<endl;
	}
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, p;
	cin >> n >> p;
	for(int i = 0; i < 32 ; i++)
	{
		int temp = n - i*p;
		if(__builtin_popcount(temp) <= i && i <= temp&&temp>0)
		    return cout << i , 0;
	}
	cout << -1;
}

#include <iostream>
#include <vector>
#include <map>

using namespace std;

long long f(long long n) {
    long long k = 0;
    while (n != 0) {
        if(n % 2 != 0) k++;
        n /= 2;
    }
    return  k;
}

int main() {
    long long n, p;
    cin >> n >> p;

    long long k = 1;
    n -= p;
    while(n > 0){
        if((f(n) <= k) && (k <= n)){
            cout << k;
            break;
        }
        n -= p;
        k++;
    }
    if(n <= 0){
        cout << -1;
    }
}

#include <bits/stdc++.h>

int main() {
	int n, p; bool flag = false;
	scanf("%d%d", &n, &p);
	for(int i = 1; i <= 1e6 && !flag; ++i) {
		int x = n - p * i, cnt = 0;
		if(x < 0) continue;
		for(int j = 0; j <= 31; ++j)
			if((x >> j) & 1) ++cnt;
		if(cnt <= i && x >= i)
			printf("%d", i), flag = true;
	}
	if(!flag) printf("-1");
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n, p;
    scanf("%lld %lld", &n, &p);
    int res= -1;
    for(int i=1; i<=20000000; i++)
    {
        ll need= n-i*p;
        if(need<=0)continue;

        int c= __builtin_popcount(need);
        if(i>=c && i<=need)
        {
            res= i;
            break;
        }
    }printf("%d\n", res);
    return 0;
}

//
//  main.cpp
//  CodeForces
//
//  Created by ssense on 06/11/20.
//  Copyright © 2020 ssense. All rights reserved.
//
#include<bits/stdc++.h>
//#include "/Users/mihaiesanu/testlib.h"
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef unsigned long long ull;
typedef long long  ll;
using namespace std;
#define FOR(n) for(int i=0;i<n;i++)
#define vt vector
#define vint vector<int>
#define all(x) v.begin(), v.end()
#define sz(a) (int)a.size()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define NMAX 100005
#define MXL 1000000000000000000
#define PI 3.14159265
#define pb push_back
#define pf push_front
#define sc second
#define endl '\n'
#define fr first
#define int ll
#define ld long double

int ceildiv(int one, int two){
    if(one%two == 0)
    {
        return one/two;
    }
    else
    {
        return one/two+1;
    }
}int power(int n, int pow, int m){
    if(pow == 0) return 1;
    if(pow%2 == 0){ll x = power(n, pow/2, m);
        return (x*x)%m;}
    else return (power(n, pow-1, m)*n)%m;
}int gcd(int a, int b){
    if(!b)return a;
    return gcd(b,a%b);
}int factorial(int n, int mod){
    if(n > 1)
        return (n*factorial(n - 1, mod))%mod;
    else
        return 1;
}int lcm(int a, int b){
    return (a*b)/gcd(a, b);
}string to_string(char c){return string(1, c);}string to_string(bool b){return b?"true":"false";}string to_string(const char* s){return string(s);}string to_string(string s){return s;}string to_string(vt<bool>v){string res;FOR(sz(v))res+=char('0'+v[i]);return res;}template<class T> string to_string(T v){bool f=1;string res;for(auto x:v){if(!f)res+=' ';f=0;res+=to_string(x);}return res;}template<class A> void write(A x){cout << to_string(x);}template<class H, class... T> void write(const H& h, const T&... t){write(h);write(t...);}void print(){write("\n");}template<class H, class... T> void print(const H& h, const T&... t) { write(h);if(sizeof...(t))write(' ');print(t...);}vector<int> read(int n){
    vector<int> a;
    for(int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        a.pb(x);
    }
    return a;
}

void solve()
{
    int n, p;
    cin >> n >> p;
    for(int i = 0; i < 100; i++)
    {
        int ans = __builtin_popcount(n-i*p);
        if(ans <= i && n-i*p >= i)
        {
            cout << i << endl;
            return;
        }
    }
    cout << -1 << endl;

}

int32_t main() {
    startt
    int t;
    //cin >> t;
    t = 1;
    while(t--)
    {
        solve();
    }
}

//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR0(i,n) for(i=0;i<n;i++)
#define FOR(i,j,n) for(i=j;i<n;i++)
#define FORD(i,j,k) for(i=j;i>=k;i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define inf 1000000000
#define ninf -1000000000
#define endl '\n'
#define she_told_me_dont_worry ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long int b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< ii > vii;
int main()
{
    she_told_me_dont_worry
    ll n, p;
    cin>>n>>p;
    ll i;
    bool is = false;
    ll ans;
    FOR(i, 1, 1000005)
    {
        ll cur = n - i*p;
        if( cur <= 0 ) break;
        ll j = 0;
        ll cnt[101] = {};
        while(cur)
        {
            j++;
            ll lg = log2(cur);
            cnt[lg]++;
            cur -= pow(2, lg);
        }
        cur = 64;
        while( j < i && cur > 0)
        {
            if(!cnt[cur]){cur--; continue;}
            cnt[cur-1] += 2;
            cnt[cur]--;
            j++;
        }
        if(j == i){
            ans = i;
            is = true;
            break;
        }
    }
    if(is) cout<<ans<<endl;
    else cout<<-1<<endl;
    cerr <<endl<< "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
}

#include <bits/stdc++.h>

using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr)
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(),v.end()
#define w(q) int q; cin>>q; while(q--)
#define setp(n) fixed << setprecision(n)
#define sp " "
#define co continue
#define re return
const ll mod = 1000000007;
const int N = 1001;

ll lcm(ll a, ll b) {
    re (a * b) / __gcd(a, b);
}

bool isprime(ll x) {
    if (x % 2 == 0 && x != 2) re 0;
    for (ll i = 3; i * i <= x; i++)
        if (x % i == 0)
            re 0;
    re 1;
}

ll power(ll a, ll b) {
    if (b == 0)
        re 1;
    ll p = power(a, b / 2);
    p = p * p;
    if (b % 2)
        p = p * a;
    re p;
}

ll inv(ll x) {
    re power(x, mod - 2);
}

/*----------------------------------------------------------------------------------------------------------------*/

ll n, p;
vll v;

bool can(ll x) {
    ll t = n;
    t -= p * x;
    if (t < 0)
        re 0;
    while (t != 0) {
        ll s = 0;
        while (power(2, s) <= abs(t))
            s++;
        s--;
        t -= power(2, s);
        v.pb(power(2, s));
        if (v.size() > x)
            re 0;
    }
    if (x == v.size())
        re 1;
    ll d = x - v.size();
    for (auto i : v) {
        if (i != 1) {
            ll c = 1, t = i;
            while (t != 1)
                t /= 2, c *= 2;
            if (c - 1 >= d)
                re 1;
            d -= c - 1;
        }
    }
    re 0;
}

int main() {
    fast;
    cin >> n >> p;
    ll l = 1, r = 3000, ans = -1;
    for (int i = 1; i <= 3000; i++) {
        if (can(i)) {
            ans = i;
            break;
        }
        v.clear();
    }
    cout << ans;
    re 0;
}

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
typedef pair<int, bool> sP;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef vector<vector<ll>> mat;
typedef vector<ll> vec;

void solve() {
	ll n, p; cin >> n >> p;
	ll k = 1;
	while (k) {
		ll r = n - k * p;
		if (r < min((ll)0,1 + p))break;
		int cnt = 0;
		rep(i, 40) {
			ll z = (ll)1 << i;
			if (z&r)cnt++;
		}
		if (cnt <= k && k <= r) {
			cout << k << endl; return;
		}
		k++;
	}
	cout << -1 << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(7);
	//init();
	solve();
	//stop
	return 0;
}

#pragma GCC optimize("Ofast")
/*#pragma GCC target("avx,avx2,fma")
#pragma GCC target ("sse4")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define BOOST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(v) v.begin(),v.end()
#define sl(a) a.size()
#define mod 1000000007
#define lcm(a,b) (a*b)/__gcd(a,b)
#define MAX1 100006
#define f first
#define s second
#define pi 2*acos(0.0)
bool isPowerOfTwo (ll x) {  return x && (!(x&(x-1)));  }
ll Modular_Exponentiation(ll x, ll n, ll M)
{
    if(n==0) return 1;
    if(n%2) return (x*Modular_Exponentiation((x*x)%M,n/2,M))%M;
    else return (Modular_Exponentiation((x*x)%M,n/2,M))%M;
}
bool isPrime(ll n)
{
    if (n<=1)  return false;
    if (n<=3)  return true;
    if (n%2==0||n%3==0) return false;
    for(ll i=5; i*i<=n; i+=6)
        if (n%i==0||n%(i+2)==0) return false;

    return true;
}
/********************************************************************************************/
void solve()
{
    ll n,p,ans=1e18;  cin>>n>>p;
    rep(i,1,31)
    {
        ll temp=n-i*p;
        if(temp>0)
        {
            ll t=__builtin_popcount(temp);
            if(t<=i)
            {
                if(t==i) ans=min(ans,i);
                else if(temp>=i) ans=min(ans,i);
            }
        }
    }
    cout<<(ans==1e18?-1:ans);
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt","r",stdin);
    //for getting error from error.txt
    freopen("error.txt", "w", stderr);
    // for writing output to output.txt
    freopen("output.txt","w",stdout);
    #endif
    BOOST
    int T=1; //cin>>T;
    while(T--) solve();
    cerr<<"Time taken : "<<fixed<<setprecision(5)<<((float)clock()/CLOCKS_PER_SEC)*1000<<" ms"<<"\n";
    cerr<<"My CLOCKS_PER_SEC= "<<CLOCKS_PER_SEC<<"\n";
    return 0;
}

#include <iostream>
using namespace std;

int bts(long long k)
{
	int ans = 0;
	for (long long mask = 1; mask <= k; mask <<= 1)
	{
		if (mask & k)
		{
			++ans;
		}
	}
	return ans;
}

int main()
{
	long long n, p;
	cin >> n >> p;
	long long t = 1;
	while (n-t*p>0)
	{
		if (bts(n - t * p) <= t && n - t * p >= t)
		{
			cout << t;
//			cin >> p;
			return 0;
		}
		++t;

	}
	cout << -1;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i)>= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
#define fi first
#define se second
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 9e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

void print(ll a[], ll n){for(ll i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n";}
ll power(ll x, ll y, ll M = inf){
    ll ans = 1;
    x %= M;
    while(y){
        if(y&1)
            ans = (x * ans) % M;

        x = (x * x) % M;
        y >>= 1;
    }
    return ans;
}
ll modInverse(ll n) {return power(n, mod-2, mod);}
inline ll mul(ll a, ll b){ return (a * b) % mod; }
inline ll sub(ll a, ll b){ ll c = a - b; if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = a + b; if(c > mod) c -= mod; return c; }
inline ll divi(ll a, ll b){ return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

//const ll N = 1e5 + 1;
//vector<ll> adj[N];
//bool visited[N];

int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout<<fixed<<setprecision(10);

    ll n, p;
    cin>>n>>p;
    for(ll i=1;i<100;i++){
    	n = n-p;
    	if(__builtin_popcount(n) <= i && n>=i){
    		cout<<i;
    		return 0;
    	}
    }
    cout<<-1;
    return 0;
}

//besmellah
#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	long long n, p;
	cin >> n >> p;
	for (long long i = 1; i <= 1e8; i ++){
		if (n <= i * p)
			break;
		if (n - i * p >= i && __builtin_popcountll(n - i * p) <= i){
			cout << i;
			return 0;
		}
	}
	cout << -1;
}

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
ll countSetBits(ll n)
{
    ll count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}
ll setBitNumber(ll n)
{
    cout << endl;
    if (n == 0)
        return 0;
    ll msb = 0;
    while (n != 0) {
        n = n / 2;
        msb++;
    }
    msb--;
    //cout << " returning " << (1 << msb) << endl;
    return (1 << msb);
}
ll countTotal(ll n)
{
    ll count = 0;
    while(n)
    {
        count += setBitNumber(n)/2;
        n >>= 1;
    }
    return count;
}
int main()
{
    ll n, p;
    cin >> n >> p;
    ll k = 1;
    while(n - k*p > 0)
    {
        ll bits = countSetBits(n - k*p);
        if(bits > k)
        {
            k++;
            continue;
        }
        else if(bits == k)
        {
            cout << k << endl;
            return 0;
        }
        ll count = 0;
        if((n - k*p)%2 == 1)
        {
            count = countTotal(n - k*p - 1);
            count++;
        }
        else
        {
            count = countTotal(n - k*p);
        }
        if(count >= k)
        {
            cout << k << endl;
            return 0;
        }
        k++;
    }
    cout << -1 << endl;
    return 0;
}

#include<bits/stdc++.h>
#define ls rt<<1
#define rs rt<<1|1
#define lson rt<<1,l,M
#define rson rt<<1|1,M+1,r
#define PI acos(-1)
#define MM int M=(l+r)>>1;
#define fu(i,r,t) for(int i=r;i<=t;i++)
#define fd(i,r,t) for(int i=r;i>=t;i--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define endl '\n'
#define lcm(a,b) (a*b)/__gcd(a,b)
#define Case(i,ans) cout<<"Case "<<i<<": "<<ans<<endl;
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout<<setprecision(i)<<fixed<<ans<<endl;
#define ppb pop_back
#define ppf pop_front
#define pb push_back
#define pf push_front
#define pq priority_queue
#define lowbit(x) (x)&(-x)
#define all(V) V.begin(),V.end()
#define ms multiset
#define mod(x) ((x+mo_num)%mo_num)
#define vc vector
#define vct vector<int>
#define fa(i,V) for(auto i:V)
#define out(i) cout<<(i)<<endl;
#define fi first
#define se second
#define fun(i) fu(i,1,n)
#define fut(i) fu(i,1,t)
#define fum(i) fu(i,1,m)
#define ld long double
#define umap unordered_map
#define P pair<int,int>
#define SET set<int>
using namespace std;
const int INF=LLONG_MAX-1e10;
const int maxn=1e6+5;
const int mo_num=1e9+7;
int n,m,a[maxn],b[maxn],ans;

bool can(int nn,int i)
{
    int val=nn-i*m;
    int len=0;
    while(val)
    {
        if(val%2)len++;
        val>>=1;
    }
    if(len<=i && (nn-i*m)>=i)return 1;
    else return 0;
}
int32_t main()
{
    IOS
    cin>>n>>m;
    fu(i,1,100)
    {
        if(n-i*m<=0){out(-1);return 0;}
        if(can(n,i)){
            out(i)
            return 0;
        }
    }
    out(-1)
    return 0;
}

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);
    int nxt, p;
    cin >> nxt >> p;

    vector<int> v;
    for (int i=0; i<63; i++) {
        v.push_back((1LL<<i));
    }
    reverse(v.begin(),v.end());

    bool ok = 0;
    int mn = 1e15;
    for (int j=1; j<=100000; j++) {
        int n = nxt - p*j;
        int per = 0;
        if (n == 0) continue;
        int cnt = 0;

        for (int i=0; i<v.size(); i++) {
            if (v[i] <= n) {
                cnt++;
                n -= v[i];
                per += v[i]-1;
            }
        }

        if (n==0 && j >= cnt && j-cnt <= per) {
            ok = 1;
            mn = j;
            break;
        }
    }

    if (ok) cout << mn << endl;
    else cout << -1 << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

int main()
{
	int n;
	int p;
	cin >> n >> p;
	int sol=INT_MAX;
	for(int i=1;i<32;i++)
    {
        int nn=n-p*i;
        int koliko=__builtin_popcount(nn);
        if(koliko>i||nn<=0||i-koliko>=nn)
            continue;
        sol=min(sol,i);
    }
    if(sol==INT_MAX)
        printf("-1\n");
    else
        printf("%i\n",sol);
    return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<set>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 1e9;
int main()
{
	ll n,p,ans=1;
	cin>>n>>p;
	while(n-ans*p>0)
	{
		if(__builtin_popcountll(n-ans*p)<=ans)//统计二进制中1的个数
		{
			if(n-ans*p>=ans)
			{
			cout<<ans<<endl;
			return 0;
		}
		}
		ans++;
	}
	cout<<-1<<endl;
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <bitset>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <unordered_map>
#include <tuple>
#include <cmath>
#include <functional>
#include <algorithm>
#include <set>
#include <deque>
#include <cassert>
#include <numeric>
using namespace std;
#define int long long
#define kekek ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int cntzero(int num) {
    int ans = 0;
    while (num != 0) {
        if (num % 2) ans++;
        num /= 2;
    }
    return ans;
}

signed main() {
    kekek;
    int n, p;
    cin >> n >> p;
    //clock() < 1.95 * CLOCKS_PER_SEC
    for (int i = 1; clock() < 1.95 * CLOCKS_PER_SEC; i++) {
        int newn = n - p * i;
        if (newn < i) continue;
        if (cntzero(newn) <= i) {
            cout << i;
            //system("pause");
            return 0;
        }
    }

    cout << -1;

    //system("pause");
    return 0;
}

#include <bits/stdc++.h>
#define N 1000005
#define ll long long int
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int,int>
#define lli pair<ll,ll>
#define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
#define fast2 freopen ("badhair.gir","r",stdin);freopen ("badhair.cik","w",stdout);
#define mod 1000000007
#define fs(x,y) for(ll i=1;i<=y;i++) cin>>x[i]
#define fo(i,x,y) for(ll i=x;i<=y;i++)
#define INF 1000000000005
using namespace std;

ll n,m,ar[N],sum,t,pw[65];

ll check(ll n,ll k)
{
	ll tut=0,tutmac=0;
	for(int i=0;i<60;i++)
	{
		if(pw[i]&n)
		{
			tut++;
			tutmac+=pw[i];
		}
	}
	if(tut>k || tutmac<k)
		return 0;
	// cout<<n<<sp<<k<<endl;
	// cout<<tut<<s

	return k;
}

int main()
{
	fast;
	cin>>n>>m;
	ll flag=INT_MAX;
	pw[0]=1;
	fo(i,1,60)
	pw[i]=pw[i-1]*2;
	fo(i,1,60)
	{
		if(n<=m*i)
			break;
		ll a=check(n-m*i,i);
		if(a>=1)
			flag=min(a,flag);

	}
	if(flag==INT_MAX)
		flag=-1;
	cout<<flag;

}

/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/

#include<cstdio>
#include<iostream>
#include<map>

using namespace std;

typedef long long ll;

ll fun(ll k,int i)
{
    ll c=0;
    ll zui=0, j=1;
    while(k)
    {
        if(k&1)
        {
            c++;
            zui=zui+j;
        }
        j=j*2;
        k=k/2;
    }
    if(c<i&&zui>=i)
    {
        c=i;
    }
    return c;
}

int main()
{
    ll n, p, z=-1;
    cin >> n >> p;
    for(int i=1;i<=1000;i++)
    {
        ll k=n-p*i;
        if(k>=0&&fun(k,i)==i)
        {
            z=i;
            break;
        }
    }
    cout << z << endl;
    return 0;
}

#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pa pair<int,int>
#define ll long long
#define inf 1000000000
#define mod 1000000007
using namespace std;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n, p;
int bin[32] = {0};
bool check(int x)
{
    int m = n - x * p;
    int res = 0;
    if(m < x)return 0;
    for(int i = 0; i <= 31; i++)
        if(m & bin[i])
        {
            x--;
            if(x < 0)return 0;
        }
    return 1;
}
int main()
{
    bin[0] = 1;
    for(int i = 1; i <= 31; i++)
        bin[i] = bin[i - 1] << 1;
    n = read(); p = read();
    for(int i = 1; i <= 100; i++)
        if(check(i))
        {
            cout << i << endl;
            return 0;
        }
    puts("-1");
    return 0;
}

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <map>

#ifdef _MSC_VER
#define getchar_unlocked _getchar_nolock
#endif

using std::cout;
using std::endl;
using std::cin;
using std::vector;
using std::string;
using std::set;
using std::pair;
using std::stack;
using std::queue;
using std::map;

#define ll long long
#define forall(m_it, m_con) for (auto m_it = m_con.begin(); m_it < m_con.end(); m_it++)

// ===== // ===== // ===== //
ll n, p;
// ===== // ===== // ===== //
int read_int();
// ===== // ===== // ===== //
// ===== // ===== // ===== //
void start();
// ===== // ===== // ===== //
vector<int> mask(32);
// ===== // ===== // ===== //

int main()
{
	start();
	ll a;
	int numb;
	if (p + 1 > n)
	{
		cout << -1;
		return 0;
	}
	for (int i = 1; i < 100000; i++)
	{
		a = n - p * i;
		numb = 0;
		for (int j = 0; j < 32; j++)
			if (mask[j] & a)
				numb++;
		if (numb <= i && i <= a)
		{
			cout << i;
			return 0;
		}
	}
	cout << -1;
	return 0;
}

// ===== // ===== // ===== //

void start()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	mask[0] = 1;
	for (int i = 1; i < 32; i++)
		mask[i] = mask[i - 1] << 1;
	cin >> n >> p;
}

// ===== // ===== // ===== //
int read_int()
{
	char c;
	int ans = 0;
	c = _getchar_nolock();
	while (true)
	{
		if (c == '\n')
			return ans;
		if (c == ' ')
			return ans;
		ans *= 10;
		ans += c - '0';
		c = _getchar_nolock();
	}
}
// ===== // ===== // ===== //

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
using ll = long long;
using i2 = pair<int, int>;
using vi = vector<int>;

const int inf = 1e9;

int bits_on(int n) {
    int res = 0;
    while(n != 0) {
        if(n & 1) res++;
        n >>= 1;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, p; cin >> n >> p;
    int res = inf;
    for(int k = 1; k <= 30; k++) {
        if(n-p*k >= k && bits_on(n-p*k) <= k){
            res = min(res, k);break;}
    }
    cout << (res == inf ? -1:res);

    return 0;
}

# include <bits/stdc++.h>
# define X first
# define Y second
# define mem(a,b) memset(a,b,sizeof(a))
# define lb(x) (x & -x)
# define debug freopen("r.txt","r",stdin)
# define pi pair<int,int>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll n,p;
int fun(){
    for(int i = 0;i < 54 && n - i * p >= 0;i++){
        ll t = n - p * i;
        int j = 0;
        for(;t;t -= lb(t),j++);
        if(n - p * i >= i && j <= i) return i;
    }
    return -1;
}
int main(){
    scanf("%lld %lld",&n,&p);
    int ans = fun();
    printf("%d\n",ans);
}

// Author -- Frame

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<iostream>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG printf("Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
const double eps=1e-10;

uint seed=19260817;
const uint _RAND_MAX_=4294967295u;
Finline uint Rand(){return seed=seed*998244353u+1000000007u;}

template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(a>b)&&(a=b);}
template <typename _Tp>Finline bool _cmp(const _Tp &a,const _Tp &b){return abs(a-b)<=eps;}
template <typename _Tp>Finline void read(_Tp& x)
{
    register char ch(getchar());
    bool f(false);
    while(ch<48||ch>57) f|=ch==45,ch=getchar();
    x=ch&15,ch=getchar();
    while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
    if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
    read(t);read(args...);
}
Finline int read_str(char *s)
{
    register char ch(getchar());
    while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
    register char *tar=s;
    *tar=ch,ch=getchar();
    while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
    return tar-s+1;
}

const int N=100005;

int main()
{
	ll n,p;
	read(n,p);
	if(p>=n)
	{
		printf("-1\n");
		return 0;
	}
	ll cnt=0;
	while(true)
	{
		ll now=n-cnt*p;
		if(now<=0) break;
		ll num=0,maxx=0;
		for(ll i=0;i<=32;++i)
		{
			if(now&(1ll<<i))
			{
				++num;
				maxx+=i+1;
			}
		}
		if(num<=cnt&&maxx>=cnt)
		{
			printf("%lld\n",cnt);
			return 0;
		}
		++cnt;
	}
	printf("-1\n");
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define print {cout<<i;return 0;}
const int maxn=31;
inline int read(){
	int a=1,b=0;char t=getchar();
	while(t<'0'||t>'9') a=(t=='-'?-1:1),t=getchar();
	while(t>='0'&&t<='9') b=b*10+t-'0',t=getchar();
	return a*b;
}
signed main(){
	int n=read(),p=read();
	for(int i=1;i<=n-p*i;++i){
		bitset<maxn>bit(n-p*i);
		if(bit.count()<=i) print
	}
	cout<<-1;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long int
// #define lli long long int
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define endl '\n'
#define all(x) x.begin(),x.end()
#define f(i,j,l) for(i=j;i<l;i++)

// int const mod = 1e9+7;
int power(int a,int b){
    int res = 1;
    while(b){
        if(b&1){
            res = res*a;
            // res %= mod;
        }
        a=a*a;
        // a%=mod;
        b=b/2;
    }
    return res;
}

// int inverse_mod(int x){
//     return power(x,mod-2);
// }

int const N = 205;

void solve(){
    int n,p,i,j,l;
    cin>>n>>p;
    int ans = -1;
    for(i=1;i<=32;i++){
        int x = n - p*i;

        int cnt = 0;
        int y = 0;
        // cout<<x<<" ";
        y = x;
        while(x > 0){
            // y=y*2LL;
            if(x&1LL) cnt++;
            x = (x>>1LL);
        }
        // cout<<cnt<<" "<<y<<endl;
        if(i>=cnt && i<=y) {ans = i;break;}
    }
    cout<<ans<<endl;
}

int32_t main(){
    IOS;
    int t;
    t = 1;
    // cin>>t;
    // scanf("%d",&t);
    for(int tt=1;tt<=t;tt++){
        // cout<<"Case #"<<tt<<": ";
        solve();
    }
    return 0;
}

/**
*  author: rits1272
*  created: Oct 23, 2020
**/
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define print(x) cout << "[" << #x << ":" << x << "] ";
#define println(x) cout << "[" << #x << ":" << x << "]\n";
#define pb push_back
const int MOD = 1e9 + 7;
const int INF = 1e18;

pair<int, int> summands(int x){
    int cnt = 0, total = 0, i = 0;
    while(x > 0){
	if(x&1){
	    cnt++;
	    total += max(1ll, i);
	}
	x/=2;
	i++;
    }
    return {cnt, total};
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, p;
    cin >> n >> p;
    //println(summands(n))
    for(int i = 1; i <= 1e5; i++){
	int m = n - i*p;
	if(m > 0){
	    pair<int, int> p = summands(m);
	    int terms = p.first;
	    int total_terms = p.second;
	    //print(m) print(i) print(terms) println(total_terms)
	    if(terms > i)
		continue;
	    else if(total_terms >= i){
		cout << i << '\n';
		return 0;
	    }
	}
    }
    cout << -1 << '\n';
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long

int32_t main(){
  	ios_base::sync_with_stdio (false);
  	int n, m;
  	cin >> n >> m;
  	for(int i=1; (n-=m)>0 && i<=n; i++)
  		if(__builtin_popcount(n)<=i)
  			return cout<<i, 0;
   	return cout<<-1, 0;
}

#include <bits/stdc++.h>
using namespace std;

bool check(int val,int k){
    if(val<=0){
        return false;
    }
    else if(val==1&&k>1){
        return false;
    }
    else{
        int count = 0;
        vector<int> bin;
        while(val>0){
            if(val%2==1)
                count++;
            bin.push_back(val%2);
            val=val/2;
        }
        //cout<<count<<" "<<k<<endl;
        if(count>k)
            return false;
        else{
            for(int i = 2;i<bin.size();i++){
                count = count+pow(2,i-1);
                //cout<<count<<" ";

            }
            //cout<<endl;
            if(count>=k){
                return true;

            }
            else{
                return false;
            }

        }
    }
}

int main(){
    int n,p;
    cin>>n>>p;
    bool flag = true;
    for(int i = 1;i<=31;i++){
        int val = n-(i*p);
        if(check(val,i)==true){
            cout<<i;
            flag = false;
            break;
        }

    }
    if(flag)
        cout<<-1;

}

#include <bits/stdc++.h>
using namespace std;
#define M 100005
typedef long long LL;
typedef pair<int,int> Node;
#define fi first
#define se second
template<class T>bool tomax(T &x,T y){
	if(x<y)return x=y,1;
	return 0;
}
template<class T>bool tomin(T &x,T y){
	if(x>y)return x=y,1;
	return 0;
}
int Cnt(LL x){
	int cnt=0;
	for(;x;x>>=1)cnt+=x&1;
	return cnt;
}
int main(){
	int n,p;
	cin>>n>>p;
	if(p>0&&p+1>n)return puts("-1"),0;
	for(int i=1;;i++){
		LL res=-1LL*i*p+n;
		if(res<0)break;
		if(Cnt(res)<=i&&i<=res)
			return cout<<i,0;
		if(i>50)break;
	}
	puts("-1");
	return 0;
}

/*input
1 1

*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define mp make_pair
#define loop(i,l,r) for(signed i=(l); i<=(r); i++)
#define rloop(i,l,r) for(signed i=(l); i>=(r); i--)
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define int long long
#define bit(x,y) ((x>>y)&1LL)
#define sz(x) ((signed)x.size())
void Max(int &x, int y) {x = max(x, y);}
void Min(int &x, int y) {x = min(x, y);}
//const int N=;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
	return os << '(' << a.first << ", " << a.second << ')';
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) {
	os << '[';
	for (unsigned int i = 0; i < a.size(); i++)
		os << a[i] << (i < a.size() - 1 ? ", " : "");
	os << ']';
	return os;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int n, p;
	cin >> n >> p;
	for (int i = 1; i <= 100; i++) {
		int nx = n - p * i;
		if (nx <= 0) {
			cout << -1 << endl;
			exit(0);
		}
		if (__builtin_popcount(nx) <= i && i <= nx) {
			cout << i << endl;
			exit(0);
		}
	}
	cout << -1 << endl;
	exit(0);
}

#include<bits/stdc++.h>
using namespace std;
int n,p,ans=1e9;
bool ask(int num)
{
	int m=n-num*p;
	int rem=m,cnt=0;
	if(m<0)return false;
	while(m)
	{
		cnt+=m%2;
		m/=2;
	}
	return (num>=cnt&&rem>=num);
}
int main()
{
	cin>>n>>p;
	for(int i=0;i<32;i++)
	{
		if(ask(i))
		{
			ans=i;
			break;
		}
	}
	if(ans==1e9)
	{
		cout<<"-1\n";
	}
	else
		cout<<ans<<endl;
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <cstdint>
#define x first
#define y second

using namespace std;
typedef long long ll;

int get_count(int64_t n) {
    int64_t res = 0;
    while (n > 0) {
        res += n % 2;
        n /= 2;
    }
    return res;
}

int main() {
    int64_t n, p;
    cin >> n >> p;
    for (int64_t k = 1; k <= 32; ++k) {
        int64_t v = n - k * p;
        if (v <= 0) {
            break;
        }
        if (get_count(v) <= k && k <= v) {
            cout << k << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(){
	ios::sync_with_stdio(0);
	cout << setprecision(10) << fixed;
	long long n, p;
	cin >> n >> p;
	for (int i = 1; i <= 31; ++i){
		long long tempn = n - 1ll * i * p;
		int cnt = __builtin_popcount(tempn);
		if (cnt == i && tempn > 0){
			cout << i;
			return 0;
		}
		else if (cnt < i && tempn > 0){
			int j = 64 - __builtin_clzll(tempn) - 1;
			while (cnt < i && j > 0){
				if (!((cnt >> j) & 1)) cnt++;
				j--;
			}
			if (cnt == i){
				cout << i;
				return 0;
			}
		}
	}
	cout << -1;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ld long double

#define int int64_t

typedef pair<int, int> II;
typedef pair<int, II> III;

const int INF = (1 << 30) - 1;
const long long oo = (1ll << 62) - 1;
const long double PI = 3.1415926535898;
///BEGIN CODE

int n, p;
II a[1000];

int32_t main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> p;
    for(int i = 1; i <= 1000000; i++)
    {
        int x = n - p * i;
        if(x < 0) break;
        int cntbit = __builtin_popcount(x);
        int cnt = 0;
        for(int mask = 0; mask <= 30; mask++)
        {
            if((x >> mask) & 1) cnt += (1 << mask);
        }
        if(cnt >= i && cntbit <= i)
        {
            cout << i;
            return 0;
        }
    }
    cout << -1;

}

/**
    Code by: Best_Nakroth 12TH.

*/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#define LL long long
#define re register
#define li inline
#define LB(x) x&(-x)

using namespace std;
li int read(){
	re int f=1,s=0;re char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c<='9'&&c>='0')s=s*10+c-48,c=getchar();
	return f*s;
}
int n,p,ans,flag;

li int lowbit(int x){
    re int num=0;
    for(;x;x-=LB(x))num++;
    return num;
}

int main(){
	n=read(),p=read();
    for(re int i=1;;++i){
        if(n-i*p<i)break;
        if(lowbit(n-i*p)>i)continue;
        ans=i;
        flag=1;
        break;
    }
    if(flag)printf("%d",ans);
    else printf("-1");
	return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
#include<set>
#include<map>

typedef unsigned long long ll;

using namespace std;

struct Point {
	int x, y, k;
};

double pi = 3.14159265358979;

double RAD(double x1, double y1, double x2, double y2) {
	if (!(sqrt(x1*x1 + y1 * y1)*sqrt(x2*x2 + y2 * y2)))
	{
		return  pi / 2;
		return 0;
	}
	double r = acos((x1*x2 + y1 * y2) / (sqrt(x1*x1 + y1 * y1)*sqrt(x2*x2 + y2 * y2)));
	return double(abs(r) + (r < 0)*(pi / 2));
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, p;
	cin >> n >> p;
	int minn = -1;
	int k = 0;
	for (int i = 1; i <= 1000; i++)
	{
		if (n - p * i<i)
		{
			cout << minn;
			return 0;
		}
		int n1 = n - i * p;
		if (n1 == 1 && i!=1)
		{
			cout << minn;
			return 0;
		}
		while (n1)
		{
			//cout << n1 % 2;
			k += n1 % 2;
			n1 /= 2;
		}
		//cout << ' ' << k << '\n';
		if (k <= i && (minn == -1 || minn>i))
		{
			minn = i;
		}
		k = 0;
	}
	cout << minn;
	return 0;
}

#include<cstdio>
int n,p;
int main(){
	scanf("%d %d",&n,&p);
	for(int i=0;i<=n-p*i;++i)	if(i>=__builtin_popcount(n-p*i))	return printf("%d",i)&0;
	return 0&puts("-1");
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll get(ll x)
{
    ll cnt=0;
    while(x)
    {
        cnt+=x%2;
        x=x/2;
    }
    return cnt;
}

int main()
{
    ll n,k;
    scanf("%lld%lld",&n,&k);
    ll sign=0,g=1;
    while(sign!=1){
        ll m=n-g*k;
        if(m<=0){
            cout<<-1<<endl;
            break;
        }
        ll v=get(m);//1µÄ¸öÊý
        if(get(m)<=g&&m>=g){
            cout<<g<<endl;
            break;
        }
        g++;
    }
    return 0;
}

#include<bits/stdc++.h>
#define ll       long long
#define ld       long double
#define hell     1000000007
#define endl     '\n'
using namespace std;

ll get_binary(ll x)
{
	ll p=x,cnt=0;
	while(p)
	{
		if(p%2==1)
			cnt++;
		p=p/2;
	}
	return cnt;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen ("input.txt","r",stdin);
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		ll n,p,i,k,flag=0;
		cin>>n>>p;
		for(k=0;k<=31;k++)
		{
			ll p1=n-(k*p);
			if(get_binary(p1)<=k and p1>=k)
			{
				cout<<k;
				return 0;
			}
		}
		cout<<-1;
	}
	return 0;
}

#include <bits/stdc++.h>
#define int long long int
using namespace std;
int fun(int n,int x){int c=0;int y=0,j=0;
	while(n){
		if(n%2==1){c++;y+=pow(2,j);}
		n/=2;j++;
	}//cout<<c<<"cc";
	//cout<<c<<x;
	if(c>x)return 0;
	else if(y<x)return 0;
	else return 1;
	return c;
}
main() {
	// your code goes here
	int n,p;cin>>n>>p;int ans=-1;
	if(n==1 && p==1){cout<<-1;return 0;}
	for(int i=1;i<=32;i++){//cout<<n-p*i<<" ";
		if(n-p*i<0)break;
		int k=fun(n-i*p,i);
		if(k==1){ans=i;break;}
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>

#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const int maxN = 1e5 + 10, maxL = 60;

int get(ll n) {
	int res = 0;
	for (int i=0; i<maxL; i++)
		if(n & (1LL << i)) res ++;
	return res;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, p; cin >> n >> p;

	for (int i=1; i<60; i++) {
		ll new_n = n - 1LL * i * p;
		if(new_n < i) continue ;
		if(get(new_n) <= i) return cout << i << endl, 0;
	}

	cout << -1 << endl;

}

#include <iostream>
using namespace std;
int main(void)
{
	long long n, p;
	cin >> n >> p;
	long long val = 1;
	while (n - val * p > 0)
	{
		long long cnt = 0, tmp = n - val * p;
		while (tmp)
		{
			cnt = tmp & 1 ? cnt + 1 : cnt;
			tmp >>= 1;
		}
		if (cnt <= val)
			break;
		val++;
	}
	if (n - val * p <= 0)
		cout << -1 << endl;
	else
		cout << val << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int bit_no(int n)
{
    int count=0;
    while(n)
    {
        n&=(n-1);
        count++;
    }
    return count;

}
int main()
{
    int n,p;
    cin>>n>>p;
    int count=0;
    while(n>0)
    {

        if(bit_no(n)<=count)
        {
            cout<<count<<endl;
            return 0;
        }
        n-=p;
        count++;

    }
    cout<<-1<<endl;

}

#include <bits/stdc++.h>
#define s second
#define f first
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

typedef double ld;
typedef long long ll;
typedef pair<int,int> pii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

ll N,P;

string bin(ll x){

	string ans = "";

	if(x >= 0){
		while(x){
			ans += ((x & 1) + '0');
			x /= 2;
		}
		reverse(all(ans));
		return ans;
	}
	else{
		x *= -1;
		while(x){
			ans += (!(x & 1) + '0');
			x /= 2;
		}
		reverse(all(ans));

		int carry = 1;
		for(int i = ans.size() - 1; i >= 0; i--){
			int v = (ans[i] - '0' + carry);
			if(v == 2) ans[i] = 0, carry = 1;
			else ans[i] = '1', carry = 0;
		}
		if(carry){
			reverse(all(ans));
			ans += '1';
			reverse(all(ans));
		}

	}

	return ans;
}

int main(){ _

	cin >> N >> P;

	ll first = 1 + P;

	if(N < first) cout << -1 << endl;
	else{
		string s = bin(N+P);
		int ans = 0;
		for(auto x : s) ans += (x == '1');
		cout << ans << endl;
	}

	return 0;
}

// Problem : C. p-binary
// Contest : Codeforces - Technocup 2020 - Elimination Round 2
// URL : https://codeforces.com/problemset/problem/1225/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;

#define ff first
#define fs second
#define lb lower_bound
#define ub upper_bound

#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector< vi >
#define vpii vector< pii >
#define vb vector<bool>
#define si set<int>
#define mii map<int, int>
#define mci map<char, int>
#define md 1000000007
#define pb push_back
#define eb emplace_back
#define fio ios_base::sync_with_stdio (false); cin.tie (NULL);  cout.tie (NULL);
#define qw1(x) cerr<<#x<<": "<<x<<" "<<endl
#define qw2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define qw3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define qw4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl

#define f(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define rl(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define all(x) x.begin(),x.end()
#define endl '\n'
void printv(vector<int>  v){ for(int i=0;i<v.size();i++) cout<<v[i]<<' '; cout<<endl;   }
int bem(int m , int n, int mod) { int res=1; while(n){
	     			        if(n%2!=0) { res*=m; res%=mod; }
	    			        m*=m;   m%=mod;   n/=2;
			                          }
				return res;	}
int be(int a, int b) { int res=1; 	while(b){
				if(b%2!=0) res*=a;
				a*=a; b/=2;
				}
			return res;	}
void sieve(int n, vb &prime){
    prime[0]=false;     prime[1]=false;
    for(int i=2;i*i<=n;i++){
         if(prime[i]==true){
              for(int j=i*i;j<=n;j+=i)    prime[j]=false;
             	  }
    	 }
	}
int ns(int x)
{
	int cnt=0;
	while(x){ x= x&(x-1); cnt++; }
	return cnt;
}
void solve()
  {
int n, p; cin>>n>>p;
if(p==0) { cout<<ns(n); return; }
f(i, 1, 35)
{
	// qw2(i, ns(n-(i*p)));
	if(ns(n-(i*p))<=i && ns(n-(i*p))>1 )
	 { cout<<i<<endl; return;}
}
cout<<-1;

  }

int32_t main ()
{
//fio
int t=1;
//cin>>t;
while(t--){
	solve();
}

}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i = (a); i < (b); i++)
#define iter(it,c) for(auto it = (c).begin(); it != (c).end();++it)
#define pb push_back
#define fs first
#define sc second
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

const ll INF = ~(1<<31);
const double pi = acos(-1);
const double EPS = 1e-9;
typedef unsigned long long ull;

template <class T> T smod(T a, T b) {
  return (a % b + b) % b; }

int main() {
    cin.sync_with_stdio(false);
    ll n,p;
    cin >> n >> p;
    if(n == 1 && p == 1) {
        cout << -1 << endl;
        return 0;
    }
    ll val = abs(n + p);
    ll tts = 0;
    for(ll i = 0; i < 40; i++) {
        if(val & (1ll<<i)) tts++;
    }
    cout << tts << endl;
    return 0;
}

#include <bits/stdc++.h>
typedef long long ll ;
using namespace std;

int n , p ;

bool check(int mid){

     int m = n - mid * p ;

     if(m == 0) return false ;
     int cnt = 0 ;

     for(int i = 50 ; i >= 0 ; i--){
         ll bit = (1LL << i);
         if(m >= bit) m -= bit , cnt++ ;
     }

     return cnt <= mid ;
}

int main() {

    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    cin >> n >> p ;

    int low = 1 , high = n , res = -1 ;

    while(low <= high){
          int mid = (low + high)/2;
          if(check(mid))
              res = mid , high = mid - 1;
          else
             low = mid + 1;
    }

    cout << res << endl ;

    return 0;
}

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
using namespace std;
#define int long long
void solve(){
  int n,p;
  cin >> n >> p;
  for(int i = 1;i <= 1007;i++){
    int cur = n - p * i;
    int cnt = 0;
    if(cur <= 0){
      continue;
    }
    for(int j = 0;j <= 32;j++){
      if(cur & (1ll << j)){
        cnt++;
      }
    }
    if(i == cnt || (((i - cnt) % 2 ) && (cnt <= i))){
      cout << i;
      return;
    }
  }
  cout << -1;
}
signed main(){
  solve();
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n,t,m,k,d,s;
int main()
{
	scanf("%d %d",&n,&m);
	s=0;
	if(m==n)
	cout<<-1;
	else if(m==0)
	{
		while(n)
		{
	        n=n&(n-1);
	        s++;
		}
		printf("%d",s);
	}
	else
	{
		for(int i=1;i<=10000;i++)
		{
			int k=n-i*m;
			int p=log(k);
			s=0;
			while(k)
			{
		        k=k&(k-1);
		        s++;
		 	}
			if(s==i)
			{
				printf("%d",s);
				return 0;
			}
			else
			{
				int u=1;
				for(int j=1;j<=p;j++)
				{
					s+=u;
					u=u*2;
					if(s>=i)
					{
						printf("%d",i);
						return 0;
					}
				}
			}
		}
		printf("-1");
	}
}

#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
#define pii pair < int, int >
#define TIME 1.0*clock() / CLOCKS_PER_SEC

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree <T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

mt19937 gen(chrono::system_clock::now().time_since_epoch().count());

const int N = 2e5 + 7;
const ll M = 1e9 + 7;
const ll M2 = 1e9 + 9;
const int mod = 998244353;
const int rx[8] = {1, 1, -1, -1, -2, -2, 2, 2};
const int ry[8] = {-2, 2, -2, 2, 1, -1, 1, -1};
const ld eps = 1e-7;
const double pi = acos(-1.0);
const int SX[4] = {0, 0, -1, 1};
const int SY[4] = {1, -1, 0, 0};

#define int ll

int mem(ll x) {
    if (x <= 0) return 1e9;
    int kol = 0;
    for (int i = 32; i > -1; i--) {
        if ((1<<i) & x) kol++;
    }
    return kol;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("cheval.in", "r", stdin);
//    freopen("cheval.out", "w", stdout);
#endif
    int n , p;
    cin >> n >> p;
    for (int z = 1; z <= 10000000; z++) {
        if (mem(n - z  * p) <= z) {
            cout << z << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

int foo(int x){
    int ret = 0;
    int tmp = 0;
    while(x){
        tmp++;
        ret+=(x%2);
        x/=2;
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,p;
    cin >> n >> p;
    bool flag = false;
    for(int i=1;i<=32;i++){
       if(n-(i*p)>0 && foo(n-(i*p)) <= i){
            if(n-(i*p)==1){
                if(i==1){
                    cout << i << '\n';
                    flag = true;
                    break;
                }
                continue;
            }
            cout << i << '\n';
            flag = true;
            break;
        }
    }
    if(!flag)cout << -1 << '\n';
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
 ll count_one (ll n)
        { ll count=0;
            while( n )
            {
            n = n&(n-1);
               count++;
            }
            return count;
    }
int main()
{
    ll n,p;cin>>n>>p;ll k=0;

   for(ll k=1;k<=32;k++)
   {
        if(n-k*p<1){
        cout<<"-1";return 0;
    }
      if( count_one(n-k*p)<k)
      {
          cout<<k;return 0;
      }
   }

}

#include <bits/stdc++.h>
using namespace std;
int cnt(int n){
    if(n<0){
        return 0x3f3f3f3f;
    }
    int ret = 0;
    while(n){
        if(n&1){
            ret++;
        }
        n>>=1;
    }
    return ret;
}
int main(){
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i = 0;i<=32;i++){
        //cout<<"i == "<<i<<"  "<<n - i*p<<"  "<<cnt(n - i*p)<<endl;
        if(cnt(n - i*p)<=i && !(cnt(n - i*p) == 0&&i != 0)){
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define bug cout<<"HY"<<endl
#define boost ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL)
#define resize(A) A.resize(unique(A.begin(), A.end()) - A.begin())
typedef pair<int,int> P;
P csb(int a)
{
    int cnt=0,c=0;
    while(a!=0)
    {
        if(a&1)
        cnt=cnt+pow(2,c);
        c++;
        a=a&(a-1);
    }
    P p1;
    p1.fi=c;
    p1.se=cnt;
    return p1;
}
int32_t main()
{
   boost;
   int n,p,i,num=10000000;
   cin>>n>>p;
   for(i=1;i<num;i++)
   {
       int a=n-i*p;
       if(a<=0)
       {cout<<-1<<endl; break;}
       P p=csb(a);
       //cout<<c<<endl;
       if(p.se>=i && p.fi<=i)
       {cout<<i<<endl;break;}
       //cout<<i<<endl;
   }
   if(i==num)
    cout<<-1<<endl;
}

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define MOD 1000000007
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
#define nl "\n"
#define INF 1e18
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define mod 998244353
#define fl(a,x) memset(a,x,sizeof(a));
#define pt(a,x) cout << get<x>(a) << " " ;
#define mt make_tuple

#define int long long

typedef vector<int> vii;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<vector<int> > vv;

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

void solve() {
	int n,p; cin >> n >> p;

	int i = 1 ;

	while(n - i*p >0 and i < 1e6 ) {
		if(__builtin_popcountll(n-i*p)<=i) {
			if(n-i*p == 1  and n-i*p < i ) { ++i ;continue ; }
			cout << i ; return ;
		}
		++i ;
	}

	cout << -1 ;
}

int32_t main() {

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    fast;
	int t=1;
	// cin>>t;
	rep(i,0,t) {
		solve() ;
	}

	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v;
int main()
{
    ll n,p;
    cin>>n>>p;
    ll ans=0;
    if(p==0)
        ans=__builtin_popcount(n);
    else if(p>=n)
        ans=-1;
    else
    {
        ll c=0;
        while(1)
        {
            c++;
            ll val=n-c*p;
            if(__builtin_popcount(val)<=c)
            {
                //cout<<val<<' '<<c<<endl;
                ans=c;
                break;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define int                 long long int
#define all(c)              c.begin(), c.end()
#define init(arr, num)      memset(arr, num, sizeof(arr))
#define fast                ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inputFile           freopen("in5.txt", "r", stdin);
#define outputFile          freopen("output.txt", "w", stdout);
#define modM                1000000007

#ifdef Voneone
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>  void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl;}
template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args) {
		const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | ";
		__f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{

	int n, p;
	cin >> n >> p;

	if(p == 0){
		cout << __builtin_popcountll(n);
		return;
	}

	for(int i = 1; i < 33; i++){

		int new_num = n - i * p;

		trace(new_num, i);
		if(new_num <= 0)
			break;

		if(__builtin_popcountll(new_num) <= i){
			cout << i;
			return;
		}

	}

	cout << -1;

}

signed main()
{

	#ifdef _DEBUG
		inputFile;
	#endif
	fast;
	int T = 1;
	// cin >> T;
	while(T--)
	{
		solve();
		cout << endl;
	}

	return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    long long n, p;
    cin >> n >> p;

    long long l = 0, h = 1e15, ans = -1;
    while (l <= h) {
        long long m = (l + h) / 2, t = n - m * p;
        if (t <= 0 || m > t) {
            h = m - 1;
        } else {
            long long set = 0, copy = t;
            while (copy) {
                if (copy % 2)
                    set++;
                copy /= 2;
            }

            if (m >= set)
                ans = m, h = m - 1;
            else
                l = m + 1;
        }
    }

    cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, p, cnt, tot, t;
bool solve(ll x, ll num) {
    ll ans = 0, ans1 = 0;
    //printf("%d %d\n", x, num);
    ll c = 1;
    while(x) {
        if(x % 2 == 1) ans ++, ans1 += c;
        x /= 2;
        c *= 2;
    }
    if(ans <= num && ans1 >= num) return true;
    else return false;
}
int main(){
    scanf("%I64d%I64d", &n, &p);
    t = sqrt(double(n));
    for(ll i = 1; i <= t; i++) {
        //printf("i = %d\n", i);
        if(n - p * i <= 0) break;
        if(solve(n - p * i, i)) {
            printf("%I64d\n", i);
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}

#include <bits/stdc++.h>
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define emdl "\n"
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
// bool sortBy(const pair<int,pair<int,int>> &a,const pair<int,pair<int,int>> &b){
// 	if(a.S.F==b.S.F)
// 		return a.F<b.F;
// 	return a.S.F<b.S.F;
// }
// void reverseStr(string& str)
// {
//     int n = str.length();
//     for (int i = 0; i < n / 2; i++)
//         swap(str[i], str[n - i - 1]);
// }
// int binarySearch(int arr[], int l, int r, int x)
// {
//     if (r >= l) {
//         int mid = l + (r - l) / 2;

//         if (arr[mid] == x)
//             return mid;
//         if (arr[mid] > x)
//             return binarySearch(arr, l, mid - 1, x);

//         return binarySearch(arr, mid + 1, r, x);
//     }
//     return l;
// }
// vector<vector<int>> mat;
// vector<set<int>> sa;
// vector<int> vis,dis;
// void bfs(int u){
// 	queue<int> q;
// 	int v;
// 	vis[u]=1;
// 	q.push(u);
// 	while(!q.empty()){
// 		v=q.front();
// 		q.pop();
// 		for(auto i:mat[v]){
// 			if(!vis[i]){
// 				vis[i]=1;
// 				dis[i]=dis[v]+1;
// 				q.push(i);
// 			}
// 		}
// 	}
// }
// #define MAXN 10000001
// int pf[MAXN];
// void sieve(){
// 	long long int i,j;
//     pf[1] = 1;
//     for (i=2; i<MAXN; i++)
//     pf[i] = i;
//     for (i=2; i*i<MAXN; i++){
//         if (pf[i] == i){
//             for (j=i*i; j<MAXN; j+=i)
//                 if (pf[j]==j)
//                     pf[j] = i;
//         }
//     }
// }
// vector<int> factors(int x){
//     vector<int> ret;
//     while (x != 1){
// 		int fact=pf[x];
//         ret.push_back(pf[x]);
// 		while(fac!=1&&x%fact==0)
//         x/=fact;
//     }
//     return ret;
// }
void init(){

}
int poww(long long int x){
	int len=0;
	while(x!=0){
		len+=x%2;
		x/=2;
	}
	return len;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
	int qwe=1;
	//cin>>qwe;
	init();
	// sieve();
	while(qwe--){
		long long int n;
		cin>>n;
		int p;
		cin>>p;
		int ans=-1;
		if(n==1&&p==1){
			cout<<-1;
			return 0;
		}
		for(int i=1;i<=36;i++){
			//cout<<i<<" ";
			if(n-(i*p)<=0)
			break;
			if(n-(i*p)==1){
				if(i==1){
					ans=1;
					break;
				}else{
					continue;
				}
			}
			if(i>=poww(n-(i*p))){
				ans=i;
				break;
			}
		}
		//cout<<poww(28);
		cout<<ans;
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;

long long chk(long long m)
{
	long long cnt = 0;
	while(m)
	{
		if(m%2==1)
			cnt++;
		m /= 2;
	}
	return cnt;
}

int main()
{
	long long n,p;
	cin>>n>>p;
	long long m = n-p;
	long long cnt = 1;
	while(m>0 && cnt < 10000000)
	{
		if(chk(m)==cnt)
		{
			cout<<cnt<<endl;
			return 0;
		}
		cnt++;
		m -= p;
	}
	cout<<-1<<endl;
}

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//#define ll cpp_int
#define ll long long
#define rep(i,n)  for(ll i=0;i<(n);++i)
#define fi(a, b, c, d) for(int a = b; a < c; a += d)
#define fl(a, b, c, d) for(ll a = b; a < c; a += d)
#define fei(a, b, c, d) for(int a = b; a <= c; a += d)
#define fel(a, b, c, d) for(ll a = b; a <= c; a += d)
#define all(x)   (x).begin(),(x).end()
#define vi vector<int>
#define vll vector<ll>
#define viip vector<pair<int,int>>
#define vllp vector<pair<ll,ll>>
#define pb 	emplace_back
//pairs
#define pi pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define F first
#define S second
//using boost::multiprecision::cpp_int;
using namespace std;
bool iseven(ll n)
{
    if(n%2==0)
    return true;
    else
    {
        return false;
    }

}
ll position(ll a)
{
  //vector<ll>p(30,0);
  //unordered_set<ll>s;
  ll s=0;
  long double x=a;
  int i=0;
  while(a>0)
  {
      if((a&1)==1)
      {
         // s.insert(i);
         s++;
      }

      a=a>>1;
      i++;
  }

  return s;

}
ll power(ll x, unsigned ll y)
{
    ll res = 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res*x;

        // y must be even now
        y = y>>1; // y = y/2
        x = x*x;  // Change x to x^2
    }
    return res;
}
void print(vector<ll> a,ll n)
{
   for(int i=0;i<n;i++)
    cout<<a[i]<<" ";
    cout<<"\n";
}
bool sec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{   if(a.first==b.first)
    return (a.second > b.second);
    else
    {
        return (a.first > b.first);
    }

}
vector<ll> suffixsum(vll &a,ll n)
{
    vll pre(n,0);
  pre[n-1]=a[n-1];
 for(int i=n-2;i>=0;i--)
  {
      pre[i]=pre[i+1]+a[i];
  }
  return pre;
}
vector<ll> preffixsum(vll &a,ll n)
{
    vll pre(n,0);
  pre[0]=a[0];
 for(int i=1;i<n;i++)
  {
      pre[i]=pre[i-1]+a[i];
  }
  return pre;
}
map<pair<ll,ll>,ll>m;
ll fun(vll &a,int i,ll s)
{
    if(i>=36||s<0)
    {
        return 9999999999999999;

    }
    if(s==0)
    return 0;
    //if(m.find({i,s})==m.end())
    //{
      //  m[{i,s}]=
    //}
    //return m[{i,s}];
    return min(fun(a,i+1,s-a[i])+1,fun(a,i+1,s));
}
void solve ()
{
  ll n,p;
  cin>>n>>p;
  fl(i,1,32,1)
  {
      ll a=n-(i*p);

      if(position(a)!=0&&i>=position(a))
      {
          cout<<i<<"\n";
          return;
      }
  }

    cout<<-1<<"\n";

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t=1;
    //cin>>t;
     while(t--)
    {
        solve();
    }

return 0;
}

// g++ -std=c++14

/*

Today might be the chance to grasp the chance to let your talent bloom.
May be tomorrow, the day after, or next year...
May be even when you are 30. I'm not sure if physique has anything to do with it
but if you think that it will never come, it probably never will.

- Tooru Oikawa.

*/

#include<bits/stdc++.h>

typedef long long ll;
typedef long double lld;
using namespace std;

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define endl "\n"
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define all(c) c.begin(),c.end()
#define inf 1000000000000000001

#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)

template<typename S, typename T>
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.fi<<", "<<p.se<<')';return out;}

template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
ll l=v.size();for(ll i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}

template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}

template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

#define int ll

int32_t main(){ _
    int n, p;
    cin >> n >> p;
    for (int i = 1; i < 50; i++) {
        int temp = i * p;
        int lol = (n - temp);
        if (lol == 0) continue;
        int cnt = __builtin_popcountll(lol);
        // tr(lol, cnt, i);
        if (cnt <= i) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll a[300];
int main()
{
    a[1] = 1;
    int n;
    for (int i = 2;; ++i)
    {
        a[i] = a[i - 1] * 2;
        if (a[i] > 1e9)
        {
            n = i;
            break;
        }
    }
    ll m, p, tmp = 0;
    scanf("%lld%lld", &m, &p);
    if(m-p<=0){
        printf("-1\n");
        return 0;
    }
    for (int i = 1;; ++i)
    {
        tmp=m-i*p;
        for(int j=0;j<i;++j){
            int z=upper_bound(a+1,a+1+n,tmp)-a-1;
            tmp-=a[z];
        }
        if(tmp==0){
            printf("%lld\n", i);
            break;
        }
    }
    return 0;
}

    //Littleboy123 Template 1.6
    #include <bits/stdc++.h>
    using namespace std;

    //pragmas

    #pragma comment(linker, "/stack:200000000")
    #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
    #pragma GCC target ("avx2")
    #pragma GCC optimization ("O3")
    #pragma GCC optimization ("unroll-loops")

    typedef pair<int, int> ii;
    typedef vector<int> vi;
    typedef vector<ii> vii;
    typedef long long int ll;
    typedef unsigned long long int ull;
    typedef long double ld;

    const long double pi = acos(-1.0);

    #define FOR(a,b,c) for(ll (a)=(b);(a)<(c);++(a))
    #define FORE(a,b,c) for(ll (a)=(b);(a)<=(c);++(a))
    #define FORN(a,b,c) for(ll (a)=(b);(a)>(c);--(a))
    #define FORNE(a,b,c) for(ll (a)=(b);(a)>=(c);--(a))
    #define MEM(a,b) memset((a), (b), sizeof((a)))
    #define MAX(a,b) (a) = max((a), (b))
    #define MIN(a,b) (a) = min((a), (b))
    #define nl '\n'
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define eb emplace_back

    void init(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    }

    void infile(){
        freopen("input.in", "r", stdin);
    }

    void outfile(){
        freopen("output.out", "w", stdout);
    }

    ll modex(ll a, ll b, ll mod){
        if(b == 0)
            return 1;
        if(b == 1)
            return a;
        ll tmp = modex(a, b/2, mod);
        tmp = (tmp * tmp) % mod;
        if(b & 1)
            tmp = (tmp * a) % mod;
        return tmp;
    }

    //End of template

    ll ns[50];

    ll cari(ll num){
        ll cnt = 0;
        ll now = 49;
        while(now >= 0 && num != 0){
            cnt += num/ns[now];
            num %= ns[now];
            now--;
        }
        //cout << num << ' ' << cnt << nl;
        if(num != 0)
            return INT_MAX;
        return cnt;
    }

    int main(){
        init();

        ll n, p;
        cin >> n >> p;
        if(p >= 2 && (ll)(n/p) >= n%p && n % p != 0){
            cout << -1 << nl;
            return 0;
        }
        ll start = 1;
        FOR(i, 0, 50){
            ns[i] = start;
            start *= 2;
        }
        ll mins = INT_MAX;
        ll minim;
        FORE(i, 0, 1000){
            if(n-(i*p) <= 0)
                break;
            minim = cari(n-(i*p));
            //cout << i << " "<< minim << nl;
            if(minim <= i)
                mins = min(mins, i);
        }
        if(mins == INT_MAX)
            cout << -1 << nl;
        else
            cout << mins << nl;
        return 0;
    }

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int64_t n, p;
    cin >> n >> p;
    for (int i = 1; i < 1000000; ++i) {
        if (p * i + i >= n) {
            cout << -1 << endl;
            return 0;
        }
        int64_t k = n - (p * i);
        if (__builtin_popcount(k) > i || k < i) continue;
        cout << i << endl;
        return 0;
    }
    cout << -1 << endl;
    return 0;
}

#include<bits/stdc++.h>
typedef long long int ll;
typedef double db;
using namespace std;
#define forn(i,n) for(int i = 0; i < int(n); ++i)
int n,m,i,j;

int num_in_2pow(int num)
{
	int pow_num=0;
	while(num>0)
	{
		if(num&1) pow_num++;
		num>>=1;
	}
	return pow_num;
}

int main()
{
	int p;
	cin>>n>>p;
	int num=n;
	if(num-p <=0)
	{
		cout<<"-1";
		return 0;
	}

	i=1;
	while(i)
	{
		num-=p;

		j=num_in_2pow(num);

		if(num<=0 || (i!=1 && num==1))
		{	cout<<"-1"; return 0;}

		if(i>=j )
		{
			cout<<i;
			return 0;
		}

		i++;
	}
}

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, p;
int main()
{
    //freopen("input.txt","r",stdin);
    cin >> n >> p;
    if( n == 0 )
    {
        cout << 0;
        return 0;
    }
    for( int i = 0; i < 40; i++ )
    {
        long long y = n - p * i,  sum = 0;
        while( y )
        {
            sum += y % 2;
            y /= 2;
        }
        //cout << i << ' ' << sum << endl;
        if( i >= sum && sum > 0 )
        {
            cout << i;
            return 0;
        }
    }
    cout << -1;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

vector<long long> vec(63);

long long bs(long long val) {
    long long l = 0, r = 62, mid, ans = -1;
    while(l <= r) {
        mid = (l + r) / 2;
        if(vec[mid] <= val) {
            ans = mid;
            l = mid + 1;
        } else r = mid - 1;
    }

    return ans;
}

int main() {
    int k, p;
    cin >> k >> p;

    for(long long i = 0; i < 63; i++) {
        vec[i] = (1LL << i);
    }

    long long anss = LONG_LONG_MAX;
    for(int i = 62; i >= 0; i--) {
        long long needed = max((long long)(k + p), vec[i]), cnt = 0, current = k;
        while (current) {
            long long ans = bs(needed);
            if (ans == -1) {
                cout << -1;
                return 0;
            }
            cnt++;

            current -= vec[ans] + p;
            needed = current - p;
            if(current < 0) {
                cnt = LONG_LONG_MAX;
                break;
            }
        }
        anss = min(anss, cnt);
    }

    if(anss == LONG_LONG_MAX) cout << -1;
    else cout << anss;
}

#include <bits/stdc++.h>
using namespace std;
int cnt(int n){
    if(n<0){
        return 0x3f3f3f3f;
    }
    int ret = 0;
    while(n){
        if(n&1){
            ret++;
        }
        n>>=1;
    }
    return ret;
}
int main(){
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i = 0;i<=32;i++){
        //cout<<"i == "<<i<<"  "<<n - i*p<<"  "<<cnt(n - i*p)<<endl;
        if(cnt(n - i*p)<=i && !(cnt(n - i*p) == 0&&i != 0)){
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define quicker ios::sync_with_stdio(false);
#define first fi
#define second sec
typedef long double ld;
typedef long long ll;
const long long INF=2147483647;
#define rep(i,x,y) for (ll i=x;i<=y;i++)
#define per(i,x,y) for (ll i=x;i>=y;i--)
const ll HOLLAND_PIG=1;
ll FXT(ll p){ll s=HOLLAND_PIG-1;while(p){++s;p&=p-HOLLAND_PIG;}return s;}
ll max(ll x,ll y)
{
	if (x>y)
	{
		return x;
	}
	else
	{
		return y;
	}
}

ll min(ll x,ll y)
{
	if (x<y)
	{
		return x;
	}
	else
	{
		return y;
	}
}
int main()
{
	ll n,p;
	cin>>n>>p;
	rep(ff,HOLLAND_PIG,HOLLAND_PIG+33)
	{
		if(n-ff*p<=HOLLAND_PIG-1){break;}
		if(FXT(n-ff*p)<=ff)
		{cout<<ff<<endl;return 0;	}
	}
	cout<<"-1"<<endl;
	return 0;
}

/*input
101 50
*/
#include <bits/stdc++.h>
using namespace std;

#define int                 long long int
#define all(c)              c.begin(), c.end()
#define init(arr, num)      memset(arr, num, sizeof(arr))
#define fast                ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inputFile           freopen("in5.txt", "r", stdin);
#define outputFile          freopen("output.txt", "w", stdout);
#define modM                1000000007

#ifdef Voneone
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>  void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl;}
template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args) {
		const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | ";
		__f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void solve()
{

	int n, p;
	cin >> n >> p;

	for(int i = 1; i < 63; i++){

		int new_num = n - i * p;

		trace(new_num, i);
		if(new_num <= 1)
			continue;

		if(__builtin_popcountll(new_num) <= i){
			cout << i;
			return;
		}

	}

	cout << -1;

}

signed main()
{

	#ifdef _DEBUG
		inputFile;
	#endif
	fast;
	int T = 1;
	// cin >> T;
	while(T--)
	{
		solve();
		cout << endl;
	}

	return 0;

}

#include<bits/stdc++.h>
#define ll long long int
#define sz size
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;

ll n, p, i, k, x, z, j;
deque<ll>v;

int main()
{
    cin >> n >> p;
    for(i = 1; i <= 31; ++i)
    {
        v.clear();
        k = 0;
        x = n - i * p;
        if(x < 1)
        {
            cout << -1;
            return false;
        }
        while(x > 0)
        {
            k += (x % 2);
            v.pb(x % 2);
            x /= 2;
        }
        for(j = 0; j < v.sz(); ++j)
        {
            if(v[j] == 1)z += (v.sz() - j) ;
        }
        if(k <= i && z >= i)
        {
            cout << i;
            return false ;
        }
    }
    cout << -1;
    return false;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int count_bit(int value)
{
	int count = 0;
	while(value)
	{
		count++;
		value = value & (value - 1);
	}
	return count;
}
int main()
{
    int n,p;scanf("%d%d",&n,&p);
    if(n<=p)puts("-1");
    else {
        int x;
        if(p!=0){
           x = n/p;
        if(count_bit(n-x*p)>x) {puts("-1");return 0;}
        }
        int l=1,r=10000,m;
      while(l<=r)
      {
          m = (l+r)/2;
          ll tmp = m*p;
          ll res = n-m*p;
          if(tmp>=n) {
            r = m-1;
          }
          else {
            if(count_bit(res)>m){
                l = m+1;
            }
            else r = m-1;
          }
      }
      printf("%d\n",l);
    }

}

#include <bits/stdc++.h>
#define int long long
#define MOD 1000000007
using namespace std;
int binary_length(int x)
{
    int ans = 0;
    while(x)
    {
        x = x>>1;ans++;
    }
    return ans;
}
bool is_possible(int n, int m) // can you split some number n into m powers of 2?
{
    if(n<=0)return false;
    if(__builtin_popcount(n) > m)
    {
        return false;
    }
    else
    {
        int ans = 0;
        int val = 1;
        while(n)
        {
            ans+= (n&1)*val;
            val*=2;
            n= n>>1;
        }
        if(ans>=m)return true;
    }
    return false;
}
int32_t main()
{
    ios_base::sync_with_stdio(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int n,p;
    cin>>n>>p;
    int m = 1;
    // cout<<binary_length(24)<<'\n';
    // if()
    // cout<<__builtin_popcount(28)<<'\n'
    if(n==1)
    {
        if(p==1 || p== -1)
        {
            cout<<-1;
        }
        else
        {
            cout<<2;
        }
        return 0;
    }
    int low = 1;
    int high = 1e5;
    while(low < high)
    {
        int mid = low + (high - low)/2;
        // cout<<mid<<' ';
        if(is_possible(n-mid*p, mid))
        {
            high = mid;
        }
        else
        {
            low = mid + 1;
        }
    }
    // cout<<low<<'\n';

    if(!is_possible(n - low*p, low))
    {
        cout<<-1;
    }
    else
    {
        cout<<low;
    }
    // cout<<-1;
}

#include <iostream>

using namespace std;

int main(){
    int n, p;
    cin >> n >> p;
    if (n < p){
        cout << -1;
        return 0;
    }
    int answer = 1000000000;
    int actions = 0;
    while(n > p){
        n -= p;
        actions++;
        int number = n;
        int times = 0;
        while(number > 0){
            times += number % 2;
            number /= 2;
        }
        if (times <= actions)
            answer = min(answer, max(times, actions));
        if (actions == 10000)
            break;
    }

    if (answer != 1000000000 && answer != 0)
        cout << answer;
    else
        cout << -1;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    //freopen("inc.txt", "r", stdin);
    int n, p, ans = 0, last_n = 0;
    cin >> n >> p;
    for (int i = 1; i < 2000000; i++) {
        long long int nn = n - p * i;
        if (nn <= 0 || nn == 1 && i != 1) {
            continue;
        }
        int cnt1 = 1;
        while (nn != 1) {
            cnt1 += nn % 2;
            nn /= 2;
        }//*/
        if (nn <= i || cnt1 <= i) {
            cout << i;
            return 0;
        }
    }
    cout << -1;
}

#include<iostream>
#include<bitset>
using namespace std;
const long long INF = 1000;
long long ans,t,f,n,p;
int count(long long f) {
	long long loc=0;
	while (f > 0) if ((f & 1) == 1) { loc++; f = f >> 1; }
	else { f = f >> 1; }
	return loc;
}
bool check(long long k) {
	if (count(f-k*p)>k||count(f - k * p)<=0) {
		return 0;
	}
	else {
		if (count(f-k*p) <= k) {
			if (k < ans) ans = k;
		}
		return 1;
	}
}
int main() {
	long long  m, r, l;
	cin >> n>>p; f = n; ans = INF;
	l = 0; r = INF;
	while (r-1 > l) {
		m = (r + l) / 2; t = 0;
		if (check(m)) {
			r = m;
		}
		else {
			l = m;
		}
	}
	if (ans!=INF)
		cout << ans;
	else {
		cout << -1;
	}
}

#include <bits/stdc++.h>
using namespace std;

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define ll long long
#define ld long double
#define endl "\n"

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

bool possible(ll n, ll p, ll k) {
    ll target = n - k * p;
    if(target <= 0) {
        return false;
    }

    int cnt = __builtin_popcountll(target);

    return cnt <= k && cnt > 0;
}

void solve() {
    ll n, p;

    cin >> n >> p;

    for(int k = 1; k <= 100; k++) {
        if(possible(n, p, k)) {
            cout << k << endl;
            return;
        }
    }

    cout << -1 << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}

#include <bits/stdc++.h>
#define f first
#define s second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb emplace_back
#define mp make_pair

typedef long long ll;
typedef long double ld;

const ll INF = 1e9 + 7, mod = 998244353, MAXN = 1e5 + 2, SQ = 322;
const ld EPS = 1e-6, PI = 3.141592653589793238462643;

using namespace std;

#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
//#pragma GCC optimize("branch-target-load-optimize")
//#pragma GCC optimize("branch-target-load-optimize2")
//#pragma GCC optimize("btr-bb-exclusive")

namespace in_out {
    template <typename T1, typename T2>
    ostream& operator<<(ostream& out, pair <T1, T2> a) {
        out << a.f << " " << a.s;
        return out;
    }

    template <typename T1, typename T2>
    istream& operator>>(istream& in, pair <T1, T2> &a) {
        in >> a.f >> a.s;
        return in;
    }

    template <typename T>
    ostream& operator<<(ostream& out, vector<T> a) {
        for (auto i : a)
            out << i << " ";
        out << '\n';
        return out;
    }

    template <typename T>
    istream& operator>>(istream& in, vector<T> &a) {
        for (ll i = 0; i < a.size(); ++i)
            in >> a[i];
        return in;
    }
}

using namespace in_out;

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #ifdef DEBUG
        freopen("out.txt", "w", stdout);
        freopen("in.txt", "r", stdin);
    #endif

    ll n, p;
    cin >> n >> p;
    if (n == 1 && p == 1)
    	return cout << -1, 0;

    vector <ll> pw(40, 1);
    for (int i = 1; i < 40; i++)
    	pw[i] = 2 * pw[i - 1];

    for (int i = 0; i < 35; i++) {
    	int x = n - i * p;
    	x = abs(x);
    	int kek = 0;
    	for (int i = 35; i >= 0; i--) {
    		while (x >= pw[i]) {
    			x -= pw[i];
    			kek++;
    		}
    	}
    	if (kek <= i)
    		return cout << i, 0;
    }
    cout << -1;
   	return 0;
}

//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out=0,fh=1;
	char jp=getchar();
	while ((jp>'9'||jp<'0')&&jp!='-')
		jp=getchar();
	if (jp=='-')
		fh=-1,jp=getchar();
	while (jp>='0'&&jp<='9')
		out=out*10+jp-'0',jp=getchar();
	return out*fh;
}
#define lowbit(x) x&(-x)
int popc(int x)
{
	int s=0;
	while(x)
		++s,x-=lowbit(x);
	return s;
}
int n,p;
int solve()
{
	if(n<31*p+31)
		return -1;
	for(int k=1;n-k*p>0;++k)
		if(popc(n-k*p)<=k)
			return k;
	return -1;
}
int main()
{
	n=read(),p=read();
	cout<<solve()<<endl;
	return 0;
}

#include <bits/stdc++.h>
#include <string>
#include <cstdlib>

using namespace std;

int to_binary_one_num (int x) {
    int ans = 0;
    while (x) {
        ans = ans + x % 2;
        x = x / 2;
    }
    return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, p;
    cin >> n >> p;
    int ans = n + 1, step = 1;
    int bin = to_binary_one_num(n - step * p);
    if (p != 0) {
        while (n - step * p >= (int) pow(2, step)) {
            if (bin == step) {
                if (step < ans) {
                    ans = step;
                }
            } else if (step > bin) {
                if (step < ans) {
                    ans = step;
                }
            }
            // cout << n - step * p << " " << step << " " << bin << endl;
            step = step + 1;
            bin = to_binary_one_num(n - step * p);
        }
    } else {
        ans = to_binary_one_num(n);
    }

    if (ans == n + 1) {
        cout << -1;
    } else {
        cout << ans;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int get_min_summands(int &n, int &p){

	int x = 30;
	int cnt = 0;
	while(x>=0 && n>0){
		int k = (1<<x) + p;
		if(k<=0){
			break;
		}

		if(k<=n){
			n -= k;
			cnt++;
		}else
			x--;
	}
	if(n)
		return -1;
	return cnt;
}

int main(){
	int n, p;
	cin>>n>>p;

	int cnt = get_min_summands(n, p);
	cout<<cnt<<"\n";
}

#include <bits/stdc++.h>

#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first

using namespace std;

typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 555;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const int inf = 1e9, N = 2e5 + 48, mod = 1e9 + 7;
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;

int n, p, ans;

main () {
	cin >> n >> p;
	forn (x, 0, inf / 20) {
		int nn = n - p*x;
		if (nn > 0 && __builtin_popcount(nn) <= x) {
			cout << x << endl;
			return 0;
		}

	}
	cout << -1 << endl;
}

/*************************************************************************
    > File Name: C.cpp
    > Author: PumpkinYing
    > Created Time: 10/26/2019 7:34:47 PM
 ************************************************************************/

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;

int main() {
	ll n,p;
	scanf("%lld%lld",&n,&p);
	int ans = -1;
	for(int i = 1;i < 40;i++) {
		int mx = 0;
		int mn = 0;
		ll tmp = n-i*p;
		ll base = 1;
		while(tmp) {
			if(tmp%2) {
				mn++;
				mx += base;
			}
			base *= 2;
			tmp /= 2;
		}
		if(i >= mn && i <= mx) {
			ans = i;
			break;
		}
	}
	printf("%d\n",ans);
    return 0;
}

#include "bits/stdc++.h"
using namespace std;

////////////// Prewritten code follows. Look down for solution. ////////////////

#define fs first
#define sc second
#define pb push_back
#define int long long
#define len(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define test int _; cin >> _; while(_--)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+7;

inline ll gcd(ll a, ll b){while(b){ a %= b; swap(a, b); } return a; }
inline ll mod(ll a, ll b){return ((a%b)+b)%b;}
inline ll expo(ll b, ll p, ll m = MOD){
  ll ans = 1;
  while(p > 0){
    if(p&1LL) ans = mod(ans*b, m);
    b = mod(b*b, m);
    p >>= 1LL;
  }
  return ans;
}

////////////////////////// Solution starts below. //////////////////////////////

bool check(int n){
  if(n <= 0) return false;
  int cnt = 0;
  for(int i = 0; i < 32; i++){
    if(n & (1<<i)) cnt++;
  }
  return cnt != 0;
}

int32_t main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, p;
  cin >> n >> p;
  int best = INF;
  set<int> st;
  for(int i = 1; i < 32; i++){
    int cnt = i;
    int aux = n - (i*p);
    if(aux <= 0) continue;
    for(int j = 31; j >= 0; j--){
      int k = (1LL<<j);
      if(aux >= k){
        while(aux >= k){
          aux -= k;
          cnt--;
          if(cnt == 0) break;
        }
      }
      if(cnt == 0) break;
    }
    if(aux == 0){
      best = i;
      break;
    }
  }
  cout << (best == INF ? -1 : best) << endl;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	int64_t n, p;
	cin >> n >> p;

	int ans = (int)1e9;
	for (int i = 0; i <= 1000000; i++)
	{
		int64_t b = n - p * i;
		if (b <= 0) continue;

		int cnt = 0;
		for (int j = 0; j < 33; j++)
		{
			if (b & (1LL << j))
				cnt++;
		}

		if (cnt <= i)
		{
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
const int MX = 3e5 + 1, MOD = 1e9 + 7;
ll a[MX], b[MX], n, p;

void solve(){
    cin>>n>>p;
    bool fl = false;
    for(int i = 1; i <= 31; i++){
        long long s = n - p*i;
        if(s <= 0) break;
        int cnt = 0;
        while(s > 0){
            if(s & 1) cnt++;
            s /= 2;
        }
        if(cnt <= i){
            cout<<i<<"\n";
            fl = true;
            break;
        }
    }
    if(!fl) cout<<"-1\n";
}
int main()
{
//    ios_base::sync_with_stdio(false);
//    cin.tie(NULL);
    /*
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    */
    int tc = 1;
    //cin>>tc;
    while(tc--){
        solve();
    }
}

#include<bits/stdc++.h>
#define ll long long int
#define sz size
#define pb push_back
#define pf push_front
#define mod 1000000007
using namespace std;

ll n, p, i, k, x, z;

int main()
{
    cin >> n >> p;
    for(i = 1; i <= 31; ++i)
    {
        k = 0;
        x = n - i * p;
        if(x < 1)
        {
            cout << -1;
            return false;
        }
        while(x > 0)
        {
            k += (x % 2);
            z ++;
            x /= 2;
        }
        if(k <= i)
        {
            if(i > k && k == 1)continue;
            cout << i;
            return false ;
        }
    }
    cout << -1;
    return false;
}

#include <bits/stdc++.h>
#include<list>
#include<algorithm>
#define ll long long int
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define rep(a,b,c) for(a=b;a<c;a++)
#define repj(a,b,c) for(a=b;a<c;a++)
#define repr for(i=n-1;i>=0;i--)
#define itr(v) for(auto ii=v.begin();ii!=v.end();ii++)
#define pii pair<ll,ll>
#define pll pair<ll, ll>
#define inf 100000001
#define mod 1000000007
#define lb lower_bound
#define up upper_bound
#define rep1 for(i=1;i<=n;i++)
#define vr vector
#define ef else if
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll myXOR(ll x, ll y)
{
	return (x|y)&(~x|~y);
}
ll powerLL(ll x, ll n)
{
    ll result = 1;
    while (n) {
        if (n & 1)
            result = result * x % mod;
        n = n / 2;
        x = x * x % mod;
    }
    return result;
}
bool comp(ll a, ll b)
{
	return a>b;
}
int main()
{
	fastio
	ll t,n,m,i,j,k,l,r,x,y,a,b,c,d,p,nl,np,q;
	string str,str1;
	cin>>n>>p;
	if(n-p<=0)
	{
		cout<<-1;
	}
	else
	{
		i=1;
		ll nd;
		m=0;
		while(m++<=1e8)
		{
			nd=n-p*i;
			if(nd<=0)
				break;
			a = log(nd)/log(2);
			b = pow(2,a);
			c = nd-b;
			// cout<<a<<" "<<b<<" "<<c<<endl;
			ll count = 0;
			while (c)
			{
			    count += c & 1;
			    c >>= 1;
			}
			if(count==i-1)
			{
				cout<<count+1;
				return 0;
			}
			if(count<i-1)
			{
				cout<<i;
				return 0;
			}
			i++;
		}
		cout<<-1;
	}
}

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

int largestPower(long long x)
{
	int count=0;
	while(x>1)
	{
		x=x/2;
		count++;
	}
	return count;
}

long long powerTwo(int x)
{
	long long res=1;
	for (int i=0;i<x;i++)
	{
		res*=2;
	}
	return res;
}

pair<int,int> summands(long long x)
{
	int temp=0;
	int count=0;
	int count2=0;
	while (x>0)
	{
		temp=powerTwo(largestPower(x));
		x-=temp;
		count++;
		if (temp>1) count2++;
	}
	return make_pair(count,count2);
}

int main()
{
	long long n;
	int p;
	cin>>n>>p;
	if (p==0)
	{
		cout<<summands(n).first;
	}
	else
	{
		int tempmin=9999;
		for (int i=1;i<50;i++)
		{
			if ((summands(n-i*p).first==i&&summands(n-i*p).first>0)||(summands(n-i*p).first<i&&summands(n-i*p).first>0&&i-summands(n-i*p).first<summands(n-i*p).second))
			{
				tempmin=min(tempmin,i);
			}
			//cout<<summands(n-i*p).first<<endl;
		}
		if (tempmin!=9999) cout<<tempmin;
		else cout<<-1;
	}
}

// Problem : C. p-binary
// Contest : Codeforces - Technocup 2020 - Elimination Round 2
// URL : https://codeforces.com/problemset/problem/1225/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;

#define ff first
#define fs second
#define lb lower_bound
#define ub upper_bound

#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector< vi >
#define vpii vector< pii >
#define vb vector<bool>
#define si set<int>
#define mii map<int, int>
#define mci map<char, int>
#define md 1000000007
#define pb push_back
#define eb emplace_back
#define fio ios_base::sync_with_stdio (false); cin.tie (NULL);  cout.tie (NULL);
#define qw1(x) cerr<<#x<<": "<<x<<" "<<endl
#define qw2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define qw3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define qw4(a, b, c, d) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl

#define f(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define rl(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define all(x) x.begin(),x.end()
#define endl '\n'
void printv(vector<int>  v){ for(int i=0;i<v.size();i++) cout<<v[i]<<' '; cout<<endl;   }
int bem(int m , int n, int mod) { int res=1; while(n){
	     			        if(n%2!=0) { res*=m; res%=mod; }
	    			        m*=m;   m%=mod;   n/=2;
			                          }
				return res;	}
int be(int a, int b) { int res=1; 	while(b){
				if(b%2!=0) res*=a;
				a*=a; b/=2;
				}
			return res;	}
void sieve(int n, vb &prime){
    prime[0]=false;     prime[1]=false;
    for(int i=2;i*i<=n;i++){
         if(prime[i]==true){
              for(int j=i*i;j<=n;j+=i)    prime[j]=false;
             	  }
    	 }
	}
int ns(int x)
{
	int cnt=0;
	while(x){ x= x&(x-1); cnt++; }
	return cnt;
}
void solve()
  {
int n, p; cin>>n>>p;
if(p==0) { cout<<ns(n); return; }
f(i, 1, 35)
{
	// qw2(i, ns(n-(i*p)));
	if((n-i*p)>0)
	if(ns(n-(i*p))<=i && ns(n-(i*p))>1 )
	 { cout<<i<<endl; return;}
}
cout<<-1;

  }

int32_t main ()
{
//fio
int t=1;
//cin>>t;
while(t--){
	solve();
}

}

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define f(i,x,y) for(i=x;i<y;i++)
#define vll vector<ll>
#define fast    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

void solve()
{
    ll n,p,i;
    cin>>n>>p;

    vll power(31,0);
    f(i,0,31)
    {
        power[i]=pow(2,i);
    }
    ll ans=INT_MAX;
    f(i,1,31)
    {
        ll num=n-i*p;
        //cout<<num<<endl;
        if(num<1)
        {
            if(ans==INT_MAX)
                cout<<"-1"<<endl;
            else
                cout<<ans<<endl;
            return ;
        }
        ll c=0;
        //cout<<num<<" ";
        while(num!=0)
        {
            num&=(num-1);
            c++;
        }
        //cout<<c<<" "<<i<<endl;
        if(i==c)
            ans=min(ans,c);
        else if(c<i)
        {
            //cout<<c<<" "<<i<<" ";
            ll k=i-c;
            if(k && (!(k&(k-1))))
                ans=min(ans,i);
            k++;
            //cout<<k<<endl;
            if(k && (!(k&(k-1))))
                ans=min(ans,i);
        }

    }
    cout<<ans<<endl;
}

int main()
{
    fast;
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
    return 0;
}

#pragma warning(disable:4996)
#include"bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define RE assert(0);
const long long inf = 1e17;
typedef pair<int, int> P;
int dgt(int x) {
	if (!x)return 0;
	return x % 2 + dgt(x / 2);
}

signed main() {
	int n, p; cin >> n >> p;

	rep(i, 60) {
		int k = n - p*i;
		if (k > 0 && dgt(k) <= i) {
			cout << i << endl; return 0;
		}
	}
	cout << -1 << endl;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
long long n,p;
cin>>n>>p;
int max1=33;
for(long long i=0;i<=32;i++){
    if(n-p*i>0){
        long long c=n-p*i;
        long long f=__builtin_popcountll(c);
        if(f<=i){
          if(max1>i){
              max1=i;
          }
        }
    }
}
if(max1==33){
    cout<<-1<<endl;
    return 0;
}
cout<<max1;
return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define pb push_back
#define f(i,x,y) for(i=x;i<y;i++)
#define vll vector<ll>
#define fast    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

void solve()
{
    ll n,p,i;
    cin>>n>>p;

    vll power(31,0);
    f(i,0,31)
    {
        power[i]=pow(2,i);
    }
    ll ans=INT_MAX;
    f(i,1,31)
    {
        ll num=n-i*p;
        //cout<<num<<endl;
        if(num<1)
        {
            if(ans==INT_MAX)
                cout<<"-1"<<endl;
            else
                cout<<ans<<endl;
            return ;
        }
        ll c=0;
        //cout<<num<<" ";
        while(num!=0)
        {
            num&=(num-1);
            c++;
        }
        if(i==c)
            ans=min(ans,c);
        else if(c<i)
        {
            //cout<<c<<" "<<i<<" ";
            ll k=i-c;
            k++;
            //cout<<k<<endl;
            if(k && (!(k&(k-1))))
                ans=min(ans,i);
        }

    }
    cout<<ans<<endl;
}

int main()
{
    fast;
    ll t=1;
    //cin>>t;
    while(t--)
        solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=200010;
int a[maxn],vis[1000010];
int main()
{
    ll n;
    int p;//printf("%I64d",1LL<<40);
    scanf("%I64d%d",&n,&p);
    for(int num=1;num<63;num++)
    {
        ll now=n-num*p;
        if(now<=0)
            break;
        int cnt=0;
        while(now)
        {
            cnt+=now%2;
            now>>=1;
        }
        if(cnt<=num)
        {
            printf("%d",num);
            return 0;
        }
    }
    printf("-1");
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long n,p;
int main(){
	cin>>n>>p;
	for(int i=1;i<=63;i++){
		long long m=n-p*i;
		if(m<=0) {puts("-1");break;}
		else if(__builtin_popcountll(m)<=i) {
			cout<<i;break;
		}
	}
	return 0;
}

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <cmath>
#include <algorithm>
#include <set>
#include <fstream>
#include  <iterator>
#include <sstream>
#include <queue>

int highestPowerof2(int n, int p)
{
	int x = n - p;
	if (x == 0)
		return -1;
	return floor(log2(x));

}
int main()
{
	int n, p;
	std::cin >> n >> p;
	int ans = 0;

	if (p >= 0)
	{
		while (n > 0)
		{
			int power = highestPowerof2(n, p);
			if (power == -1)
			{
				ans = -1;
				break;
			}
			n -= (pow(2, power) + p);
			n = abs(n);
			++ans;
		}
	}
	else
	{
		int toGet = highestPowerof2(n + 2 * p, 0);
		int delta = n + 2 * p - pow(2, toGet);
		int power = highestPowerof2(delta, 0);
		if (pow(2, power) == delta)
			ans = 2;
		if (ans == 0)
		{
			while (n > 0)
			{
				int power = highestPowerof2(n, p);
				if (power == -1)
				{
					ans = -1;
					break;
				}
				n -= (pow(2, power) + p);
				n = abs(n);
				++ans;
			}
		}
	}

	std::cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, p;
int main()
{
    //freopen("input.txt","r",stdin);
    cin >> n >> p;
    if( n == 0 )
    {
        cout << 0;
        return 0;
    }
    for( int i = 0; i < 40; i++ )
    {
        long long y = n - p * i,  sum = 0;
        while( y )
        {
            sum += y % 2;
            y /= 2;
        }
        cout << i << ' ' << sum << endl;
        if( i >= sum && !sum )
        {
            cout << i;
            return 0;
        }
    }
    cout << -1;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define MOD (ll)1000000007

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n, p;
    cin >> n >> p;
    for (ll i = 0; i < 10000000ll; i++)
    {
        ll temp = n - p * i;
        if (temp <= 0 || i > temp)
        {
            break;
        }
        ll sb = 0;
        for (ll j = 0; j < 15; j++)
        {
            if (temp & (1ll << j))
            {
                sb++;
            }
        }
        if (sb == i || (sb <= i && i <= temp))
        {
            cout << i << endl;
            return 0;
        }
    }
    if (p != -1ll && n % (p + 1ll) == 0)
    {
        cout << (n / (p + 1ll)) << endl;
        return 0;
    }
    cout << -1 << endl;
}

#include <iostream>
using namespace std;

int count(int n){
    int count=0;
    while(n){
        n=n&(n-1);
        count++;
    }
    return count;
}

int main() {
	int n;
	cin>>n;
	int p;
	cin>>p;
	int itr=0;
	int x=itr+1;
	if(p>=n){
	    cout<<-1;
	    return 0;
	}
	while(x>itr && itr<100){
	    n=n-p;
	    itr++;
	    x=count(n);
	}
	if(itr==100)
	    cout<<-1;
	else
	    cout<<itr;
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
ll n,p;
ll w;
ll pow(int a,int b){
	ll ans=1;
	while(b){
		if(b&1) ans=a*ans;
		a=a*a;
		b=b/2;
	}
	return ans;
}
int check(ll x){
	int ans=0;
	w=1;
	while(x){
	  if(x%2==1) ans++;
	  x=x/2;
	  w=w*2;
	}
	w=w/2;
	return ans;
}
int main(){

//	for(int i=0;i<=40;i++){
//		ll pp=pow(2,i);
//		cout<<i<<" "<<pp<<endl;
//	}
	cin>>n>>p;
	int ans=INF;
	int tmp;
	if(p==0){
	    tmp=check(n);
	    ans=min(ans,tmp);
	     cout<<ans<<endl;
	}else{
		for(int i=1;i<=30;i++){
			ll k=i*p;
			ll N=n-k;
			if(N<0) break;
			int tmp=check(N);
		//	cout<<N<<' '<<i<<" "<<tmp<<endl;
	        if(i==tmp) ans=min(ans,i);
	       // else if(i<tmp)
	        else if(i>tmp){
	            if(w==1){
	        		if(i==1) ans=min(ans,i);
				}
	        	else if(i<=tmp+w) ans=min(ans,i);
			}
		}
    }
	if(ans==INF) ans=-1;
	cout<<ans<<endl;
	return 0;
}

#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>
using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define rng(i,s,n) for(int i = (s) ; i < (n) ; i++)
#define rep(i,n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==(int)(n)-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) for(int qq123_=(n);qq123_;qq123_--)
//#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ld EPS = 1e-10;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll lcm(ll i, ll j) { return i / gcd(i, j) * j; }
ll mod_pow(ll x, ll n, ll p = mod) {
	ll res = 1; x %= p;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
	return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++)
			fact_[i] = fact_[i - 1] * i % Mod;
		comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
		for (int i = fn_ - 2; i > 0; i--)
			comp_[i] = comp_[i + 1] * (i + 1) % Mod;
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
signed main() {
	int n, p; cin >> n >> p;
	int res = -1;
	rng(i, 1, 10000000) {
		if (n > i * p) {
			if (bitcount(n - i * p) <= i) {
				if (res < 0) res = i;
				else chmin(res, i);
			}
		}
		else break;
	}
	cout << res << endl;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;
const int maxn = 200010;

int main()
{
    int n, p, k;
    while(~scanf("%d%d", &n, &p)){
        int s = 0;
        for(k = 1; k < 32; k++){
            s = n - (p * k);
            if (s < 1){
                continue;
            }
            int t = 0;
            while(s){
                if(s%2 == 1)
                    t++;
                s /= 2;
            }
            if(k == t){
                printf("%d\n", k);
                break;
            }
            if(k == t+1 && p < 0){
                int pp = -p;
                int x = 0;
                while(pp){
                    if(pp % 2 == 1)
                        x++;
                    pp /= 2;
                }
                s = n - (p * k);
                if(x == 1 && (s&(-p)) == 0){
                    printf("%d\n", k);
                    break;
                }
            }
        }
        if(k == 32)
            printf("-1\n");
    }
    return 0;
}

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define f first
#define s second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define len(x) ((int)x.size())

typedef long long ll;
typedef long double ld;

using namespace std;

namespace IO {
#define AUTO_T template <typename T>
#define AUTO_TT template <class T1, class T2>
	AUTO_T ostream& operator << (ostream& out, vector <T>& p) {
		for (auto& el : p)
			out << el << ' ';
		return out;
	}
	AUTO_T istream& operator >> (istream& inp, vector <T>& p) {
		for (auto& el : p)
			inp >> el;
		return inp;
	}
	AUTO_T ostream& operator << (ostream& out, deque <T>& p) {
		for (auto& el : p)
			out << el << ' ';
		return out;
	}
	AUTO_T istream& operator >> (istream& inp, deque <T>& p) {
		for (auto& el : p)
			inp >> el;
		return inp;
	}
	AUTO_TT ostream& operator << (ostream& out, pair <T1, T2>& p) {
		out << p.f << ' ' << p.s << endl;
		return out;
	}
	AUTO_TT istream& operator >> (istream& inp, pair <T1, T2>& p) {
		inp >> p.f >> p.s;
		return inp;
	}
}
using namespace IO;

signed main()
{
	ios::sync_with_stdio(0);
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll n, p;
	cin >> n >> p;

	vector <ll> step;
	ll c = 1;
	for (int i = 0; i < 40; i++) {
		for (int j = 0; j < 100; j++)
			step.push_back(c);
		c *= 2;
	}

	for (int i = 1; i <= 40; i++) {
		ll t = n - (i * p);
		ll tt = t;
		int kol = 0;
		vector <ll> st;
		for (int j = len(step) - 1; j >= 0; j--) {
			if (t - step[j] >= 0) {
				t -= step[j];
				st.push_back(step[j]);
				kol++;
			}
		}
		if (t == 0) {
			if (kol == i)
				return cout << i, 0;
		}
	}

	cout << -1;

	return 0;
}

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <map>
#include <sstream>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <functional>
#include <utility>
#include <stdio.h>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
ll n,m;
ll res=1e18;
ll cnt1(ll x)
{
	ll r=0;
	while (x) r+=x%2,x/=2;
	return r;
}
ll deal(ll x)
{
	ll r=cnt1(x);
	if (x%2) r=(r-1)*2+1;
	else r*=2;
	return r;
}
int main()
{
	cin >>n>>m;
	for (ll i=0;i<min(deal(n),(ll)60);i++)
	{
		if (n<=i*m) break;
		if (cnt1(n-i*m)<=i && deal(n-i*m)>=i)
		{
			cout <<i;
			return 0;
		}
	}
	cout <<"-1";
	return 0;
}

#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define int long long

using namespace std;

int n, p;

main() {
    cin >> n >> p;
    for (int i = 1; i <= 1e6; i++) {
        if (n - i * p <= 0) continue;
        if (__builtin_popcountll(n - i * p) <= i) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define MAX_N 2000

vector<int>power2;

int cnt(ll n){
    int count = 0;
    while(n){
        if(n&1)count++;
        n >>= 1;
    }
    return count;
}

int main(){
    ll n, p;
    while(cin >> n >> p){
        int ans = -1;
        for(int i = 1; i <= 32; i++){
            ll tempN = n - i*p;
            if(tempN <= 0){
                continue;
            }
            int k = cnt(tempN);
            if(k == i){
                ans = i;
                break;
            }else if(k < i){
                ans = i;
                break;
            }
        }
        if(ans != -1){
            cout << ans << "\n";
        }else{
            cout << "-1\n";
        }
    }
}

    #include <bits/stdc++.h>
    using namespace std;
    #define dude ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define endl "\n"

    #define ll long long
    #define pb push_back
    void fun()
    {
        #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        #endif
    }
   int mod=1000000000+7;
ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;}

int32_t main(){
fun();
dude;
 	int n,m,i;
 	cin>>n>>m;
 	i=0;
 	if(n-m<=0)
 	{
 		cout<<-1;
 		return 0;
 	}
 	while(1)
 	{
 		n-=m;
 		i++;
 		if(__builtin_popcount(n)<=i)
 			break;
 	}
 	cout<<i;
 	return 0;
 }

#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#include <assert.h>
#include <set>

using namespace std;

#define REP(i,fn_) for(int i=0; i<(fn_); ++i)
#define RREP(i,fn_) for(int i=(fn_)-1; i>=0; --i)
#define FOR(i,fa_,fb_) for(int i=(fa_); i<=(fb_); ++i)
#define RFOR(i,fa_,fb_) for (int i=(fa_); i>=(fb_); --i)
#define ALL(fc_) (fc_).begin(), (fc_).end()
#define MEMALL(fe_,fv_) memset((fe_), (fv_),sizeof((fe_)))
#define MEM(fe_,fv_,fs_) memset((fe_),(fv_), sizeof((fe_)[0])*(fs_))
#define MP make_pair

typedef long long lld;
typedef vector<int> VI;
typedef vector<lld> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<lld, lld> PL;
typedef vector<double> VD;
typedef vector<VD> VVD;

const lld MOD = 1e9 + 7;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }
template<typename T> void add(T &a, T b)
{
	a += b;
	if (a < 0)a += MOD;
	if (a >= MOD)a -= MOD;
}
void FastIn(int &T)
{
	char C; bool F = 0;
	for (; C = getchar(), C<'0' || C>'9';)if (C == '-')F = 1;
	for (T = C - '0'; C = getchar(), C >= '0'&&C <= '9'; T = T * 10 + C - '0');
	F && (T = -T);
}

int in() { int x; scanf("%d", &x); return x; }
lld lin() { lld x; scanf("%lld", &x); return x; }
double din() { double x; scanf("%lf", &x); return x; }
lld p2[63];
void initP2()
{
	p2[0] = 1;
	FOR(i, 1, 62)
	{
		p2[i] = p2[i - 1] * 2;
	}
}

int countBitsBaseB(lld n, lld b)
{
	if (n == 0)return 1;
	int ret = 0;
	while (n)
	{
		ret++;
		n /= b;
	}

	return ret;
}

int count1(lld n)
{
	int ret = 0;
	while (n)
	{
		ret += n & 1;
		n >>= 1;
	}

	return ret;
}

lld c[1024][1024] = { 0 };

void initC()
{
	REP(i, 1024)
	{
		REP(j, i + 1)
		{
			if (j == 0 || j == i)
			{
				c[i][j] = 1;
			}
			else
			{
				c[i][j] = c[i - 1][j - 1];
				add(c[i][j], c[i - 1][j]);
			}
		}
	}
}

bool IsPrime(int n)
{
	if (n < 2)return false;
	for (int i = 2; i*i <= n; i++)
	{
		if (n%i == 0)return false;
	}

	return true;
}

void init()
{
	initP2();
	initC();
}

#define MAXN (int)(2e5+10)

const unsigned INVALID = 2000000001;
#define lson(x) ((x)<<1)
#define rson(x) (lson(x)|1)

int main()
{
	//freopen("C:\\Users\\Lingdi_hz17\\Downloads\\graph3.in", "r", stdin);
	//freopen("test.out", "w", stdout);
	init();

	int n = in(), p = in();

	if (p == 0)
	{
		printf("%d", count1(n));
		return 0;
	}

	for (int k = 1; n - k*p > 0; k++)
	{
		int tmp = n - k*p;
		int cnt = count1(tmp);
		if (cnt <= k&& tmp>=k && tmp % 2 == k % 2)
		{
			printf("%d", k);
			return 0;
		}
	}

	puts("-1");

	return 0;
}
/*
2 1
1 2 1
2
0
1 1
*/

#include<bits/stdc++.h>
using namespace std;

int n,p;
int kq;

int main(){
    cin>>n>>p;kq=-1;
    for(int i=1;i<=31;i++){
        long long z=0ll+n-i*p;
        int ii=__builtin_popcount(z);
        if (ii>i) continue;
        else{
            kq=i;break;
        }
    }
    cout<<kq;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,p;
inline int check(int x)
{
	int m=n-p*x,cnt=0,mask;
	if(m<=0) return false;
	for(int i=1;;i++)
		if((1ll<<i)>m)
		{
			mask=i;
			break;
		}
	for(int i=mask;i>=0;i--)
	{
		while((1ll<<i)<=m)
			m-=(1ll<<i),cnt++;
	}
	return cnt<=x;
}
signed main()
{
	scanf("%lld%lld",&n,&p);
	for(int i=1;i<=1e7;i++)
		if(check(i))
			return printf("%d\n",i),0;
	puts("-1");
	return 0;
}

#include <iostream>
using namespace std;

bool IsRepresentable(int n, int i){
	if (n < 0)
		return false;
	int ones = 0;
	int possible_increment = 0;
	int increments = 0;

    while (n) {

		if (n % 2) {
			++ones;

			if (ones > i) return false;

			possible_increment += increments;
		}
		++increments;
		n /= 2;
	}

	return (ones + possible_increment > i);
}

int main() {
	int n, p;
	cin >> n >> p;

	for (int i = 1; i < 100000; ++i) {
		if (IsRepresentable(n + (i * p * -1), i)) {
			cout << i << endl;
			return 0;
		}
	}

	cout << -1;
	return 0;
}

/************************************************************************************/
/*******************************BEGIN HEADER*****************************************/
#pragma GCC optimize("O2")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC target("avx","sse4.1","sse4.2","ssse3","f16c")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <numeric>
#include <algorithm>
#include <ctime>
#include <cassert>
#include <cstring>
#include <functional>
#include <iostream>
#include <iomanip>
using namespace std;

#define sz(x) ((int)(x).size())
#define isempty(x) ((int)(x).size()==0)
#define all(x) begin((x)),end((x))
#define has(x,y) (find(all(x),(y))!=end(y))
#define pb push_back
#ifndef DEBUG
#define cerr if(0)cerr
#endif

typedef long long ll;
typedef pair<int,int> pii;

template<class T> using priority_queue_t = __gnu_pbds::priority_queue<T>;
template<class T> using set_t = __gnu_pbds::tree<T,__gnu_pbds::null_type>;
template<class T,class U> ostream& operator<<(ostream& os, const pair<T,U>& p){return os << "(" << p.first << "," << p.second << ")";}
template<class T> auto operator<<(ostream& os, const T& it)
	-> typename enable_if<decltype(it.begin(),true_type{})::value && !is_same<T,string>::value,ostream&>::type
{
	if(begin(it) == end(it))return os << "[]";
	os << "[ " << *begin(it);
	for(auto i=++begin(it); i!=end(it); ++i) os << ", " << *i;
	return os << " ]";
}

/******************************* END HEADER ***************************************/
/**********************************************************************************/

int main(){
	int n,p;
	cin>>n>>p;

	int v[30];
	v[0] = p;
	for(int i=1; i<30; ++i)v[i] = (1<<i)+p;

	int ans=-1;
	for(int i=1; i<30; ++i){
		int x = n-i*p;
		if(x<=0)continue;
		if(__builtin_popcount(x) <= i){ ans=i; break; }
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
#define ll long long int

using namespace std;

ll n,p;
ll arr[33];

int getRes(ll current, int counter, int nOfSteps){
    if(counter == 0){
        return -1;
    }
    int min = -1;
    for(int i=0;i<33;i++){
        if(current-arr[i] == 0){
            return nOfSteps + 1;
        }
        else{
            int res = -1;
            if(abs(current)/abs(current-arr[i]) < 6) {
                res = getRes(current - arr[i], counter - 1, nOfSteps + 1);
            }
            if(min == -1 || (res < min && res != -1)){
                min = res;
            }
        }
    }
    return min;
}

int main() {
    cin>>n>>p;
//    for(ll i=0;i<33;i++){
//        arr[i] = (ll)pow(2,i) + p;
//    }
    if(p >= n){
        cout << -1 << endl;
    }
    else if(p == 0){
        cout << __builtin_popcountll(n) << endl;
    }
    else{
        n+=p;
        n = abs(n);
        cout << __builtin_popcountll(n) << endl;
    }
//    ll currentNumber = n;
//    int res = 0;
//    for(int i=32;i>=0;i++){
//        if(currentNumber<=arr[i]){
//            currentNumber-=arr[i];
//            res++;
//        }
//    }
//    cout << getRes(n,6,0) << endl;

    return 0;
}

/******************************
 *
 * created by: Shivam prajapati
 *
 * *****************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef long long unsigned int llu;
const int INF = 0x3f3f3f3f;

#define ff first
#define ss second
#define ins insert
#define tc int tt; cin>>tt; while(tt--)

#define  sync ios_base::sync_with_stdio(false);  cin.tie(NULL);
#define pb push_back
#define pf pop_front
#define mp make_pair
#define all(v) v.begin(),v.end()

#define mpi unordered_map<ll, ll>
#define pr pair<ll, ll>
#define vi vector<ll>
#define vc vector<char>

#define mpc unordered_map<char, ll>
#define it_loop(it,m) for(auto it = m.begin(); it!=m.end(); it++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define repx(i,begin, end) for(ll i=begin; i<=end;i++)

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1e9+7
#define count_ones __builtin_popcountl              // #ones in binary rep.
#define what_is(x) cerr << #x << " is " << x << endl  //#x -> actual name of variable

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define eb emplace_back                    // faster than push_back
#define mt make_tuple                        //  for returning more than one value with tie(args ...) = tuple
#define fill(a,n,start) iota(a,a+n,start)    // fill array in increasing order

#define shuffle(a,n) shuffle(a,a+n,default_random_engine(0))
#define print_arr(a,n) rep(i,n) cout<<a[i]<<endl
#define read_arr(a,n) rep(i,n) cin>>a[i]
#define ook order_of_key   // ook(n) It returns to the number of items that are strictly smaller than our item n

#define nds new_data_set    //pbds array
#define fbo find_by_order   // fbo(i), returns an iterator at the ith index

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

/* gcd __gcd(a,b)
 * sparse_table: st[i][j] = f(st[i][j-1],st[i+(1<<(j-1))][j-1])
 * sparse_table: for range query in immutable array
 * modified queue: stack<pair<ll,ll>> s1,s2   s1->add,  s2->delete
 * long long binpow(long long a, long long b) {    long long res = 1;while (b > 0) {if (b & 1)res = res * a;a = a * a;b >>= 1;}return res;}
 * " \n"[0] is ' ' and " \n"[1] is '\n'
 * we can also use ignore keyword in tie to ignore a value ->  tie(b, ignore, a, ignore)
 * 0 based indexing in pbds ordered_set
 * add(l,x) & add(r+1,-x) for BIT range update point query
 * use scanf and printf insted of fastio
 * gcc optimizations work best with arrays not vectors
 * use int till 1e5
*/

void solve(){

	ll n,p;
	cin>>n>>p;
	for(ll i=0;i<=32;i++){
		if(n-i*p>=0){
			int x = count_ones(n-i*p);

			if(x<=i){
				string s = bitset<32>(n-i*p).to_string();
				ll sum=0;
				reverse(all(s));
				for(size_t j=1;j<s.size();j++){
					if(s[j]=='1'){
						sum+=pow(2,j);
					}
				}
				if(i<=sum){
					cout<<i<<endl;
					return;
				}

			}

		}
	}
	cout<<-1<<endl;

}

int main(){
  sync;
  //tc{ solve(); }
  solve();
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n,p;
    cin>>n>>p;
    for(int i=0;i<5000000;i++)
    {
        bitset<100> bs(n-i*p);
        if(bs.count()<=i&&n-i*p>i)
        {
            cout<<i;
            return 0;
        }
    }
    cout<<-1;
}

#include<bits/stdc++.h>
using namespace std;

#define sc1(x) scanf("%d",&(x))
#define sc2(x,y) scanf("%d%d",&(x),&(y))
#define scs(x) scanf("%s",(x))
#define scLL(x) scanf("%lld",&(x))
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
#define all(x) x.begin(),x.end()

typedef pair<int,int> pii;
typedef long long LL;
typedef pair<LL,LL> pLL;
typedef vector<vector<vector<int> > > viii;
typedef pair<int,pair<pii,pii> > node;

const int apsz = 26;
const LL o = 1;
const int maxn = 1000007;
const int ooi = 1e9+7;
const int up = 61;

inline int bitcount(LL x)
{
	int ans=0;
	while(x)
	{
		if(x&1) ans++;
		x>>=1;
	}
	return ans;
}
int tw[100];
inline int bs(LL x)
{
	return bitcount(x);
}
int main()
{
	IO;
	LL n,p;
	cin>>n>>p;
	LL y=1;
	for(;y<up;y++)
	{
		LL tmp = n-p*y;
		if(tmp<=0) return cout<<-1<<endl,0;
		LL tb = bs(tmp);
		if(tb>y)continue;
		LL ty = y;
		for(LL i = 0;i<up;i++)
		{
			if((o<<i)&tmp)
			{
				LL j=0;
				tb--;
				while(j<i&&(o<<j+1)+tb<=ty)j++;
				ty-=(o<<j);
			}
		}
		if(ty==0)
			return cout<<y<<endl,0;
	}
	cout<<1<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define int long long int
using namespace std;
int fun(int n,int x){int c=0;
	while(n){
		if(n%2==1){c++;}
		n/=2;
	}//cout<<c<<"cc";
	//cout<<c<<x;
	if(c>x)return 0;
	else return 1;
	return c;
}
main() {
	// your code goes here
	int n,p;cin>>n>>p;int ans=-1;
	if(n==1 && p==1){cout<<1;return 0;}
	for(int i=1;i<=32;i++){//cout<<n-p*i<<" ";
		if(n-p*i<0)break;
		int k=fun(n-i*p,i);
		if(k==1){ans=i;break;}
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef bitset<15>mask;
#define F first
#define S second
ll n,m,x,y,z,k,sol,sum,ans,l,r,xx,yy,a[1000000],b[1000000];
vector<ll>v1;
vector<ll>v2;
pair<ll,pair<ll,ll> >pp[1000000];
pair<ll,ll>p[1000000];
map<ll,ll>ma;
string s1,s2,s;
char c;
int main()
{
//freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin>>n>>m;
    for(ll i=0;i<=30;i++)
    {
        x=pow(2,i);
        v1.push_back(x);
    }
    if(m>=n)
    {
        cout<<-1<<endl;
        return 0;
    }
    ll ex=(-m),idx=1;

    while(true)
    {
    x=n+idx*ex;
    if(x<=0)
    {
        cout<<-1<<endl;
        return 0;
    }
    ans++;
    idx++;
    k=0;
    for(int i=0;i<ans;i++)
    {
        for(int q=0;q<=30;q++)
        {
            if(v1[q]>x)
            {
                if(v1[q]==1)
                {
                    cout<<-1;
                    return 0;
                }
                x-=v1[q-1];
                break;
            }
            else if(v1[q]==x)
            {
                x=0;
                break;
            }
        }
    }
    if(x==0)
    {
        cout<<ans<<endl;
        return 0;
    }

    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define lli long long
#define For(i,n) for(int i=0;i<n;i++)
#define vi vector<int>
int main(){
    int n,p,flag=-1;
    cin>>n>>p;
    int k=1;
    while(k++)
    {
        int store=n-k*p,count=0;
        if(store<=0) break;
        while(store)
        {
            if(store&1)
                count++;
            store=store>>1;
        }
        if(count<=k)
        {
            flag=0;
            cout<<k;
            break;
        }
    }
    if(flag!=0)
        cout<<"-1";
    cout<<endl;
    return 0;
}

#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
typedef long long ll;
//
using namespace std;
const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3fffffff;

ll n, p;
int main()
{
	cin >> n >> p;
	int minnum = INT_MAX;
	for (int i = 1;i <= 1e5;i++)
	{
		int cnt = 0;
		ll temp = n + ((-i)*p);
		int ex = 31;
		int e = 0;
		for (ll j = pow(2, 31);j >= 1;j /= 2,ex--)
		{
			if (temp >= j)
			{
				temp -= j;
				e += ex;
				cnt++;
				j *= 2;
				ex++;
			}
			if (temp == 0) break;
		}
		if (temp == 0 && cnt <= i&&e>=i-1)
			minnum = min(minnum, i);
		if (i > minnum) break;
	}
	if (minnum == INT_MAX) cout << "-1";
	else cout << minnum;
	return ~~(0 - 0);
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod=1337;
ll power(ll a,ll b)
{
    if(b==0) return 1;
    ll temp=power(a,b/2);
    if(b%2==0)
    {
        return temp*temp;
    }
    else
    {

        return temp*temp*a;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    //cin>>t;
    t=1;
    while(t--)
    {
      ll n,p;
      cin>>n>>p;
      int i=0;
      ll m=n;
      while(n-(i*p)>0)
      {
          m=n-(i*p);
          ll cnt=0;
          for(int i=0;i<31;i++)
          {
              if(m&(1<<i))
              {
                  cnt++;
              }
          }
          if(cnt<=i)
          {
              cout<<i<<"\n";
              exit(0);
          }
          i++;
      }
      cout<<-1<<"\n";
    }
}

#include<bits/stdc++.h>
#include<stdlib.h>
#include<string.h>
#define ll long long
#define iof ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pi 3.1415926535897
const ll mod =1000000007;
#define inf 1<<60
using namespace std;

int main()
{
   ll n,p;
   cin>>n>>p;
   ll ans=INT_MAX;
   for(ll a=1;a<=32;a++)
   {
       ll x= n-a*p;
       if(x>0)
       {
           ll q= __builtin_popcount(x);
           if(q<=a)
           {
               ans=min(ans,a);
           }
       }
   }
   if(ans==INT_MAX)
    cout<<-1<<endl;
   else
    cout<<ans<<endl;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int maxn = 1000 + 10;
const int mod = 1e6 + 7;
const int base = 31;

int cntdig(ll x){
	int ret = 0;
	while (x){
		x -= x & -x;
		ret ++;
	}
	return ret;
}

int main(){
	ios_base::sync_with_stdio(false);
	ll n, p;
	cin >> n >> p;
	for (int i = 1; i <= 100; i++){
		n -= p;
		if (n <= 0)
			return cout << -1 << endl, 0;
		if (cntdig(n) <= i)
			return cout << i << endl, 0;
	}
	cout << -1 << endl;
}

#include <iostream>
// Author: MrLolthe1st
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<long long, long long>
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define int long long
#define _ << " " <<
#define all(a) a.begin(), a.end()
#define srt(a) std::sort(a.begin(), a.end())
#define rev(a) std::reverse(a.begin(), a.end())
#define double long double
typedef string str;

template<typename T>
inline istream& operator >>(istream& in, vec<T>& a) {
	for (int i = 0; i < a.size(); i++) in >> a[i];
	return in;
}

template<typename T>
inline ostream& operator <<(ostream& out, vec<T>& a) {
	for (int i = 0; i < a.size(); i++) out << a[i] << " ";
	return out;
}

template<typename T, typename E>
inline istream& operator >>(istream& in, pair<T, E>& a) {
	in >> a.first >> a.second;
	return in;
}

template<typename T>
T gcd(T a, T b) {
	T c;
	while (b) {
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}

template<typename T>
T lcm(T a, T b) {
	return (a / gcd(a, b)) * b;
}

#include <sstream>
#define sqr(a) (a)*(a)
#define X first
#define Y second
int popcount(int a) {
	int z = 0;
	while (a) z += a & 1, a >>= 1;
	return z;
}

int maxbit(int a) {
	int cnt = 0;
	while (a) cnt++, a >>= 1;
	return cnt;
}

void solve()
{
	//cout << popcount(332633440 - (1 << 28));
	//return;
	int t = 1;
	while (t--) {
		int p, n;
		cin >> n >> p;
		for (int i = 0; i < 31 && n >= 0; i++, n = n - p) {
			if (popcount(n) > i && n >= i) {
				cout << i; return;
			}
		}
		cout << -1;
	}
}

int32_t main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
	int k;
	cin >> k;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
//typedef pair<ll,ll>P;
#define N  1000010
#define M  200010
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1

const ll MOD=1e9+7,inv2=(MOD+1)/2;
      double eps=1e-6;
      int dx[4]={-1,1,0,0};
      int dy[4]={0,0,-1,1};

      ll b[N],n;

int read()
{
   int v=0,f=1;
   char c=getchar();
   while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
   while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,   c=getchar();
   return v*f;
}

int calc(ll n)
{
    int res=0;
    while(n)
    {
        res+=(int)(n%2);
        n/=2;
    }
    return res;
}

int main()
{
    scanf("%I64d",&n);
    int p=read();
    if(p>=0&&n<=p+1)
    {
        printf("-1\n");
        return 0;
    }
    int ans=-1;
    rep(i,1,60)
    {
        ll t=n-p*i;
        if(t<=0) continue;
        if(calc(t)<=i){ans=i; break;}
    }
    printf("%d\n",ans);
    return 0;
}

#include<iostream>
using namespace std;
#define ll long long int
int main()
{
    ll n,p;
    cin>>n>>p;
    ll g=n-p;
    ll m=1;
    while(g>0)
    {
        ll k=g;
        ll coint=0;
        while(k){
            if(k%2==1)
            coint++;
            k=k/2;
        }
        if(m>=coint)
        {cout<<m<<endl;return 0;}
        g=g-p;
        m++;

    }
    cout<<-1<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define emdl "\n"
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
// bool sortBy(const pair<int,pair<int,int>> &a,const pair<int,pair<int,int>> &b){
// 	if(a.S.F==b.S.F)
// 		return a.F<b.F;
// 	return a.S.F<b.S.F;
// }
// void reverseStr(string& str)
// {
//     int n = str.length();
//     for (int i = 0; i < n / 2; i++)
//         swap(str[i], str[n - i - 1]);
// }
// int binarySearch(int arr[], int l, int r, int x)
// {
//     if (r >= l) {
//         int mid = l + (r - l) / 2;

//         if (arr[mid] == x)
//             return mid;
//         if (arr[mid] > x)
//             return binarySearch(arr, l, mid - 1, x);

//         return binarySearch(arr, mid + 1, r, x);
//     }
//     return l;
// }
// vector<vector<int>> mat;
// vector<set<int>> sa;
// vector<int> vis,dis;
// void bfs(int u){
// 	queue<int> q;
// 	int v;
// 	vis[u]=1;
// 	q.push(u);
// 	while(!q.empty()){
// 		v=q.front();
// 		q.pop();
// 		for(auto i:mat[v]){
// 			if(!vis[i]){
// 				vis[i]=1;
// 				dis[i]=dis[v]+1;
// 				q.push(i);
// 			}
// 		}
// 	}
// }
// #define MAXN 10000001
// int pf[MAXN];
// void sieve(){
// 	long long int i,j;
//     pf[1] = 1;
//     for (i=2; i<MAXN; i++)
//     pf[i] = i;
//     for (i=2; i*i<MAXN; i++){
//         if (pf[i] == i){
//             for (j=i*i; j<MAXN; j+=i)
//                 if (pf[j]==j)
//                     pf[j] = i;
//         }
//     }
// }
// vector<int> factors(int x){
//     vector<int> ret;
//     while (x != 1){
// 		int fact=pf[x];
//         ret.push_back(pf[x]);
// 		while(fac!=1&&x%fact==0)
//         x/=fact;
//     }
//     return ret;
// }
void init(){

}
int poww(long long int x){
	int len=0;
	while(x!=0){
		len+=x%2;
		x/=2;
	}
	return len;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
	int qwe=1;
	//cin>>qwe;
	init();
	// sieve();
	while(qwe--){
		long long int n;
		cin>>n;
		int p;
		cin>>p;
		int ans=-1;
		if(n==1&&p==1){
			cout<<-1;
			return 0;
		}
		for(int i=1;i<=10;i++){
			//cout<<i*p<<" ";
			if(i>=poww(n-(i*p))){
				ans=i;
				break;
			}
		}
		//cout<<poww(28);
		cout<<ans;
	}

	return 0;
}

/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define f first
#define se second
#define Speedforce boost();
#define fop(s1, s2) freopen(s1, "r", stdin);freopen(s2, "w", stdout);

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const ll inf = 1e9 + 7;
const ld PI = acos(-1);

ll bin_pow(ll x, ll y){
    ll res = 1, base = x;
    while(y){
        if(y & 1){
            res *= base;
            //res %= inf;
        }
        base *= base;
        //base %= inf;
        y /= 2;
    }
    return res;
}

ll n, p;

int main(){
    Speedforce
    //fop("input.txt", "output.txt"cout
    cin >> n >> p;
    if(n < 1 + p){
        cout << -1;
        return 0;
    }
    for(ll k = 1;k <= 100000;k ++){
        ll m = n - p * k;
        ll cnt1 = __builtin_popcount(m), cnt2 = 0;
        for(int i = 0;i < 60;i ++){
            if(m & (1<<i)){
                cnt2 += (1<<i);
            }
        }
        if(cnt1 <= k && k <= cnt2){
            cout << k;
            return 0;
        }
    }
    cout << -1;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;
const int maxn = 200010;

int main()
{
    int n, p, k;
    while(~scanf("%d%d", &n, &p)){
        int s = 0;
        for(k = 4; k < 32; k++){
            s = n - (p * k);
            int t = 0;
            while(s){
                if(s%2 == 1)
                    t++;
                s /= 2;
            }
            if(k == t){
                printf("%d\n", k);
                break;
            }
            if(k == t+1 && p < 0){
                int pp = -p;
                int x = 0;
                while(pp){
                    if(pp % 2 == 1)
                        x++;
                    pp /= 2;
                }
                s = n - (p * k);
                if(x == 1 && (s&(-p)) == 0){
                    printf("%d\n", k);
                    break;
                }
            }
        }
        if(k == 32)
            printf("-1\n");
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define MEM(x,val) memset(x,val,sizeof(x))
#define ll long long
#define F first
#define S second
#define pii pair<int,int>
#define ALL(x) x.begin(),x.end()
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);
int II(){int q;scanf("%d",&q);return q;}
ll LII(){ll q;scanf("%lld",&q);return q;}

#define int ll

void solve(){
  int N,P; cin>>N>>P;
  vector<int>v;

  for(int i = 0; i<35; i++){
    v.pb((1LL<<i) + P);
  }

  vector<pair<int,int>>vv;
  for(auto it:v) if(it > 0) vv.pb({it,1});

  for(int i = 0; i<v.size(); i++){
    for(int j = i; j<v.size(); j++){
      if(i == j) continue;
      if((v[j] + v[i]) > 0){
        vv.pb({v[j] + v[i],2});
      }
    }
  }
  sort(ALL(vv));
  /*for(auto it:vv){
    cout<<it.F<<" "<<it.S<<endl;
  }*/
  if(vv.front().F > N){
    cout<<-1<<endl;
    return;
  }
  int ans = 0;
  while(N){
    int val = -1,val1 = 2;
    for(auto it:vv){
      if(it.F <= N){
        if(it.F == val) val1 = min(val1,it.S);
        else val = it.F,val1 = it.S;
      }
    }
    if(val == -1){
      cout<<-1<<endl;
      return;
    }
    N -= val;
    ans += val1;
  }
  cout<<ans<<endl;
}

int32_t main(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  #endif
  int tt; tt = 1;
  int cas=1;
  while(tt--){
    //printf("Case %d: ",cas++);
    solve();
  }
}

/*
ID: computerbox --> Huseyn Hajiyev
LANG: C++
TASK: target_mode_on
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define _CRT_SECURE_NO_WARNINGS
//#include <boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;
#define FAST_READ ios_base::sync_with_stdio(0);
#define in freopen("input.txt", "r", stdin);
#define out freopen("output.txt", "w", stdout);
#define ll long long
#define debt(x,y)cout<<"#x = "<<(x)<<" and "<<"#y = "<<(y)<<endl;
#define deb(x)cout<<"#x = "<<(x)<<endl;
#define COUT(n, a) cout<< fixed << setprecision(a) << n<<endl
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define arr(a,n) for(ll i=1;i<=n;i++) cout<<a[i]<<" "; cout << "\n";
#define vecc(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout << "\n";
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;
#define MAX3 2010
#define MAX4 20010
#define MAX5 200010
#define MAX6 2000010
#define MOD1 1000000007
#define MOD2 998244353

//cin.ignore (7777, '\n');

using namespace std;
using namespace __gnu_pbds;

#define debug(args...)(Debugger()) , args
class Debugger
{
  public:
  Debugger(const std::string& _separator=", ") :
  first(true),separator(_separator){}
  template<typename ObjectType>
  Debugger& operator,(const ObjectType& v)
  {
        if(!first)cerr<<separator;
        cerr<<v;
        first=false;
        return *this;
  }
  ~Debugger() {cerr<<endl;}
  private:
    bool first;
    string separator;
};

template <typename T1, typename T2>
inline ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p)
{
   return os<<"("<<p.first<<", "<<p.second<<")";
}
template<typename T>
inline ostream &operator<<(ostream & os,const vector<T>& v)
{
    bool first=true;
    os<<"[";
    for(unsigned ll i=0;i<v.size();i++)
    {
        if(!first)os<<", ";
        os<<v[i];
        first=false;
    }
        return os<<"]";
}
template<typename T>
inline ostream &operator<<(ostream & os,const set<T>&v)
{
    bool first=true;
    os<<"[";
    for(typename set<T>::const_iterator ii=v.begin();ii!=v.end();++ii)
    {
        if(!first)os<<", ";
        os<<*ii;
        first=false;
    }
    return os<<"]";
}
template<typename T1, typename T2>
inline ostream &operator<<(ostream & os,const map<T1, T2>&v)
{
        bool first=true;
        os<<"[";
        for(typename map<T1,T2>::const_iterator ii=v.begin(); ii!=v.end();++ii)
        {
            if(!first)os<<", ";
            os<<*ii;
            first=false;
        }
        return os<<"]";
}

ll n,p;
ll cnt=0;
set<ll>nuj;

ll check(ll x)
{
  ll nn=n-p*x;
  if(n<=0)return 0;
  ll del=x;
  while(nn>0 && del--)
  {
	ll it=*(--nuj.upper_bound(nn));
	nn-=it;
   }
   if(del>=0 && nn==0)return 1;
   else return 0;
}

int main(){
FAST_READ;
cin>>n>>p;
for(ll i=62;i>=0;i--)
{
 if((1LL<<i)!=0)nuj.insert(((1LL<<i)));
}

for(ll i=1;i<=30;i++)
{
  if(check(i))
  {
	cout<<i<<endl;
	return 0;
  }
}

cout<<-1<<endl;
return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=2e5+7;
const ll inf=1e18;
#define ft first
#define sd second
#define faster ios::sync_with_stdio(0),cin.tie(0)
ll n,p;
bool judge(ll m){
    ll x=n-m*p;
    if(x<=0)return 0;
    int cnt=0;
    while(x)cnt+=x%2,x/=2;
    return cnt<=m;
}
void solve(){
    ll l=1,r=1e18;
    if(p>0)r=n/p;
    ll ans=inf;
    while(l<=r){
        ll m=(l+r)>>1;
        if(n-m*p<=0)r=m-1;
        else {
            if(judge(m)){
                r=m-1;
                ans=m;
            }
            else l=m+1;
        }
    }
    if(ans==inf)puts("-1");
    else cout<<ans<<'\n';
}
int main(){
    faster;
    cin>>n>>p;
    solve();
    return 0;
}

#include <iostream>

using namespace std;

int main (void)
{
	int n, p;
	cin >> n >> p;

	int ans;
	int error = 0;
	for(ans = 1; ans <= 32; ans++)
	{
		int temp = 536870912;
		n -= p;
		if( n <= 0)
		{
			error = 1;
			break;
		}

		int cnt = 0;
		while( temp != 0)
		{
			if( n & temp)
				cnt++;

			temp = temp / 2;
		}

		if( cnt <= ans)
			break;
	}

	if( !error)
		cout << ans << "\n";
	else
		cout << -1 << "\n";
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#define ll long long
#define maxn 200010
inline ll read()
{
	ll x=0; char c=getchar(),f=1;
	for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
	for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
	return x*f;
}
inline void write(ll x)
{
	static int buf[20],len; len=0;
	if(x<0)x=-x,putchar('-');
	for(;x;x/=10)buf[len++]=x%10;
	if(!len)putchar('0');
	else while(len)putchar(buf[--len]+'0');
}
inline void writeln(ll x){write(x); putchar('\n');}
inline void writesp(ll x){write(x); putchar(' ');}
int n,p;
int calc(int n)
{
	int cnt=0;
	for(;n;n>>=1)cnt+=(n&1);
	return cnt;
}
int main()
{
	n=read(); p=read();
	if(!p){
		writeln(calc(n)); return 0;
	}
	for(int i=0;i<=1000;i++){
		int k=n-p*i;
		if(k>i&&calc(k)<=i){
			writeln(i); return 0;
		}
	}
	puts("-1");
	return 0;
}

/*
 * Author :  pasnger
 * Date   :  2020 Oct 25 16:14:30
*/

#include<bits/stdc++.h>
using namespace std;

#define deb(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
void DBG() { cerr << "]\n"; }

template<typename T, typename ...Args> void DBG(T first, Args... args) {
  cerr << first; if (sizeof...(args))cerr<<", "; DBG(args...);
}

void solve(){
  int n, k;
  cin>>n>>k;
  int num = n;
  int times = 1;
  int ans = INT_MAX;
  while(true){
    int rem = num-k*times;
    if(rem<=0){
      cout<<-1<<"\n";
      return;
    }
    if(__builtin_popcount(rem)==times || times==ans){
      cout<<min(ans, times)<<"\n";
      return;
    }else{
      if(rem<=0){
        cout<<-1<<"\n";
        return;
      }
      for(int j=log2(num); j>=0; j--){
        if((num&(1<<j)) && (num%((int)pow(2, j))==0)){
          /* cout<<times  +rem/(pow(2, j))<<"\n"; */
          /* return; */
          ans = min(ans, times + (int)(num/pow(2, j)));
          break;
        }
      }
    }
    /* deb(rem, times, __builtin_popcount(rem), ans); */
    times++;
  }
  cout<<-1<<"\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  /* cin>>t; */
  while(t--) solve();
}

/*Ankon Ghosh
  KUET,ECE 2K16*/
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
///#include <ext/pb_ds/detail/standard_policies.hpp>*/
///using namespace __gnu_pbds;
using namespace std;
#define  ff             first
#define  ss             second
#define  m_p            make_pair
#define  p_b            push_back
#define  ll             long long
#define  inf            1e9
#define  eps            1e-8
#define  MAX            100005
#define  mod            1000000007
#define  fast           ios_base::sync_with_stdio(false); cin.tie(NULL)
#define  output         freopen("output.txt","w",stdout)
#define  FOR(i,n)       for(ll i=0;i<n;i++)
#define  FOR1(i,n)      for(ll i=1;i<=n;i++)
#define  POPCOUNT(x)       __builtin_popcountll(x)
#define  RIGHTMOST(x)      __builtin_ctzll(x)
#define  LEFTMOST(x)    (63-__builtin_clzll((x)))
#define  setpre(number) cout<<fixed<<setprecision(7)<<number<<endl

/*----------------------Graph Moves----------------*/
//ll dx[]={+1,-1,+0,+0};
//ll dy[]={+0,+0,+1,-1};
//ll dx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//ll dy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//ll dx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//ll dy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

ll ta,n,m,a,b,c,d,x,y,sum=0,cnt,k,tot=0,q,w,u,s,e,h,tb;
ll taka=-1;
vector<ll>v,nega,posi;
vector<pair<ll,ll> >happy;
map<ll,ll>mp;
int main()
{
    ///freopen("input.txt","r",stdin);
    fast;
    ll p;
    ///for(ll i=1;i<=10;i++) cout<<LEFTMOST(i)<<" ";
    ///cout<<endl;
    cin>>n>>p;
    for(ll i=1;i<=31;i++)
    {
        ll x=(n-(i*p));
        if(x<=0) continue;
        ll z=POPCOUNT(x);
        ll mini=LEFTMOST(x);
        ll maxi=min(mini,i);
        ///cout<<maxi<<" "<<z<<endl;
        if(maxi>=z or i==z)
        {
            cout<<i<<endl;
            return 0;
        }
    }
    if(n!=0)
        cout<<-1<<endl;
    return 0;

}