Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		n, m := io.NextInt(), io.NextInt()
		if n == 0 {
			break
		}
		p := make([][]int, m)
		for i := 0; i < n; i++ {
			d, v := io.NextInt()-1, io.NextInt()
			p[d] = append(p[d], v)
		}
		sum := 0
		for i := 0; i < m; i++ {
			max := 0
			for _, v := range p[i] {
				max = intMax(v, max)
			}
			sum += max
		}
		io.Println(sum)
	}
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>
#include <utility>
using namespace std;
#define sz size()
#define rep(i,a,n) for(int i = (a);i < (n);i++)
#define lep(i,a,n) for(int i = (a);i <= (n);i++)
//#define clr(i,n) memset(i, i+sizeof(i), n)
#define pb push_back
#define print(X) cout << (X) << endl;
typedef long long ll;
typedef pair<int,int> P;
int n,m,l;
string s,t;
int d[100000], dp[100][100];

int main(){
    int a,b;
    while(cin >> n >> m, (n+m)){
        vector<int> vec(m);
        int ans = 0;

        // rep(i,0,n)vec[i] = 0;
        rep(i,0,n){
            cin >> a >> b;
            vec[a-1] = max(vec[a-1], b);
        }
        rep(i,0,m)ans += vec[i];
        print(ans);
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,b) FOR(i,0,b)
#define INF 1e9

struct problem{
  int d;
  int v;
};

void solve(int N, int M){
  int max_d_prob[M],max_sum_v = 0;
  fill(max_d_prob,max_d_prob+M,0);
  struct problem prob[N];

  REP(i,N){
    cin>>prob[i].d>>prob[i].v;
    max_d_prob[prob[i].d-1] = max(max_d_prob[prob[i].d-1],prob[i].v);
  }

  REP(i,M) max_sum_v += max_d_prob[i]; //cout<<max_d_prob[i]<<" ";

  cout<<max_sum_v<<endl;
}

int main(){
  int N,M;

  while(1){
    cin>>N>>M;
    if(N == 0 && M == 0) break;
    solve(N,M);
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int n, m;
    while(cin >> n >> m, n+m){
        vector<int> vec[m];
        int d, v;
        for(int i = 0; i < n; i++){
            cin >> d >> v;
            vec[d-1].push_back(v);
        }
        int ans = 0;
        for(int i = 0; i < m; i++)  ans += *max_element(vec[i].begin(), vec[i].end());
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
int main(void){
  for(;;){
    int N, M;
    cin >> N >> M;
    if(N == 0 && M == 0){ return 0; }
    int d[N], v[N];
    for(int i = 0 ; i < N ; i++){
      cin >> d[i] >> v[i];
    }
    int max[M];
    for(int i = 0 ; i < M ; i++){
      max[i] = -1;
    }
    for(int i = 0 ; i < N ; i++){
      if(max[d[i]-1] < v[i]){
    	max[d[i]-1] = v[i];
      }
    }
    int ans = 0;
    for(int i = 0 ; i < M ; i++){
      ans += max[i];
    }
    cout << ans << endl;
  }
}

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

int main() {
    int n, m;
    while (cin >> n >> m, n | m) {
        map<int, int> mp;
        for (int i = 0; i < n; i++) {
            int a, b;
            cin >> a >> b;
            mp[a] = max(mp[a], b);
        }
        int sum = 0;
        for (auto e : mp) sum += e.second;
        cout << sum << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n,m;
    while(cin >>n >>m,n){
        vector<int> a(m);
        rep(i,n){
            int d,v;
            cin >>d >>v;
            --d;
            a[d] = max(a[d], v);
        }

        int ans = 0;
        rep(i,m) ans += a[i];
        cout << ans << endl;
    }
    return 0;
}

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

int main(){
    
    while(1){

        int n, m; cin >> n >> m;
        if(!n) break;
        vector<int> a(m, 0);
        for(int i = 0; i < n; i++){
            int d, v; cin >> d >> v;
            d--;
            a[d] = max(a[d], v);
        }
        
        int ans = 0;
        for(int i = 0; i < m; i++) ans += a[i];
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif

using namespace std;

typedef long long int ll;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template<typename T>
void chmax(T &a, T b) {
    a = max(a, b);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    while (cin >> n >> m, n + m) {
        vector<int> a(m, 0);
        REP(i, 0, n) {
            int d, v;
            cin >> d >> v;
            chmax(a[d - 1], v);
        }
        cout << accumulate(a.begin(), a.end(), 0) << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

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

int n,m;
vector<int> d[101];

int main(void){
	while(1){
		scanf("%d%d",&n,&m);
		if(n==0 && m==0)break;
		for(int i=0;i<=m;i++){
			d[i].clear();
		}
		for(int i=0;i<n;i++){
			int dd;
			int vv;
			scanf("%d%d",&dd,&vv);
			d[dd].push_back(vv);
		}
		for(int i=1;i<=m;i++){
			sort(d[i].begin(),d[i].end());
			reverse(d[i].begin(),d[i].end());
		}
		int res=0;
		for(int i=1;i<=m;i++){
			if(d[i].size()==0)continue;
			res+=d[i][0];
		}
		printf("%d\n",res);

	}
	return 0;
}