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

const ll MAXN = 200000;

ll N, K, ans=1000000000000, tmp=-1;
vector<ll> A, B;

signed main(){
    cin >> N >> K;
    for(int i=0;i<N;i++) {
        ll a;
        cin >> a;
        if(a<0) A.push_back(a);
        else B.push_back(a);
    }
    reverse(A.begin(), A.end());
    ll As = (ll)A.size();
    ll Bs = (ll)B.size();
    for(int i=0;i<min(K, As);i++) {
        if(Bs-1>=K-i-2&&K-i-2>=0) {
            ans = min(ans, 2*min(abs(A[i]), abs(B[K-i-2])) + max(abs(A[i]), abs(B[K-i-2])));
        }
    }
    if(As>=K) ans = min(ans, abs(A[K-1]));
    for(int i=0;i<min(K, Bs);i++) {
        if(As-1>=K-i-2&&K-i-2>=0) {
            ans = min(ans, 2*min(abs(B[i]), abs(A[K-i-2])) + max(abs(B[i]), abs(A[K-i-2])));
        }
    }
    if((ll)B.size()>=K) ans = min(ans, abs(B[K-1]));
    /*
    if(K>tmp+1) {
        for(int i=0;i<=tmp;i++) {
            if(K+i-1<=N-1) ans = min(ans, 2*min(abs(A[i]), abs(A[K+i-1])) + max(abs(A[i]), abs(A[K+i-1])));
        }
    }
    if(K>N-tmp-1) {
        for(int i=tmp+1;i<N;i++) {
            if(i+1-K>=0) ans = min(ans, 2*min(abs(A[i]), abs(A[i+1-K])) + max(abs(A[i]), abs(A[i+1-K])));
        }
    }
    */
    cout << ans << endl;
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

#define FORR(i,b,e) for(int i=(b);i<(int)(e);++i)
#define FOR(i,e) FORR(i,0,e)
#define dump(var) cerr << #var ": " << var << "\n"
#define dumpc(con) for(auto& e: con) cerr << e << " "; cerr<<"\n"

typedef long long ll;
typedef unsigned long long ull;

const double EPS = 1e-6;
const int d4[] = {0, -1, 0, 1, 0};

using namespace std;

struct UnionFind {
    vector<int> v;
    int groups;
    UnionFind() {}
    UnionFind(int n): groups(n) {
        v.resize(n+1);
        FORR(i, 1, n+1) v[i] = i;
    }
    int find(int p) {
        if (v[p] == p) return p;
        return v[p] = find(v[p]);
    }
    int merge(int p, int q) {
        p = find(p);
        q = find(q);
        if (p != q) {
            v[q] = p;
            groups--;
        }
        return groups;
    }
};

struct Edge {
    int s, t, w;
    bool operator<(const Edge &o) const {
        return w < o.w;
    }
};

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

    int V, E;
    cin >> V >> E;
    vector<Edge> edge(E);
    FOR(i, E) {
        auto &e = edge[i];
        cin >> e.s >> e.t >> e.w;
    }
    sort(edge.begin(), edge.end());

    UnionFind uf(V);
    int weight = 0;
    for (auto &e: edge) {
        if (uf.find(e.s) != uf.find(e.t)) {
            weight += e.w;
            uf.merge(e.s, e.t);
            if (uf.groups == 1) break;
        }
    }
    
    cout << weight << endl;

    return 0;
}