#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,a,brojSetova,ostatak;
    cin >> n >> a;
    if(n<3*a) cout << "-1";
    ostatak=(n-3*a)%3
    if(ostatak==2) brojSetova=3;
    if(ostatak==1) brojSetova=4;
    if(n<=((a*2-2)/3))
    {
        if(brojSetova==3)
        {
            cout << a
        }
    }
}