#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long int ll;
int d[100100];
vector<int> g[100100];
void dfs(int s,int p){
    for(int t:g[s]){
        if(t!=p){
            d[t]=d[s]+1;
            dfs(t,s);
        }
    }
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,k; cin >> n >> k;
    if(n<k){
        cout << -1 << endl;
        return 0;
    }
    cout << k-1 << endl;
    return 0;
    for(int i=0;i<n-1;i++){
        int x,y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    d[0]=0;
    dfs(0,-1);
    vector<int> a(n);
    for(int i=0;i<n;i++){
        a[i]=d[i];
    }
    sort(a.begin(),a.end());
    ll ans=0;
    for(int i=0;i<k;i++){
        ans+=a[i];
    }
    cout << ans << endl;
}